File: test_local_ip_association.py

package info (click to toggle)
python-openstackclient 7.4.0-3
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 11,012 kB
  • sloc: python: 135,291; makefile: 140; sh: 22
file content (319 lines) | stat: -rw-r--r-- 11,122 bytes parent folder | download | duplicates (3)
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
#   Copyright 2021 Huawei, Inc. All rights reserved.
#
#   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.
#

from unittest import mock
from unittest.mock import call

from osc_lib import exceptions

from openstackclient.network.v2 import local_ip_association
from openstackclient.tests.unit.identity.v2_0 import fakes as identity_fakes_v2
from openstackclient.tests.unit.network.v2 import fakes as network_fakes


class TestLocalIPAssociation(network_fakes.TestNetworkV2):
    def setUp(self):
        super().setUp()
        self.local_ip = network_fakes.create_one_local_ip()
        self.fixed_port = network_fakes.create_one_port()
        self.project = identity_fakes_v2.FakeProject.create_one_project()
        self.network_client.find_port = mock.Mock(return_value=self.fixed_port)


class TestCreateLocalIPAssociation(TestLocalIPAssociation):
    def setUp(self):
        super().setUp()
        self.new_local_ip_association = (
            network_fakes.create_one_local_ip_association(
                attrs={
                    'fixed_port_id': self.fixed_port.id,
                    'local_ip_id': self.local_ip.id,
                }
            )
        )
        self.network_client.create_local_ip_association = mock.Mock(
            return_value=self.new_local_ip_association
        )

        self.network_client.find_local_ip = mock.Mock(
            return_value=self.local_ip
        )

        # Get the command object to test
        self.cmd = local_ip_association.CreateLocalIPAssociation(
            self.app, None
        )

        self.columns = (
            'local_ip_address',
            'fixed_port_id',
            'fixed_ip',
            'host',
        )

        self.data = (
            self.new_local_ip_association.local_ip_address,
            self.new_local_ip_association.fixed_port_id,
            self.new_local_ip_association.fixed_ip,
            self.new_local_ip_association.host,
        )

    def test_create_no_options(self):
        arglist = [
            self.new_local_ip_association.local_ip_id,
            self.new_local_ip_association.fixed_port_id,
        ]
        verifylist = [
            ('local_ip', self.new_local_ip_association.local_ip_id),
            ('fixed_port', self.new_local_ip_association.fixed_port_id),
        ]
        parsed_args = self.check_parser(self.cmd, arglist, verifylist)
        columns, data = self.cmd.take_action(parsed_args)

        self.network_client.create_local_ip_association.assert_called_once_with(
            self.new_local_ip_association.local_ip_id,
            **{
                'fixed_port_id': self.new_local_ip_association.fixed_port_id,
            },
        )
        self.assertEqual(set(self.columns), set(columns))
        self.assertEqual(set(self.data), set(data))

    def test_create_all_options(self):
        arglist = [
            self.new_local_ip_association.local_ip_id,
            self.new_local_ip_association.fixed_port_id,
            '--fixed-ip',
            self.new_local_ip_association.fixed_ip,
        ]
        verifylist = [
            ('local_ip', self.new_local_ip_association.local_ip_id),
            ('fixed_port', self.new_local_ip_association.fixed_port_id),
            ('fixed_ip', self.new_local_ip_association.fixed_ip),
        ]
        parsed_args = self.check_parser(self.cmd, arglist, verifylist)
        columns, data = self.cmd.take_action(parsed_args)

        self.network_client.create_local_ip_association.assert_called_once_with(
            self.new_local_ip_association.local_ip_id,
            **{
                'fixed_port_id': self.new_local_ip_association.fixed_port_id,
                'fixed_ip': self.new_local_ip_association.fixed_ip,
            },
        )
        self.assertEqual(set(self.columns), set(columns))
        self.assertEqual(set(self.data), set(data))


