TestEVC.test_without_uni_a()   A
last analyzed

Complexity

Conditions 2

Size

Total Lines 10
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 8
nop 1
dl 0
loc 10
rs 10
c 0
b 0
f 0
1
"""Module to test the EVCBase class."""
2
import sys
3
from unittest import TestCase
4
from unittest.mock import patch
5
6
# pylint: disable=wrong-import-position
7
sys.path.insert(0, "/var/lib/kytos/napps/..")
8
# pylint: enable=wrong-import-position
9
from napps.kytos.mef_eline.models import EVC  # NOQA  pycodestyle
10
from napps.kytos.mef_eline.scheduler import (
11
    CircuitSchedule,
12
)  # NOQA  pycodestyle
13
from napps.kytos.mef_eline.tests.helpers import (
14
    get_uni_mocked,
15
    get_controller_mock,
16
)  # NOQA  pycodestyle
17
18
19
class TestEVC(TestCase):  # pylint: disable=too-many-public-methods
20
    """Tests to verify EVC class."""
21
22
    def test_attributes_empty(self):
23
        """Test if the EVC raises an error with name is required."""
24
        attributes = {"controller": get_controller_mock()}
25
        error_message = "name is required."
26
        with self.assertRaises(ValueError) as handle_error:
27
            EVC(**attributes)
28
        self.assertEqual(str(handle_error.exception), error_message)
29
30
    def test_without_uni_a(self):
31
        """Test if the EVC raises and error with UNI A is required."""
32
        attributes = {
33
            "controller": get_controller_mock(),
34
            "name": "circuit_name",
35
        }
36
        error_message = "uni_a is required."
37
        with self.assertRaises(ValueError) as handle_error:
38
            EVC(**attributes)
39
        self.assertEqual(str(handle_error.exception), error_message)
40
41
    def test_with_invalid_uni_a(self):
42
        """Test if the EVC raises and error with invalid UNI A."""
43
        attributes = {
44
            "controller": get_controller_mock(),
45
            "name": "circuit_name",
46
            "uni_a": get_uni_mocked(tag_value=82),
47
        }
48
        error_message = "VLAN tag 82 is not available in uni_a"
49
        with self.assertRaises(ValueError) as handle_error:
50
            EVC(**attributes)
51
        self.assertEqual(str(handle_error.exception), error_message)
52
53
    def test_without_uni_z(self):
54
        """Test if the EVC raises and error with UNI Z is required."""
55
        attributes = {
56
            "controller": get_controller_mock(),
57
            "name": "circuit_name",
58
            "uni_a": get_uni_mocked(is_valid=True),
59
        }
60
        error_message = "uni_z is required."
61
        with self.assertRaises(ValueError) as handle_error:
62
            EVC(**attributes)
63
        self.assertEqual(str(handle_error.exception), error_message)
64
65
    def test_with_invalid_uni_z(self):
66
        """Test if the EVC raises and error with UNI Z is required."""
67
        attributes = {
68
            "controller": get_controller_mock(),
69
            "name": "circuit_name",
70
            "uni_a": get_uni_mocked(is_valid=True),
71
            "uni_z": get_uni_mocked(tag_value=83),
72
        }
73
        error_message = "VLAN tag 83 is not available in uni_z"
74
        with self.assertRaises(ValueError) as handle_error:
75
            EVC(**attributes)
76
        self.assertEqual(str(handle_error.exception), error_message)
77
78
    def test_update_read_only(self):
79
        """Test if raises an error when trying to update read only attr."""
80
        attributes = {
81
            "controller": get_controller_mock(),
82
            "name": "circuit_name",
83
            "uni_a": get_uni_mocked(is_valid=True),
84
            "uni_z": get_uni_mocked(is_valid=True),
85
        }
86
        update_attr = [
87
            ("archived", True),
88
            ("_id", True),
89
            ("active", True),
90
            ("current_path", []),
91
            ("creation_time", "date"),
92
        ]
93
94
        for name, value in update_attr:
95
            with self.subTest(name=name, value=value):
96
                update_dict = {name: value}
97
                error_message = f"{name} can't be updated."
98
                with self.assertRaises(ValueError) as handle_error:
99
                    evc = EVC(**attributes)
100
                    evc.update(**update_dict)
101
                self.assertEqual(str(handle_error.exception), error_message)
102
103
    @patch("napps.kytos.mef_eline.models.EVC.sync")
104
    def test_update_disable(self, _sync_mock):
105
        """Test if evc is disabled."""
106
        attributes = {
107
            "controller": get_controller_mock(),
108
            "name": "circuit_name",
109
            "enable": True,
110
            "uni_a": get_uni_mocked(is_valid=True),
111
            "uni_z": get_uni_mocked(is_valid=True),
112
        }
113
        update_dict = {"enable": False}
114
        evc = EVC(**attributes)
115
        evc.update(**update_dict)
116
        self.assertIs(evc.is_enabled(), False)
117
118
    @patch("napps.kytos.mef_eline.models.EVC.sync")
119
    def test_update_queue(self, _sync_mock):
120
        """Test if evc is set to redeploy."""
121
        attributes = {
122
            "controller": get_controller_mock(),
123
            "name": "circuit_name",
124
            "enable": True,
125
            "uni_a": get_uni_mocked(is_valid=True),
126
            "uni_z": get_uni_mocked(is_valid=True),
127
        }
