summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorScott Moser <smoser@brickies.net>2017-03-15 14:33:45 -0400
committerScott Moser <smoser@brickies.net>2017-03-17 21:31:56 -0400
commit5beecdf88b630a397b3722ddb299e9a37ff02737 (patch)
tree620b47c50cd392789bcd8afd3dc355bcbbf751bc /tests
parent58cc8f7521725d4f007ce90001a28326bc240231 (diff)
downloadvyos-cloud-init-5beecdf88b630a397b3722ddb299e9a37ff02737.tar.gz
vyos-cloud-init-5beecdf88b630a397b3722ddb299e9a37ff02737.zip
net: add renderers for automatically selecting the renderer.
Previously, the distro had hard coded which network renderer it would use. This adds support for just picking the right renderer based on what is available. Now, that can be set via a priority in system_info, but should generally work. That config looks like: system_info: network: renderers: ["eni", "sysconfig"] When no renderers are found, a specific RendererNotFoundError is raised. stages.py is modified to catch that and log it at error level. This path should not really be exercised, but could occur if for example an Ubuntu system did not have ifupdown, or a rhel system did not have sysconfig. In such a system previously we would have quietly rendered ENI configuration but that would have been ignored. This is one step better in that we at least log the error.
Diffstat (limited to 'tests')
-rw-r--r--tests/unittests/test_net.py45
1 files changed, 45 insertions, 0 deletions
diff --git a/tests/unittests/test_net.py b/tests/unittests/test_net.py
index dca44b37..902204a0 100644
--- a/tests/unittests/test_net.py
+++ b/tests/unittests/test_net.py
@@ -4,6 +4,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 renderers
from cloudinit.net import sysconfig
from cloudinit.sources.helpers import openstack
from cloudinit import util
@@ -1050,6 +1051,50 @@ class TestEniRoundTrip(CiTestCase):
expected, [line for line in found if line])
+class TestNetRenderers(CiTestCase):
+ @mock.patch("cloudinit.net.renderers.sysconfig.available")
+ @mock.patch("cloudinit.net.renderers.eni.available")
+ def test_eni_and_sysconfig_available(self, m_eni_avail, m_sysc_avail):
+ m_eni_avail.return_value = True
+ m_sysc_avail.return_value = True
+ found = renderers.search(priority=['sysconfig', 'eni'], first=False)
+ names = [f[0] for f in found]
+ self.assertEqual(['sysconfig', 'eni'], names)
+
+ @mock.patch("cloudinit.net.renderers.eni.available")
+ def test_search_returns_empty_on_none(self, m_eni_avail):
+ m_eni_avail.return_value = False
+ found = renderers.search(priority=['eni'], first=False)
+ self.assertEqual([], found)
+
+ @mock.patch("cloudinit.net.renderers.sysconfig.available")
+ @mock.patch("cloudinit.net.renderers.eni.available")
+ def test_first_in_priority(self, m_eni_avail, m_sysc_avail):
+ # available should only be called until one is found.
+ m_eni_avail.return_value = True
+ m_sysc_avail.side_effect = Exception("Should not call me")
+ found = renderers.search(priority=['eni', 'sysconfig'], first=True)
+ self.assertEqual(['eni'], [found[0]])
+
+ @mock.patch("cloudinit.net.renderers.sysconfig.available")
+ @mock.patch("cloudinit.net.renderers.eni.available")
+ def test_select_positive(self, m_eni_avail, m_sysc_avail):
+ m_eni_avail.return_value = True
+ m_sysc_avail.return_value = False
+ found = renderers.select(priority=['sysconfig', 'eni'])
+ self.assertEqual('eni', found[0])
+
+ @mock.patch("cloudinit.net.renderers.sysconfig.available")
+ @mock.patch("cloudinit.net.renderers.eni.available")
+ def test_select_none_found_raises(self, m_eni_avail, m_sysc_avail):
+ # if select finds nothing, should raise exception.
+ m_eni_avail.return_value = False
+ m_sysc_avail.return_value = False
+
+ self.assertRaises(net.RendererNotFoundError, renderers.select,
+ priority=['sysconfig', 'eni'])
+
+
def _gzip_data(data):
with io.BytesIO() as iobuf:
gzfp = gzip.GzipFile(mode="wb", fileobj=iobuf)