diff options
-rw-r--r-- | cloudinit/net/__init__.py | 11 | ||||
-rw-r--r-- | cloudinit/net/network_state.py | 2 | ||||
-rw-r--r-- | tests/unittests/test_net.py | 68 |
3 files changed, 74 insertions, 7 deletions
diff --git a/cloudinit/net/__init__.py b/cloudinit/net/__init__.py index e911ed0c..0202cbd8 100644 --- a/cloudinit/net/__init__.py +++ b/cloudinit/net/__init__.py @@ -107,7 +107,7 @@ class ParserError(Exception): """Raised when parser has issue parsing the interfaces file.""" -def parse_net_config_data(net_config): +def parse_net_config_data(net_config, skip_broken=True): """Parses the config, returns NetworkState object :param net_config: curtin network config dict @@ -116,20 +116,19 @@ def parse_net_config_data(net_config): if 'version' in net_config and 'config' in net_config: ns = network_state.NetworkState(version=net_config.get('version'), config=net_config.get('config')) - ns.parse_config() + ns.parse_config(skip_broken=skip_broken) state = ns.network_state - return state -def parse_net_config(path): +def parse_net_config(path, skip_broken=True): """Parses a curtin network configuration file and return network state""" ns = None net_config = util.read_conf(path) if 'network' in net_config: - ns = parse_net_config_data(net_config.get('network')) - + ns = parse_net_config_data(net_config.get('network'), + skip_broken=skip_broken) return ns diff --git a/cloudinit/net/network_state.py b/cloudinit/net/network_state.py index 2530a601..2feffa71 100644 --- a/cloudinit/net/network_state.py +++ b/cloudinit/net/network_state.py @@ -142,7 +142,7 @@ class NetworkState(object): raise RuntimeError("No handler found for" " command '%s'" % command_type) try: - handler(command) + handler(self, command) except InvalidCommand: if not skip_broken: raise diff --git a/tests/unittests/test_net.py b/tests/unittests/test_net.py index 6daf9601..37b48efb 100644 --- a/tests/unittests/test_net.py +++ b/tests/unittests/test_net.py @@ -1,7 +1,9 @@ from cloudinit import util from cloudinit import net from cloudinit.net import cmdline +from cloudinit.net import eni from .helpers import TestCase +from .helpers import mock import base64 import copy @@ -9,6 +11,8 @@ import io import gzip import json import os +import shutil +import tempfile DHCP_CONTENT_1 = """ DEVICE='eth0' @@ -69,6 +73,70 @@ STATIC_EXPECTED_1 = { } +class TestEniNetRendering(TestCase): + + @mock.patch("cloudinit.net.sys_dev_path") + @mock.patch("cloudinit.net.sys_netdev_info") + @mock.patch("cloudinit.net.get_devicelist") + def test_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", + } + } + + def netdev_info(name, field): + return dev_characteristics[name][field] + + mock_sys_netdev_info.side_effect = netdev_info + + tmp_dir = tempfile.mkdtemp() + self.addCleanup(shutil.rmtree, tmp_dir) + + 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 + + network_cfg = net.generate_fallback_config() + network_state = net.parse_net_config_data(network_cfg, + skip_broken=False) + + render_dir = os.path.join(tmp_dir, "render") + os.makedirs(render_dir) + + renderer = eni.Renderer() + renderer.render_network_state(render_dir, network_state, + eni="interfaces", + links_prefix=None, + netrules=None) + + self.assertTrue(os.path.exists(os.path.join(render_dir, + 'interfaces'))) + with open(os.path.join(render_dir, 'interfaces')) as fh: + contents = fh.read() + + expected = """auto lo +iface lo inet loopback + +auto eth1000 +iface eth1000 inet dhcp +""" + self.assertEqual(expected, contents) + + class TestNetConfigParsing(TestCase): simple_cfg = { 'config': [{"type": "physical", "name": "eth0", |