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
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
|
# This file is part of cloud-init. See LICENSE file for license information.
from cloudinit import net
from cloudinit.net import _natural_sort_key
from cloudinit.net import cmdline
from cloudinit.net import eni
from cloudinit.net import netplan
from cloudinit.net import network_state
from cloudinit.net import renderers
from cloudinit.net import sysconfig
from cloudinit.sources.helpers import openstack
from cloudinit import util
from .helpers import CiTestCase
from .helpers import dir2dict
from .helpers import mock
from .helpers import populate_dir
import base64
import copy
import gzip
import io
import json
import os
import textwrap
import yaml
DHCP_CONTENT_1 = """
DEVICE='eth0'
PROTO='dhcp'
IPV4ADDR='192.168.122.89'
IPV4BROADCAST='192.168.122.255'
IPV4NETMASK='255.255.255.0'
IPV4GATEWAY='192.168.122.1'
IPV4DNS0='192.168.122.1'
IPV4DNS1='0.0.0.0'
HOSTNAME='foohost'
DNSDOMAIN=''
NISDOMAIN=''
ROOTSERVER='192.168.122.1'
ROOTPATH=''
filename=''
UPTIME='21'
DHCPLEASETIME='3600'
DOMAINSEARCH='foo.com'
"""
DHCP_EXPECTED_1 = {
'name': 'eth0',
'type': 'physical',
'subnets': [{'broadcast': '192.168.122.255',
'control': 'manual',
'gateway': '192.168.122.1',
'dns_search': ['foo.com'],
'type': 'dhcp',
'netmask': '255.255.255.0',
'dns_nameservers': ['192.168.122.1']}],
}
DHCP6_CONTENT_1 = """
DEVICE6=eno1
HOSTNAME=
DNSDOMAIN=
IPV6PROTO=dhcp6
IPV6ADDR=2001:67c:1562:8010:0:1::
IPV6NETMASK=64
IPV6DNS0=2001:67c:1562:8010::2:1
IPV6DOMAINSEARCH=
HOSTNAME=
DNSDOMAIN=
"""
DHCP6_EXPECTED_1 = {
'name': 'eno1',
'type': 'physical',
'subnets': [{'control': 'manual',
'dns_nameservers': ['2001:67c:1562:8010::2:1'],
'netmask': '64',
'type': 'dhcp6'}]}
STATIC_CONTENT_1 = """
DEVICE='eth1'
PROTO='static'
IPV4ADDR='10.0.0.2'
IPV4BROADCAST='10.0.0.255'
IPV4NETMASK='255.255.255.0'
IPV4GATEWAY='10.0.0.1'
IPV4DNS0='10.0.1.1'
IPV4DNS1='0.0.0.0'
HOSTNAME='foohost'
UPTIME='21'
DHCPLEASETIME='3600'
DOMAINSEARCH='foo.com'
"""
STATIC_EXPECTED_1 = {
'name': 'eth1',
'type': 'physical',
'subnets': [{'broadcast': '10.0.0.255', 'control': 'manual',
'gateway': '10.0.0.1',
'dns_search': ['foo.com'], 'type': 'static',
'netmask': '255.255.255.0',
'dns_nameservers': ['10.0.1.1'],
'address': '10.0.0.2'}],
}
# Examples (and expected outputs for various renderers).
OS_SAMPLES = [
{
'in_data': {
"services": [{"type": "dns", "address": "172.19.0.12"}],
"networks": [{
"network_id": "dacd568d-5be6-4786-91fe-750c374b78b4",
"type": "ipv4", "netmask": "255.255.252.0",
"link": "tap1a81968a-79",
"routes": [{
"netmask": "0.0.0.0",
"network": "0.0.0.0",
"gateway": "172.19.3.254",
}],
"ip_address": "172.19.1.34", "id": "network0"
}],
"links": [
{
"ethernet_mac_address": "fa:16:3e:ed:9a:59",
"mtu": None, "type": "bridge", "id":
"tap1a81968a-79",
"vif_id": "1a81968a-797a-400f-8a80-567f997eb93f"
},
],
},
'in_macs': {
'fa:16:3e:ed:9a:59': 'eth0',
},
'out_sysconfig': [
('etc/sysconfig/network-scripts/ifcfg-eth0',
"""
# Created by cloud-init on instance boot automatically, do not edit.
#
BOOTPROTO=none
DEFROUTE=yes
DEVICE=eth0
GATEWAY=172.19.3.254
HWADDR=fa:16:3e:ed:9a:59
IPADDR=172.19.1.34
NETMASK=255.255.252.0
NM_CONTROLLED=no
ONBOOT=yes
TYPE=Ethernet
USERCTL=no
""".lstrip()),
('etc/sysconfig/network-scripts/route-eth0',
"""
# Created by cloud-init on instance boot automatically, do not edit.
#
ADDRESS0=0.0.0.0
GATEWAY0=172.19.3.254
NETMASK0=0.0.0.0
""".lstrip()),
('etc/resolv.conf',
"""
; Created by cloud-init on instance boot automatically, do not edit.
;
nameserver 172.19.0.12
""".lstrip()),
('etc/udev/rules.d/70-persistent-net.rules',
"".join(['SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ',
'ATTR{address}=="fa:16:3e:ed:9a:59", NAME="eth0"\n']))]
},
{
'in_data': {
"services": [{"type": "dns", "address": "172.19.0.12"}],
"networks": [{
"network_id": "public-ipv4",
"type": "ipv4", "netmask": "255.255.252.0",
"link": "tap1a81968a-79",
"routes": [{
"netmask": "0.0.0.0",
"network": "0.0.0.0",
"gateway": "172.19.3.254",
}],
"ip_address": "172.19.1.34", "id": "network0"
}, {
"network_id": "private-ipv4",
"type": "ipv4", "netmask": "255.255.255.0",
"link": "tap1a81968a-79",
"routes": [],
"ip_address": "10.0.0.10", "id": "network1"
}],
"links": [
{
"ethernet_mac_address": "fa:16:3e:ed:9a:59",
"mtu": None, "type": "bridge", "id":
"tap1a81968a-79",
"vif_id": "1a81968a-797a-400f-8a80-567f997eb93f"
},
],
},
'in_macs': {
'fa:16:3e:ed:9a:59': 'eth0',
},
'out_sysconfig': [
('etc/sysconfig/network-scripts/ifcfg-eth0',
"""
# Created by cloud-init on instance boot automatically, do not edit.
#
BOOTPROTO=none
DEFROUTE=yes
DEVICE=eth0
GATEWAY=172.19.3.254
HWADDR=fa:16:3e:ed:9a:59
IPADDR=172.19.1.34
IPADDR1=10.0.0.10
NETMASK=255.255.252.0
NETMASK1=255.255.255.0
NM_CONTROLLED=no
ONBOOT=yes
TYPE=Ethernet
USERCTL=no
""".lstrip()),
('etc/resolv.conf',
"""
; Created by cloud-init on instance boot automatically, do not edit.
;
nameserver 172.19.0.12
""".lstrip()),
('etc/udev/rules.d/70-persistent-net.rules',
"".join(['SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ',
'ATTR{address}=="fa:16:3e:ed:9a:59", NAME="eth0"\n']))]
},
{
'in_data': {
"services": [{"type": "dns", "address": "172.19.0.12"}],
"networks": [{
"network_id": "public-ipv4",
"type": "ipv4", "netmask": "255.255.252.0",
"link": "tap1a81968a-79",
"routes": [{
"netmask": "0.0.0.0",
"network": "0.0.0.0",
"gateway": "172.19.3.254",
}],
"ip_address": "172.19.1.34", "id": "network0"
}, {
"network_id": "public-ipv6-a",
"type": "ipv6", "netmask": "",
"link": "tap1a81968a-79",
"routes": [
{
"gateway": "2001:DB8::1",
"netmask": "::",
"network": "::"
}
],
"ip_address": "2001:DB8::10", "id": "network1"
}, {
"network_id": "public-ipv6-b",
"type": "ipv6", "netmask": "64",
"link": "tap1a81968a-79",
"routes": [
],
"ip_address": "2001:DB9::10", "id": "network2"
}, {
"network_id": "public-ipv6-c",
"type": "ipv6", "netmask": "64",
"link": "tap1a81968a-79",
"routes": [
],
"ip_address": "2001:DB10::10", "id": "network3"
}],
"links": [
{
"ethernet_mac_address": "fa:16:3e:ed:9a:59",
"mtu": None, "type": "bridge", "id":
"tap1a81968a-79",
"vif_id": "1a81968a-797a-400f-8a80-567f997eb93f"
},
],
},
'in_macs': {
'fa:16:3e:ed:9a:59': 'eth0',
},
'out_sysconfig': [
('etc/sysconfig/network-scripts/ifcfg-eth0',
"""
# Created by cloud-init on instance boot automatically, do not edit.
#
BOOTPROTO=none
DEFROUTE=yes
DEVICE=eth0
GATEWAY=172.19.3.254
HWADDR=fa:16:3e:ed:9a:59
IPADDR=172.19.1.34
IPV6ADDR=2001:DB8::10
IPV6ADDR_SECONDARIES="2001:DB9::10/64 2001:DB10::10/64"
IPV6INIT=yes
IPV6_DEFAULTGW=2001:DB8::1
NETMASK=255.255.252.0
NM_CONTROLLED=no
ONBOOT=yes
TYPE=Ethernet
USERCTL=no
""".lstrip()),
('etc/resolv.conf',
"""
; Created by cloud-init on instance boot automatically, do not edit.
;
nameserver 172.19.0.12
""".lstrip()),
('etc/udev/rules.d/70-persistent-net.rules',
"".join(['SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ',
'ATTR{address}=="fa:16:3e:ed:9a:59", NAME="eth0"\n']))]
}
]
EXAMPLE_ENI = """
auto lo
iface lo inet loopback
dns-nameservers 10.0.0.1
dns-search foo.com
auto eth0
iface eth0 inet static
address 1.2.3.12
netmask 255.255.255.248
broadcast 1.2.3.15
gateway 1.2.3.9
dns-nameservers 69.9.160.191 69.9.191.4
auto eth1
iface eth1 inet static
address 10.248.2.4
netmask 255.255.255.248
broadcast 10.248.2.7
"""
RENDERED_ENI = """
auto lo
iface lo inet loopback
dns-nameservers 10.0.0.1
dns-search foo.com
auto eth0
iface eth0 inet static
address 1.2.3.12
broadcast 1.2.3.15
dns-nameservers 69.9.160.191 69.9.191.4
gateway 1.2.3.9
netmask 255.255.255.248
auto eth1
iface eth1 inet static
address 10.248.2.4
broadcast 10.248.2.7
netmask 255.255.255.248
""".lstrip()
NETWORK_CONFIGS = {
'small': {
'expected_eni': textwrap.dedent("""\
auto lo
iface lo inet loopback
dns-nameservers 1.2.3.4 5.6.7.8
dns-search wark.maas
iface eth1 inet manual
auto eth99
iface eth99 inet dhcp
# control-alias eth99
iface eth99 inet static
address 192.168.21.3/24
dns-nameservers 8.8.8.8 8.8.4.4
dns-search barley.maas sach.maas
post-up route add default gw 65.61.151.37 || true
pre-down route del default gw 65.61.151.37 || true
""").rstrip(' '),
'expected_netplan': textwrap.dedent("""
network:
version: 2
ethernets:
eth1:
match:
macaddress: cf:d6:af:48:e8:80
nameservers:
addresses:
- 1.2.3.4
- 5.6.7.8
search:
- wark.maas
set-name: eth1
eth99:
addresses:
- 192.168.21.3/24
dhcp4: true
match:
macaddress: c0:d6:9f:2c:e8:80
nameservers:
addresses:
- 8.8.8.8
- 8.8.4.4
- 1.2.3.4
- 5.6.7.8
search:
- barley.maas
- sach.maas
- wark.maas
routes:
- to: 0.0.0.0/0
via: 65.61.151.37
set-name: eth99
""").rstrip(' '),
'yaml': textwrap.dedent("""
version: 1
config:
# Physical interfaces.
- type: physical
name: eth99
mac_address: "c0:d6:9f:2c:e8:80"
subnets:
- type: dhcp4
- type: static
address: 192.168.21.3/24
dns_nameservers:
- 8.8.8.8
- 8.8.4.4
dns_search: barley.maas sach.maas
routes:
- gateway: 65.61.151.37
netmask: 0.0.0.0
network: 0.0.0.0
metric: 2
- type: physical
name: eth1
mac_address: "cf:d6:af:48:e8:80"
- type: nameserver
address:
- 1.2.3.4
- 5.6.7.8
search:
- wark.maas
"""),
},
'v4_and_v6': {
'expected_eni': textwrap.dedent("""\
auto lo
iface lo inet loopback
auto iface0
iface iface0 inet dhcp
# control-alias iface0
iface iface0 inet6 dhcp
""").rstrip(' '),
'expected_netplan': textwrap.dedent("""
network:
version: 2
ethernets:
iface0:
dhcp4: true
dhcp6: true
""").rstrip(' '),
'yaml': textwrap.dedent("""\
version: 1
config:
- type: 'physical'
name: 'iface0'
subnets:
- {'type': 'dhcp4'}
- {'type': 'dhcp6'}
""").rstrip(' '),
},
'all': {
'expected_eni': ("""\
auto lo
iface lo inet loopback
dns-nameservers 8.8.8.8 4.4.4.4 8.8.4.4
dns-search barley.maas wark.maas foobar.maas
iface eth0 inet manual
auto eth1
iface eth1 inet manual
bond-master bond0
bond-mode active-backup
bond-xmit-hash-policy layer3+4
bond_miimon 100
auto eth2
iface eth2 inet manual
bond-master bond0
bond-mode active-backup
bond-xmit-hash-policy layer3+4
bond_miimon 100
iface eth3 inet manual
iface eth4 inet manual
# control-manual eth5
iface eth5 inet dhcp
auto bond0
iface bond0 inet6 dhcp
bond-mode active-backup
bond-slaves none
bond-xmit-hash-policy layer3+4
bond_miimon 100
hwaddress aa:bb:cc:dd:ee:ff
auto br0
iface br0 inet static
address 192.168.14.2/24
bridge_ageing 250
bridge_bridgeprio 22
bridge_fd 1
bridge_gcint 2
bridge_hello 1
bridge_maxage 10
bridge_pathcost eth3 50
bridge_pathcost eth4 75
bridge_portprio eth3 28
bridge_portprio eth4 14
bridge_ports eth3 eth4
bridge_stp off
bridge_waitport 1 eth3
bridge_waitport 2 eth4
# control-alias br0
iface br0 inet6 static
address 2001:1::1/64
auto bond0.200
iface bond0.200 inet dhcp
vlan-raw-device bond0
vlan_id 200
auto eth0.101
iface eth0.101 inet static
address 192.168.0.2/24
dns-nameservers 192.168.0.10 10.23.23.134
dns-search barley.maas sacchromyces.maas brettanomyces.maas
gateway 192.168.0.1
hwaddress aa:bb:cc:dd:ee:11
mtu 1500
vlan-raw-device eth0
vlan_id 101
# control-alias eth0.101
iface eth0.101 inet static
address 192.168.2.10/24
post-up route add -net 10.0.0.0 netmask 255.0.0.0 gw 11.0.0.1 metric 3 || true
pre-down route del -net 10.0.0.0 netmask 255.0.0.0 gw 11.0.0.1 metric 3 || true
"""),
'expected_netplan': textwrap.dedent("""
network:
version: 2
ethernets:
eth0:
match:
macaddress: c0:d6:9f:2c:e8:80
nameservers:
addresses:
- 8.8.8.8
- 4.4.4.4
- 8.8.4.4
search:
- barley.maas
- wark.maas
- foobar.maas
set-name: eth0
eth1:
match:
macaddress: aa:d6:9f:2c:e8:80
nameservers:
addresses:
- 8.8.8.8
- 4.4.4.4
- 8.8.4.4
search:
- barley.maas
- wark.maas
- foobar.maas
set-name: eth1
eth2:
match:
macaddress: c0:bb:9f:2c:e8:80
nameservers:
addresses:
- 8.8.8.8
- 4.4.4.4
- 8.8.4.4
search:
- barley.maas
- wark.maas
- foobar.maas
set-name: eth2
eth3:
match:
macaddress: 66:bb:9f:2c:e8:80
nameservers:
addresses:
- 8.8.8.8
- 4.4.4.4
- 8.8.4.4
search:
- barley.maas
- wark.maas
- foobar.maas
set-name: eth3
eth4:
match:
macaddress: 98:bb:9f:2c:e8:80
nameservers:
addresses:
- 8.8.8.8
- 4.4.4.4
- 8.8.4.4
search:
- barley.maas
- wark.maas
- foobar.maas
set-name: eth4
eth5:
dhcp4: true
match:
macaddress: 98:bb:9f:2c:e8:8a
nameservers:
addresses:
- 8.8.8.8
- 4.4.4.4
- 8.8.4.4
search:
- barley.maas
- wark.maas
- foobar.maas
set-name: eth5
bonds:
bond0:
dhcp6: true
interfaces:
- eth1
- eth2
parameters:
mii-monitor-interval: 100
mode: active-backup
transmit-hash-policy: layer3+4
bridges:
br0:
addresses:
- 192.168.14.2/24
- 2001:1::1/64
interfaces:
- eth3
- eth4
parameters:
ageing-time: 250
forward-delay: 1
hello-time: 1
max-age: 10
path-cost:
eth3: 50
eth4: 75
priority: 22
vlans:
bond0.200:
dhcp4: true
id: 200
link: bond0
eth0.101:
addresses:
- 192.168.0.2/24
- 192.168.2.10/24
gateway4: 192.168.0.1
id: 101
link: eth0
macaddress: aa:bb:cc:dd:ee:11
nameservers:
addresses:
- 192.168.0.10
- 10.23.23.134
search:
- barley.maas
- sacchromyces.maas
- brettanomyces.maas
""").rstrip(' '),
'yaml': textwrap.dedent("""
version: 1
config:
# Physical interfaces.
- type: physical
name: eth0
mac_address: "c0:d6:9f:2c:e8:80"
- type: physical
name: eth1
mac_address: "aa:d6:9f:2c:e8:80"
- type: physical
name: eth2
mac_address: "c0:bb:9f:2c:e8:80"
- type: physical
name: eth3
mac_address: "66:bb:9f:2c:e8:80"
- type: physical
name: eth4
mac_address: "98:bb:9f:2c:e8:80"
# specify how ifupdown should treat iface
# control is one of ['auto', 'hotplug', 'manual']
# with manual meaning ifup/ifdown should not affect the iface
# useful for things like iscsi root + dhcp
- type: physical
name: eth5
mac_address: "98:bb:9f:2c:e8:8a"
subnets:
- type: dhcp
control: manual
# VLAN interface.
- type: vlan
name: eth0.101
vlan_link: eth0
vlan_id: 101
mac_address: aa:bb:cc:dd:ee:11
mtu: 1500
subnets:
- type: static
address: 192.168.0.2/24
gateway: 192.168.0.1
dns_nameservers:
- 192.168.0.10
- 10.23.23.134
dns_search:
- barley.maas
- sacchromyces.maas
- brettanomyces.maas
- type: static
address: 192.168.2.10/24
# Bond.
- type: bond
name: bond0
# if 'mac_address' is omitted, the MAC is taken from
# the first slave.
mac_address: "aa:bb:cc:dd:ee:ff"
bond_interfaces:
- eth1
- eth2
params:
bond-mode: active-backup
bond_miimon: 100
bond-xmit-hash-policy: "layer3+4"
subnets:
- type: dhcp6
# A Bond VLAN.
- type: vlan
name: bond0.200
vlan_link: bond0
vlan_id: 200
subnets:
- type: dhcp4
# A bridge.
- type: bridge
name: br0
bridge_interfaces:
- eth3
- eth4
ipv4_conf:
rp_filter: 1
proxy_arp: 0
forwarding: 1
ipv6_conf:
autoconf: 1
disable_ipv6: 1
use_tempaddr: 1
forwarding: 1
# basically anything in /proc/sys/net/ipv6/conf/.../
params:
bridge_ageing: 250
bridge_bridgeprio: 22
bridge_fd: 1
bridge_gcint: 2
bridge_hello: 1
bridge_maxage: 10
bridge_maxwait: 0
bridge_pathcost:
- eth3 50
- eth4 75
bridge_portprio:
- eth3 28
- eth4 14
bridge_stp: 'off'
bridge_waitport:
- 1 eth3
- 2 eth4
subnets:
- type: static
address: 192.168.14.2/24
- type: static
address: 2001:1::1/64 # default to /64
# A global nameserver.
- type: nameserver
address: 8.8.8.8
search: barley.maas
# global nameservers and search in list form
- type: nameserver
address:
- 4.4.4.4
- 8.8.4.4
search:
- wark.maas
- foobar.maas
# A global route.
- type: route
destination: 10.0.0.0/8
gateway: 11.0.0.1
metric: 3
""").lstrip(),
}
}
CONFIG_V1_EXPLICIT_LOOPBACK = {
'version': 1,
'config': [{'name': 'eth0', 'type': 'physical',
'subnets': [{'control': 'auto', 'type': 'dhcp'}]},
{'name': 'lo', 'type': 'loopback',
'subnets': [{'control': 'auto', 'type': 'loopback'}]},
]}
def _setup_test(tmp_dir, mock_get_devicelist, mock_read_sys_net,
mock_sys_dev_path):
mock_get_devicelist.return_value = ['eth1000']
dev_characteristics = {
'eth1000': {
"bridge": False,
"carrier": False,
"dormant": False,
"operstate": "down",
"address": "07-1C-C6-75-A4-BE",
}
}
def fake_read(devname, path, translate=None,
on_enoent=None, on_keyerror=None,
on_einval=None):
return dev_characteristics[devname][path]
mock_read_sys_net.side_effect = fake_read
def sys_dev_path(devname, path=""):
return tmp_dir + devname + "/" + path
for dev in dev_characteristics:
os.makedirs(os.path.join(tmp_dir, dev))
with open(os.path.join(tmp_dir, dev, 'operstate'), 'w') as fh:
fh.write("down")
mock_sys_dev_path.side_effect = sys_dev_path
class TestSysConfigRendering(CiTestCase):
@mock.patch("cloudinit.net.sys_dev_path")
@mock.patch("cloudinit.net.read_sys_net")
@mock.patch("cloudinit.net.get_devicelist")
def test_default_generation(self, mock_get_devicelist,
mock_read_sys_net,
mock_sys_dev_path):
tmp_dir = self.tmp_dir()
_setup_test(tmp_dir, mock_get_devicelist,
mock_read_sys_net, mock_sys_dev_path)
network_cfg = net.generate_fallback_config()
ns = network_state.parse_net_config_data(network_cfg,
skip_broken=False)
render_dir = os.path.join(tmp_dir, "render")
os.makedirs(render_dir)
renderer = sysconfig.Renderer()
renderer.render_network_state(ns, render_dir)
render_file = 'etc/sysconfig/network-scripts/ifcfg-eth1000'
with open(os.path.join(render_dir, render_file)) as fh:
content = fh.read()
expected_content = """
# Created by cloud-init on instance boot automatically, do not edit.
#
BOOTPROTO=dhcp
DEVICE=eth1000
HWADDR=07-1C-C6-75-A4-BE
NM_CONTROLLED=no
ONBOOT=yes
TYPE=Ethernet
USERCTL=no
""".lstrip()
self.assertEqual(expected_content, content)
def test_multiple_ipv4_default_gateways(self):
"""ValueError is raised when duplicate ipv4 gateways exist."""
net_json = {
"services": [{"type": "dns", "address": "172.19.0.12"}],
"networks": [{
"network_id": "dacd568d-5be6-4786-91fe-750c374b78b4",
"type": "ipv4", "netmask": "255.255.252.0",
"link": "tap1a81968a-79",
"routes": [{
"netmask": "0.0.0.0",
"network": "0.0.0.0",
"gateway": "172.19.3.254",
}, {
"netmask": "0.0.0.0", # A second default gateway
"network": "0.0.0.0",
"gateway": "172.20.3.254",
}],
"ip_address": "172.19.1.34", "id": "network0"
}],
"links": [
{
"ethernet_mac_address": "fa:16:3e:ed:9a:59",
"mtu": None, "type": "bridge", "id":
"tap1a81968a-79",
"vif_id": "1a81968a-797a-400f-8a80-567f997eb93f"
},
],
}
macs = {'fa:16:3e:ed:9a:59': 'eth0'}
render_dir = self.tmp_dir()
network_cfg = openstack.convert_net_json(net_json, known_macs=macs)
ns = network_state.parse_net_config_data(network_cfg,
skip_broken=False)
renderer = sysconfig.Renderer()
with self.assertRaises(ValueError):
renderer.render_network_state(ns, render_dir)
self.assertEqual([], os.listdir(render_dir))
def test_multiple_ipv6_default_gateways(self):
"""ValueError is raised when duplicate ipv6 gateways exist."""
net_json = {
"services": [{"type": "dns", "address": "172.19.0.12"}],
"networks": [{
"network_id": "public-ipv6",
"type": "ipv6", "netmask": "",
"link": "tap1a81968a-79",
"routes": [{
"gateway": "2001:DB8::1",
"netmask": "::",
"network": "::"
}, {
"gateway": "2001:DB9::1",
"netmask": "::",
"network": "::"
}],
"ip_address": "2001:DB8::10", "id": "network1"
}],
"links": [
{
"ethernet_mac_address": "fa:16:3e:ed:9a:59",
"mtu": None, "type": "bridge", "id":
"tap1a81968a-79",
"vif_id": "1a81968a-797a-400f-8a80-567f997eb93f"
},
],
}
macs = {'fa:16:3e:ed:9a:59': 'eth0'}
render_dir = self.tmp_dir()
network_cfg = openstack.convert_net_json(net_json, known_macs=macs)
ns = network_state.parse_net_config_data(network_cfg,
skip_broken=False)
renderer = sysconfig.Renderer()
with self.assertRaises(ValueError):
renderer.render_network_state(ns, render_dir)
self.assertEqual([], os.listdir(render_dir))
def test_openstack_rendering_samples(self):
for os_sample in OS_SAMPLES:
render_dir = self.tmp_dir()
ex_input = os_sample['in_data']
ex_mac_addrs = os_sample['in_macs']
network_cfg = openstack.convert_net_json(
ex_input, known_macs=ex_mac_addrs)
ns = network_state.parse_net_config_data(network_cfg,
skip_broken=False)
renderer = sysconfig.Renderer()
renderer.render_network_state(ns, render_dir)
for fn, expected_content in os_sample.get('out_sysconfig', []):
with open(os.path.join(render_dir, fn)) as fh:
self.assertEqual(expected_content, fh.read())
def test_config_with_explicit_loopback(self):
ns = network_state.parse_net_config_data(CONFIG_V1_EXPLICIT_LOOPBACK)
render_dir = self.tmp_path("render")
os.makedirs(render_dir)
renderer = sysconfig.Renderer()
renderer.render_network_state(ns, render_dir)
found = dir2dict(render_dir)
nspath = '/etc/sysconfig/network-scripts/'
self.assertNotIn(nspath + 'ifcfg-lo', found.keys())
expected = """\
# Created by cloud-init on instance boot automatically, do not edit.
#
BOOTPROTO=dhcp
DEVICE=eth0
NM_CONTROLLED=no
ONBOOT=yes
TYPE=Ethernet
USERCTL=no
"""
self.assertEqual(expected, found[nspath + 'ifcfg-eth0'])
class TestEniNetRendering(CiTestCase):
@mock.patch("cloudinit.net.sys_dev_path")
@mock.patch("cloudinit.net.read_sys_net")
@mock.patch("cloudinit.net.get_devicelist")
def test_default_generation(self, mock_get_devicelist,
mock_read_sys_net,
mock_sys_dev_path):
tmp_dir = self.tmp_dir()
_setup_test(tmp_dir, mock_get_devicelist,
mock_read_sys_net, mock_sys_dev_path)
network_cfg = net.generate_fallback_config()
ns = network_state.parse_net_config_data(network_cfg,
skip_broken=False)
render_dir = os.path.join(tmp_dir, "render")
os.makedirs(render_dir)
renderer = eni.Renderer(
{'eni_path': 'interfaces', 'netrules_path': None})
renderer.render_network_state(ns, render_dir)
self.assertTrue(os.path.exists(os.path.join(render_dir,
'interfaces')))
with open(os.path.join(render_dir, 'interfaces')) as fh:
contents = fh.read()
expected = """
auto lo
iface lo inet loopback
auto eth1000
iface eth1000 inet dhcp
"""
self.assertEqual(expected.lstrip(), contents.lstrip())
def test_config_with_explicit_loopback(self):
tmp_dir = self.tmp_dir()
ns = network_state.parse_net_config_data(CONFIG_V1_EXPLICIT_LOOPBACK)
renderer = eni.Renderer()
renderer.render_network_state(ns, tmp_dir)
expected = """\
auto lo
iface lo inet loopback
auto eth0
iface eth0 inet dhcp
"""
self.assertEqual(
expected, dir2dict(tmp_dir)['/etc/network/interfaces'])
class TestNetplanNetRendering(CiTestCase):
@mock.patch("cloudinit.net.netplan._clean_default")
@mock.patch("cloudinit.net.sys_dev_path")
@mock.patch("cloudinit.net.read_sys_net")
@mock.patch("cloudinit.net.get_devicelist")
def test_default_generation(self, mock_get_devicelist,
mock_read_sys_net,
mock_sys_dev_path,
mock_clean_default):
tmp_dir = self.tmp_dir()
_setup_test(tmp_dir, mock_get_devicelist,
mock_read_sys_net, mock_sys_dev_path)
network_cfg = net.generate_fallback_config()
ns = network_state.parse_net_config_data(network_cfg,
skip_broken=False)
render_dir = os.path.join(tmp_dir, "render")
os.makedirs(render_dir)
render_target = 'netplan.yaml'
renderer = netplan.Renderer(
{'netplan_path': render_target, 'postcmds': False})
renderer.render_network_state(ns, render_dir)
self.assertTrue(os.path.exists(os.path.join(render_dir,
render_target)))
with open(os.path.join(render_dir, render_target)) as fh:
contents = fh.read()
print(contents)
expected = """
network:
version: 2
ethernets:
eth1000:
dhcp4: true
match:
macaddress: 07-1c-c6-75-a4-be
set-name: eth1000
"""
self.assertEqual(expected.lstrip(), contents.lstrip())
self.assertEqual(1, mock_clean_default.call_count)
class TestNetplanCleanDefault(CiTestCase):
snapd_known_path = 'etc/netplan/00-snapd-config.yaml'
snapd_known_content = textwrap.dedent("""\
# This is the initial network config.
# It can be overwritten by cloud-init or console-conf.
network:
version: 2
ethernets:
all-en:
match:
name: "en*"
dhcp4: true
all-eth:
match:
name: "eth*"
dhcp4: true
""")
stub_known = {
'run/systemd/network/10-netplan-all-en.network': 'foo-en',
'run/systemd/network/10-netplan-all-eth.network': 'foo-eth',
'run/systemd/generator/netplan.stamp': 'stamp',
}
def test_clean_known_config_cleaned(self):
content = {self.snapd_known_path: self.snapd_known_content, }
content.update(self.stub_known)
tmpd = self.tmp_dir()
files = sorted(populate_dir(tmpd, content))
netplan._clean_default(target=tmpd)
found = [t for t in files if os.path.exists(t)]
self.assertEqual([], found)
def test_clean_unknown_config_not_cleaned(self):
content = {self.snapd_known_path: self.snapd_known_content, }
content.update(self.stub_known)
content[self.snapd_known_path] += "# user put a comment\n"
tmpd = self.tmp_dir()
files = sorted(populate_dir(tmpd, content))
netplan._clean_default(target=tmpd)
found = [t for t in files if os.path.exists(t)]
self.assertEqual(files, found)
def test_clean_known_config_cleans_only_expected(self):
astamp = "run/systemd/generator/another.stamp"
anet = "run/systemd/network/10-netplan-all-lo.network"
ayaml = "etc/netplan/01-foo-config.yaml"
content = {
self.snapd_known_path: self.snapd_known_content,
astamp: "stamp",
anet: "network",
ayaml: "yaml",
}
content.update(self.stub_known)
tmpd = self.tmp_dir()
files = sorted(populate_dir(tmpd, content))
netplan._clean_default(target=tmpd)
found = [t for t in files if os.path.exists(t)]
expected = [util.target_path(tmpd, f) for f in (astamp, anet, ayaml)]
self.assertEqual(sorted(expected), found)
class TestNetplanPostcommands(CiTestCase):
mycfg = {
'config': [{"type": "physical", "name": "eth0",
"mac_address": "c0:d6:9f:2c:e8:80",
"subnets": [{"type": "dhcp"}]}],
'version': 1}
@mock.patch.object(netplan.Renderer, '_netplan_generate')
@mock.patch.object(netplan.Renderer, '_net_setup_link')
def test_netplan_render_calls_postcmds(self, mock_netplan_generate,
mock_net_setup_link):
tmp_dir = self.tmp_dir()
ns = network_state.parse_net_config_data(self.mycfg,
skip_broken=False)
render_dir = os.path.join(tmp_dir, "render")
os.makedirs(render_dir)
render_target = 'netplan.yaml'
renderer = netplan.Renderer(
{'netplan_path': render_target, 'postcmds': True})
renderer.render_network_state(ns, render_dir)
mock_netplan_generate.assert_called_with(run=True)
mock_net_setup_link.assert_called_with(run=True)
@mock.patch.object(netplan, "get_devicelist")
@mock.patch('cloudinit.util.subp')
def test_netplan_postcmds(self, mock_subp, mock_devlist):
mock_devlist.side_effect = [['lo']]
tmp_dir = self.tmp_dir()
ns = network_state.parse_net_config_data(self.mycfg,
skip_broken=False)
render_dir = os.path.join(tmp_dir, "render")
os.makedirs(render_dir)
render_target = 'netplan.yaml'
renderer = netplan.Renderer(
{'netplan_path': render_target, 'postcmds': True})
expected = [
mock.call(['netplan', 'generate'], capture=True),
mock.call(['udevadm', 'test-builtin', 'net_setup_link',
'/sys/class/net/lo'], capture=True),
]
with mock.patch.object(os.path, 'islink', return_value=True):
renderer.render_network_state(ns, render_dir)
mock_subp.assert_has_calls(expected)
class TestEniNetworkStateToEni(CiTestCase):
mycfg = {
'config': [{"type": "physical", "name": "eth0",
"mac_address": "c0:d6:9f:2c:e8:80",
"subnets": [{"type": "dhcp"}]}],
'version': 1}
my_mac = 'c0:d6:9f:2c:e8:80'
def test_no_header(self):
rendered = eni.network_state_to_eni(
network_state=network_state.parse_net_config_data(self.mycfg),
render_hwaddress=True)
self.assertIn(self.my_mac, rendered)
self.assertIn("hwaddress", rendered)
def test_with_header(self):
header = "# hello world\n"
rendered = eni.network_state_to_eni(
network_state=network_state.parse_net_config_data(self.mycfg),
header=header, render_hwaddress=True)
self.assertIn(header, rendered)
self.assertIn(self.my_mac, rendered)
def test_no_hwaddress(self):
rendered = eni.network_state_to_eni(
network_state=network_state.parse_net_config_data(self.mycfg),
render_hwaddress=False)
self.assertNotIn(self.my_mac, rendered)
self.assertNotIn("hwaddress", rendered)
class TestCmdlineConfigParsing(CiTestCase):
simple_cfg = {
'config': [{"type": "physical", "name": "eth0",
"mac_address": "c0:d6:9f:2c:e8:80",
"subnets": [{"type": "dhcp"}]}]}
def test_cmdline_convert_dhcp(self):
found = cmdline._klibc_to_config_entry(DHCP_CONTENT_1)
self.assertEqual(found, ('eth0', DHCP_EXPECTED_1))
def test_cmdline_convert_dhcp6(self):
found = cmdline._klibc_to_config_entry(DHCP6_CONTENT_1)
self.assertEqual(found, ('eno1', DHCP6_EXPECTED_1))
def test_cmdline_convert_static(self):
found = cmdline._klibc_to_config_entry(STATIC_CONTENT_1)
self.assertEqual(found, ('eth1', STATIC_EXPECTED_1))
def test_config_from_cmdline_net_cfg(self):
files = []
pairs = (('net-eth0.cfg', DHCP_CONTENT_1),
('net-eth1.cfg', STATIC_CONTENT_1))
macs = {'eth1': 'b8:ae:ed:75:ff:2b',
'eth0': 'b8:ae:ed:75:ff:2a'}
dhcp = copy.deepcopy(DHCP_EXPECTED_1)
dhcp['mac_address'] = macs['eth0']
static = copy.deepcopy(STATIC_EXPECTED_1)
static['mac_address'] = macs['eth1']
expected = {'version': 1, 'config': [dhcp, static]}
with util.tempdir() as tmpd:
for fname, content in pairs:
fp = os.path.join(tmpd, fname)
files.append(fp)
util.write_file(fp, content)
found = cmdline.config_from_klibc_net_cfg(files=files,
mac_addrs=macs)
self.assertEqual(found, expected)
def test_cmdline_with_b64(self):
data = base64.b64encode(json.dumps(self.simple_cfg).encode())
encoded_text = data.decode()
raw_cmdline = 'ro network-config=' + encoded_text + ' root=foo'
found = cmdline.read_kernel_cmdline_config(cmdline=raw_cmdline)
self.assertEqual(found, self.simple_cfg)
def test_cmdline_with_b64_gz(self):
data = _gzip_data(json.dumps(self.simple_cfg).encode())
encoded_text = base64.b64encode(data).decode()
raw_cmdline = 'ro network-config=' + encoded_text + ' root=foo'
found = cmdline.read_kernel_cmdline_config(cmdline=raw_cmdline)
self.assertEqual(found, self.simple_cfg)
class TestCmdlineReadKernelConfig(CiTestCase):
macs = {
'eth0': '14:02:ec:42:48:00',
'eno1': '14:02:ec:42:48:01',
}
def test_ip_cmdline_read_kernel_cmdline_ip(self):
content = {'net-eth0.conf': DHCP_CONTENT_1}
files = sorted(populate_dir(self.tmp_dir(), content))
found = cmdline.read_kernel_cmdline_config(
files=files, cmdline='foo ip=dhcp', mac_addrs=self.macs)
exp1 = copy.deepcopy(DHCP_EXPECTED_1)
exp1['mac_address'] = self.macs['eth0']
self.assertEqual(found['version'], 1)
self.assertEqual(found['config'], [exp1])
def test_ip_cmdline_read_kernel_cmdline_ip6(self):
content = {'net6-eno1.conf': DHCP6_CONTENT_1}
files = sorted(populate_dir(self.tmp_dir(), content))
found = cmdline.read_kernel_cmdline_config(
files=files, cmdline='foo ip6=dhcp root=/dev/sda',
mac_addrs=self.macs)
self.assertEqual(
found,
{'version': 1, 'config': [
{'type': 'physical', 'name': 'eno1',
'mac_address': self.macs['eno1'],
'subnets': [
{'dns_nameservers': ['2001:67c:1562:8010::2:1'],
'control': 'manual', 'type': 'dhcp6', 'netmask': '64'}]}]})
def test_ip_cmdline_read_kernel_cmdline_none(self):
# if there is no ip= or ip6= on cmdline, return value should be None
content = {'net6-eno1.conf': DHCP6_CONTENT_1}
files = sorted(populate_dir(self.tmp_dir(), content))
found = cmdline.read_kernel_cmdline_config(
files=files, cmdline='foo root=/dev/sda', mac_addrs=self.macs)
self.assertIsNone(found)
def test_ip_cmdline_both_ip_ip6(self):
content = {'net-eth0.conf': DHCP_CONTENT_1,
'net6-eth0.conf': DHCP6_CONTENT_1.replace('eno1', 'eth0')}
files = sorted(populate_dir(self.tmp_dir(), content))
found = cmdline.read_kernel_cmdline_config(
files=files, cmdline='foo ip=dhcp ip6=dhcp', mac_addrs=self.macs)
eth0 = copy.deepcopy(DHCP_EXPECTED_1)
eth0['mac_address'] = self.macs['eth0']
eth0['subnets'].append(
{'control': 'manual', 'type': 'dhcp6',
'netmask': '64', 'dns_nameservers': ['2001:67c:1562:8010::2:1']})
expected = [eth0]
self.assertEqual(found['version'], 1)
self.assertEqual(found['config'], expected)
class TestNetplanRoundTrip(CiTestCase):
def _render_and_read(self, network_config=None, state=None,
netplan_path=None, target=None):
if target is None:
target = self.tmp_dir()
if network_config:
ns = network_state.parse_net_config_data(network_config)
elif state:
ns = state
else:
raise ValueError("Expected data or state, got neither")
if netplan_path is None:
netplan_path = 'etc/netplan/50-cloud-init.yaml'
renderer = netplan.Renderer(
config={'netplan_path': netplan_path})
renderer.render_network_state(ns, target)
return dir2dict(target)
def testsimple_render_small_netplan(self):
entry = NETWORK_CONFIGS['small']
files = self._render_and_read(network_config=yaml.load(entry['yaml']))
self.assertEqual(
entry['expected_netplan'].splitlines(),
files['/etc/netplan/50-cloud-init.yaml'].splitlines())
def testsimple_render_v4_and_v6(self):
entry = NETWORK_CONFIGS['v4_and_v6']
files = self._render_and_read(network_config=yaml.load(entry['yaml']))
self.assertEqual(
entry['expected_netplan'].splitlines(),
files['/etc/netplan/50-cloud-init.yaml'].splitlines())
def testsimple_render_all(self):
entry = NETWORK_CONFIGS['all']
files = self._render_and_read(network_config=yaml.load(entry['yaml']))
self.assertEqual(
entry['expected_netplan'].splitlines(),
files['/etc/netplan/50-cloud-init.yaml'].splitlines())
class TestEniRoundTrip(CiTestCase):
def _render_and_read(self, network_config=None, state=None, eni_path=None,
netrules_path=None, dir=None):
if dir is None:
dir = self.tmp_dir()
if network_config:
ns = network_state.parse_net_config_data(network_config)
elif state:
ns = state
else:
raise ValueError("Expected data or state, got neither")
if eni_path is None:
eni_path = 'etc/network/interfaces'
renderer = eni.Renderer(
config={'eni_path': eni_path, 'netrules_path': netrules_path})
renderer.render_network_state(ns, dir)
return dir2dict(dir)
def testsimple_convert_and_render(self):
network_config = eni.convert_eni_data(EXAMPLE_ENI)
files = self._render_and_read(network_config=network_config)
self.assertEqual(
RENDERED_ENI.splitlines(),
files['/etc/network/interfaces'].splitlines())
def testsimple_render_all(self):
entry = NETWORK_CONFIGS['all']
files = self._render_and_read(network_config=yaml.load(entry['yaml']))
self.assertEqual(
entry['expected_eni'].splitlines(),
files['/etc/network/interfaces'].splitlines())
def testsimple_render_small(self):
entry = NETWORK_CONFIGS['small']
files = self._render_and_read(network_config=yaml.load(entry['yaml']))
self.assertEqual(
entry['expected_eni'].splitlines(),
files['/etc/network/interfaces'].splitlines())
def testsimple_render_v4_and_v6(self):
entry = NETWORK_CONFIGS['v4_and_v6']
files = self._render_and_read(network_config=yaml.load(entry['yaml']))
self.assertEqual(
entry['expected_eni'].splitlines(),
files['/etc/network/interfaces'].splitlines())
def test_routes_rendered(self):
# as reported in bug 1649652
conf = [
{'name': 'eth0', 'type': 'physical',
'subnets': [{
'address': '172.23.31.42/26',
'dns_nameservers': [], 'gateway': '172.23.31.2',
'type': 'static'}]},
{'type': 'route', 'id': 4,
'metric': 0, 'destination': '10.0.0.0/12',
'gateway': '172.23.31.1'},
{'type': 'route', 'id': 5,
'metric': 0, 'destination': '192.168.2.0/16',
'gateway': '172.23.31.1'},
{'type': 'route', 'id': 6,
'metric': 1, 'destination': '10.0.200.0/16',
'gateway': '172.23.31.1'},
]
files = self._render_and_read(
network_config={'config': conf, 'version': 1})
expected = [
'auto lo',
'iface lo inet loopback',
'auto eth0',
'iface eth0 inet static',
' address 172.23.31.42/26',
' gateway 172.23.31.2',
('post-up route add -net 10.0.0.0 netmask 255.240.0.0 gw '
'172.23.31.1 metric 0 || true'),
('pre-down route del -net 10.0.0.0 netmask 255.240.0.0 gw '
'172.23.31.1 metric 0 || true'),
('post-up route add -net 192.168.2.0 netmask 255.255.0.0 gw '
'172.23.31.1 metric 0 || true'),
('pre-down route del -net 192.168.2.0 netmask 255.255.0.0 gw '
'172.23.31.1 metric 0 || true'),
('post-up route add -net 10.0.200.0 netmask 255.255.0.0 gw '
'172.23.31.1 metric 1 || true'),
('pre-down route del -net 10.0.200.0 netmask 255.255.0.0 gw '
'172.23.31.1 metric 1 || true'),
]
found = files['/etc/network/interfaces'].splitlines()
self.assertEqual(
expected, [line for line in found if line])
class TestNetRenderers(CiTestCase):
@mock.patch("cloudinit.net.renderers.sysconfig.available")
@mock.patch("cloudinit.net.renderers.eni.available")
def test_eni_and_sysconfig_available(self, m_eni_avail, m_sysc_avail):
m_eni_avail.return_value = True
m_sysc_avail.return_value = True
found = renderers.search(priority=['sysconfig', 'eni'], first=False)
names = [f[0] for f in found]
self.assertEqual(['sysconfig', 'eni'], names)
@mock.patch("cloudinit.net.renderers.eni.available")
def test_search_returns_empty_on_none(self, m_eni_avail):
m_eni_avail.return_value = False
found = renderers.search(priority=['eni'], first=False)
self.assertEqual([], found)
@mock.patch("cloudinit.net.renderers.sysconfig.available")
@mock.patch("cloudinit.net.renderers.eni.available")
def test_first_in_priority(self, m_eni_avail, m_sysc_avail):
# available should only be called until one is found.
m_eni_avail.return_value = True
m_sysc_avail.side_effect = Exception("Should not call me")
found = renderers.search(priority=['eni', 'sysconfig'], first=True)
self.assertEqual(['eni'], [found[0]])
@mock.patch("cloudinit.net.renderers.sysconfig.available")
@mock.patch("cloudinit.net.renderers.eni.available")
def test_select_positive(self, m_eni_avail, m_sysc_avail):
m_eni_avail.return_value = True
m_sysc_avail.return_value = False
found = renderers.select(priority=['sysconfig', 'eni'])
self.assertEqual('eni', found[0])
@mock.patch("cloudinit.net.renderers.sysconfig.available")
@mock.patch("cloudinit.net.renderers.eni.available")
def test_select_none_found_raises(self, m_eni_avail, m_sysc_avail):
# if select finds nothing, should raise exception.
m_eni_avail.return_value = False
m_sysc_avail.return_value = False
self.assertRaises(net.RendererNotFoundError, renderers.select,
priority=['sysconfig', 'eni'])
class TestGetInterfacesByMac(CiTestCase):
_data = {'bonds': ['bond1'],
'bridges': ['bridge1'],
'vlans': ['bond1.101'],
'own_macs': ['enp0s1', 'enp0s2', 'bridge1-nic', 'bridge1',
'bond1.101', 'lo'],
'macs': {'enp0s1': 'aa:aa:aa:aa:aa:01',
'enp0s2': 'aa:aa:aa:aa:aa:02',
'bond1': 'aa:aa:aa:aa:aa:01',
'bond1.101': 'aa:aa:aa:aa:aa:01',
'bridge1': 'aa:aa:aa:aa:aa:03',
'bridge1-nic': 'aa:aa:aa:aa:aa:03',
'lo': '00:00:00:00:00:00',
'greptap0': '00:00:00:00:00:00',
'tun0': None}}
data = {}
def _se_get_devicelist(self):
return list(self.data['devices'])
def _se_get_interface_mac(self, name):
return self.data['macs'][name]
def _se_is_bridge(self, name):
return name in self.data['bridges']
def _se_is_vlan(self, name):
return name in self.data['vlans']
def _se_interface_has_own_mac(self, name):
return name in self.data['own_macs']
def _mock_setup(self):
self.data = copy.deepcopy(self._data)
self.data['devices'] = set(list(self.data['macs'].keys()))
mocks = ('get_devicelist', 'get_interface_mac', 'is_bridge',
'interface_has_own_mac', 'is_vlan')
self.mocks = {}
for n in mocks:
m = mock.patch('cloudinit.net.' + n,
side_effect=getattr(self, '_se_' + n))
self.addCleanup(m.stop)
self.mocks[n] = m.start()
def test_raise_exception_on_duplicate_macs(self):
self._mock_setup()
self.data['macs']['bridge1-nic'] = self.data['macs']['enp0s1']
self.assertRaises(RuntimeError, net.get_interfaces_by_mac)
def test_excludes_any_without_mac_address(self):
self._mock_setup()
ret = net.get_interfaces_by_mac()
self.assertIn('tun0', self._se_get_devicelist())
self.assertNotIn('tun0', ret.values())
def test_excludes_stolen_macs(self):
self._mock_setup()
ret = net.get_interfaces_by_mac()
self.mocks['interface_has_own_mac'].assert_has_calls(
[mock.call('enp0s1'), mock.call('bond1')], any_order=True)
self.assertEqual(
{'aa:aa:aa:aa:aa:01': 'enp0s1', 'aa:aa:aa:aa:aa:02': 'enp0s2',
'aa:aa:aa:aa:aa:03': 'bridge1-nic', '00:00:00:00:00:00': 'lo'},
ret)
def test_excludes_bridges(self):
self._mock_setup()
# add a device 'b1', make all return they have their "own mac",
# set everything other than 'b1' to be a bridge.
# then expect b1 is the only thing left.
self.data['macs']['b1'] = 'aa:aa:aa:aa:aa:b1'
self.data['devices'].add('b1')
self.data['bonds'] = []
self.data['own_macs'] = self.data['devices']
self.data['bridges'] = [f for f in self.data['devices'] if f != "b1"]
ret = net.get_interfaces_by_mac()
self.assertEqual({'aa:aa:aa:aa:aa:b1': 'b1'}, ret)
self.mocks['is_bridge'].assert_has_calls(
[mock.call('bridge1'), mock.call('enp0s1'), mock.call('bond1'),
mock.call('b1')],
any_order=True)
def test_excludes_vlans(self):
self._mock_setup()
# add a device 'b1', make all return they have their "own mac",
# set everything other than 'b1' to be a vlan.
# then expect b1 is the only thing left.
self.data['macs']['b1'] = 'aa:aa:aa:aa:aa:b1'
self.data['devices'].add('b1')
self.data['bonds'] = []
self.data['bridges'] = []
self.data['own_macs'] = self.data['devices']
self.data['vlans'] = [f for f in self.data['devices'] if f != "b1"]
ret = net.get_interfaces_by_mac()
self.assertEqual({'aa:aa:aa:aa:aa:b1': 'b1'}, ret)
self.mocks['is_vlan'].assert_has_calls(
[mock.call('bridge1'), mock.call('enp0s1'), mock.call('bond1'),
mock.call('b1')],
any_order=True)
def test_duplicates_of_empty_mac_are_ok(self):
"""Duplicate macs of 00:00:00:00:00:00 should be skipped."""
self._mock_setup()
empty_mac = "00:00:00:00:00:00"
addnics = ('greptap1', 'lo', 'greptap2')
self.data['macs'].update(dict((k, empty_mac) for k in addnics))
self.data['devices'].update(set(addnics))
ret = net.get_interfaces_by_mac()
self.assertEqual('lo', ret[empty_mac])
class TestInterfacesSorting(CiTestCase):
def test_natural_order(self):
data = ['ens5', 'ens6', 'ens3', 'ens20', 'ens13', 'ens2']
self.assertEqual(
sorted(data, key=_natural_sort_key),
['ens2', 'ens3', 'ens5', 'ens6', 'ens13', 'ens20'])
data2 = ['enp2s0', 'enp2s3', 'enp0s3', 'enp0s13', 'enp0s8', 'enp1s2']
self.assertEqual(
sorted(data2, key=_natural_sort_key),
['enp0s3', 'enp0s8', 'enp0s13', 'enp1s2', 'enp2s0', 'enp2s3'])
def _gzip_data(data):
with io.BytesIO() as iobuf:
gzfp = gzip.GzipFile(mode="wb", fileobj=iobuf)
gzfp.write(data)
gzfp.close()
return iobuf.getvalue()
# vi: ts=4 expandtab
|