summaryrefslogtreecommitdiff
path: root/tests/unittests/test_distros/test_netconfig.py
diff options
context:
space:
mode:
Diffstat (limited to 'tests/unittests/test_distros/test_netconfig.py')
-rw-r--r--tests/unittests/test_distros/test_netconfig.py140
1 files changed, 139 insertions, 1 deletions
diff --git a/tests/unittests/test_distros/test_netconfig.py b/tests/unittests/test_distros/test_netconfig.py
index a1df066a..d09e46af 100644
--- a/tests/unittests/test_distros/test_netconfig.py
+++ b/tests/unittests/test_distros/test_netconfig.py
@@ -2,6 +2,7 @@
import copy
import os
+import re
from io import StringIO
from textwrap import dedent
from unittest import mock
@@ -14,7 +15,7 @@ from cloudinit.tests.helpers import (
FilesystemMockingTestCase, dir2dict)
from cloudinit import subp
from cloudinit import util
-
+from cloudinit import safeyaml
BASE_NET_CFG = '''
auto lo
@@ -88,6 +89,24 @@ V1_NET_CFG = {'config': [{'name': 'eth0',
'type': 'physical'}],
'version': 1}
+V1_NET_CFG_WITH_DUPS = """\
+# same value in interface specific dns and global dns
+# should produce single entry in network file
+version: 1
+config:
+ - type: physical
+ name: eth0
+ subnets:
+ - type: static
+ address: 192.168.0.102/24
+ dns_nameservers: [1.2.3.4]
+ dns_search: [test.com]
+ interface: eth0
+ - type: nameserver
+ address: [1.2.3.4]
+ search: [test.com]
+"""
+
V1_NET_CFG_OUTPUT = """\
# This file is generated from information provided by the datasource. Changes
# to it will not persist across an instance reboot. To disable cloud-init's
@@ -771,6 +790,125 @@ class TestNetCfgDistroArch(TestNetCfgDistroBase):
with_netplan=True)
+class TestNetCfgDistroPhoton(TestNetCfgDistroBase):
+
+ def setUp(self):
+ super(TestNetCfgDistroPhoton, self).setUp()
+ self.distro = self._get_distro('photon', renderers=['networkd'])
+
+ def create_conf_dict(self, contents):
+ content_dict = {}
+ for line in contents:
+ if line:
+ line = line.strip()
+ if line and re.search(r'^\[(.+)\]$', line):
+ content_dict[line] = []
+ key = line
+ elif line:
+ assert key
+ content_dict[key].append(line)
+
+ return content_dict
+
+ def compare_dicts(self, actual, expected):
+ for k, v in actual.items():
+ self.assertEqual(sorted(expected[k]), sorted(v))
+
+ def _apply_and_verify(self, apply_fn, config, expected_cfgs=None,
+ bringup=False):
+ if not expected_cfgs:
+ raise ValueError('expected_cfg must not be None')
+
+ tmpd = None
+ with mock.patch('cloudinit.net.networkd.available') as m_avail:
+ m_avail.return_value = True
+ with self.reRooted(tmpd) as tmpd:
+ apply_fn(config, bringup)
+
+ results = dir2dict(tmpd)
+ for cfgpath, expected in expected_cfgs.items():
+ actual = self.create_conf_dict(results[cfgpath].splitlines())
+ self.compare_dicts(actual, expected)
+ self.assertEqual(0o644, get_mode(cfgpath, tmpd))
+
+ def nwk_file_path(self, ifname):
+ return '/etc/systemd/network/10-cloud-init-%s.network' % ifname
+
+ def net_cfg_1(self, ifname):
+ ret = """\
+ [Match]
+ Name=%s
+ [Network]
+ DHCP=no
+ [Address]
+ Address=192.168.1.5/24
+ [Route]
+ Gateway=192.168.1.254""" % ifname
+ return ret
+
+ def net_cfg_2(self, ifname):
+ ret = """\
+ [Match]
+ Name=%s
+ [Network]
+ DHCP=ipv4""" % ifname
+ return ret
+
+ def test_photon_network_config_v1(self):
+ tmp = self.net_cfg_1('eth0').splitlines()
+ expected_eth0 = self.create_conf_dict(tmp)
+
+ tmp = self.net_cfg_2('eth1').splitlines()
+ expected_eth1 = self.create_conf_dict(tmp)
+
+ expected_cfgs = {
+ self.nwk_file_path('eth0'): expected_eth0,
+ self.nwk_file_path('eth1'): expected_eth1,
+ }
+
+ self._apply_and_verify(self.distro.apply_network_config,
+ V1_NET_CFG,
+ expected_cfgs.copy())
+
+ def test_photon_network_config_v2(self):
+ tmp = self.net_cfg_1('eth7').splitlines()
+ expected_eth7 = self.create_conf_dict(tmp)
+
+ tmp = self.net_cfg_2('eth9').splitlines()
+ expected_eth9 = self.create_conf_dict(tmp)
+
+ expected_cfgs = {
+ self.nwk_file_path('eth7'): expected_eth7,
+ self.nwk_file_path('eth9'): expected_eth9,
+ }
+
+ self._apply_and_verify(self.distro.apply_network_config,
+ V2_NET_CFG,
+ expected_cfgs.copy())
+
+ def test_photon_network_config_v1_with_duplicates(self):
+ expected = """\
+ [Match]
+ Name=eth0
+ [Network]
+ DHCP=no
+ DNS=1.2.3.4
+ Domains=test.com
+ [Address]
+ Address=192.168.0.102/24"""
+
+ net_cfg = safeyaml.load(V1_NET_CFG_WITH_DUPS)
+
+ expected = self.create_conf_dict(expected.splitlines())
+ expected_cfgs = {
+ self.nwk_file_path('eth0'): expected,
+ }
+
+ self._apply_and_verify(self.distro.apply_network_config,
+ net_cfg,
+ expected_cfgs.copy())
+
+
def get_mode(path, target=None):
return os.stat(subp.target_path(target, path)).st_mode & 0o777