Skip to content

Commit 529105e

Browse files
Zuulopenstack-gerrit
authored andcommitted
Merge "nocloud: fix network config regression"
2 parents cf583bf + 1a98b67 commit 529105e

File tree

2 files changed

+143
-117
lines changed

2 files changed

+143
-117
lines changed

cloudbaseinit/metadata/services/nocloudservice.py

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -237,10 +237,11 @@ def parse(self, network_config):
237237
networks = []
238238
services = []
239239

240-
network_config = network_config.get('network') \
241-
if network_config else {}
242-
network_config = network_config.get('config') \
243-
if network_config else None
240+
if network_config and network_config.get('network'):
241+
network_config = network_config.get('network')
242+
if network_config:
243+
network_config = network_config.get('config')
244+
244245
if not network_config:
245246
LOG.warning("Network configuration is empty")
246247
return
@@ -479,8 +480,9 @@ def parse(self, network_config):
479480
networks = []
480481
services = []
481482

482-
network_config = network_config.get('network') \
483-
if network_config else {}
483+
if network_config and network_config.get('network'):
484+
network_config = network_config.get('network')
485+
484486
if not network_config:
485487
LOG.warning("Network configuration is empty")
486488
return
@@ -529,12 +531,21 @@ class NoCloudNetworkConfigParser(object):
529531

530532
@staticmethod
531533
def parse(network_data):
532-
network_data_version = network_data.get("network", {}).get("version")
534+
# we can have a network key in some cases
535+
if network_data.get("network"):
536+
network_data = network_data.get("network")
537+
network_data_version = network_data.get("version")
538+
533539
if network_data_version == 1:
534540
network_config_parser = NoCloudNetworkConfigV1Parser()
535-
return network_config_parser.parse(network_data)
541+
elif network_data_version == 2:
542+
network_config_parser = NoCloudNetworkConfigV2Parser()
543+
else:
544+
raise exception.CloudbaseInitException(
545+
"Unsupported network_data_version: '%s'"
546+
% network_data_version)
536547

537-
return NoCloudNetworkConfigV2Parser().parse(network_data)
548+
return network_config_parser.parse(network_data)
538549

539550

540551
class NoCloudConfigDriveService(baseconfigdrive.BaseConfigDriveService):

cloudbaseinit/tests/metadata/services/test_nocloudservice.py

Lines changed: 123 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import ddt
1616
import importlib
1717
import os
18+
import textwrap
1819
import unittest
1920
import unittest.mock as mock
2021

