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
|
# -*- coding: utf-8 -*-
# Copyright 2014 Red Hat, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import json
import six
from os_net_config import objects
from os_net_config.tests import base
from os_net_config import utils
class TestRoute(base.TestCase):
def test_from_json(self):
data = '{"next_hop": "172.19.0.1", "ip_netmask": "172.19.0.0/24"}'
route = objects.Route.from_json(json.loads(data))
self.assertEqual("172.19.0.1", route.next_hop)
self.assertEqual("172.19.0.0/24", route.ip_netmask)
self.assertFalse(route.default)
def test_from_json_default_route(self):
data = '{"next_hop": "172.19.0.1", "ip_netmask": "172.19.0.0/24", ' \
'"default": true}'
route = objects.Route.from_json(json.loads(data))
self.assertEqual("172.19.0.1", route.next_hop)
self.assertEqual("172.19.0.0/24", route.ip_netmask)
self.assertTrue(route.default)
data = '{"next_hop": "172.19.0.1", "ip_netmask": "172.19.0.0/24", ' \
'"default": "true"}'
route = objects.Route.from_json(json.loads(data))
self.assertEqual("172.19.0.1", route.next_hop)
self.assertEqual("172.19.0.0/24", route.ip_netmask)
self.assertTrue(route.default)
class TestAddress(base.TestCase):
def test_ipv4_address(self):
address = objects.Address('192.168.1.1/24')
self.assertEqual("192.168.1.1", address.ip)
self.assertEqual("255.255.255.0", address.netmask)
self.assertEqual(4, address.version)
def test_ipv6_address(self):
address = objects.Address('2001:abc:a::/64')
self.assertEqual("2001:abc:a::", address.ip)
self.assertEqual("ffff:ffff:ffff:ffff::", address.netmask)
self.assertEqual(6, address.version)
def test_from_json(self):
data = '{"ip_netmask": "192.0.2.5/24"}'
address = objects.Address.from_json(json.loads(data))
self.assertEqual("192.0.2.5", address.ip)
self.assertEqual("255.255.255.0", address.netmask)
self.assertEqual(4, address.version)
def test_from_json_invalid(self):
self.assertRaises(objects.InvalidConfigException,
objects.Address.from_json,
{})
data = '{"ip_netmask": false}'
json_data = json.loads(data)
self.assertRaises(objects.InvalidConfigException,
objects.Address.from_json,
json_data)
class TestInterface(base.TestCase):
def test_interface_addresses(self):
v4_addr = objects.Address('192.168.1.1/24')
v6_addr = objects.Address('2001:abc:a::/64')
interface = objects.Interface('foo', addresses=[v4_addr, v6_addr])
self.assertEqual("192.168.1.1", interface.v4_addresses()[0].ip)
self.assertEqual("2001:abc:a::", interface.v6_addresses()[0].ip)
def test_from_json_dhcp(self):
data = '{"type": "interface", "name": "em1", "use_dhcp": true}'
interface = objects.object_from_json(json.loads(data))
self.assertEqual("em1", interface.name)
self.assertTrue(interface.use_dhcp)
def test_from_json_defroute(self):
data = '{"type": "interface", "name": "em1", "use_dhcp": true}'
interface1 = objects.object_from_json(json.loads(data))
data = """{
"type": "interface",
"name": "em1",
"use_dhcp": true,
"defroute": false
}
"""
interface2 = objects.object_from_json(json.loads(data))
self.assertTrue(interface1.defroute)
self.assertFalse(interface2.defroute)
def test_from_json_dhclient_args(self):
data = """{
"type": "interface",
"name": "em1",
"use_dhcp": true,
"dhclient_args": "--foobar"
}
"""
interface1 = objects.object_from_json(json.loads(data))
self.assertEqual("--foobar", interface1.dhclient_args)
def test_from_json_dns_servers(self):
data = """{
"type": "interface",
"name": "em1",
"use_dhcp": true,
"dns_servers": ["1.2.3.4"]
}
"""
interface1 = objects.object_from_json(json.loads(data))
self.assertEqual(["1.2.3.4"], interface1.dns_servers)
def test_from_json_dhcp_nic1(self):
def dummy_numbered_nics(nic_mapping=None):
return {"nic1": "em3"}
self.stubs.Set(objects, '_numbered_nics', dummy_numbered_nics)
data = '{"type": "interface", "name": "nic1", "use_dhcp": true}'
interface = objects.object_from_json(json.loads(data))
self.assertEqual("em3", interface.name)
self.assertTrue(interface.use_dhcp)
def test_from_json_with_addresses(self):
data = """{
"type": "interface",
"name": "em1",
"use_dhcp": false,
"mtu": 1501,
"addresses": [{
"ip_netmask": "192.0.2.1/24"
}],
"routes": [{
"next_hop": "192.0.2.1",
"ip_netmask": "192.0.2.1/24"
}]
}
"""
interface = objects.object_from_json(json.loads(data))
self.assertEqual("em1", interface.name)
self.assertFalse(interface.use_dhcp)
self.assertFalse(interface.use_dhcpv6)
self.assertEqual(1501, interface.mtu)
address1 = interface.v4_addresses()[0]
self.assertEqual("192.0.2.1", address1.ip)
self.assertEqual("255.255.255.0", address1.netmask)
route1 = interface.routes[0]
self.assertEqual("192.0.2.1", route1.next_hop)
self.assertEqual("192.0.2.1/24", route1.ip_netmask)
class TestVlan(base.TestCase):
def test_from_json_dhcp(self):
data = '{"type": "vlan", "device": "em1", "vlan_id": 16,' \
'"use_dhcp": true}'
vlan = objects.object_from_json(json.loads(data))
self.assertEqual("em1", vlan.device)
self.assertEqual(16, vlan.vlan_id)
self.assertTrue(vlan.use_dhcp)
def test_from_json_dhcp_nic1(self):
def dummy_numbered_nics(nic_mapping=None):
return {"nic1": "em4"}
self.stubs.Set(objects, '_numbered_nics', dummy_numbered_nics)
data = '{"type": "vlan", "device": "nic1", "vlan_id": 16,' \
'"use_dhcp": true}'
vlan = objects.object_from_json(json.loads(data))
self.assertEqual("em4", vlan.device)
self.assertEqual(16, vlan.vlan_id)
self.assertTrue(vlan.use_dhcp)
class TestBridge(base.TestCase):
def test_from_json_dhcp(self):
data = """{
"type": "ovs_bridge",
"name": "br-foo",
"use_dhcp": true,
"members": [{
"type": "interface",
"name": "em1"
}]
}
"""
bridge = objects.object_from_json(json.loads(data))
self.assertEqual("br-foo", bridge.name)
self.assertTrue(bridge.use_dhcp)
interface1 = bridge.members[0]
self.assertEqual("em1", interface1.name)
self.assertTrue(interface1.ovs_port)
self.assertEqual("br-foo", interface1.bridge_name)
def test_from_json_dhcp_with_nic1(self):
def dummy_numbered_nics(nic_mapping=None):
return {"nic1": "em5"}
self.stubs.Set(objects, '_numbered_nics', dummy_numbered_nics)
data = """{
"type": "ovs_bridge",
"name": "br-foo",
"use_dhcp": true,
"members": [{
"type": "interface",
"name": "nic1"
}]
}
"""
bridge = objects.object_from_json(json.loads(data))
self.assertEqual("br-foo", bridge.name)
self.assertTrue(bridge.use_dhcp)
interface1 = bridge.members[0]
self.assertEqual("em5", interface1.name)
self.assertTrue(interface1.ovs_port)
self.assertEqual("br-foo", interface1.bridge_name)
def test_from_json_primary_interface(self):
data = """{
"type": "ovs_bridge",
"name": "br-foo",
"use_dhcp": true,
"members": [
{
"type": "interface",
"name": "em1",
"primary": "true"
},
{
"type": "interface",
"name": "em2"
}]
}
"""
bridge = objects.object_from_json(json.loads(data))
self.assertEqual("br-foo", bridge.name)
self.assertTrue(bridge.use_dhcp)
self.assertEqual("em1", bridge.primary_interface_name)
interface1 = bridge.members[0]
self.assertEqual("em1", interface1.name)
self.assertTrue(interface1.ovs_port)
self.assertTrue(interface1.primary)
self.assertEqual("br-foo", interface1.bridge_name)
interface2 = bridge.members[1]
self.assertEqual("em2", interface2.name)
self.assertTrue(interface2.ovs_port)
self.assertEqual("br-foo", interface2.bridge_name)
class TestLinuxBridge(base.TestCase):
def test_from_json_dhcp(self):
data = """{
"type": "linux_bridge",
"name": "br-foo",
"use_dhcp": true,
"members": [{
"type": "interface",
"name": "em1"
}]
}
"""
bridge = objects.object_from_json(json.loads(data))
self.assertEqual("br-foo", bridge.name)
self.assertTrue(bridge.use_dhcp)
interface1 = bridge.members[0]
self.assertEqual("em1", interface1.name)
self.assertFalse(interface1.ovs_port)
self.assertEqual("br-foo", interface1.linux_bridge_name)
def test_from_json_dhcp_with_nic1(self):
def dummy_numbered_nics(nic_mapping=None):
return {"nic1": "em5"}
self.stubs.Set(objects, '_numbered_nics', dummy_numbered_nics)
data = """{
"type": "linux_bridge",
"name": "br-foo",
"use_dhcp": true,
"members": [{
"type": "interface",
"name": "nic1"
}]
}
"""
bridge = objects.object_from_json(json.loads(data))
self.assertEqual("br-foo", bridge.name)
self.assertTrue(bridge.use_dhcp)
interface1 = bridge.members[0]
self.assertEqual("em5", interface1.name)
self.assertFalse(interface1.ovs_port)
self.assertEqual("br-foo", interface1.linux_bridge_name)
def test_from_json_primary_interface(self):
data = """{
"type": "linux_bridge",
"name": "br-foo",
"use_dhcp": true,
"members": [
{
"type": "interface",
"name": "em1",
"primary": "true"
},
{
"type": "interface",
"name": "em2"
}]
}
"""
bridge = objects.object_from_json(json.loads(data))
self.assertEqual("br-foo", bridge.name)
self.assertTrue(bridge.use_dhcp)
self.assertEqual("em1", bridge.primary_interface_name)
interface1 = bridge.members[0]
self.assertEqual("em1", interface1.name)
self.assertFalse(interface1.ovs_port)
self.assertTrue(interface1.primary)
self.assertEqual("br-foo", interface1.linux_bridge_name)
interface2 = bridge.members[1]
self.assertEqual("em2", interface2.name)
self.assertFalse(interface2.ovs_port)
self.assertEqual("br-foo", interface2.linux_bridge_name)
class TestIvsBridge(base.TestCase):
def test_interface_from_json(self):
data = """{
"type": "ivs_bridge",
"members": [{
"type": "interface",
"name": "nic2"
}]
}
"""
bridge = objects.object_from_json(json.loads(data))
self.assertEqual("ivs", bridge.name)
interface1 = bridge.members[0]
self.assertEqual("nic2", interface1.name)
self.assertEqual(False, interface1.ovs_port)
self.assertEqual("ivs", interface1.ivs_bridge_name)
def test_ivs_interface_from_json(self):
data = """{
"type": "ivs_bridge",
"members": [{
"type": "ivs_interface",
"name": "storage",
"vlan_id": 202
}]
}
"""
bridge = objects.object_from_json(json.loads(data))
self.assertEqual("ivs", bridge.name)
interface1 = bridge.members[0]
self.assertEqual("storage202", interface1.name)
self.assertEqual(False, interface1.ovs_port)
self.assertEqual("ivs", interface1.ivs_bridge_name)
def test_bond_interface_from_json(self):
data = """{
"type": "ivs_bridge",
"members": [{
"type": "linux_bond",
"name": "bond1",
"members": [
{"type": "interface", "name": "nic2"},
{"type": "interface", "name": "nic3"}
]
}]
}
"""
err = self.assertRaises(objects.InvalidConfigException,
objects.IvsBridge.from_json,
json.loads(data))
expected = 'IVS does not support bond interfaces.'
self.assertIn(expected, err)
class TestBond(base.TestCase):
def test_from_json_dhcp(self):
data = """{
"type": "ovs_bond",
"name": "bond1",
"use_dhcp": true,
"members": [
{
"type": "interface",
"name": "em1"
},
{
"type": "interface",
"name": "em2"
}
]
}
"""
bridge = objects.object_from_json(json.loads(data))
self.assertEqual("bond1", bridge.name)
self.assertTrue(bridge.use_dhcp)
interface1 = bridge.members[0]
self.assertEqual("em1", interface1.name)
interface2 = bridge.members[1]
self.assertEqual("em2", interface2.name)
def test_from_json_dhcp_with_nic1_nic2(self):
def dummy_numbered_nics(nic_mapping=None):
return {"nic1": "em1", "nic2": "em2"}
self.stubs.Set(objects, '_numbered_nics', dummy_numbered_nics)
data = """{
"type": "ovs_bond",
"name": "bond1",
"use_dhcp": true,
"members": [
{
"type": "interface",
"name": "nic1"
},
{
"type": "interface",
"name": "nic2"
}
]
}
"""
bridge = objects.object_from_json(json.loads(data))
self.assertEqual("bond1", bridge.name)
self.assertTrue(bridge.use_dhcp)
interface1 = bridge.members[0]
self.assertEqual("em1", interface1.name)
interface2 = bridge.members[1]
self.assertEqual("em2", interface2.name)
class TestLinuxBond(base.TestCase):
def test_from_json_dhcp(self):
data = """{
"type": "linux_bond",
"name": "bond1",
"use_dhcp": true,
"members": [
{
"type": "interface",
"name": "em1"
},
{
"type": "interface",
"name": "em2"
}
]
}
"""
bridge = objects.object_from_json(json.loads(data))
self.assertEqual("bond1", bridge.name)
self.assertTrue(bridge.use_dhcp)
interface1 = bridge.members[0]
self.assertEqual("em1", interface1.name)
interface2 = bridge.members[1]
self.assertEqual("em2", interface2.name)
def test_from_json_dhcp_with_nic1_nic2(self):
def dummy_numbered_nics(nic_mapping=None):
return {"nic1": "em1", "nic2": "em2"}
self.stubs.Set(objects, '_numbered_nics', dummy_numbered_nics)
data = """{
"type": "ovs_bond",
"name": "bond1",
"use_dhcp": true,
"members": [
{
"type": "interface",
"name": "nic1"
},
{
"type": "interface",
"name": "nic2"
}
]
}
"""
bridge = objects.object_from_json(json.loads(data))
self.assertEqual("bond1", bridge.name)
self.assertTrue(bridge.use_dhcp)
interface1 = bridge.members[0]
self.assertEqual("em1", interface1.name)
interface2 = bridge.members[1]
self.assertEqual("em2", interface2.name)
class TestOvsTunnel(base.TestCase):
def test_from_json(self):
data = """{
"type": "ovs_bridge",
"name": "br-foo",
"members": [{
"type": "ovs_tunnel",
"name": "tun0",
"tunnel_type": "gre",
"ovs_options": [
"remote_ip=192.168.1.1"
],
"ovs_extra": [
"ovs extra"
]
}]
}
"""
bridge = objects.object_from_json(json.loads(data))
self.assertEqual("br-foo", bridge.name)
tun0 = bridge.members[0]
self.assertEqual("tun0", tun0.name)
self.assertFalse(tun0.ovs_port)
self.assertEqual("br-foo", tun0.bridge_name)
self.assertEqual("gre", tun0.tunnel_type)
self.assertEqual(
["options:remote_ip=192.168.1.1"],
tun0.ovs_options)
self.assertEqual(
["ovs extra"],
tun0.ovs_extra)
class TestOvsPatchPort(base.TestCase):
def test_from_json(self):
data = """{
"type": "ovs_patch_port",
"name": "br-pub-patch",
"bridge_name": "br-ex",
"peer": "br-ex-patch"
}
"""
patch_port = objects.object_from_json(json.loads(data))
self.assertEqual("br-pub-patch", patch_port.name)
self.assertEqual("br-ex", patch_port.bridge_name)
self.assertEqual("br-ex-patch", patch_port.peer)
class TestNumberedNicsMapping(base.TestCase):
# We want to test the function, not the dummy..
stub_numbered_nics = False
def tearDown(self):
super(TestNumberedNicsMapping, self).tearDown()
objects._NUMBERED_NICS = None
def _stub_active_nics(self, nics):
def dummy_ordered_active_nics():
return nics
self.stubs.Set(utils, 'ordered_active_nics', dummy_ordered_active_nics)
def test_numbered_nics_default(self):
self._stub_active_nics(['em1', 'em2'])
expected = {'nic1': 'em1', 'nic2': 'em2'}
self.assertEqual(expected, objects._numbered_nics())
def test_numbered_nics_mapped(self):
self._stub_active_nics(['em1', 'em2'])
mapping = {'nic1': 'em2', 'nic2': 'em1'}
expected = {'nic1': 'em2', 'nic2': 'em1'}
self.assertEqual(expected, objects._numbered_nics(nic_mapping=mapping))
def test_numbered_nics_mapped_partial(self):
self._stub_active_nics(['em1', 'em2', 'em3', 'em4'])
mapping = {'nic1': 'em2', 'nic2': 'em1'}
expected = {'nic1': 'em2', 'nic2': 'em1', 'nic3': 'em3', 'nic4': 'em4'}
self.assertEqual(expected, objects._numbered_nics(nic_mapping=mapping))
def test_numbered_nics_map_error_notactive(self):
self._stub_active_nics(['em1', 'em2'])
mapping = {'nic1': 'em3', 'nic2': 'em1'}
expected = {'nic2': 'em1'}
self.assertEqual(expected, objects._numbered_nics(nic_mapping=mapping))
def test_numbered_nics_map_error_duplicate(self):
self._stub_active_nics(['em1', 'em2'])
mapping = {'nic1': 'em1', 'nic2': 'em1'}
err = self.assertRaises(objects.InvalidConfigException,
objects._numbered_nics, nic_mapping=mapping)
expected = 'em1 already mapped, check mapping file for duplicates'
self.assertIn(expected, six.text_type(err))
def test_numbered_nics_map_mac(self):
def dummy_interface_mac(name):
mac_map = {'em1': '12:34:56:78:9a:bc',
'em2': '12:34:56:de:f0:12'}
return mac_map[name]
self.stubs.Set(utils, 'interface_mac', dummy_interface_mac)
self._stub_active_nics(['em1', 'em2'])
mapping = {'nic1': '12:34:56:de:f0:12', 'nic2': '12:34:56:78:9a:bc'}
expected = {'nic1': 'em2', 'nic2': 'em1'}
self.assertEqual(expected, objects._numbered_nics(nic_mapping=mapping))
def test_numbered_nics_no_active(self):
self._stub_active_nics([])
expected = {}
# This only emits a warning, so it should still work
self.assertEqual(expected, objects._numbered_nics())
|