Passed
Push — master ( 2ac47d...3b720a )
by Vinicius
08:30 queued 06:31
created

TestTopoController.test_disable_link()   A

Complexity

Conditions 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 6
nop 1
dl 0
loc 8
rs 10
c 0
b 0
f 0
ccs 5
cts 5
cp 1
crap 1
1
"""Module to TopoController."""
2
3 1
from unittest import TestCase
4 1
from unittest.mock import MagicMock
5
6 1
from napps.kytos.topology.controllers import TopoController
7
from napps.kytos.topology.db.models import LinkDoc, SwitchDoc
8 1
9 1
10
class TestTopoController(TestCase):  # pylint: disable=too-many-public-methods
11
    """Test the Main class."""
12 1
13
    def setUp(self) -> None:
14
        """Execute steps before each tests."""
15 1
        self.topo = TopoController(MagicMock())
16
        self.dpid = "00:00:00:00:00:00:00:01"
17 1
        self.interface_id = f"{self.dpid}:1"
18 1
        self.link_id = "some_id"
19 1
20 1
    def test_boostrap_indexes(self) -> None:
21
        """Test_boostrap_indexes."""
22 1
        self.topo.bootstrap_indexes()
23
24 1
        expected_indexes = [
25
            ("switches", [("interfaces.id", 1)]),
26 1
            ("links", [("endpoints.id", 1)]),
27
        ]
28
        mock = self.topo.mongo.bootstrap_index
29
        assert mock.call_count == len(expected_indexes)
30 1
        indexes = [(v[0][0], v[0][1]) for v in mock.call_args_list]
31 1
        assert expected_indexes == indexes
32 1
33 1
    def test_get_topology(self) -> None:
34
        """Test_get_topology."""
35 1
        self.topo.get_switches = MagicMock()
36
        self.topo.get_links = MagicMock()
37 1
        assert "topology" in self.topo.get_topology()
38 1
        assert self.topo.get_switches.call_count == 1
39 1
        assert self.topo.get_links.call_count == 1
40 1
41 1
    def test_get_links(self) -> None:
42
        """test_get_links."""
43 1
        assert "links" in self.topo.get_links()
44
        assert self.topo.db.links.aggregate.call_count == 1
45 1
        arg = self.topo.db.links.aggregate.call_args[0]
46 1
        assert arg[0] == [{"$sort": {"_id": 1}},
47 1
                          {"$project": LinkDoc.projection()}]
48 1
49
    def test_get_switches(self) -> None:
50
        """test_get_switches."""
51 1
        assert "switches" in self.topo.get_switches()
52
        assert self.topo.db.switches.aggregate.call_count == 1
53 1
        arg = self.topo.db.switches.aggregate.call_args[0]
54 1
        assert arg[0] == [
55 1
            {"$sort": {"_id": 1}},
56 1
            {"$project": SwitchDoc.projection()},
57
        ]
58
59
    def test_get_interfaces(self) -> None:
60
        """test_get_interfaces."""
61 1
        assert "interfaces" in self.topo.get_interfaces()
62
        assert self.topo.db.switches.aggregate.call_count == 1
63 1
        arg = self.topo.db.switches.aggregate.call_args[0]
64 1
        assert arg[0] == [
65 1
            {"$sort": {"_id": 1}},
66 1
            {"$project": {"interfaces": 1, "_id": 0}},
67
            {"$unwind": "$interfaces"},
68
            {"$replaceRoot": {"newRoot": "$interfaces"}},
69
        ]
70
71
    def test_enable_switch(self) -> None:
72
        """test_enable_switch."""
73 1
        self.topo.enable_switch(self.dpid)
74
75 1
        self.topo.db.switches.find_one_and_update.assert_called()
76
        arg1, arg2 = self.topo.db.switches.find_one_and_update.call_args[0]
77 1
        assert arg1 == {"_id": self.dpid}
78 1
        assert arg2["$set"]["enabled"]
79 1
80 1
    def test_disable_switch(self) -> None:
