build.tests.unit.test_utils   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 426
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 15
eloc 277
dl 0
loc 426
ccs 101
cts 101
cp 1
rs 10
c 0
b 0
f 0

14 Methods

Rating   Name   Duplication   Size   Complexity  
A TestUtils.test_convert_vlan() 0 10 1
A TestUtils.test_prepare_json_empty() 0 7 1
A TestUtils.test_find_endpoint_b() 0 15 1
A TestUtils.test_get_stored_flows_error() 0 8 2
A TestUtils.test_get_stored_flows() 0 12 1
A TestUtils.test_prepare_json() 0 46 1
A TestUtils.test_find_endpoint_a() 0 15 1
B TestUtils.test_compare_endpoints1() 0 163 1
A TestUtils.test_find_endpoint_link_none() 0 13 1
A TestUtils.test_match_field_ip() 0 19 1
A TestUtils.test_convert_list_entries() 0 14 1
A TestUtils.test_prepare_list_json() 0 44 1
A TestUtils.test_convert_entries_vlan() 0 15 1
A TestUtils.test_match_field_dl_vlan() 0 17 1
1
"""Module to test the utils.py file."""
2 1
import pytest
3 1
from unittest.mock import patch, MagicMock
4 1
from httpx import RequestError
5 1
from tenacity import RetryError
6 1
from kytos.core.interface import Interface
7 1
from kytos.lib.helpers import get_link_mock
8 1
from napps.amlight.sdntrace_cp import utils, settings
9
10
11
# pylint: disable=too-many-public-methods, duplicate-code, protected-access
12 1
class TestUtils():
13
    """Test utils.py functions."""
14
15 1
    @patch("httpx.get")
16 1
    def test_get_stored_flows(self, get_mock):
17
        "Test get_stored_flows"
18 1
        response = MagicMock()
19 1
        response.status_code = 200
20 1
        response.json.return_value = {"result": "ok"}
21 1
        get_mock.return_value = response
22
23 1
        api_url = f'{settings.FLOW_MANAGER_URL}/stored_flows/?state=installed'
24 1
        result = utils.get_stored_flows()
25 1
        get_mock.assert_called_with(api_url, timeout=20)
26 1
        assert result['result'] == "ok"
27
28 1
    @patch("time.sleep")
29 1
    @patch("httpx.get")
30 1
    def test_get_stored_flows_error(self, get_mock, _):
31
        """Test retries when get_stored_flows fails"""
32 1
        get_mock.side_effect = RequestError(MagicMock())
33 1
        with pytest.raises(RetryError):
34 1
            utils.get_stored_flows()
35 1
        assert get_mock.call_count == 3
36
37 1
    def test_convert_list_entries(self):
38
        """Verify convert entries with a list of one example"""
39 1
        eth = {"dl_vlan": 100}
40 1
        dpid = {"dpid": "00:00:00:00:00:00:00:01", "in_port": 1}
41 1
        switch = {"switch": dpid, "eth": eth}
42 1
        entries = {"trace": switch}
43
44 1
        result = utils.convert_list_entries([entries])
45 1
        expected = [{
46
                "dpid": "00:00:00:00:00:00:00:01",
47
                "in_port": 1,
48
                "dl_vlan": [100],
49
            }]
50 1
        assert result == expected
51
52 1
    def test_convert_entries_vlan(self):
53
        """Verify convert entries with simple example with vlan."""
54
55 1
        eth = {"dl_vlan": 100}
56 1
        dpid = {"dpid": "00:00:00:00:00:00:00:01", "in_port": 1}
57 1
        switch = {"switch": dpid, "eth": eth}
58 1
        entries = {"trace": switch}
59
60 1
        result = utils.convert_entries(entries)
61 1
        expected = {
62
                "dpid": "00:00:00:00:00:00:00:01",
63
                "in_port": 1,
64
                "dl_vlan": [100],
65
            }
66 1
        assert result == expected
67
68 1
    def test_prepare_json(self):
69
        """Verify prepare json with simple tracepath result."""
