forked from xenserver/python-libs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_ifrename_logic.py
More file actions
654 lines (515 loc) · 25.1 KB
/
test_ifrename_logic.py
File metadata and controls
654 lines (515 loc) · 25.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
from __future__ import print_function
import logging
import sys
import unittest
from copy import deepcopy
try:
import cStringIO as StringIO
except ImportError:
import StringIO
from xcp.net.ifrename.logic import *
from xcp.logger import LOG, openLog, closeLogs
def apply_transactions(lst, trans):
if not len(trans):
raise LogicError("No transactions")
for (src, dst) in trans:
for e in lst:
if e.tname and e.tname == src:
e.tname = dst
break
if e.kname == src:
if e.tname is not None:
raise LogicError("Matched kname with non-null tname. %s"
% (locals(),))
e.tname = dst
break
else:
raise LogicError("Not found suitable eth for transaction. %s"
% (locals(),))
class TestSimpleLogic(unittest.TestCase):
def setUp(self):
self.siobuff = StringIO.StringIO()
openLog(self.siobuff, logging.NOTSET)
def tearDown(self):
closeLogs()
self.siobuff.close()
def debug_state(self, ts):
print("", file=sys.stderr)
print(self.siobuff.getvalue(), file=sys.stderr)
print("", file=sys.stderr)
if len(ts):
for (s,d) in ts:
print("'%s' -> '%s'" % (s, d), file=sys.stderr)
else:
print("No transactions", file=sys.stderr)
print("", file=sys.stderr)
def test_newhw_norules_1eth(self):
"""
One previously unrecognised nic, with no other rules. Expecting
it to be named as eth0
"""
eth0 = MACPCI("ab:cd:ef:12:34:56","0000:00:0f.0","side-12-eth1")
cur_state = [eth0]
ts = rename_logic([],
deepcopy(cur_state),
[],
[])
self.assertTrue(len(ts) == 1)
apply_transactions(cur_state, ts)
self.assertTrue(eth0.tname == "eth0")
def test_newhw_norules_2eth(self):
"""
Two previously unrecognised nics, with no other rules. Expecting
them to be renamed to eth0 and 1 respectivly
"""
eth0 = MACPCI("ab:cd:ef:12:34:56","0000:00:0f.0","side-12-eth1")
eth1 = MACPCI("aa:cd:ef:12:34:56","0000:00:01.0","side-33-eth0")
cur_state = [eth0, eth1]
ts = rename_logic([],
deepcopy(cur_state),
[],
[])
self.assertTrue(len(ts) == 2)
apply_transactions(cur_state, ts)
self.assertTrue(eth0.tname == "eth0")
self.assertTrue(eth1.tname == "eth1")
def test_newhw_1srule_1eth(self):
"""
One previously unrecognised nic with a static rule refering to it.
Expecting it to be named to eth0 as per the static rule.
"""
eth0 = MACPCI("ab:cd:ef:12:34:56","0000:00:0f.0", "side-12-eth1")
srule_eth0 = MACPCI("ab:cd:ef:12:34:56","0000:00:0f.0", None, "eth0")
cur_state = [eth0]
ts = rename_logic([srule_eth0],
deepcopy(cur_state),
[],
[])
self.assertTrue(len(ts) == 1)
apply_transactions(cur_state, ts)
self.assertTrue(eth0.tname == "eth0")
def test_newhw_2srule_1eth(self):
"""
One previously recognised nic with two static rules. Expecting
the nic to be renamed from eth1 to eth0 to fall in line with the static
rule
"""
cur_eth0 = MACPCI("12:34:56:78:90:12","0000:00:01.0", "eth0")
srule_eth0 = MACPCI("ab:cd:ef:12:34:56","0000:00:0f.0", None, "eth0")
srule_eth1 = MACPCI("12:34:56:78:90:12","0000:00:01.0", None, "eth1")
cur_state = [cur_eth0]
ts = rename_logic([srule_eth0, srule_eth1],
deepcopy(cur_state),
[],
[])
self.assertTrue(len(ts) == 1)
apply_transactions(cur_state, ts)
self.assertTrue(cur_eth0.tname == "eth1")
def test_newhw_2srule_2eth(self):
"""
Two nics, one recognised and one not, with two static rules.
Expecting 2 or 3 transactions (depending on the logic) resulting
in the currently named eth0 changing name to eth1, and the currently
sideways nic to be named eth0
"""
cur_eth0 = MACPCI("12:34:56:78:90:12","0000:00:01.0", "eth0")
cur_eth1 = MACPCI("ab:cd:ef:12:34:56","0000:00:0f.0", "side-12-eth1")
srule_eth0 = MACPCI("ab:cd:ef:12:34:56","0000:00:0f.0", None, "eth0")
srule_eth1 = MACPCI("12:34:56:78:90:12","0000:00:01.0", None, "eth1")
cur_state = [cur_eth0, cur_eth1]
ts = rename_logic([srule_eth0, srule_eth1],
deepcopy(cur_state),
[],
[])
self.assertTrue(len(ts) == 3 or len(ts) == 2)
apply_transactions(cur_state, ts)
self.assertTrue(cur_eth0.tname == "eth1")
self.assertTrue(cur_eth1.tname == "eth0")
def test_nosrules_1eth_incorrect_udev(self):
"""
One currently unrecognised nic which has an entry in last state
information. It should be renamed as per the last state entry to eth3
"""
cur_eth0 = MACPCI("ab:cd:ef:12:34:56","0000:00:0f.0", "side-12-eth0")
last_eth3 = MACPCI("ab:cd:ef:12:34:56","0000:00:0f.0", None, "eth3")
cur_state = [cur_eth0]
ts = rename_logic([],
deepcopy(cur_state),
[last_eth3],
[])
# Expecting 1 transactions
self.assertTrue(len(ts) == 1)
apply_transactions(cur_state, ts)
self.assertTrue(cur_eth0.tname == "eth3")
def test_nosrules_1eth_correct_udev(self):
"""
One recognised nic which also has a last state entry. This is the
default condition for rebooting the server without changing hardware,
and 0 transactions should take place.
"""
cur_eth1 = MACPCI("ab:cd:ef:12:34:56","0000:00:0f.0", "eth1")
last_eth1 = MACPCI("ab:cd:ef:12:34:56","0000:00:0f.0", None, "eth1")
cur_state = [cur_eth1]
ts = rename_logic([],
deepcopy(cur_state),
[last_eth1],
[])
# Expecting 0 transactions
self.assertTrue(len(ts) == 0)
def test_1srule_1eth_1last_correct_udev(self):
"""
One recognised nic, in line with its last state entry, but a (new)
static rule which has a different idea for it. Expecting it to be
renamed to eth0 as per the static rule
"""
cur_eth1 = MACPCI("ab:cd:ef:12:34:56","0000:00:0f.0", "eth1")
srule_eth0 = MACPCI("ab:cd:ef:12:34:56","0000:00:0f.0", None, "eth0")
last_eth1 = MACPCI("ab:cd:ef:12:34:56","0000:00:0f.0", None, "eth1")
cur_state = [cur_eth1]
ts = rename_logic([srule_eth0],
deepcopy(cur_state),
[last_eth1],
[])
# Expecting 1 transactions
self.assertTrue(len(ts) == 1)
apply_transactions(cur_state, ts)
self.assertTrue(cur_eth1.tname == "eth0")
def test_1srule_1eth_already_complete(self):
"""
One recognised nic, and a static rule for that nic. Expecting no
transactions at all.
"""
cur_eth0 = MACPCI("00:13:72:2d:2a:ec", "0000:04:00.0", "eth0")
srule_eth0 = MACPCI("00:13:72:2d:2a:ec", "0000:04:00.0", None, "eth0")
cur_state = [cur_eth0]
ts = rename_logic([srule_eth0],
deepcopy(cur_state),
[],
[])
self.assertTrue(len(ts) == 0)
def test_1drule_1eth_already_complete(self):
"""
One recognised nic, and a dynamic rule for that nic. Expecting no
transactions at all.
"""
cur_eth0 = MACPCI("00:13:72:2d:2a:ec", "0000:04:00.0", "eth0")
drule_eth0 = MACPCI("00:13:72:2d:2a:ec", "0000:04:00.0", None, "eth0")
cur_state = [cur_eth0]
ts = rename_logic([],
deepcopy(cur_state),
[drule_eth0],
[])
self.assertTrue(len(ts) == 0)
class TestUseCases(unittest.TestCase):
def setUp(self):
self.siobuff = StringIO.StringIO()
openLog(self.siobuff, logging.NOTSET)
def tearDown(self):
closeLogs()
self.siobuff.close()
def debug_state(self, ts):
print("", file=sys.stderr)
print(self.siobuff.getvalue(), file=sys.stderr)
print("", file=sys.stderr)
if len(ts):
print("Transactions:", file=sys.stderr)
for (s,d) in ts:
print("'%s' -> '%s'" % (s, d), file=sys.stderr)
else:
print("No transactions", file=sys.stderr)
print("", file=sys.stderr)
def test_usecase1(self):
"""
No changes from last boot. No transactions and all nics
retain their same name
"""
cur_eth0 = MACPCI("01:23:45:67:89:01", "0000:01:00.0", "eth0")
cur_eth1 = MACPCI("11:23:45:67:89:01", "0000:02:00.0", "eth1")
cur_eth2 = MACPCI("21:23:45:67:89:01", "0000:03:00.0", "eth2")
cur_eth3 = MACPCI("31:23:45:67:89:01", "0000:04:00.0", "eth3")
cur_eth4 = MACPCI("41:23:45:67:89:01", "0000:05:00.0", "eth4")
cur_state = [cur_eth0, cur_eth1, cur_eth2, cur_eth3, cur_eth4]
last_eth0 = MACPCI("01:23:45:67:89:01", "0000:01:00.0", None, "eth0")
last_eth1 = MACPCI("11:23:45:67:89:01", "0000:02:00.0", None, "eth1")
last_eth2 = MACPCI("21:23:45:67:89:01", "0000:03:00.0", None, "eth2")
last_eth3 = MACPCI("31:23:45:67:89:01", "0000:04:00.0", None, "eth3")
last_eth4 = MACPCI("41:23:45:67:89:01", "0000:05:00.0", None, "eth4")
last_state = [last_eth0, last_eth1, last_eth2, last_eth3, last_eth4]
ts = rename([], cur_state, last_state, [])
self.assertTrue(len(ts) == 0)
for eth in cur_state:
self.assertTrue(eth.kname == eth.tname)
def test_usecase5(self):
"""
Brand new hardware. (Use case based upon plugging the hard drive from
a broken server into a new identical one).
"""
cur_eth0 = MACPCI("02:23:45:67:89:01", "0000:01:00.0", "side-1-eth0")
cur_eth1 = MACPCI("12:23:45:67:89:01", "0000:02:00.0", "side-34-eth1")
cur_eth2 = MACPCI("22:23:45:67:89:01", "0000:03:00.0", "side-71-eth2")
cur_eth3 = MACPCI("32:23:45:67:89:01", "0000:04:00.0", "side-3012-eth3")
cur_eth4 = MACPCI("42:23:45:67:89:01", "0000:05:00.0", "side-4332-eth4")
cur_state = [cur_eth0, cur_eth1, cur_eth2, cur_eth3, cur_eth4]
last_eth0 = MACPCI("01:23:45:67:89:01", "0000:01:00.0", None, "eth0")
last_eth1 = MACPCI("11:23:45:67:89:01", "0000:02:00.0", None, "eth1")
last_eth2 = MACPCI("21:23:45:67:89:01", "0000:03:00.0", None, "eth2")
last_eth3 = MACPCI("31:23:45:67:89:01", "0000:04:00.0", None, "eth3")
last_eth4 = MACPCI("41:23:45:67:89:01", "0000:05:00.0", None, "eth4")
last_state = [last_eth0, last_eth1, last_eth2, last_eth3, last_eth4]
ts = rename([], deepcopy(cur_state), last_state, [])
self.assertTrue(len(ts) == 5)
apply_transactions(cur_state, ts)
for cur, last in zip(cur_state, last_state):
self.assertTrue(cur.tname == last.tname)
def test_CA_94279(self):
"""
CA-94279 occured because the logic did no honour the order paramater
from biosdevname if it was the only deciding factor. (It appears rare
for the embedded network cards to have a greater PCI sbdf than the non-
embedded cards)
Check that in the order is honoured in the case where it is the only
deciding factor.
"""
cur_eth0 = MACPCI("00:1b:21:aa:ef:f0", "0000:03:00.0", "side-1-eth0", order=2)
cur_eth1 = MACPCI("00:1b:21:aa:ef:f1", "0000:03:00.1", "side-2-eth1", order=3)
cur_eth2 = MACPCI("00:1b:21:aa:ef:f4", "0000:04:00.0", "side-3-eth2", order=4)
cur_eth3 = MACPCI("00:1b:21:aa:ef:f5", "0000:04:00.1", "side-4-eth3", order=5)
cur_eth4 = MACPCI("60:eb:69:ed:9a:16", "0000:06:00.0", "side-5-eth4", order=0)
cur_eth5 = MACPCI("60:eb:69:ed:9a:17", "0000:06:00.1", "side-6-eth5", order=1)
cur_state = [ cur_eth0, cur_eth1, cur_eth2, cur_eth3, cur_eth4, cur_eth5 ]
ts = rename([], deepcopy(cur_state), [], [])
apply_transactions(cur_state, ts)
expected_names = [ "eth2", "eth3", "eth4", "eth5", "eth0", "eth1" ]
for cur, exp in zip(cur_state, expected_names):
self.assertEqual(cur.tname, exp)
def test_rshp_new_hardware(self):
"""
Brand new hardware - including duplicate PCI ID. (Use case based upon
plugging the hard drive from a broken server into a new identical one,
for the Rackspace HP blades with Mellanox cards with two eths per
function).
"""
cur_eth0 = MACPCI("02:23:45:67:89:01", "0000:01:00.0", "side-1-eth0")
cur_eth1 = MACPCI("12:23:45:67:89:01", "0000:02:00.0", "side-34-eth1")
cur_eth2 = MACPCI("22:23:45:67:89:01", "0000:03:00.0", "side-71-eth2")
# Devices with the same PCI ID are assumed to differ by 1 in the MAC
cur_eth3 = MACPCI("32:23:45:67:89:01", "0000:04:00.0", "side-3012-eth3")
cur_eth4 = MACPCI("32:23:45:67:89:02", "0000:04:00.0", "side-4332-eth4")
cur_state = [cur_eth0, cur_eth1, cur_eth2, cur_eth3, cur_eth4]
last_eth0 = MACPCI("01:23:45:67:89:01", "0000:01:00.0", None, "eth0")
last_eth1 = MACPCI("11:23:45:67:89:01", "0000:02:00.0", None, "eth1")
last_eth2 = MACPCI("21:23:45:67:89:01", "0000:03:00.0", None, "eth2")
# Devices with the same PCI ID are assumed to differ by 1 in the MAC
# Make eth3 have a MAC higher than eth4 to ensure that we order the new NICs correctly
last_eth3 = MACPCI("31:23:45:67:89:02", "0000:04:00.0", None, "eth3")
last_eth4 = MACPCI("31:23:45:67:89:01", "0000:04:00.0", None, "eth4")
last_state = [last_eth0, last_eth1, last_eth2, last_eth4, last_eth3]
# Reverse order of input to ensure logic copes correctly
ts = rename([], deepcopy(cur_state[::-1]), last_state, [])
self.assertTrue(len(ts) == 5)
apply_transactions(cur_state, ts)
for cur, last in zip(cur_state, last_state):
self.assertEqual(cur.tname, last.tname)
def test_bad_biosdevname_order(self):
"""
Brand new hardware - including multi-nic functions and incorrect biosdevname ordering
"""
# Devices with the same PCI ID are assumed to differ by 1 in the MAC
# Interlace two multi-nic functions, and have single-nic functions scattered around
cur_state = []
cur_state.append(MACPCI("00:00:00:00:00:01", "0000:01:00.0", order=0, kname="side-0-eth0"))
cur_state.append(MACPCI("00:00:44:00:01:01", "0000:02:00.0", order=2, kname="side-0-eth2"))
cur_state.append(MACPCI("00:00:44:00:01:02", "0000:02:00.0", order=6, kname="side-0-eth6"))
cur_state.append(MACPCI("00:00:44:00:01:03", "0000:02:00.0", order=1, kname="side-0-eth1"))
cur_state.append(MACPCI("00:00:00:00:02:01", "0000:03:00.0", order=4, kname="side-0-eth4"))
# Second set of multi-nic functions has a lower MAC address than the first to make sure
# orders aren't mixed between multi-nic functions
cur_state.append(MACPCI("00:00:22:00:03:01", "0000:04:00.0", order=7, kname="side-0-eth5"))
cur_state.append(MACPCI("00:00:22:00:03:02", "0000:04:00.0", order=3, kname="side-0-eth3"))
cur_state.append(MACPCI("00:00:22:00:03:03", "0000:04:00.0", order=5, kname="side-0-eth7"))
cur_state.append(MACPCI("00:00:00:00:04:01", "0000:05:00.0", order=8, kname="side-0-eth8"))
expected_order = []
expected_order.append(("eth0", "00:00:00:00:00:01")) # non-MNF; biosdevname order
expected_order.append(("eth1", "00:00:44:00:01:01")) # MNF 1, ordered by MAC
expected_order.append(("eth2", "00:00:44:00:01:02")) # MNF 1, ordered by MAC
expected_order.append(("eth3", "00:00:22:00:03:01")) # MNF 2, ordered by MAC
expected_order.append(("eth4", "00:00:00:00:02:01")) # non-MNF; biosdevname order
expected_order.append(("eth5", "00:00:22:00:03:02")) # MNF 2, ordered by MAC
expected_order.append(("eth6", "00:00:44:00:01:03")) # MNF 1, ordered by MAC
expected_order.append(("eth7", "00:00:22:00:03:03")) # MNF 2, ordered by MAC
expected_order.append(("eth8", "00:00:00:00:04:01")) # non-MNF; biosdevname order
# Reverse order of input to ensure logic copes correctly
ts = rename([], deepcopy(cur_state[::-1]), [], [])
self.assertEqual(len(ts), 9)
apply_transactions(cur_state, ts)
cur_state.sort(key=lambda x: x.tname)
for cur, expected in zip(cur_state, expected_order):
self.assertEqual(cur.tname, expected[0])
self.assertEqual(cur.mac, expected[1])
def test_ibft_new_hardware(self):
"""
Brand new hardware. (Use case based upon plugging the hard drive from
a broken server into a new identical one).
"""
cur_eth0 = MACPCI("02:23:45:67:89:01", "0000:01:00.0", "side-1-eth0")
cur_eth1 = MACPCI("12:23:45:67:89:01", "0000:02:00.0", "side-34-eth1")
cur_eth2 = MACPCI("22:23:45:67:89:01", "0000:03:00.0", "ibft0")
cur_eth3 = MACPCI("32:23:45:67:89:01", "0000:04:00.0", "side-3012-eth3")
cur_eth4 = MACPCI("42:23:45:67:89:01", "0000:05:00.0", "side-4332-eth4")
cur_state = [cur_eth0, cur_eth1, cur_eth2, cur_eth3, cur_eth4]
ts = rename([], deepcopy(cur_state), [], [])
self.assertEqual(len(ts), 4)
apply_transactions(cur_state, ts)
self.assertEqual(cur_eth0.tname, "eth0")
self.assertEqual(cur_eth1.tname, "eth1")
self.assertEqual(cur_eth3.tname, "eth2")
self.assertEqual(cur_eth4.tname, "eth3")
def test_ibft_no_change(self):
"""
No changes from last boot. No transactions and all nics
retain their same name
"""
cur_eth0 = MACPCI("01:23:45:67:89:01", "0000:01:00.0", "eth0")
cur_eth1 = MACPCI("11:23:45:67:89:01", "0000:02:00.0", "eth1")
cur_eth2 = MACPCI("21:23:45:67:89:01", "0000:03:00.0", "ibft0")
cur_eth3 = MACPCI("31:23:45:67:89:01", "0000:04:00.0", "eth2")
cur_eth4 = MACPCI("41:23:45:67:89:01", "0000:05:00.0", "eth3")
cur_state = [cur_eth0, cur_eth1, cur_eth2, cur_eth3, cur_eth4]
last_eth0 = MACPCI("01:23:45:67:89:01", "0000:01:00.0", None, "eth0")
last_eth1 = MACPCI("11:23:45:67:89:01", "0000:02:00.0", None, "eth1")
last_eth3 = MACPCI("31:23:45:67:89:01", "0000:04:00.0", None, "eth2")
last_eth4 = MACPCI("41:23:45:67:89:01", "0000:05:00.0", None, "eth3")
last_state = [last_eth0, last_eth1, last_eth3, last_eth4]
ts = rename([], cur_state, last_state, [])
self.assertEqual(len(ts), 0)
for eth in cur_state:
# Skip the iBFT NIC since it doesn't get a tname and is completely
# ignored.
if eth.kname != "ibft0":
self.assertEqual(eth.kname, eth.tname)
def test_ibft_nic_to_ibft(self):
"""
Convert a NIC to iBFT. Since iBFT NICs are ignored, no transactions
are expected.
"""
cur_eth0 = MACPCI("01:23:45:67:89:01", "0000:01:00.0", "eth0")
cur_eth1 = MACPCI("11:23:45:67:89:01", "0000:02:00.0", "eth1")
cur_eth2 = MACPCI("21:23:45:67:89:01", "0000:03:00.0", "ibft0")
cur_eth3 = MACPCI("31:23:45:67:89:01", "0000:04:00.0", "eth3")
cur_eth4 = MACPCI("41:23:45:67:89:01", "0000:05:00.0", "eth4")
cur_state = [cur_eth0, cur_eth1, cur_eth2, cur_eth3, cur_eth4]
last_eth0 = MACPCI("01:23:45:67:89:01", "0000:01:00.0", None, "eth0")
last_eth1 = MACPCI("11:23:45:67:89:01", "0000:02:00.0", None, "eth1")
last_eth2 = MACPCI("21:23:45:67:89:01", "0000:03:00.0", None, "eth2")
last_eth3 = MACPCI("31:23:45:67:89:01", "0000:04:00.0", None, "eth3")
last_eth4 = MACPCI("41:23:45:67:89:01", "0000:05:00.0", None, "eth4")
last_state = [last_eth0, last_eth1, last_eth2, last_eth3, last_eth4]
ts = rename([], cur_state, last_state, [])
self.assertEqual(len(ts), 0)
for eth in cur_state:
# Skip the iBFT NIC since it doesn't get a tname and is completely
# ignored.
if eth.kname != "ibft0":
self.assertEqual(eth.kname, eth.tname)
class TestInputSanitisation(unittest.TestCase):
def setUp(self):
"""
Set up a lot of MACPCI objects.
This reflection magic creates many self.cXXX objects where XXX
represents the indicies of mac, pci and eth names.
e.g. self.c123 means the 1st mac, 2nd pci and 3rd eth
self.c221 means the 2nd mac, 2nd pci and 1st eth
In addition, set up equivelent self.sXXX objects which have a kname
set to None and a tname set to the 'eth'
"""
self.siobuff = StringIO.StringIO()
openLog(self.siobuff)
macs = ["ab:cd:ef:01:23:45", "02:46:8A:CE:15:79", "ab:ba:ab:ba:ab:ba"]
pcis = ["0000:00:00.1", "0000:00:00.2", "0001:00:00.2"]
eths = ["eth1", "eth2", "eth3"]
for (mn, m) in enumerate(macs):
for (pn, p) in enumerate(pcis):
for (en, e) in enumerate(eths):
setattr(self, "c%d%d%d" % (mn+1, pn+1, en+1),
MACPCI(m, p, e, None))
setattr(self, "s%d%d%d" % (mn+1, pn+1, en+1),
MACPCI(m, p, None, e))
def tearDown(self):
closeLogs()
self.siobuff.close()
def assertNotRaises(self, excp, fn, *argl, **kwargs):
"""Because unittest.TestCase seems to be missing this functionality"""
try:
fn(*argl, **kwargs)
except excp as e:
self.fail("function raised %s unexpectedly: %s"
% (excp, e))
def test_srule_eth_unaliased(self):
self.assertNotRaises(StaticRuleError,
rename,
[self.s111],
[],
[],
[])
def test_srule_eth_alias(self):
"""
Ensure that no static rules attempt to alias the same eth name
"""
srule_inputs = [ [self.s111, self.s221],
[self.s111, self.s221, self.s331],
[self.s111, self.s222, self.s331],
[self.s112, self.s222, self.s331] ]
for i in srule_inputs:
self.assertRaises(StaticRuleError, rename,
i, [], [], [])
def test_srule_mac_alias(self):
"""
Ensure that no static rules attempt to alias the same mac address
"""
srule_inputs = [ [self.s111, self.s122],
[self.s111, self.s122, self.s133],
[self.s111, self.s222, self.s133],
[self.s211, self.s222, self.s133] ]
for i in srule_inputs:
self.assertRaises(StaticRuleError, rename,
i, [], [], [])
def test_curstate_eth_alias(self):
"""
Ensure that no current state entries attempt to alias the same eth name
"""
curstate_inputs = [ [self.c111, self.c221],
[self.c111, self.c221, self.c331],
[self.c111, self.c222, self.c331],
[self.c112, self.c222, self.c331] ]
for i in curstate_inputs:
self.assertRaises(CurrentStateError, rename,
[], i, [], [])
def test_curstate_mac_alias(self):
"""
Ensure that no current state attempt to alias the same mac address
"""
curstate_inputs = [ [self.c111, self.c122],
[self.c111, self.c122, self.c133],
[self.c111, self.c222, self.c133],
[self.c211, self.c222, self.c133] ]
for i in curstate_inputs:
self.assertRaises(CurrentStateError, rename,
[], i, [], [])
def test_laststate_input(self):
"""
Ensure that last state only accepts rules in the form
(xx:xx.x,xx:xx:xx:xx:xx:xx)->eth##
"""
self.assertRaises(LastStateError, rename,
[], [], [self.c123], [])
self.assertNotRaises(LastStateError, rename,
[], [], [self.s123], [])
def test_oldstate_input(self):
"""
Ensure that old state only accepts rules in the form
(xx:xx.x,xx:xx:xx:xx:xx:xx)->eth##
"""
self.assertRaises(OldStateError, rename,
[], [], [], [self.c123])
self.assertNotRaises(OldStateError, rename,
[], [], [], [self.s123])