summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoshua Harlow <harlowja@gmail.com>2016-06-10 15:02:21 -0700
committerJoshua Harlow <harlowja@gmail.com>2016-06-10 15:02:21 -0700
commitcfcac2a70bc4dd9b47358261750ff535e78f4d5f (patch)
treea5da210c1a19797d47928fb79fe6b35ed3494be9
parente5d4ee779981a0e572dc0ca4519b5bf770e3bcaa (diff)
downloadvyos-cloud-init-cfcac2a70bc4dd9b47358261750ff535e78f4d5f.tar.gz
vyos-cloud-init-cfcac2a70bc4dd9b47358261750ff535e78f4d5f.zip
Add a sysconfig rendering test
-rw-r--r--cloudinit/net/sysconfig.py9
-rw-r--r--tests/unittests/test_net.py93
2 files changed, 74 insertions, 28 deletions
diff --git a/cloudinit/net/sysconfig.py b/cloudinit/net/sysconfig.py
index ff7e467f..01770213 100644
--- a/cloudinit/net/sysconfig.py
+++ b/cloudinit/net/sysconfig.py
@@ -102,7 +102,7 @@ class ConfigMap(object):
return len(self._conf)
def to_string(self):
- buf = compat.StringIO()
+ buf = six.StringIO()
buf.write(self.default_header)
if self._conf:
buf.write("\n")
@@ -110,7 +110,7 @@ class ConfigMap(object):
value = self._conf[key]
if isinstance(value, bool):
value = self._bool_map[value]
- if not isinstance(value, compat.string_types):
+ if not isinstance(value, six.string_types):
value = str(value)
buf.write("%s=%s\n" % (key, _quote_value(value)))
return buf.getvalue()
@@ -229,7 +229,7 @@ class Renderer(object):
"""Given state, emit udev rules to map mac to ifname."""
# TODO(harlowja): this seems shared between eni renderer and
# this, so move it to a shared location.
- content = compat.StringIO()
+ content = six.StringIO()
for iface in network_state.iter_interfaces(_filter_by_physical):
# for physical interfaces write out a persist net udev rule
if 'name' in iface and iface.get('mac_address'):
@@ -253,7 +253,7 @@ class Renderer(object):
iface_cfg['DHCPV6C'] = True
iface_cfg['IPV6INIT'] = True
iface_cfg['BOOTPROTO'] = 'dhcp'
- elif subnet_type == 'dhcp4':
+ elif subnet_type in ['dhcp4', 'dhcp']:
iface_cfg['BOOTPROTO'] = 'dhcp'
elif subnet_type == 'static':
iface_cfg['BOOTPROTO'] = 'static'
@@ -414,6 +414,7 @@ class Renderer(object):
self, target, network_state, sysconf_dir="etc/sysconfig/",
netrules='etc/udev/rules.d/70-persistent-net.rules',
dns='etc/resolv.conf'):
+ network_state = NetworkStateHelper(network_state)
if target:
base_sysconf_dir = os.path.join(target, sysconf_dir)
else:
diff --git a/tests/unittests/test_net.py b/tests/unittests/test_net.py
index 7998111a..e899c749 100644
--- a/tests/unittests/test_net.py
+++ b/tests/unittests/test_net.py
@@ -2,6 +2,7 @@ from cloudinit import net
from cloudinit.net import cmdline
from cloudinit.net import eni
from cloudinit.net import network_state
+from cloudinit.net import sysconfig
from cloudinit import util
from .helpers import mock
@@ -75,7 +76,36 @@ STATIC_EXPECTED_1 = {
}
-class TestEniNetRendering(TestCase):
+def _setup_test(tmp_dir, mock_get_devicelist, mock_sys_netdev_info,
+ 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 netdev_info(name, field):
+ return dev_characteristics[name][field]
+
+ mock_sys_netdev_info.side_effect = netdev_info
+
+ 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(TestCase):
@mock.patch("cloudinit.net.sys_dev_path")
@mock.patch("cloudinit.net.sys_netdev_info")
@@ -83,35 +113,50 @@ class TestEniNetRendering(TestCase):
def test_default_generation(self, mock_get_devicelist,
mock_sys_netdev_info,
mock_sys_dev_path):
- mock_get_devicelist.return_value = ['eth1000', 'lo']
-
- dev_characteristics = {
- 'eth1000': {
- "bridge": False,
- "carrier": False,
- "dormant": False,
- "operstate": "down",
- "address": "07-1C-C6-75-A4-BE",
- }
- }
+ tmp_dir = tempfile.mkdtemp()
+ self.addCleanup(shutil.rmtree, tmp_dir)
+ _setup_test(tmp_dir, mock_get_devicelist,
+ mock_sys_netdev_info, mock_sys_dev_path)
- def netdev_info(name, field):
- return dev_characteristics[name][field]
+ network_cfg = net.generate_fallback_config()
+ ns = network_state.parse_net_config_data(network_cfg,
+ skip_broken=False)
- mock_sys_netdev_info.side_effect = netdev_info
+ render_dir = os.path.join(tmp_dir, "render")
+ os.makedirs(render_dir)
- tmp_dir = tempfile.mkdtemp()
- self.addCleanup(shutil.rmtree, tmp_dir)
+ renderer = sysconfig.Renderer()
+ renderer.render_network_state(render_dir, ns)
+
+ 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 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")
+class TestEniNetRendering(TestCase):
- mock_sys_dev_path.side_effect = sys_dev_path
+ @mock.patch("cloudinit.net.sys_dev_path")
+ @mock.patch("cloudinit.net.sys_netdev_info")
+ @mock.patch("cloudinit.net.get_devicelist")
+ def test_default_generation(self, mock_get_devicelist,
+ mock_sys_netdev_info,
+ mock_sys_dev_path):
+ tmp_dir = tempfile.mkdtemp()
+ self.addCleanup(shutil.rmtree, tmp_dir)
+ _setup_test(tmp_dir, mock_get_devicelist,
+ mock_sys_netdev_info, mock_sys_dev_path)
network_cfg = net.generate_fallback_config()
ns = network_state.parse_net_config_data(network_cfg,