70 1
        trace_result = [
71
            {
72
                "in": {
73
                    "dpid": "00:00:00:00:00:00:00:01",
74
                    "port": 1,
75
                    "time": "2022-06-01 01:01:01.100000",
76
                    "type": "starting",
77
                }
78
            },
79
            {
80
                "in": {
81
                    "dpid": "00:00:00:00:00:00:00:03",
82
                    "port": 3,
83
                    "time": "2022-06-01 01:01:01.100000",
84
                    "type": "intermediary",
85
                    "vlan": 100,
86
                },
87
                "out": {
88
                    "port": 1,
89
                    "vlan": 123,
90
                },
91
            }
92
        ]
93
94 1
        result = utils.prepare_json(trace_result)
95 1
        expected = {
96
                "result": [
97
                    {
98
                        "dpid": "00:00:00:00:00:00:00:01",
99
                        "port": 1,
100
                        "time": "2022-06-01 01:01:01.100000",
101
                        "type": "starting",
102
                    },
103
                    {
104
                        "dpid": "00:00:00:00:00:00:00:03",
105
                        "port": 3,
106
                        "time": "2022-06-01 01:01:01.100000",
107
                        "type": "intermediary",
108
                        "vlan": 100,
109
                        "out": {"port": 1, "vlan": 123},
110
                    },
111
                ]
112
            }
113 1
        assert result == expected
114
115 1
    def test_prepare_list_json(self):
116
        """Verify prepare list with a simple tracepath result."""
117 1
        trace_result = [
118
            {
119
                "in": {
120
                    "dpid": "00:00:00:00:00:00:00:01",
121
                    "port": 1,
122
                    "time": "2022-06-01 01:01:01.100000",
123
                    "type": "starting",
124
                }
125
            },
126
            {
127
                "in": {
128
                    "dpid": "00:00:00:00:00:00:00:03",
129
                    "port": 3,
130
                    "time": "2022-06-01 01:01:01.100000",
131
                    "type": "intermediary",
132
                    "vlan": 100,
133
                },
134
                "out": {
135
                    "port": 1,
136
                    "vlan": 123,
137
                },
138
            }
139
        ]
140
141 1
        result = utils._prepare_json(trace_result)
142 1
        expected = [
143
                    {
144
                        "dpid": "00:00:00:00:00:00:00:01",
145
                        "port": 1,
146
                        "time": "2022-06-01 01:01:01.100000",
147
                        "type": "starting",
148
                    },
149
                    {
150
                        "dpid": "00:00:00:00:00:00:00:03",
151
                        "port": 3,
152
                        "time": "2022-06-01 01:01:01.100000",
153
                        "type": "intermediary",
154
                        "vlan": 100,
155
                        "out": {"port": 1, "vlan": 123},
156
                    },
157
                ]
158 1
        assert result == expected
159
160 1
    def test_prepare_json_empty(self):
161
        """Verify prepare json with empty result."""
162 1
        trace_result = []
163
164 1
        result = utils.prepare_json(trace_result)
165
166 1
        assert result == {"result": []}
