Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions cloudbaseinit/tests/utils/test_debiface.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,31 @@
from cloudbaseinit.tests import testutils
from cloudbaseinit.utils import debiface

# As written by Proxmox VE (PVE::QemuServer::Cloudinit::configdrive2_network)
# for "ipconfig0: ip=...,gw=...,ip6=...,gw6=..." and "ipconfig1: ip=dhcp,
# ip6=...": the IPv6 settings of an interface get their own inet6 stanza.
PROXMOX_DUAL_STACK = """\
auto lo
iface lo inet loopback

dns_nameservers 192.0.2.53 2001:db8::53
dns_search example.com
auto eth0
iface eth0 inet static
address 192.0.2.10
netmask 255.255.255.0
gateway 192.0.2.1
iface eth0 inet6 static
address 2001:db8:10::10
netmask 64
gateway 2001:db8:10::1
auto eth1
iface eth1 inet dhcp
iface eth1 inet6 static
address 2001:db8:11::10
netmask 64
"""


class TestInterfacesParser(unittest.TestCase):

Expand Down Expand Up @@ -85,3 +110,20 @@ def test_nothing_to_parse(self):

def test_parse(self):
self._test_parse_nics()

def test_parse_inet6_stanza_same_iface(self):
nics = debiface.parse(PROXMOX_DUAL_STACK)

fields = ("name", "mac", "address", "netmask", "gateway",
"address6", "netmask6", "gateway6")
found = [tuple(getattr(nic, field) for field in fields)
for nic in nics]
self.assertEqual([
# The inet6 stanza of eth0 is the IPv6 half of eth0, not a
# second eth0 which would replace the IPv4 settings.
("eth0", None, "192.0.2.10", "255.255.255.0", "192.0.2.1",
"2001:db8:10::10", "64", "2001:db8:10::1"),
# No static IPv4 to merge into: unchanged behaviour.
("eth1", None, "2001:db8:11::10", "64", None,
None, None, None),
], found)
26 changes: 20 additions & 6 deletions cloudbaseinit/utils/debiface.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,24 +67,38 @@
MAC: lambda value: value.upper(),
DNSNS: lambda value: value.strip().split()
}
IFACE_STANZA = re.compile(r"iface\s+(?P<name>\S+)\s+(?P<family>inet6?)"
r"\s+(?P<method>\S+)")


def _get_iface_blocks(data):
""""Yield interface blocks as pairs of v4 and v6 halves."""
lines, lines6 = [], []
crt_lines = lines
static_name = None # name of the current static IPv4 stanza
for line in data.splitlines():
line = line.strip()
if not line or line.startswith("#"):
continue
if "iface" in line:
if "inet6" in line:
stanza = IFACE_STANZA.match(line)
if (stanza and static_name and
stanza.group("family") == "inet6" and
stanza.group("name") == static_name):
# IPv6 stanza of an interface that already has a static
# IPv4 one (e.g. Proxmox VE): it is the v6 half of the
# same NIC, not a new NIC.
crt_lines = lines6
if lines:
yield lines, lines6
lines[:] = []
lines6[:] = []
crt_lines = lines
else:
if lines:
yield lines, lines6
lines[:] = []
lines6[:] = []
crt_lines = lines
static_name = None
if (stanza and stanza.group("family", "method") ==
("inet", "static")):
static_name = stanza.group("name")
crt_lines.append(line)
if lines:
yield lines, lines6
Expand Down