class TestDeleteLocalIPAssociation(TestLocalIPAssociation):
    def setUp(self):
        super().setUp()
        self._local_ip_association = (
            network_fakes.create_local_ip_associations(
                count=2,
                attrs={
                    'local_ip_id': self.local_ip.id,
                },
            )
        )
        self.network_client.delete_local_ip_association = mock.Mock(
            return_value=None
        )

        self.network_client.find_local_ip = mock.Mock(
            return_value=self.local_ip
        )
        # Get the command object to test
        self.cmd = local_ip_association.DeleteLocalIPAssociation(
            self.app, None
        )

    def test_local_ip_association_delete(self):
        arglist = [
            self.local_ip.id,
            self._local_ip_association[0].fixed_port_id,
        ]
        verifylist = [
            ('local_ip', self.local_ip.id),
            ('fixed_port_id', [self._local_ip_association[0].fixed_port_id]),
        ]

        parsed_args = self.check_parser(self.cmd, arglist, verifylist)

        result = self.cmd.take_action(parsed_args)

        self.network_client.delete_local_ip_association.assert_called_once_with(
            self.local_ip.id,
            self._local_ip_association[0].fixed_port_id,
            ignore_missing=False,
        )

        self.assertIsNone(result)

    def test_multi_local_ip_associations_delete(self):
        arglist = []
        fixed_port_id = []

        arglist.append(str(self.local_ip))

        for a in self._local_ip_association:
            arglist.append(a.fixed_port_id)
            fixed_port_id.append(a.fixed_port_id)

        verifylist = [
            ('local_ip', str(self.local_ip)),
            ('fixed_port_id', fixed_port_id),
        ]
        parsed_args = self.check_parser(self.cmd, arglist, verifylist)

        result = self.cmd.take_action(parsed_args)

        calls = []
        for a in self._local_ip_association:
            calls.append(
                call(a.local_ip_id, a.fixed_port_id, ignore_missing=False)
            )

        self.network_client.delete_local_ip_association.assert_has_calls(calls)
        self.assertIsNone(result)

    def test_multi_local_ip_association_delete_with_exception(self):
        arglist = [
            self.local_ip.id,
            self._local_ip_association[0].fixed_port_id,
            'unexist_fixed_port_id',
        ]
        verifylist = [
            ('local_ip', self.local_ip.id),
            (
                'fixed_port_id',
                [
                    self._local_ip_association[0].fixed_port_id,
                    'unexist_fixed_port_id',
                ],
            ),
        ]
        parsed_args = self.check_parser(self.cmd, arglist, verifylist)

        delete_mock_result = [None, exceptions.CommandError]

        self.network_client.delete_local_ip_association = mock.MagicMock(
            side_effect=delete_mock_result
        )

        try:
            self.cmd.take_action(parsed_args)
            self.fail('CommandError should be raised.')
        except exceptions.CommandError as e:
            self.assertEqual(
                '1 of 2 Local IP Associations failed to delete.', str(e)
            )

        self.network_client.delete_local_ip_association.assert_any_call(
            self.local_ip.id, 'unexist_fixed_port_id', ignore_missing=False
        )
        self.network_client.delete_local_ip_association.assert_any_call(
            self.local_ip.id,
            self._local_ip_association[0].fixed_port_id,
            ignore_missing=False,
        )


class TestListLocalIPAssociation(TestLocalIPAssociation):
    columns = (
        'Local IP ID',
        'Local IP Address',
        'Fixed port ID',
        'Fixed IP',
        'Host',
    )

    def setUp(self):
        super().setUp()
        self.local_ip_associations = (
            network_fakes.create_local_ip_associations(
                count=3,
                attrs={
                    'local_ip_id': self.local_ip.id,
                    'fixed_port_id': self.fixed_port.id,
                },
            )
        )
        self.data = []
        for lip_assoc in self.local_ip_associations:
            self.data.append(
                (
                    lip_assoc.local_ip_id,
                    lip_assoc.local_ip_address,
                    lip_assoc.fixed_port_id,
                    lip_assoc.fixed_ip,
                    lip_assoc.host,
                )
            )
        self.network_client.local_ip_associations = mock.Mock(
            return_value=self.local_ip_associations
        )
        self.network_client.find_local_ip = mock.Mock(
            return_value=self.local_ip
        )
        self.network_client.find_port = mock.Mock(return_value=self.fixed_port)
        # Get the command object to test
        self.cmd = local_ip_association.ListLocalIPAssociation(self.app, None)

    def test_local_ip_association_list(self):
        arglist = [self.local_ip.id]
        verifylist = [('local_ip', self.local_ip.id)]

        parsed_args = self.check_parser(self.cmd, arglist, verifylist)
        columns, data = self.cmd.take_action(parsed_args)

        self.network_client.local_ip_associations.assert_called_once_with(
            self.local_ip, **{}
        )
        self.assertEqual(set(self.columns), set(columns))
        self.assertEqual(set(self.data), set(list(data)))

    def test_local_ip_association_list_all_options(self):
        arglist = [
            '--fixed-port',
            self.local_ip_associations[0].fixed_port_id,
            '--fixed-ip',
            self.local_ip_associations[0].fixed_ip,
            '--host',
            self.local_ip_associations[0].host,
            self.local_ip_associations[0].local_ip_id,
        ]

        verifylist = [
            ('fixed_port', self.local_ip_associations[0].fixed_port_id),
            ('fixed_ip', self.local_ip_associations[0].fixed_ip),
            ('host', self.local_ip_associations[0].host),
            ('local_ip', self.local_ip_associations[0].local_ip_id),
        ]

        parsed_args = self.check_parser(self.cmd, arglist, verifylist)
        columns, data = self.cmd.take_action(parsed_args)

        attrs = {
            'fixed_port_id': self.local_ip_associations[0].fixed_port_id,
            'fixed_ip': self.local_ip_associations[0].fixed_ip,
            'host': self.local_ip_associations[0].host,
        }

        self.network_client.local_ip_associations.assert_called_once_with(
            self.local_ip, **attrs
        )
        self.assertEqual(set(self.columns), set(columns))
        self.assertEqual(set(self.data), set(list(data)))