167
168 1
    @pytest.mark.parametrize(
169
        "endpoint1,endpoint2,result",
170
        [
171
            (
172
                {"dpid": "00:00:00:00:00:00:00:01"},
173
                {"dpid": "00:00:00:00:00:00:00:02"},
174
                False
175
            ),
176
            (
177
                {
178
                    "dpid": "00:00:00:00:00:00:00:03",
179
                    "out_port": 2,
180
                    "out_vlan": 200,
181
                },
182
                {
183
                    "dpid": "00:00:00:00:00:00:00:03",
184
                    "in_port": 3,
185
                    "in_vlan": 100,
186
                },
187
                False
188
            ),
189
            (
190
                {
191
                    "dpid": "00:00:00:00:00:00:00:03",
192
                    "in_port": 3,
193
                    "in_vlan": 100,
194
                },
195
                {
196
                    "dpid": "00:00:00:00:00:00:00:03",
197
                    "in_port": 3,
198
                    "in_vlan": 100,
199
                },
200
                False
201
            ),
202
            (
203
                {
204
                    "dpid": "00:00:00:00:00:00:00:03",
205
                    "in_port": 3,
206
                    "in_vlan": 100,
207
                },
208
                {
209
                    "dpid": "00:00:00:00:00:00:00:03",
210
                    "out_port": 2,
211
                    "out_vlan": 200,
212
                },
213
                False
214
            ),
215
            (
216
                {
217
                    "dpid": "00:00:00:00:00:00:00:03",
218
                    "in_port": 3,
219
                    "out_port": 2,
220
                    "in_vlan": 100,
221
                },
222
                {
223
                    "dpid": "00:00:00:00:00:00:00:03",
224
                    "in_port": 2,
225
                    "out_port": 3,
226
                    "out_vlan": 200,
227
                },
228
                False
229
            ),
230
            (
231
                {
232
                    "dpid": "00:00:00:00:00:00:00:03",
233
                    "in_port": 3,
234
                    "out_port": 2,
235
                    "in_vlan": 100,
236
                },
237
                {
238
                    "dpid": "00:00:00:00:00:00:00:03",
239
                    "in_port": 2,
240
                    "out_port": 3,
241
                },
242
                False
243
            ),
244
            (
245
                {
246
                    "dpid": "00:00:00:00:00:00:00:03",
247
                    "in_port": 3,
248
                    "out_port": 2,
249
                },
250
                {
251
                    "dpid": "00:00:00:00:00:00:00:03",
252
                    "in_port": 2,
253
                    "out_port": 3,
254
                    "out_vlan": 200,
255
                },
256
                False
257
            ),
258
            (
259
                {
260
                    "dpid": "00:00:00:00:00:00:00:01",
261
                    "in_port": 3,
262
                    "out_port": 2,
263
                    "out_vlan": 200,
264
                },
265
                {
266
                    "dpid": "00:00:00:00:00:00:00:01",
267
                    "in_port": 2,
268
                    "out_port": 3,
269
                    "in_vlan": 100,
270
                },
271
                False
272
            ),
273
            (
274
                {
275
                    "dpid": "00:00:00:00:00:00:00:01",
276
                    "in_port": 3,
277
                    "out_port": 2,
278
                    "out_vlan": 200,
279
                },
280
                {
281
                    "dpid": "00:00:00:00:00:00:00:01",
282
                    "in_port": 2,
283
                    "out_port": 3,
284
                },
285
                False
286
            ),
287
            (
288
                {
289
                    "dpid": "00:00:00:00:00:00:00:01",
290
                    "in_port": 3,
291
                    "out_port": 2,
292
                },
293
                {
294
                    "dpid": "00:00:00:00:00:00:00:01",
295
                    "in_port": 2,
296
                    "out_port": 3,
297
                    "in_vlan": 100,
298
                },
299
                False
300
            ),
301
            (
302
                {
303
                    "dpid": "00:00:00:00:00:00:00:03",
304
                    "in_port": 3,
305
                    "in_vlan": 100,
306
                },
307
                {
308
                    "dpid": "00:00:00:00:00:00:00:03",
309
                    "out_port": 3,
310
                    "out_vlan": 100,
311
                },
312
                True
313
            ),
314
        ]
315
    )
316 1
    def test_compare_endpoints1(self, endpoint1, endpoint2, result):
317
        """Test for compare endpoinst for the internal conditional no.
318
        1 - first: Test endpoint1 dpid != endpoint2 dpid
319
        2 - second: Test endpoint1 without in_port
320
        3 - second: Test endpoint2 without out_port
321
        4 - second: Test endpoint1 in_port != endpoint2 out_port
322
        5 - third: Test endpoint1 in_vlan != endpoint2 out_vlan
323
        6 - first: Test endpoint1 with in_vlan and endpoint2 without out_vlan
324
        7 - first: Test endpoint1 without in_vlan and endpoint2 with out_vlan
325
        8 - fifth: Test endpoint1 out_vlan != endpoint2 in_vlan
326
        9 - fifth: Test endpoint1 with out_vlan and endpoint2 without in_vlan
327
        10 - fifth: Test endpoint1 without out_vlan and endpoint2 with in_vlan
328
        11 - fifth: Test endpoint1 out_vlan != endpoint2 in_vlan
329
        """
330 1
        assert utils._compare_endpoints(endpoint1, endpoint2) == result
331
332 1
    def test_find_endpoint_b(self):
333
        """Test find endpoint with interface equals link endpoint B."""
334 1
        port = 1
