Passed
Pull Request — master (#52)
by Vinicius
02:56
created

TestLoadEntries.test_default_eth()   A

Complexity

Conditions 1

Size

Total Lines 7
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 6
nop 1
dl 0
loc 7
ccs 6
cts 6
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
"""
2
    Test tracing.trace_entries
3
"""
4 1
from unittest import TestCase
5
6 1
from napps.amlight.sdntrace.tracing.trace_entries import TraceEntries
7
8
9 1
class TestDpid(TestCase):
10
    """Test all combinations for DPID"""
11
12 1
    def setUp(self):
13 1
        self.trace_entries = TraceEntries()
14
15 1
    def test_incorrect_dpid_int(self):
16
        """DPID cannot be integer"""
17 1
        with self.assertRaises(ValueError):
18 1
            self.trace_entries.dpid = 0
19
20 1
    def test_incorrect_dpid_empty_str(self):
21
        """DPID cannot be an empty str"""
22 1
        with self.assertRaises(ValueError):
23 1
            self.trace_entries.dpid = ""
24
25 1
    def test_incorrect_dpid_invalid_characters_plus(self):
26
        """DPID cannot be a special character (only ':' is allowed)"""
27 1
        with self.assertRaises(ValueError):
28 1
            self.trace_entries.dpid = "+"
29
30 1
    def test_incorrect_dpid_invalid_characters_comma(self):
31
        """DPID cannot be a special character (only ':' is allowed)"""
32 1
        with self.assertRaises(ValueError):
33 1
            self.trace_entries.dpid = ","
34
35 1
    def test_incorrect_dpid_invalid_length(self):
36
        """DPID cannot be longer than 16 without ':'"""
37 1
        with self.assertRaises(ValueError):
38 1
            self.trace_entries.dpid = "00000000000000001"
39
40 1
    def test_correct_dpid_char(self):
41
        """Correct DPID with char"""
42 1
        self.trace_entries.dpid = "1"
43
44 1
    def test_read_dpid_char(self):
45
        """Read DPID '1'"""
46 1
        self.test_correct_dpid_char()
47 1
        self.assertEqual(self.trace_entries.dpid, "1")
48
49 1
    def test_correct_dpid_complete(self):
50
        """Correct DPID with colons, numbers, and letters"""
51 1
        self.trace_entries.dpid = "ab:cd:ef:ab:cd:ef:00:01"
52
53 1
    def test_read_dpid_complete(self):
54
        """Read DPID '1'"""
55 1
        self.test_correct_dpid_complete()
56 1
        self.assertEqual(self.trace_entries.dpid, "ab:cd:ef:ab:cd:ef:00:01")
57
58
59 1
class TestInPort(TestCase):
60
    """Test all combinations for IN_PORT"""
61
62 1
    def setUp(self):
63 1
        self.trace_entries = TraceEntries()
64
65 1
    def test_incorrect_in_port_char(self):
66
        """IN_PORT cannot be a char"""
67 1
        with self.assertRaises(ValueError):
68 1
            self.trace_entries.in_port = "1"
69
70 1
    def test_incorrect_in_port_negative(self):
71
        """IN_PORT cannot be negative"""
72 1
        with self.assertRaises(ValueError):
73 1
            self.trace_entries.in_port = -1
74
75 1
    def test_correct_in_port(self):
76
        """IN_PORT Correct"""
77 1
        self.trace_entries.in_port = 1
78
79 1
    def test_read_in_port(self):
80
        """Read DPID '1'"""
81 1
        self.test_correct_in_port()
82 1
        self.assertEqual(self.trace_entries.in_port, 1)
83
84
85 1
class TestDlSrc(TestCase):
86
    """Test all combinations for DL_SRC"""
87
88 1
    def setUp(self):
89 1
        self.trace_entries = TraceEntries()
90
91 1
    def test_incorrect_dl_src_int(self):
92
        """dl_src cannot be integer"""
93 1
        with self.assertRaises(ValueError):
94 1
            self.trace_entries.dl_src = 0
95
96 1
    def test_incorrect_dl_src_empty_str(self):
97
        """dl_src cannot be an empty str"""
98 1
        with self.assertRaises(ValueError):
99 1
            self.trace_entries.dl_src = ""
100
101 1
    def test_incorrect_dl_src_invalid_characters_plus(self):
102
        """dl_src only accepts ':' snd '-'"""
103 1
        with self.assertRaises(ValueError):
104 1
            self.trace_entries.dl_src = "+"
105
106 1
    def test_incorrect_dl_src_invalid_characters_comma(self):
107
        """dl_src cannot be a special character (only ':' is allowed)"""
108 1
        with self.assertRaises(ValueError):
109 1
            self.trace_entries.dl_src = ","
110
111 1
    def test_incorrect_dl_src_invalid_length(self):
112
        """dl_src cannot be longer than 16 without ':'"""
113 1
        with self.assertRaises(ValueError):
114 1
            self.trace_entries.dl_src = "000000000000000001"
115
116 1
    def test_correct_dl_src_char(self):
117
        """Correct dl_src with char"""
118 1
        self.trace_entries.dl_src = "ab:cd:ef:ab:cd:00"
119
120 1
    def test_read_dl_src_char(self):
121
        """Read dl_src '1'"""
122 1
        self.test_correct_dl_src_char()
123 1
        self.assertEqual(self.trace_entries.dl_src, "ab:cd:ef:ab:cd:00")
124
125
126 1
class TestDlDst(TestCase):
127
    """Test all combinations for DL_DST"""
128
129 1
    def setUp(self):
130 1
        self.trace_entries = TraceEntries()
131
132 1
    def test_incorrect_dl_dst_int(self):
133
        """dl_dst cannot be integer"""
134 1
        with self.assertRaises(ValueError):
135 1
            self.trace_entries.dl_dst = 0
136
137 1
    def test_incorrect_dl_dst_empty_str(self):
138
        """dl_dst cannot be an empty str"""
139 1
        with self.assertRaises(ValueError):
140 1
            self.trace_entries.dl_dst = ""
141
142 1
    def test_incorrect_dl_dst_invalid_characters_plus(self):
143
        """dl_dst only accepts ':' snd '-'"""
144 1
        with self.assertRaises(ValueError):
145 1
            self.trace_entries.dl_dst = "+"
146
147 1
    def test_incorrect_dl_dst_invalid_characters_comma(self):
148
        """dl_dst cannot be a special character (only ':' is allowed)"""
149 1
        with self.assertRaises(ValueError):
150 1
            self.trace_entries.dl_dst = ","
151
152 1
    def test_incorrect_dl_dst_invalid_length(self):
153
        """dl_dst cannot be longer than 16 without ':'"""
154 1
        with self.assertRaises(ValueError):
155 1
            self.trace_entries.dl_dst = "000000000000000001"
156
157 1
    def test_correct_dl_dst_char(self):
158
        """Correct dl_dst with char"""
159 1
        self.trace_entries.dl_dst = "ab:cd:ef:ab:cd:00"
160
161 1
    def test_read_dl_dst_char(self):
162
        """Read dl_dst '1'"""
163 1
        self.test_correct_dl_dst_char()
164 1
        self.assertEqual(self.trace_entries.dl_dst, "ab:cd:ef:ab:cd:00")
165
166
167 1
class TestDlVlan(TestCase):
168
    """Test all combinations for DL_VLAN"""
169
170 1
    def setUp(self):
171 1
        self.trace_entries = TraceEntries()
172
173 1
    def test_incorrect_dl_vlan_char(self):
174
        """dl_vlan cannot be a char"""
175 1
        with self.assertRaises(ValueError):
176 1
            self.trace_entries.dl_vlan = "1"
177
178 1
    def test_incorrect_dl_vlan_negative(self):
179
        """dl_vlan cannot be negative"""
180 1
        with self.assertRaises(ValueError):
181 1
            self.trace_entries.dl_vlan = -1
182
183 1
    def test_incorrect_dl_vlan_too_big(self):
184
        """dl_vlan cannot be bigger than 4095"""
185 1
        with self.assertRaises(ValueError):
186 1
            self.trace_entries.dl_vlan = 4096
187
188 1
    def test_correct_dl_vlan(self):
189
        """dl_vlan Correct"""
190 1
        self.trace_entries.dl_vlan = 1
191
192 1
    def test_read_dl_vlan(self):
193
        """Read DPID '1'"""
194 1
        self.test_correct_dl_vlan()
195 1
        self.assertEqual(self.trace_entries.dl_vlan, 1)
196
197
198 1
class TestDlType(TestCase):
199
    """Test all combinations for dl_type"""
200
201 1
    def setUp(self):
202 1
        self.trace_entries = TraceEntries()
203
204 1
    def test_incorrect_dl_type_char(self):
205
        """dl_type cannot be a char"""
206 1
        with self.assertRaises(ValueError):
207 1
            self.trace_entries.dl_type = "1"
208
209 1
    def test_incorrect_dl_type_negative(self):
210
        """dl_type cannot be negative"""
211 1
        with self.assertRaises(ValueError):
212 1
            self.trace_entries.dl_type = -1
213
214 1
    def test_incorrect_dl_type_too_big(self):
215
        """dl_type cannot be bigger than 4095"""
216 1
        with self.assertRaises(ValueError):
217 1
            self.trace_entries.dl_type = 65536
218
219 1
    def test_correct_dl_type(self):
220
        """dl_type Correct"""
221 1
        self.trace_entries.dl_type = 1
222
223 1
    def test_read_dl_type(self):
224
        """Read DPID '1'"""
225 1
        self.test_correct_dl_type()
226 1
        self.assertEqual(self.trace_entries.dl_type, 1)
227
228
229 1
class TestDlVlanPcp(TestCase):
230
    """Test all combinations for dl_vlan_pcp"""
231
232 1
    def setUp(self):
233 1
        self.trace_entries = TraceEntries()
234
235 1
    def test_incorrect_dl_vlan_pcp_char(self):
236
        """dl_vlan_pcp cannot be a char"""
237 1
        with self.assertRaises(ValueError):
238 1
            self.trace_entries.dl_vlan_pcp = "1"
239
240 1
    def test_incorrect_dl_vlan_pcp_negative(self):
241
        """dl_vlan_pcp cannot be negative"""
242 1
        with self.assertRaises(ValueError):
243 1
            self.trace_entries.dl_vlan_pcp = -1
244
245 1
    def test_incorrect_dl_vlan_pcp_too_big(self):
246
        """dl_vlan_pcp cannot be bigger than 7"""
247 1
        with self.assertRaises(ValueError):
248 1
            self.trace_entries.dl_vlan_pcp = 8
249
250 1
    def test_correct_dl_vlan_pcp(self):
251
        """dl_vlan_pcp Correct"""
252 1
        self.trace_entries.dl_vlan_pcp = 1
253
254 1
    def test_read_dl_vlan_pcp(self):
255
        """Read DPID '1'"""
256 1
        self.test_correct_dl_vlan_pcp()
257 1
        self.assertEqual(self.trace_entries.dl_vlan_pcp, 1)
258
259
260 1
class TestNwTos(TestCase):
261
    """Test all combinations for nw_tos"""
262
263 1
    def setUp(self):
264 1
        self.trace_entries = TraceEntries()
265
266 1
    def test_incorrect_nw_tos_char(self):
267
        """nw_tos cannot be a char"""
268 1
        with self.assertRaises(ValueError):
269 1
            self.trace_entries.nw_tos = "1"
270
271 1
    def test_incorrect_nw_tos_negative(self):
272
        """nw_tos cannot be negative"""
273 1
        with self.assertRaises(ValueError):
274 1
            self.trace_entries.nw_tos = -1
275
276 1
    def test_incorrect_nw_tos_too_big(self):
277
        """nw_tos cannot be bigger than 7"""
278 1
        with self.assertRaises(ValueError):
279 1
            self.trace_entries.nw_tos = 8
280
281 1
    def test_correct_nw_tos(self):
282
        """nw_tos Correct"""
283 1
        self.trace_entries.nw_tos = 1
284
285 1
    def test_read_nw_tos(self):
286
        """Read DPID '1'"""
287 1
        self.test_correct_nw_tos()
288 1
        self.assertEqual(self.trace_entries.nw_tos, 1)
289
290
291 1
class TestNwSrc(TestCase):
292
    """Test all combinations for NW_SRC"""
293
294 1
    def setUp(self):
295 1
        self.trace_entries = TraceEntries()
296
297 1
    def test_incorrect_nw_src_int(self):
298
        """nw_src cannot be integer"""
299 1
        with self.assertRaises(ValueError):
300 1
            self.trace_entries.nw_src = 0
301
302 1
    def test_incorrect_nw_src_empty_str(self):
303
        """nw_src cannot be an empty str"""
304 1
        with self.assertRaises(ValueError):
305 1
            self.trace_entries.nw_src = ""
306
307 1
    def test_incorrect_nw_src_invalid_characters_plus(self):
308
        """nw_src only accepts '.'"""
309 1
        with self.assertRaises(ValueError):
310 1
            self.trace_entries.nw_src = "+"
311
312 1
    def test_incorrect_nw_src_256(self):
313
        """Correct nw_src with char"""
314 1
        with self.assertRaises(ValueError):
315 1
            self.trace_entries.nw_src = "0.0.0.256"
316
317 1
    def test_correct_nw_src_zeroed(self):
318
        """Correct nw_src with char"""
319 1
        self.trace_entries.nw_src = "0.0.0.0"
320
321 1
    def test_read_nw_src_char(self):
322
        """Read nw_src '1'"""
323 1
        self.test_correct_nw_src_zeroed()
324 1
        self.assertEqual(self.trace_entries.nw_src, "0.0.0.0")
325
326 1
    def test_correct_nw_src_255(self):
327
        """Correct nw_src with char"""
328 1
        self.trace_entries.nw_src = "255.255.255.255"
329
330 1
    def test_read_nw_src_char_255(self):
331
        """Read nw_src '1'"""
332 1
        self.test_correct_nw_src_255()
333 1
        self.assertEqual(self.trace_entries.nw_src, "255.255.255.255")
334
335
336 1
class TestNwDst(TestCase):
337
    """Test all combinations for NW_DST"""
338
339 1
    def setUp(self):
340 1
        self.trace_entries = TraceEntries()
341
342 1
    def test_incorrect_nw_dst_int(self):
343
        """nw_dst cannot be integer"""
344 1
        with self.assertRaises(ValueError):
345 1
            self.trace_entries.nw_dst = 0
346
347 1
    def test_incorrect_nw_dst_empty_str(self):
348
        """nw_dst cannot be an empty str"""
349 1
        with self.assertRaises(ValueError):
350 1
            self.trace_entries.nw_dst = ""
351
352 1
    def test_incorrect_nw_dst_invalid_characters_plus(self):
353
        """nw_dst only accepts '.'"""
354 1
        with self.assertRaises(ValueError):
355 1
            self.trace_entries.nw_dst = "+"
356
357 1
    def test_incorrect_nw_dst_256(self):
358
        """Correct nw_dst with char"""
359 1
        with self.assertRaises(ValueError):
360 1
            self.trace_entries.nw_dst = "0.0.0.256"
361
362 1
    def test_correct_nw_dst_zeroed(self):
363
        """Correct nw_dst with char"""
364 1
        self.trace_entries.nw_dst = "0.0.0.0"
365
366 1
    def test_read_nw_dst_char(self):
367
        """Read nw_dst '1'"""
368 1
        self.test_correct_nw_dst_zeroed()
369 1
        self.assertEqual(self.trace_entries.nw_dst, "0.0.0.0")
370
371 1
    def test_correct_nw_dst_255(self):
372
        """Correct nw_dst with char"""
373 1
        self.trace_entries.nw_dst = "255.255.255.255"
374
375 1
    def test_read_nw_dst_char_255(self):
376
        """Read nw_dst '1'"""
377 1
        self.test_correct_nw_dst_255()
378 1
        self.assertEqual(self.trace_entries.nw_dst, "255.255.255.255")
379
380
381 1
class TestTpSrc(TestCase):
382
    """Test all combinations for tp_src"""
383
384 1
    def setUp(self):
385 1
        self.trace_entries = TraceEntries()
386
387 1
    def test_incorrect_tp_src_char(self):
388
        """tp_src cannot be a char"""
389 1
        with self.assertRaises(ValueError):
390 1
            self.trace_entries.tp_src = "1"
391
392 1
    def test_incorrect_tp_src_negative(self):
393
        """tp_src cannot be negative"""
394 1
        with self.assertRaises(ValueError):
395 1
            self.trace_entries.tp_src = -1
396
397 1
    def test_incorrect_tp_src_too_big(self):
398
        """tp_src cannot be bigger than 4095"""
399 1
        with self.assertRaises(ValueError):
400 1
            self.trace_entries.tp_src = 65536
401
402 1
    def test_correct_tp_src(self):
403
        """tp_src Correct"""
404 1
        self.trace_entries.tp_src = 1
405
406 1
    def test_read_tp_src(self):
407
        """Read DPID '1'"""
408 1
        self.test_correct_tp_src()
409 1
        self.assertEqual(self.trace_entries.tp_src, 1)
410
411
412 1
class TestTpDst(TestCase):
413
    """Test all combinations for tp_dst"""
414
415 1
    def setUp(self):
416 1
        self.trace_entries = TraceEntries()
417
418 1
    def test_incorrect_tp_dst_char(self):
419
        """tp_dst cannot be a char"""
420 1
        with self.assertRaises(ValueError):
421 1
            self.trace_entries.tp_dst = "1"
422
423 1
    def test_incorrect_tp_dst_negative(self):
424
        """tp_dst cannot be negative"""
425 1
        with self.assertRaises(ValueError):
426 1
            self.trace_entries.tp_dst = -1
427
428 1
    def test_incorrect_tp_dst_too_big(self):
429
        """tp_dst cannot be bigger than 4095"""
430 1
        with self.assertRaises(ValueError):
431 1
            self.trace_entries.tp_dst = 65536
432
433 1
    def test_correct_tp_dst(self):
434
        """tp_dst Correct"""
435 1
        self.trace_entries.tp_dst = 1
436
437 1
    def test_read_tp_dst(self):
438
        """Read DPID '1'"""
439 1
        self.test_correct_tp_dst()
440 1
        self.assertEqual(self.trace_entries.tp_dst, 1)
441
442
443 1
class TestLoadEntries(TestCase):
444
    """Now, load all entries at once"""
445
446 1
    def setUp(self):
447 1
        self.trace_entries = TraceEntries()
448
449 1
    def test_missing_trace(self):
450
        """key trace is mandatory"""
451 1
        with self.assertRaises(ValueError):
452 1
            entries = {}
453 1
            self.trace_entries.load_entries(entries)
454
455 1
    def test_trace_non_dict(self):
456
        """key trace has to be a dict"""
457 1
        with self.assertRaises(ValueError):
458 1
            entries = {"trace:": 0}
459 1
            self.trace_entries.load_entries(entries)
460
461 1
    def test_missing_switch(self):
462
        """key trace/switch is mandatory"""
463 1
        with self.assertRaises(ValueError):
464 1
            entries = {"trace": {}}
465 1
            self.trace_entries.load_entries(entries)
466
467 1
    def test_missing_dpid(self):
468
        """key trace/switch is mandatory"""
469 1
        with self.assertRaises(ValueError):
470 1
            dpid = {}
471 1
            switch = {"switch": dpid}
472 1
            entries = {"trace": switch}
473 1
            self.trace_entries.load_entries(entries)
474
475 1
    def test_missing_in_port(self):
476
        """key trace/switch is mandatory"""
477 1
        with self.assertRaises(ValueError):
478 1
            dpid = {"dpid": "a"}
479 1
            switch = {"switch": dpid}
480 1
            entries = {"trace": switch}
481 1
            self.trace_entries.load_entries(entries)
482
483 1
    def test_invalid_eth(self):
484
        """key trace/switch is mandatory"""
485 1
        with self.assertRaises(ValueError):
486 1
            eth = 0
487 1
            dpid = {"dpid": "a", "in_port": 1}
488 1
            switch = {"switch": dpid, "eth": eth}
489 1
            entries = {"trace": switch}
490 1
            self.trace_entries.load_entries(entries)
491
492 1
    def test_default_eth(self):
493
        """Test default eth"""
494 1
        dpid = {"dpid": "a", "in_port": 1}
495 1
        switch = {"switch": dpid}
496 1
        entries = {"trace": switch}
497 1
        self.trace_entries.load_entries(entries)
498 1
        assert not self.trace_entries.dl_vlan
499
500 1
    def test_minimally_correct(self):
501
        """key trace/switch is mandatory"""
502 1
        eth = {"dl_vlan": 100}
503 1
        dpid = {"dpid": "a", "in_port": 1}
504 1
        switch = {"switch": dpid, "eth": eth}
505 1
        entries = {"trace": switch}
506 1
        self.trace_entries.load_entries(entries)
507 1
        assert self.trace_entries.dl_vlan == eth["dl_vlan"]
508 1
        assert self.trace_entries.dpid == dpid["dpid"]
509
        assert self.trace_entries.in_port == dpid["in_port"]
510