File: test_process.py

package info (click to toggle)
python-os-faults 0.2.7-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 696 kB
  • sloc: python: 4,797; sh: 54; makefile: 24
file content (121 lines) | stat: -rw-r--r-- 4,029 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import unittest

import mock

from os_faults.api.node_collection import Host
from os_faults.api.node_collection import NodeCollection
from os_faults.drivers.services.process import ServiceAsProcess


class TestServiceAsProcess(unittest.TestCase):

    def setUp(self):
        super(TestServiceAsProcess, self).setUp()

        config = {
            'grep': 'test-service',
            'port': ['tcp', 8000],
        }
        self.cloud_management = mock.Mock()
        self.hosts = [Host('10.1.1.10')]
        self.service = ServiceAsProcess(
            'test-service', config, mock.Mock(), self.cloud_management)
        node_collection = NodeCollection(
            cloud_management=self.cloud_management, hosts=self.hosts)
        self.service.get_nodes = mock.Mock(return_value=node_collection)

    def test_unplug(self):
        # run the command
        self.service.unplug()

        # verify
        expected_task = {
            'iptables': {
                'chain': 'INPUT',
                'protocol': 'tcp',
                'jump': 'DROP',
                'destination_port': '8000',
                'action': 'insert',
                'state': 'present',
                'comment': 'Added by os-faults',
            },
            'become': 'yes',
        }
        self.cloud_management.execute_on_cloud.assert_called_once_with(
            self.hosts, expected_task)

    def test_unplug_with_other_port(self):
        # run the command
        self.service.unplug(direction='egress', other_port=['udp', 10000])

        # verify
        expected_task = {
            'iptables': {
                'chain': 'OUTPUT',
                'protocol': 'udp',
                'jump': 'DROP',
                'destination_port': '10000',
                'action': 'insert',
                'state': 'present',
                'comment': 'Added by os-faults',
            },
            'become': 'yes',
        }
        self.cloud_management.execute_on_cloud.assert_called_once_with(
            self.hosts, expected_task)

    def test_plug(self):
        # run the command
        self.service.plug()

        # verify
        expected_task = {
            'iptables': {
                'chain': 'INPUT',
                'protocol': 'tcp',
                'jump': 'DROP',
                'destination_port': '8000',
                'state': 'absent',
                'comment': 'Added by os-faults',
            },
            'become': 'yes',
        }
        self.cloud_management.execute_on_cloud.assert_called_once_with(
            self.hosts, expected_task)

    def test_plug_port_is_required_in_config(self):
        config = {
            'grep': 'test-service',
        }
        service = ServiceAsProcess(
            'test-service', config, mock.Mock(), mock.Mock())
        node_collection = NodeCollection(
            cloud_management=self.cloud_management, hosts=[])
        service.get_nodes = mock.Mock(return_value=node_collection)

        self.assertRaises(NotImplementedError, service.plug)

    def test_unplug_port_is_required_in_config(self):
        config = {
            'grep': 'test-service',
        }
        service = ServiceAsProcess(
            'test-service', config, mock.Mock(), mock.Mock())
        node_collection = NodeCollection(
            cloud_management=self.cloud_management, hosts=[])
        service.get_nodes = mock.Mock(return_value=node_collection)

        self.assertRaises(NotImplementedError, service.unplug)