335
336 1
        mock_interface = Interface("interface A", port, MagicMock())
337 1
        mock_interface.address = "00:00:00:00:00:00:00:01"
338 1
        mock_interface.link = get_link_mock(
339
            "00:00:00:00:00:00:00:02", "00:00:00:00:00:00:00:01"
340
        )
341
342 1
        mock_switch = MagicMock()
343 1
        mock_switch.get_interface_by_port_no.return_value = mock_interface
344 1
        expected = {'endpoint': mock_interface.link.endpoint_a}
345 1
        result = utils.find_endpoint(mock_switch, port)
346 1
        assert result == expected
347
348 1
    def test_find_endpoint_a(self):
349
        """Test find endpoint with interface equals link endpoint A."""
350 1
        port = 1
351
352 1
        mock_interface = Interface("interface A", port, MagicMock())
353 1
        mock_interface.address = "00:00:00:00:00:00:00:01"
354 1
        mock_interface.link = get_link_mock(
355
            "00:00:00:00:00:00:00:01", "00:00:00:00:00:00:00:03"
356
        )
357
358 1
        mock_switch = MagicMock()
359 1
        mock_switch.get_interface_by_port_no.return_value = mock_interface
360 1
        expected = {'endpoint': mock_interface.link.endpoint_b}
361 1
        result = utils.find_endpoint(mock_switch, port)
362 1
        assert result == expected
363
364 1
    def test_find_endpoint_link_none(self):
365
        """Test find endpoint without link."""
366 1
        port = 1
367
368 1
        mock_interface = Interface("interface A", port, MagicMock())
369 1
        mock_interface.address = "00:00:00:00:00:00:00:01"
370
371 1
        mock_switch = MagicMock()
372 1
        mock_switch.get_interface_by_port_no.return_value = mock_interface
373
374 1
        result = utils.find_endpoint(mock_switch, port)
375 1
        assert 'endpoint' in result
376 1
        assert result['endpoint'] is None
377
378 1
    def test_convert_vlan(self):
379
        """Test convert_vlan function"""
380 1
        value = 100
381 1
        result = utils.convert_vlan(value)
382 1
        assert result[0] == 100
383
384 1
        value = "4096/4096"
385 1
        result = utils.convert_vlan(value)
386 1
        assert result[0] == 4096
387 1
        assert result[1] == 4096
388
389 1
    @pytest.mark.parametrize(
390
        "value,field_flow,result",
391
        [
392
            (None, 0, True),
393
            (None, 10, False),
394
            (None, "4096/4096", False),
395
            ([10], 0, False),
396
            ([10], 10, True),
397
            ([10], "4096/4096", True),
398
            ([10], 11, False),
399
            ([3], "5/1", True),
400
            ([2], "5/1", False),
401
        ]
402
    )
403 1
    def test_match_field_dl_vlan(self, value, field_flow, result):
404
        """Test match_field_dl_vlan"""
405 1
        assert utils.match_field_dl_vlan(value, field_flow) == result
406
407 1
    @pytest.mark.parametrize(
408
        "field,field_flow,result",
409
        [
410
            ('192.168.20.21', '192.168.20.21', True),
411
            ('192.168.20.21', '192.168.20.21/10', True),
412
            ('192.168.20.21', '192.168.20.21/32', True),
413
            ('192.168.20.21', '192.168.20.21/255.255.255.255', True),
414
            ('192.168.20.30', '192.168.20.21', False),
415
            ('192.200.20.30', '192.168.20.21/10', False),
416
            ('2002:db8::8a3f:362:7897', '2002:db8::8a3f:362:7897', True),
417
            ('2002:db8::8a3f:362:7897', '2002:db8::8a3f:362:7897/10', True),
418
            ('2002:db8::8a3f:362:7897', '2002:db8::8a3f:362:7897/128', True),
419
            ('2002:db8::8a3f:362:7', '2002:db8::8a3f:362:7897', False),
420
            ('3002:db8::9a3f:362:7897', '2002:db8::8a3f:362:7897/10', False),
421
        ],
422
    )
423 1
    def test_match_field_ip(self, field, field_flow, result):
424
        """Test match_field_ip"""
425
        assert utils.match_field_ip(field, field_flow) == result
426