81
        """test_disable_switch."""
82 1
        self.topo.disable_switch(self.dpid)
83
84 1
        self.topo.db.switches.find_one_and_update.assert_called()
85
        arg1, arg2 = self.topo.db.switches.find_one_and_update.call_args[0]
86 1
        assert arg1 == {"_id": self.dpid}
87 1
        assert not arg2["$set"]["enabled"]
88 1
89 1
    def test_add_switch_metadata(self) -> None:
90
        """test_add_switch_metadata."""
91 1
        metadata = {"some": "value"}
92
        self.topo.add_switch_metadata(self.dpid, metadata)
93 1
94 1
        self.topo.db.switches.find_one_and_update.assert_called()
95
        arg1, arg2 = self.topo.db.switches.find_one_and_update.call_args[0]
96 1
        assert arg1 == {"_id": self.dpid}
97 1
        assert arg2["$set"]["metadata.some"] == "value"
98 1
99 1
    def test_delete_switch_metadata(self) -> None:
100
        """test_delete_switch_metadata."""
101 1
        key = "some"
102
        self.topo.delete_switch_metadata_key(self.dpid, key)
103 1
104 1
        self.topo.db.switches.find_one_and_update.assert_called()
105
        arg1, arg2 = self.topo.db.switches.find_one_and_update.call_args[0]
106 1
        assert arg1 == {"_id": self.dpid}
107 1
        assert arg2["$unset"][f"metadata.{key}"] == ""
108 1
109 1
    def test_enable_interface(self) -> None:
110
        """test_enable_interface."""
111 1
        self.topo.enable_interface(self.interface_id)
112
113 1
        self.topo.db.switches.find_one_and_update.assert_called()
114
        arg1, arg2 = self.topo.db.switches.find_one_and_update.call_args[0]
115 1
        assert arg1 == {"interfaces.id": self.interface_id}
116 1
        assert arg2["$set"]["interfaces.$.enabled"]
117 1
118 1
    def test_disable_interface(self) -> None:
119
        """test_disable_interface."""
120 1
        self.topo.disable_interface(self.interface_id)
121
122 1
        self.topo.db.switches.find_one_and_update.assert_called()
123
        arg1, arg2 = self.topo.db.switches.find_one_and_update.call_args[0]
124 1
        assert arg1 == {"interfaces.id": self.interface_id}
125 1
        assert not arg2["$set"]["interfaces.$.enabled"]
126 1
127 1
    def test_add_interface_metadata(self) -> None:
128
        """test_add_interface_metadata."""
129 1
        metadata = {"some": "value"}
130
        self.topo.add_interface_metadata(self.interface_id, metadata)
131 1
132 1
        self.topo.db.switches.find_one_and_update.assert_called()
133
        arg1, arg2 = self.topo.db.switches.find_one_and_update.call_args[0]
134 1
        assert arg1 == {"interfaces.id": self.interface_id}
135 1
        assert arg2["$set"]["interfaces.$.metadata.some"] == "value"
136 1
137 1
    def test_delete_interface_metadata_key(self) -> None:
138
        """test_delete_interface_metadata."""
139 1
        key = "some"
140
        self.topo.delete_interface_metadata_key(self.interface_id, key)
141 1
142 1
        self.topo.db.switches.find_one_and_update.assert_called()
143
        arg1, arg2 = self.topo.db.switches.find_one_and_update.call_args[0]
144 1
        assert arg1 == {"interfaces.id": self.interface_id}
145 1
        assert arg2["$unset"][f"interfaces.$.metadata.{key}"] == ""
146 1
147 1
    def test_enable_link(self) -> None:
148
        """test_enable_link."""
149 1
        self.topo.enable_link(self.link_id)
150
151 1
        self.topo.db.links.find_one_and_update.assert_called()
152
        arg1, arg2 = self.topo.db.links.find_one_and_update.call_args[0]
153 1
        assert arg1 == {"_id": self.link_id}
154 1
        assert arg2["$set"]["enabled"]
