diff options
author | Ryan Harper <ryan.harper@canonical.com> | 2019-12-06 22:11:12 -0600 |
---|---|---|
committer | Chad Smith <chad.smith@canonical.com> | 2019-12-06 21:11:12 -0700 |
commit | 34ec440c1ad61c23c34b46f1798813d0f3ada952 (patch) | |
tree | 9253f6b5fadc74a90e390d2c7c8c56bc6cdced03 | |
parent | dac551be5cf048fd481316419a6aad46a2a11abb (diff) | |
download | vyos-cloud-init-34ec440c1ad61c23c34b46f1798813d0f3ada952.tar.gz vyos-cloud-init-34ec440c1ad61c23c34b46f1798813d0f3ada952.zip |
network_state: handle empty v1 config (#45)
Sending a valid but empty v1 network config resulted in a
stacktrace during execution. Update the network_state
parse path to specific check if the 'config' key is None
(not present) versus being present but explicitly empty.
Also add some network_state unittests.
LP: #1852496
-rw-r--r-- | cloudinit/net/network_state.py | 2 | ||||
-rw-r--r-- | cloudinit/net/tests/test_network_state.py | 47 |
2 files changed, 48 insertions, 1 deletions
diff --git a/cloudinit/net/network_state.py b/cloudinit/net/network_state.py index 7d206a1a..f3e8e250 100644 --- a/cloudinit/net/network_state.py +++ b/cloudinit/net/network_state.py @@ -73,7 +73,7 @@ def parse_net_config_data(net_config, skip_broken=True): # pass the whole net-config as-is config = net_config - if version and config: + if version and config is not None: nsi = NetworkStateInterpreter(version=version, config=config) nsi.parse_config(skip_broken=skip_broken) state = nsi.get_network_state() diff --git a/cloudinit/net/tests/test_network_state.py b/cloudinit/net/tests/test_network_state.py new file mode 100644 index 00000000..fcb4a995 --- /dev/null +++ b/cloudinit/net/tests/test_network_state.py @@ -0,0 +1,47 @@ +# This file is part of cloud-init. See LICENSE file for license information. + +import mock +from cloudinit.net import network_state +from cloudinit.tests.helpers import CiTestCase + +netstate_path = 'cloudinit.net.network_state' + + +class TestNetworkStateParseConfig(CiTestCase): + + def setUp(self): + super(TestNetworkStateParseConfig, self).setUp() + nsi_path = netstate_path + '.NetworkStateInterpreter' + self.add_patch(nsi_path, 'm_nsi') + + def test_missing_version_returns_none(self): + ncfg = {} + self.assertEqual(None, network_state.parse_net_config_data(ncfg)) + + def test_unknown_versions_returns_none(self): + ncfg = {'version': 13.2} + self.assertEqual(None, network_state.parse_net_config_data(ncfg)) + + def test_version_2_passes_self_as_config(self): + ncfg = {'version': 2, 'otherconfig': {}, 'somemore': [1, 2, 3]} + network_state.parse_net_config_data(ncfg) + self.assertEqual([mock.call(version=2, config=ncfg)], + self.m_nsi.call_args_list) + + def test_valid_config_gets_network_state(self): + ncfg = {'version': 2, 'otherconfig': {}, 'somemore': [1, 2, 3]} + result = network_state.parse_net_config_data(ncfg) + self.assertNotEqual(None, result) + + def test_empty_v1_config_gets_network_state(self): + ncfg = {'version': 1, 'config': []} + result = network_state.parse_net_config_data(ncfg) + self.assertNotEqual(None, result) + + def test_empty_v2_config_gets_network_state(self): + ncfg = {'version': 2} + result = network_state.parse_net_config_data(ncfg) + self.assertNotEqual(None, result) + + +# vi: ts=4 expandtab |