summaryrefslogtreecommitdiff
path: root/test/units/modules/network/ovs/test_openvswitch_port.py
blob: 1c2ebb9b196e68e7829e1bd6d4fb3b805774f4ed (plain)
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#
# (c) 2016 Red Hat Inc.
#
# This file is part of Ansible
#
# Ansible is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Ansible is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Ansible.  If not, see <http://www.gnu.org/licenses/>.

# Make coding more python3-ish
from __future__ import (absolute_import, division, print_function)
__metaclass__ = type

from units.compat.mock import patch
from ansible.modules.network.ovs import openvswitch_port
from units.modules.utils import set_module_args
from .ovs_module import TestOpenVSwitchModule, load_fixture

test_name_side_effect_matrix = {
    'test_openvswitch_port_absent_idempotent': [
        (0, '', '')],
    'test_openvswitch_port_absent_removes_port': [
        (0, 'list_ports_test_br.cfg', ''),
        (0, 'get_port_eth2_tag.cfg', ''),
        (0, 'get_port_eth2_external_ids.cfg', ''),
        (0, '', '')],
    'test_openvswitch_port_present_idempotent': [
        (0, 'list_ports_test_br.cfg', ''),
        (0, 'get_port_eth2_tag.cfg', ''),
        (0, 'get_port_eth2_external_ids.cfg', ''),
        (0, '', '')],
    'test_openvswitch_port_present_creates_port': [
        (0, '', ''),
        (0, '', ''),
        (0, '', '')],
    'test_openvswitch_port_present_changes_tag': [
        (0, 'list_ports_test_br.cfg', ''),
        (0, 'get_port_eth2_tag.cfg', ''),
        (0, 'get_port_eth2_external_ids.cfg', ''),
        (0, '', '')],
    'test_openvswitch_port_present_changes_external_id': [
        (0, 'list_ports_test_br.cfg', ''),
        (0, 'get_port_eth2_tag.cfg', ''),
        (0, 'get_port_eth2_external_ids.cfg', ''),
        (0, '', '')],
    'test_openvswitch_port_present_adds_external_id': [
        (0, 'list_ports_test_br.cfg', ''),
        (0, 'get_port_eth2_tag.cfg', ''),
        (0, 'get_port_eth2_external_ids.cfg', ''),
        (0, '', '')],
    'test_openvswitch_port_present_clears_external_id': [
        (0, 'list_ports_test_br.cfg', ''),
        (0, 'get_port_eth2_tag.cfg', ''),
        (0, 'get_port_eth2_external_ids.cfg', ''),
        (0, '', '')],
    'test_openvswitch_port_present_runs_set_mode': [
        (0, '', ''),
        (0, '', ''),
        (0, '', '')],
}