@@ -48,55 +49,57 @@
4849
config:
4950
- type: router
5051
"""
51-
NOCLOUD_NETWORK_CONFIG_TEST_DATA_V1 = """
52-
network:
53-
version: 1
54-
config:
55-
- type: physical
56-
name: interface0
57-
mac_address: "52:54:00:12:34:00"
58-
mtu: 1450
59-
subnets:
60-
- type: static
61-
address: 192.168.1.10
62-
netmask: 255.255.255.0
63-
gateway: 192.168.1.1
64-
dns_nameservers:
65-
- 192.168.1.11
66-
- type: bond
67-
name: bond0
68-
bond_interfaces:
69-
- gbe0
70-
- gbe1
71-
mac_address: "52:54:00:12:34:00"
72-
params:
73-
bond-mode: active-backup
74-
bond-lacp-rate: false
75-
mtu: 1450
76-
subnets:
77-
- type: static
78-
address: 192.168.1.10
79-
netmask: 255.255.255.0
80-
dns_nameservers:
81-
- 192.168.1.11
82-
- type: vlan
83-
name: vlan0
84-
vlan_link: eth1
85-
vlan_id: 150
86-
mac_address: "52:54:00:12:34:00"
87-
mtu: 1450
88-
subnets:
89-
- type: static
90-
address: 192.168.1.10
91-
netmask: 255.255.255.0
92-
dns_nameservers:
93-
- 192.168.1.11
94-
- type: nameserver
95-
address:
96-
- 192.168.23.2
97-
- 8.8.8.8
98-
search: acme.local
52+
NOCLOUD_NETWORK_CONFIG_TEST_DATA_V1_LEGACY = """
53+
version: 1
54+
config:
55+
- type: physical
56+
name: interface0
57+
mac_address: "52:54:00:12:34:00"
58+
mtu: 1450
59+
subnets:
60+
- type: static
61+
address: 192.168.1.10
62+
netmask: 255.255.255.0
63+
gateway: 192.168.1.1
64+
dns_nameservers:
65+
- 192.168.1.11
66+
- type: bond
67+
name: bond0
68+
bond_interfaces:
69+
- gbe0
70+
- gbe1
71+
mac_address: "52:54:00:12:34:00"
72+
params:
73+
bond-mode: active-backup
74+
bond-lacp-rate: false
75+
mtu: 1450
76+
subnets:
77+
- type: static
78+
address: 192.168.1.10
79+
netmask: 255.255.255.0
80+
dns_nameservers:
81+
- 192.168.1.11
82+
- type: vlan
83+
name: vlan0
84+
vlan_link: eth1
85+
vlan_id: 150
86+
mac_address: "52:54:00:12:34:00"
87+
mtu: 1450
88+
subnets:
89+
- type: static
90+
address: 192.168.1.10
91+
netmask: 255.255.255.0
92+
dns_nameservers:
93+
- 192.168.1.11
94+
- type: nameserver
95+
address:
96+
- 192.168.23.2
97+
- 8.8.8.8
98+
search: acme.local
9999
"""
100+
NOCLOUD_NETWORK_CONFIG_TEST_DATA_V1 = """
101+
network:%s
102+
""" % (textwrap.indent(NOCLOUD_NETWORK_CONFIG_TEST_DATA_V1_LEGACY, " "))
100103
NOCLOUD_NETWORK_CONFIG_TEST_DATA_V2_EMPTY_CONFIG = """
101104
"""
102105
NOCLOUD_NETWORK_CONFIG_TEST_DATA_V2_CONFIG_IS_NOT_DICT = """
@@ -116,67 +119,69 @@
116119
eth0:
117120
- test
118121
"""
119-
NOCLOUD_NETWORK_CONFIG_TEST_DATA_V2 = """
120-
network:
121-
version: 2
122-
ethernets:
123-
interface0:
124-
match:
125-
macaddress: "52:54:00:12:34:00"
126-
set-name: "eth0"
122+
NOCLOUD_NETWORK_CONFIG_TEST_DATA_V2_LEGACY = """
123+
version: 2
124+
ethernets:
125+
interface0:
126+
match:
127+
macaddress: "52:54:00:12:34:00"
128+
set-name: "eth0"
129+
addresses:
130+
- 192.168.1.10/24
131+
gateway4: 192.168.1.1
132+
nameservers:
127133
addresses:
128-
- 192.168.1.10/24
129-
gateway4: 192.168.1.1
130-
nameservers:
131-
addresses:
132-
- 192.168.1.11
133-
- 192.168.1.12
134-
search:
135-
- acme.local
136-
mtu: 1450
137-
interface1:
138-
set-name: "interface1"
134+
- 192.168.1.11
135+
- 192.168.1.12
136+
search:
137+
- acme.local
138+
mtu: 1450
139+
interface1:
140+
set-name: "interface1"
141+
addresses:
142+
- 192.168.1.100/24
143+
gateway4: 192.168.1.1
144+
nameservers:
139145
addresses:
140-
- 192.168.1.100/24
141-
gateway4: 192.168.1.1
142-
nameservers:
143-
addresses:
144-
- 192.168.1.11
145-
- 192.168.1.12
146-
search:
147-
- acme.local
148-
bonds:
149-
bond0:
150-
interfaces: ["gbe0", "gbe1"]
151-
match:
152-
macaddress: "52:54:00:12:34:00"
153-
parameters:
154-
mode: active-backup
155-
lacp-rate: false
146+
- 192.168.1.11
147+
- 192.168.1.12
148+
search:
149+
- acme.local
150+
bonds:
151+
bond0:
152+
interfaces: ["gbe0", "gbe1"]
153+
match:
154+
macaddress: "52:54:00:12:34:00"
155+
parameters:
156+
mode: active-backup
157+
lacp-rate: false
158+
addresses:
159+
- 192.168.1.10/24
160+
nameservers:
156161
addresses:
157-
- 192.168.1.10/24
158-
nameservers:
159-
addresses:
160-
- 192.168.1.11
161-
mtu: 1450
162-
vlans:
163-
vlan0:
164-
id: 150
165-
link: eth1
166-
dhcp4: yes
167-
match:
168-
macaddress: "52:54:00:12:34:00"
162+
- 192.168.1.11
163+
mtu: 1450
164+
vlans:
165+
vlan0:
166+
id: 150
167+
link: eth1
168+
dhcp4: yes
169+
match:
170+
macaddress: "52:54:00:12:34:00"
171+
addresses:
172+
- 192.168.1.10/24
173+
nameservers:
169174
addresses:
170-
- 192.168.1.10/24
171-
nameservers:
172-
addresses:
173-
- 192.168.1.11
174-
mtu: 1450
175-
bridges:
176-
br0:
177-
interfaces: ['eth0']
178-
dhcp4: true
175+
- 192.168.1.11
176+
mtu: 1450
177+
bridges:
178+
br0:
179+
interfaces: ['eth0']
180+
dhcp4: true
179181
"""
182+
NOCLOUD_NETWORK_CONFIG_TEST_DATA_V2 = """
183+
network:%s
184+
""" % (textwrap.indent(NOCLOUD_NETWORK_CONFIG_TEST_DATA_V2_LEGACY, " "))
180185

181186

182187
@ddt.ddt
@@ -207,7 +212,12 @@ def test_parse_empty_result(self, input, expected_result):
207212
self.assertEqual(True, expected_result[0] in self.snatcher.output[0])
208213
self.assertEqual(result, expected_result[1])
209214

210-
def test_network_details_v2(self):
215+
@ddt.data(
216+
(NOCLOUD_NETWORK_CONFIG_TEST_DATA_V1, True),
217+
(NOCLOUD_NETWORK_CONFIG_TEST_DATA_V1_LEGACY, True)
218+
)
219+
@ddt.unpack
220+
def test_network_details_v2(self, test_data, expected_result):
211221
expected_bond = nm.Bond(
212222
members=["gbe0", "gbe1"],
213223
type=nm.BOND_TYPE_ACTIVE_BACKUP,
@@ -275,7 +285,7 @@ def test_network_details_v2(self):
275285
search='acme.local')
276286

277287
result = self._parser.parse(
278-
serialization.parse_json_yaml(NOCLOUD_NETWORK_CONFIG_TEST_DATA_V1))
288+
serialization.parse_json_yaml(test_data))
279289

280290
self.assertEqual(result.links[0], expected_link)
281291
self.assertEqual(result.networks[0], expected_network)
@@ -318,7 +328,12 @@ def test_parse_empty_result(self, input, expected_result):
318328
self.assertEqual(True, expected_result[0] in self.snatcher.output[0])
319329
self.assertEqual(result, expected_result[1])
320330

321-
def test_network_details_v2(self):
331+
@ddt.data(
332+
(NOCLOUD_NETWORK_CONFIG_TEST_DATA_V2, True),
333+
(NOCLOUD_NETWORK_CONFIG_TEST_DATA_V2_LEGACY, True)
334+
)
335+
@ddt.unpack
336+
def test_network_details_v2(self, test_data, expected_result):
322337
expected_bond = nm.Bond(
323338
members=["gbe0", "gbe1"],
324339
type=nm.BOND_TYPE_ACTIVE_BACKUP,
@@ -406,7 +421,7 @@ def test_network_details_v2(self):
406421
search='acme.local')
407422

408423
result = self._parser.parse(
409-
serialization.parse_json_yaml(NOCLOUD_NETWORK_CONFIG_TEST_DATA_V2))
424+
serialization.parse_json_yaml(test_data))
410425

411426
self.assertEqual(result.links[0], expected_link)
412427
self.assertEqual(result.links[1], expected_link_if1)

0 commit comments

Comments
 (0)