summaryrefslogtreecommitdiff
path: root/tests
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 /tests
parente5d4ee779981a0e572dc0ca4519b5bf770e3bcaa (diff)
downloadvyos-cloud-init-cfcac2a70bc4dd9b47358261750ff535e78f4d5f.tar.gz
vyos-cloud-init-cfcac2a70bc4dd9b47358261750ff535e78f4d5f.zip
Add a sysconfig rendering test
Diffstat (limited to 'tests')
-rw-r--r--tests/unittests/test_net.py93
1 files changed, 69 insertions, 24 deletions
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,