Passed
Pull Request — master (#160)
by Antonio
03:45
created

TestEVC.test_attributes_empty()   A

Complexity

Conditions 2

Size

Total Lines 7
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 6
nop 1
dl 0
loc 7
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
    def test_update_invalid(self):
104
        """Test updating with an invalid attr"""
105
        attributes = {
106
            "controller": get_controller_mock(),
107
            "name": "circuit_name",
108
            "uni_a": get_uni_mocked(is_valid=True),
109
            "uni_z": get_uni_mocked(is_valid=True),
110
        }
111
        evc = EVC(**attributes)
112
        with self.assertRaises(ValueError) as handle_error:
113
            evc.update(xyz="abc")
114
        self.assertEqual(
115
            str(handle_error.exception),
116
            'The attribute "xyz" is invalid.'
117
        )
118
119
    @patch("napps.kytos.mef_eline.models.EVC.sync")
120
    def test_update_disable(self, _sync_mock):
121
        """Test if evc is disabled."""
122
        attributes = {
123
            "controller": get_controller_mock(),
124
            "name": "circuit_name",
125
            "enable": True,
126
            "uni_a": get_uni_mocked(is_valid=True),
127
            "uni_z": get_uni_mocked(is_valid=True),
128
        }
129
        update_dict = {"enable": False}
130
        evc = EVC(**attributes)
131
        evc.update(**update_dict)
132
        self.assertIs(evc.is_enabled(), False)
133
134
    @patch("napps.kytos.mef_eline.models.EVC.sync")
135
    def test_update_queue(self, _sync_mock):
136
        """Test if evc is set to redeploy."""
137
        attributes = {
138
            "controller": get_controller_mock(),
139
            "name": "circuit_name",
140
            "enable": True,
141
            "uni_a": get_uni_mocked(is_valid=True),
142
            "uni_z": get_uni_mocked(is_valid=True),
143
        }
144
        update_dict = {"queue_id": 3}
145
        evc = EVC(**attributes)
146
        _, redeploy = evc.update(**update_dict)
147
        self.assertTrue(redeploy)
148
149
    def test_circuit_representation(self):
150
        """Test the method __repr__."""
151
        attributes = {
152
            "controller": get_controller_mock(),
153
            "name": "circuit_name",
154
            "uni_a": get_uni_mocked(is_valid=True),
155
            "uni_z": get_uni_mocked(is_valid=True),
156
        }
157
        evc = EVC(**attributes)
158
        expected_value = f"EVC({evc.id}, {evc.name})"
159
        self.assertEqual(str(evc), expected_value)
160
161
    def test_comparison_method(self):
162
        """Test the method __eq__."""
163
        attributes = {
164
            "controller": get_controller_mock(),
165
            "name": "circuit_name",
166
            "uni_a": get_uni_mocked(is_valid=True),
167
            "uni_z": get_uni_mocked(is_valid=True),
168
        }
169
        evc1 = EVC(**attributes)
170
        evc2 = EVC(**attributes)
171
172
        attributes = {
173
            "controller": get_controller_mock(),
174
            "name": "circuit_name_2",
175
            "uni_a": get_uni_mocked(is_valid=True),
176
            "uni_z": get_uni_mocked(is_valid=True),
177
        }
178
        evc3 = EVC(**attributes)
179
        evc4 = EVC(**attributes)
180
181
        self.assertEqual(evc1 == evc2, True)
182
        self.assertEqual(evc1 == evc3, False)
183
        self.assertEqual(evc2 == evc3, False)
184
        self.assertEqual(evc3 == evc4, True)
185
186
    def test_as_dict(self):
187
        """Test the method as_dict."""
188
        attributes = {
189
            "controller": get_controller_mock(),
190
            "id": "custom_id",
191
            "name": "custom_name",
192
            "uni_a": get_uni_mocked(is_valid=True),
193
            "uni_z": get_uni_mocked(is_valid=True),
194
            "start_date": "2018-08-21T18:44:54",
195
            "end_date": "2018-08-21T18:44:55",
196
            "primary_links": [],
197
            "request_time": "2018-08-21T19:10:41",
198
            "creation_time": "2018-08-21T18:44:54",
199
            "owner": "my_name",
200
            "circuit_scheduler": [
201
                CircuitSchedule.from_dict(
202
                    {
203
                        "id": 234243247,
204
                        "action": "create",
205
                        "frequency": "1 * * * *",
206
                    }
207
                ),
208
                CircuitSchedule.from_dict(
209
                    {
210
                        "id": 234243239,
211
                        "action": "create",
212
                        "interval": {"hours": 2},
213
                    }
214
                ),
215
            ],
216
            "enabled": True,
217
            "priority": 2,
218
        }
219
        evc = EVC(**attributes)
220
221
        expected_dict = {
222
            "id": "custom_id",
223
            "name": "custom_name",
224
            "uni_a": attributes["uni_a"].as_dict(),
225
            "uni_z": attributes["uni_z"].as_dict(),
226
            "start_date": "2018-08-21T18:44:54",
227
            "end_date": "2018-08-21T18:44:55",
228
            "bandwidth": 0,
229
            "primary_links": [],
230
            "backup_links": [],
231
            "current_path": [],
232
            "primary_path": [],
233
            "backup_path": [],
234
            "dynamic_backup_path": False,
235
            "request_time": "2018-08-21T19:10:41",
236
            "creation_time": "2018-08-21T18:44:54",
237
            "circuit_scheduler": [
238
                {
239
                    "id": 234243247,
240
                    "action": "create",
241
                    "frequency": "1 * * * *",
242
                },
243
                {
244
                    "id": 234243239,
245
                    "action": "create",
246
                    "interval": {"hours": 2},
247
                },
248
            ],
249
            "active": False,
250
            "enabled": True,
251
            "priority": 2,
252
        }
253
        actual_dict = evc.as_dict()
254
        for name, value in expected_dict.items():
255
            actual = actual_dict.get(name)
256
            self.assertEqual(value, actual)
257
258
    @staticmethod
259
    def test_get_id_from_cookie():
260
        """Test get_id_from_cookie."""
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 = evc.id
270
        assert evc_id
271
        assert evc.get_id_from_cookie(evc.get_cookie()) == evc_id
272
273
    @staticmethod
274
    def test_get_id_from_cookie_with_leading_zeros():
275
        """Test get_id_from_cookie with leading zeros."""
276
277
        attributes = {
278
            "controller": get_controller_mock(),
279
            "name": "circuit_name",
280
            "enable": True,
281
            "uni_a": get_uni_mocked(is_valid=True),
282
            "uni_z": get_uni_mocked(is_valid=True)
283
        }
284
        evc = EVC(**attributes)
285
        evc_id = "0a2d672d99ff41"
286
        # pylint: disable=protected-access
287
        evc._id = evc_id
288
        # pylint: enable=protected-access
289
        assert EVC.get_id_from_cookie(evc.get_cookie()) == evc_id
290