virt.network_update: handle missing ipv4 netmask attribute

In the libvirt definition, the IPv4 netmask XML attribute may be
replaced by the prefix one. Handle this situation gracefully rather than
miserably failing.
This commit is contained in:
Cédric Bosdonnat 2021-03-03 17:07:13 +01:00 committed by Gareth J. Greenaway
parent feeb2f0823
commit a2d47c58b7
3 changed files with 11 additions and 5 deletions

1
changelog/59692.fixed Normal file
View file

@ -0,0 +1 @@
Don't fail updating network without netmask ip attribute

View file

@ -7584,7 +7584,7 @@ def network_update(
if node.get("family", "ipv4") == "ipv4"
]
for ip_node in ipv4_nodes:
netmask = ip_node.attrib.pop("netmask")
netmask = ip_node.attrib.pop("netmask", None)
if netmask:
address = ipaddress.ip_network(
"{}/{}".format(ip_node.get("address"), netmask), strict=False

View file

@ -366,8 +366,11 @@ def test_update_nat_nochange(make_mock_network):
define_mock.assert_not_called()
@pytest.mark.parametrize("test", [True, False])
def test_update_nat_change(make_mock_network, test):
@pytest.mark.parametrize(
"test, netmask",
[(True, "netmask='255.255.255.0'"), (True, "prefix='24'"), (False, "prefix='24'")],
)
def test_update_nat_change(make_mock_network, test, netmask):
"""
Test updating a NAT network with changes
"""
@ -380,13 +383,15 @@ def test_update_nat_change(make_mock_network, test):
<bridge name='virbr0' stp='on' delay='0'/>
<mac address='52:54:00:cd:49:6b'/>
<domain name='my.lab' localOnly='yes'/>
<ip address='192.168.122.1' netmask='255.255.255.0'>
<ip address='192.168.122.1' {}>
<dhcp>
<range start='192.168.122.2' end='192.168.122.254'/>
</dhcp>
</ip>
</network>
"""
""".format(
netmask
)
)
assert virt.network_update(
"default",