155 1
156 1
    def test_disable_link(self) -> None:
157
        """test_disable_link."""
158 1
        self.topo.disable_link(self.link_id)
159
160 1
        self.topo.db.links.find_one_and_update.assert_called()
161
        arg1, arg2 = self.topo.db.links.find_one_and_update.call_args[0]
162 1
        assert arg1 == {"_id": self.link_id}
163 1
        assert not arg2["$set"]["enabled"]
164 1
165 1
    def test_add_link_metadata(self) -> None:
166
        """test_add_link_metadata."""
167 1
        key = "some_key"
168
        value = "some_value"
169 1
        self.topo.add_link_metadata(self.link_id, {key: value})
170 1
171 1
        self.topo.db.links.find_one_and_update.assert_called()
172
        arg1, arg2 = self.topo.db.links.find_one_and_update.call_args[0]
173 1
        assert arg1 == {"_id": self.link_id}
174 1
        assert arg2["$set"][f"metadata.{key}"] == value
175 1
176 1
    def test_delete_link_metadata_key(self) -> None:
177
        """test_delete_link_metadata_key."""
178 1
        key = "some_key"
179
        self.topo.delete_link_metadata_key(self.link_id, key)
180 1
181 1
        self.topo.db.links.find_one_and_update.assert_called()
182
        arg1, arg2 = self.topo.db.links.find_one_and_update.call_args[0]
183 1
        assert arg1 == {"_id": self.link_id}
184 1
        assert arg2["$unset"][f"metadata.{key}"] == ""
185 1
186 1
    def test_get_interfaces_details(self) -> None:
187
        """test_get_insterfaces_details."""
188 1
        interfaces_ids = ["1", "2", "3"]
189
        self.topo.get_interfaces_details(interfaces_ids)
190 1
        self.topo.db.interface_details.aggregate.assert_called_with(
191 1
            [{"$match": {"_id": {"$in": interfaces_ids}}}]
192 1
        )
193
194
    def test_upsert_interface_details(self) -> None:
195
        """test_upsert_interface_details."""
196 1
        id_ = "intf_id"
197
        available_tags = {'vlan': [[10, 4095]]}
198 1
        tag_ranges = {'vlan': [[5, 4095]]}
199
        self.topo.upsert_interface_details(
200
            id_, available_tags, tag_ranges
201
        )
202 1
        arg = self.topo.db.interface_details.find_one_and_update.call_args[0]
203
        assert arg[0] == {"_id": id_}
204 1
        assert arg[1]["$set"]["_id"] == id_
205 1
        assert arg[1]["$set"]["tag_ranges"] == tag_ranges
206 1
        assert arg[1]["$set"]["available_tags"] == available_tags
207 1
208 1
    def test_upsert_switch(self) -> None:
209
        """test_upsert_switch."""
210 1
        switch_dict = {"enabled": True, "active": True, "_id": self.dpid}
211
        self.topo.upsert_switch(self.dpid, switch_dict)
212 1
213 1
        self.topo.db.switches.find_one_and_update.assert_called()
214
        arg1, arg2 = self.topo.db.switches.find_one_and_update.call_args[0]
215 1
        assert arg1 == {"_id": self.dpid}
216 1
        for key, value in switch_dict.items():
217 1
            if key == "active":
218 1
                continue
219 1
            assert arg2["$set"][key] == value
220 1
221 1
    def test_upsert_link(self) -> None:
222
        """test_upsert_link."""
223 1
        link_dict = {
224
            "_id": self.link_id,
225 1
            "enabled": True,
226
            "active": True,
227
            "endpoint_a": {"id": "00:00:00:00:00:00:00:01:1"},
228
            "endpoint_b": {"id": "00:00:00:00:00:00:00:02:01"},
229
        }
230
        self.topo.upsert_link(self.link_id, link_dict)
231
        assert self.topo.db.switches.find_one_and_update.call_count == 2
232
        assert self.topo.db.links.find_one_and_update.call_count == 1
233