class TestOpenVSwitchPortModule(TestOpenVSwitchModule):

    module = openvswitch_port

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

        self.mock_run_command = (
            patch('ansible.module_utils.basic.AnsibleModule.run_command'))
        self.run_command = self.mock_run_command.start()
        self.mock_get_bin_path = (
            patch('ansible.module_utils.basic.AnsibleModule.get_bin_path'))
        self.get_bin_path = self.mock_get_bin_path.start()

    def tearDown(self):
        super(TestOpenVSwitchPortModule, self).tearDown()

        self.mock_run_command.stop()
        self.mock_get_bin_path.stop()

    def load_fixtures(self, test_name):
        test_side_effects = []
        for s in test_name_side_effect_matrix[test_name]:
            rc = s[0]
            out = s[1] if s[1] == '' else str(load_fixture(s[1]))
            err = s[2]
            side_effect_with_fixture_loaded = (rc, out, err)
            test_side_effects.append(side_effect_with_fixture_loaded)
        self.run_command.side_effect = test_side_effects

        self.get_bin_path.return_value = '/usr/bin/ovs-vsctl'

    def test_openvswitch_port_absent_idempotent(self):
        set_module_args(dict(state='absent',
                             bridge='test-br',
                             port='eth2'))
        self.execute_module(test_name='test_openvswitch_port_absent_idempotent')

    def test_openvswitch_port_absent_removes_port(self):
        set_module_args(dict(state='absent',
                             bridge='test-br',
                             port='eth2'))
        commands = [
            '/usr/bin/ovs-vsctl -t 5 del-port test-br eth2',
        ]
        self.execute_module(changed=True, commands=commands,
                            test_name='test_openvswitch_port_absent_removes_port')

    def test_openvswitch_port_present_idempotent(self):
        set_module_args(dict(state='present',
                             bridge='test-br',
                             port='eth2',
                             tag=10,
                             external_ids={'foo': 'bar'}))
        self.execute_module(test_name='test_openvswitch_port_present_idempotent')

    def test_openvswitch_port_present_creates_port(self):
        set_module_args(dict(state='present',
                             bridge='test-br',
                             port='eth2',
                             tag=10,
                             external_ids={'foo': 'bar'}))
        commands = [
            '/usr/bin/ovs-vsctl -t 5 add-port test-br eth2 tag=10',
            '/usr/bin/ovs-vsctl -t 5 set port eth2 external_ids:foo=bar'
        ]
        self.execute_module(changed=True,
                            commands=commands,
                            test_name='test_openvswitch_port_present_creates_port')

    def test_openvswitch_port_present_changes_tag(self):
        set_module_args(dict(state='present',
                             bridge='test-br',
                             port='eth2',
                             tag=20,
                             external_ids={'foo': 'bar'}))
        commands = [
            '/usr/bin/ovs-vsctl -t 5 set port eth2 tag=20'
        ]
        self.execute_module(changed=True,
                            commands=commands,
                            test_name='test_openvswitch_port_present_changes_tag')

    def test_openvswitch_port_present_changes_external_id(self):
        set_module_args(dict(state='present',
                             bridge='test-br',
                             port='eth2',
                             tag=10,
                             external_ids={'foo': 'baz'}))
        commands = [
            '/usr/bin/ovs-vsctl -t 5 set port eth2 external_ids:foo=baz'
        ]
        self.execute_module(changed=True,
                            commands=commands,
                            test_name='test_openvswitch_port_present_changes_external_id')

    def test_openvswitch_port_present_adds_external_id(self):
        set_module_args(dict(state='present',
                             bridge='test-br',
                             port='eth2',
                             tag=10,
                             external_ids={'foo2': 'bar2'}))
        commands = [
            '/usr/bin/ovs-vsctl -t 5 set port eth2 external_ids:foo2=bar2'
        ]
        self.execute_module(changed=True,
                            commands=commands,
                            test_name='test_openvswitch_port_present_adds_external_id')

    def test_openvswitch_port_present_clears_external_id(self):
        set_module_args(dict(state='present',
                             bridge='test-br',
                             port='eth2',
                             tag=10,
                             external_ids={'foo': None}))
        commands = [
            '/usr/bin/ovs-vsctl -t 5 remove port eth2 external_ids foo'
        ]
        self.execute_module(changed=True,
                            commands=commands,
                            test_name='test_openvswitch_port_present_clears_external_id')

    def test_openvswitch_port_present_runs_set_mode(self):
        set_module_args(dict(state='present',
                             bridge='test-br',
                             port='eth2',
                             tag=10,
                             external_ids={'foo': 'bar'},
                             set="port eth2 other_config:stp-path-cost=10"))
        commands = [
            '/usr/bin/ovs-vsctl -t 5 add-port test-br eth2 tag=10 -- set'
            ' port eth2 other_config:stp-path-cost=10',
            '/usr/bin/ovs-vsctl -t 5 set port eth2 external_ids:foo=bar'
        ]
        self.execute_module(changed=True, commands=commands,
                            test_name='test_openvswitch_port_present_runs_set_mode')