1
|
|
|
"""Test proxy_port.py""" |
2
|
|
|
|
3
|
1 |
|
from unittest.mock import MagicMock |
4
|
1 |
|
from kytos.core.common import EntityStatus |
5
|
1 |
|
from napps.kytos.telemetry_int.proxy_port import ProxyPort |
6
|
|
|
|
7
|
|
|
|
8
|
1 |
|
class TestProxyPort: |
9
|
|
|
"""Test ProxyPort.""" |
10
|
|
|
|
11
|
1 |
|
def setup_method(self): |
12
|
|
|
"""Set up test fixtures.""" |
13
|
1 |
|
self.source = MagicMock() |
14
|
|
|
|
15
|
1 |
|
def test_proxy_port_init(self): |
16
|
|
|
"""Test ProxyPort initialization.""" |
17
|
|
|
|
18
|
1 |
|
proxy_port = ProxyPort(self.source) |
19
|
1 |
|
assert proxy_port.source == self.source |
20
|
1 |
|
assert proxy_port.evc_ids == set() |
21
|
|
|
|
22
|
1 |
|
def test_repr(self): |
23
|
|
|
"""Test __repr__ method.""" |
24
|
|
|
|
25
|
1 |
|
destination = MagicMock() |
26
|
1 |
|
self.source.status = EntityStatus.UP |
27
|
1 |
|
self.source.metadata = {"looped": {"port_numbers": [1, 2]}} |
28
|
1 |
|
self.source.switch.get_interface_by_port_no.return_value = destination |
29
|
1 |
|
destination.status = EntityStatus.UP |
30
|
|
|
|
31
|
1 |
|
proxy_port = ProxyPort(self.source) |
32
|
|
|
|
33
|
1 |
|
repr_str = repr(proxy_port) |
34
|
1 |
|
assert "ProxyPort(" in repr_str |
35
|
1 |
|
assert str(self.source) in repr_str |
36
|
1 |
|
assert str(destination) in repr_str |
37
|
1 |
|
assert "EntityStatus.UP" in repr_str |
38
|
|
|
|
39
|
1 |
|
def test_destination_no_metadata(self): |
40
|
|
|
"""Test destination property when no looped metadata.""" |
41
|
|
|
|
42
|
1 |
|
self.source.metadata = {} |
43
|
1 |
|
proxy_port = ProxyPort(self.source) |
44
|
1 |
|
assert proxy_port.destination is None |
45
|
|
|
|
46
|
1 |
|
def test_destination_no_port_numbers(self): |
47
|
|
|
"""Test destination property when no port_numbers in looped metadata.""" |
48
|
|
|
|
49
|
1 |
|
self.source.metadata = {"looped": {}} |
50
|
1 |
|
proxy_port = ProxyPort(self.source) |
51
|
1 |
|
assert proxy_port.destination is None |
52
|
|
|
|
53
|
1 |
|
def test_destination_empty_port_numbers(self): |
54
|
|
|
"""Test destination property when empty port_numbers.""" |
55
|
|
|
|
56
|
1 |
|
self.source.metadata = {"looped": {"port_numbers": []}} |
57
|
1 |
|
proxy_port = ProxyPort(self.source) |
58
|
1 |
|
assert proxy_port.destination is None |
59
|
|
|
|
60
|
1 |
|
def test_destination_insufficient_port_numbers(self): |
61
|
|
|
"""Test destination property when insufficient port_numbers.""" |
62
|
1 |
|
self.source.metadata = {"looped": {"port_numbers": [1]}} |
63
|
1 |
|
proxy_port = ProxyPort(self.source) |
64
|
1 |
|
assert proxy_port.destination is None |
65
|
|
|
|
66
|
1 |
|
def test_destination_interface_not_found(self): |
67
|
|
|
"""Test destination property when destination interface not found.""" |
68
|
|
|
|
69
|
1 |
|
self.source.metadata = {"looped": {"port_numbers": [1, 2]}} |
70
|
1 |
|
self.source.switch.get_interface_by_port_no.return_value = None |
71
|
1 |
|
proxy_port = ProxyPort(self.source) |
72
|
1 |
|
assert proxy_port.destination is None |
73
|
1 |
|
self.source.switch.get_interface_by_port_no.assert_called_once_with(2) |
74
|
|
|
|
75
|
1 |
|
def test_destination_success(self): |
76
|
|
|
"""Test destination property successful case.""" |
77
|
|
|
|
78
|
1 |
|
destination = MagicMock() |
79
|
1 |
|
self.source.metadata = {"looped": {"port_numbers": [1, 2]}} |
80
|
1 |
|
self.source.switch.get_interface_by_port_no.return_value = destination |
81
|
1 |
|
proxy_port = ProxyPort(self.source) |
82
|
1 |
|
assert proxy_port.destination == destination |
83
|
1 |
|
self.source.switch.get_interface_by_port_no.assert_called_once_with(2) |
84
|
|
|
|
85
|
1 |
|
def test_status_up(self): |
86
|
|
|
"""Test status property when both interfaces are UP.""" |
87
|
|
|
|
88
|
1 |
|
destination = MagicMock() |
89
|
1 |
|
self.source.status = EntityStatus.UP |
90
|
1 |
|
self.source.metadata = {"looped": {"port_numbers": [1, 2]}} |
91
|
1 |
|
self.source.switch.get_interface_by_port_no.return_value = destination |
92
|
1 |
|
destination.status = EntityStatus.UP |
93
|
1 |
|
proxy_port = ProxyPort(self.source) |
94
|
1 |
|
assert proxy_port.status == EntityStatus.UP |
95
|
|
|
|
96
|
1 |
|
def test_status_down_source(self): |
97
|
|
|
"""Test status property when source interface is DOWN.""" |
98
|
|
|
|
99
|
1 |
|
destination = MagicMock() |
100
|
1 |
|
self.source.status = EntityStatus.DOWN |
101
|
1 |
|
self.source.metadata = {"looped": {"port_numbers": [1, 2]}} |
102
|
1 |
|
self.source.switch.get_interface_by_port_no.return_value = destination |
103
|
1 |
|
destination.status = EntityStatus.UP |
104
|
1 |
|
proxy_port = ProxyPort(self.source) |
105
|
1 |
|
assert proxy_port.status == EntityStatus.DOWN |
106
|
|
|
|
107
|
1 |
|
def test_status_down_destination(self): |
108
|
|
|
"""Test status property when destination interface is DOWN.""" |
109
|
|
|
|
110
|
1 |
|
destination = MagicMock() |
111
|
1 |
|
self.source.status = EntityStatus.UP |
112
|
1 |
|
self.source.metadata = {"looped": {"port_numbers": [1, 2]}} |
113
|
1 |
|
self.source.switch.get_interface_by_port_no.return_value = destination |
114
|
1 |
|
destination.status = EntityStatus.DOWN |
115
|
1 |
|
proxy_port = ProxyPort(self.source) |
116
|
1 |
|
assert proxy_port.status == EntityStatus.DOWN |
117
|
|
|
|
118
|
1 |
|
def test_status_no_destination(self): |
119
|
|
|
"""Test status property when destination is None.""" |
120
|
|
|
|
121
|
1 |
|
self.source.status = EntityStatus.UP |
122
|
1 |
|
self.source.metadata = {} |
123
|
1 |
|
proxy_port = ProxyPort(self.source) |
124
|
|
|
assert proxy_port.status == EntityStatus.DOWN |
125
|
|
|
|