128
        update_dict = {"queue_id": 3}
129
        evc = EVC(**attributes)
130
        _, redeploy = evc.update(**update_dict)
131
        self.assertTrue(redeploy)
132
133
    def test_circuit_representation(self):
134
        """Test the method __repr__."""
135
        attributes = {
136
            "controller": get_controller_mock(),
137
            "name": "circuit_name",
138
            "uni_a": get_uni_mocked(is_valid=True),
139
            "uni_z": get_uni_mocked(is_valid=True),
140
        }
141
        evc = EVC(**attributes)
142
        expected_value = f"EVC({evc.id}, {evc.name})"
143
        self.assertEqual(str(evc), expected_value)
144
145
    def test_comparison_method(self):
146
        """Test the method __eq__."""
147
        attributes = {
148
            "controller": get_controller_mock(),
149
            "name": "circuit_name",
150
            "uni_a": get_uni_mocked(is_valid=True),
151
            "uni_z": get_uni_mocked(is_valid=True),
152
        }
153
        evc1 = EVC(**attributes)
154
        evc2 = EVC(**attributes)
155
156
        attributes = {
157
            "controller": get_controller_mock(),
158
            "name": "circuit_name_2",
159
            "uni_a": get_uni_mocked(is_valid=True),
160
            "uni_z": get_uni_mocked(is_valid=True),
161
        }
162
        evc3 = EVC(**attributes)
163
        evc4 = EVC(**attributes)
164
165
        self.assertEqual(evc1 == evc2, True)
166
        self.assertEqual(evc1 == evc3, False)
167
        self.assertEqual(evc2 == evc3, False)
168
        self.assertEqual(evc3 == evc4, True)
169
170
    def test_as_dict(self):
171
        """Test the method as_dict."""
172
        attributes = {
173
            "controller": get_controller_mock(),
174
            "id": "custom_id",
175
            "name": "custom_name",
176
            "uni_a": get_uni_mocked(is_valid=True),
177
            "uni_z": get_uni_mocked(is_valid=True),
178
            "start_date": "2018-08-21T18:44:54",
179
            "end_date": "2018-08-21T18:44:55",
180
            "primary_links": [],
181
            "request_time": "2018-08-21T19:10:41",
182
            "creation_time": "2018-08-21T18:44:54",
183
            "owner": "my_name",
184
            "circuit_scheduler": [
185
                CircuitSchedule.from_dict(
186
                    {
187
                        "id": 234243247,
188
                        "action": "create",
189
                        "frequency": "1 * * * *",
190
                    }
191
                ),
192
                CircuitSchedule.from_dict(
193
                    {
194
                        "id": 234243239,
195
                        "action": "create",
196
                        "interval": {"hours": 2},
197
                    }
198
                ),
199
            ],
200
            "enabled": True,
201
            "priority": 2,
202
        }
203
        evc = EVC(**attributes)
204
205
        expected_dict = {
206
            "id": "custom_id",
207
            "name": "custom_name",
208
            "uni_a": attributes["uni_a"].as_dict(),
209
            "uni_z": attributes["uni_z"].as_dict(),
210
            "start_date": "2018-08-21T18:44:54",
211
            "end_date": "2018-08-21T18:44:55",
212
            "bandwidth": 0,
213
            "primary_links": [],
214
            "backup_links": [],
215
            "current_path": [],
216
            "primary_path": [],
217
            "backup_path": [],
218
            "dynamic_backup_path": False,
219
            "request_time": "2018-08-21T19:10:41",
220
            "creation_time": "2018-08-21T18:44:54",
221
            "circuit_scheduler": [
222
                {
223
                    "id": 234243247,
224
                    "action": "create",
225
                    "frequency": "1 * * * *",
226
                },
227
                {
228
                    "id": 234243239,
229
                    "action": "create",
230
                    "interval": {"hours": 2},
231
                },
232
            ],
233
            "active": False,
234
            "enabled": True,
235
            "priority": 2,
236
        }
237
        actual_dict = evc.as_dict()
238
        for name, value in expected_dict.items():
239
            actual = actual_dict.get(name)
240
            self.assertEqual(value, actual)
241
242
    @staticmethod
243
    def test_get_id_from_cookie():
244
        """Test get_id_from_cookie."""
245
        attributes = {
246
            "controller": get_controller_mock(),
247
            "name": "circuit_name",
248
            "enable": True,
249
            "uni_a": get_uni_mocked(is_valid=True),
250
            "uni_z": get_uni_mocked(is_valid=True)
251
        }
252
        evc = EVC(**attributes)
253
        evc_id = evc.id
254
        assert evc_id
255
        assert evc.get_id_from_cookie(evc.get_cookie()) == evc_id
256
257
    @staticmethod
258
    def test_get_id_from_cookie_with_leading_zeros():
259
        """Test get_id_from_cookie with leading zeros."""
260
261
        attributes = {
262
            "controller": get_controller_mock(),
263
            "name": "circuit_name",
264
            "enable": True,
265
            "uni_a": get_uni_mocked(is_valid=True),
266
            "uni_z": get_uni_mocked(is_valid=True)
267
        }
268
        evc = EVC(**attributes)
269
        evc_id = "0a2d672d99ff41"
270
        # pylint: disable=protected-access
271
        evc._id = evc_id
272
        # pylint: enable=protected-access
273
        assert EVC.get_id_from_cookie(evc.get_cookie()) == evc_id
274