From 200e811268b7c17e814cfb6b7ef3f603c0590abc Mon Sep 17 00:00:00 2001 From: Sergii Golovatiuk Date: Thu, 14 Jul 2016 16:58:56 +0200 Subject: Change StringIO to BytesIO in cc_mcollective.py * StringIO from six doesn't act as 'binary stream' in Python 3. This patch changes StringIO to BytesIO to have code compatible with Python 3 and Python 2. * Add try/except for IOError in case when server.cfg doesn't exists. This is necessary for unit tests or cases when server.cfg is not included to package * Add UnitTest for cc_mcollective.py LP: #1597699 --- .../test_handler/test_handler_mcollective.py | 60 ++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 tests/unittests/test_handler/test_handler_mcollective.py (limited to 'tests') diff --git a/tests/unittests/test_handler/test_handler_mcollective.py b/tests/unittests/test_handler/test_handler_mcollective.py new file mode 100644 index 00000000..0e3fcc8c --- /dev/null +++ b/tests/unittests/test_handler/test_handler_mcollective.py @@ -0,0 +1,60 @@ +from cloudinit.config import cc_mcollective +from cloudinit import util + +from .. import helpers + +import configobj +import logging +import shutil +from six import BytesIO +import tempfile + +LOG = logging.getLogger(__name__) + + +class TestConfig(helpers.FilesystemMockingTestCase): + def setUp(self): + super(TestConfig, self).setUp() + self.tmp = tempfile.mkdtemp() + self.addCleanup(shutil.rmtree, self.tmp) + + def test_basic_config(self): + cfg = { + 'mcollective': { + 'conf': { + 'loglevel': 'debug', + 'connector': 'rabbitmq', + 'logfile': '/var/log/mcollective.log', + 'ttl': '4294957', + 'collectives': 'mcollective', + 'main_collective': 'mcollective', + 'securityprovider': 'psk', + 'daemonize': '1', + 'factsource': 'yaml', + 'direct_addressing': '1', + 'plugin.psk': 'unset', + 'libdir': '/usr/share/mcollective/plugins', + 'identity': '1', + }, + }, + } + self.patchUtils(self.tmp) + cc_mcollective.configure(cfg['mcollective']['conf']) + contents = util.load_file("/etc/mcollective/server.cfg", decode=False) + contents = configobj.ConfigObj(BytesIO(contents)) + expected = { + 'loglevel': 'debug', + 'connector': 'rabbitmq', + 'logfile': '/var/log/mcollective.log', + 'ttl': '4294957', + 'collectives': 'mcollective', + 'main_collective': 'mcollective', + 'securityprovider': 'psk', + 'daemonize': '1', + 'factsource': 'yaml', + 'direct_addressing': '1', + 'plugin.psk': 'unset', + 'libdir': '/usr/share/mcollective/plugins', + 'identity': '1', + } + self.assertEqual(expected, dict(contents)) -- cgit v1.2.3 From 74dcabe8853e063b63be578dff7926f62bd26df8 Mon Sep 17 00:00:00 2001 From: Scott Moser Date: Thu, 14 Jul 2016 11:28:46 -0400 Subject: fix tox flake8 --- cloudinit/config/cc_mcollective.py | 2 +- .../test_handler/test_handler_mcollective.py | 28 +++++++++++----------- 2 files changed, 15 insertions(+), 15 deletions(-) (limited to 'tests') diff --git a/cloudinit/config/cc_mcollective.py b/cloudinit/config/cc_mcollective.py index 47320d5d..0c84d600 100644 --- a/cloudinit/config/cc_mcollective.py +++ b/cloudinit/config/cc_mcollective.py @@ -26,8 +26,8 @@ from six import BytesIO # and doesn't need a top level section from configobj import ConfigObj -from cloudinit import util from cloudinit import log as logging +from cloudinit import util PUBCERT_FILE = "/etc/mcollective/ssl/server-public.pem" PRICERT_FILE = "/etc/mcollective/ssl/server-private.pem" diff --git a/tests/unittests/test_handler/test_handler_mcollective.py b/tests/unittests/test_handler/test_handler_mcollective.py index 0e3fcc8c..f9448d80 100644 --- a/tests/unittests/test_handler/test_handler_mcollective.py +++ b/tests/unittests/test_handler/test_handler_mcollective.py @@ -43,18 +43,18 @@ class TestConfig(helpers.FilesystemMockingTestCase): contents = util.load_file("/etc/mcollective/server.cfg", decode=False) contents = configobj.ConfigObj(BytesIO(contents)) expected = { - 'loglevel': 'debug', - 'connector': 'rabbitmq', - 'logfile': '/var/log/mcollective.log', - 'ttl': '4294957', - 'collectives': 'mcollective', - 'main_collective': 'mcollective', - 'securityprovider': 'psk', - 'daemonize': '1', - 'factsource': 'yaml', - 'direct_addressing': '1', - 'plugin.psk': 'unset', - 'libdir': '/usr/share/mcollective/plugins', - 'identity': '1', - } + 'loglevel': 'debug', + 'connector': 'rabbitmq', + 'logfile': '/var/log/mcollective.log', + 'ttl': '4294957', + 'collectives': 'mcollective', + 'main_collective': 'mcollective', + 'securityprovider': 'psk', + 'daemonize': '1', + 'factsource': 'yaml', + 'direct_addressing': '1', + 'plugin.psk': 'unset', + 'libdir': '/usr/share/mcollective/plugins', + 'identity': '1', + } self.assertEqual(expected, dict(contents)) -- cgit v1.2.3 From 81fce7afe33eaffe66c1be87ea84f0e5580f164a Mon Sep 17 00:00:00 2001 From: Scott Moser Date: Thu, 14 Jul 2016 14:13:32 -0400 Subject: add test of apply_network fallback path. we could do this more simply by mocking fbsd.apply_network and checking it's inputs. but this pushes it through the whole path that the other test does. --- tests/unittests/test_distros/test_netconfig.py | 61 ++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) (limited to 'tests') diff --git a/tests/unittests/test_distros/test_netconfig.py b/tests/unittests/test_distros/test_netconfig.py index 9172e3aa..e3168a4a 100644 --- a/tests/unittests/test_distros/test_netconfig.py +++ b/tests/unittests/test_distros/test_netconfig.py @@ -319,3 +319,64 @@ defaultrouter="192.168.1.254" ''' self.assertCfgEquals(expected_buf, str(write_buf)) self.assertEqual(write_buf.mode, 0o644) + + def test_apply_network_config_fallback(self): + fbsd_distro = self._get_distro('freebsd') + + # a weak attempt to verify that we don't have an implementation + # of _write_network_config or apply_network_config in fbsd now, + # which would make this test not actually test the fallback. + self.assertRaises( + NotImplementedError, fbsd_distro._write_network_config, + BASE_NET_CFG) + + # now run + mynetcfg = { + 'config': [{"type": "physical", "name": "eth0", + "mac_address": "c0:d6:9f:2c:e8:80", + "subnets": [{"type": "dhcp"}]}], + 'version': 1} + + write_bufs = {} + read_bufs = { + '/etc/rc.conf': '', + '/etc/resolv.conf': '', + } + + def replace_write(filename, content, mode=0o644, omode="wb"): + buf = WriteBuffer() + buf.mode = mode + buf.omode = omode + buf.write(content) + write_bufs[filename] = buf + + def replace_read(fname, read_cb=None, quiet=False): + if fname not in read_bufs: + if fname in write_bufs: + return str(write_bufs[fname]) + raise IOError("%s not found" % fname) + else: + if fname in write_bufs: + return str(write_bufs[fname]) + return read_bufs[fname] + + with ExitStack() as mocks: + mocks.enter_context( + mock.patch.object(util, 'subp', return_value=('vtnet0', ''))) + mocks.enter_context( + mock.patch.object(os.path, 'exists', return_value=False)) + mocks.enter_context( + mock.patch.object(util, 'write_file', replace_write)) + mocks.enter_context( + mock.patch.object(util, 'load_file', replace_read)) + + fbsd_distro.apply_network_config(mynetcfg, bring_up=False) + + self.assertIn('/etc/rc.conf', write_bufs) + write_buf = write_bufs['/etc/rc.conf'] + expected_buf = ''' +ifconfig_vtnet0="DHCP" +''' + self.assertCfgEquals(expected_buf, str(write_buf)) + self.assertEqual(write_buf.mode, 0o644) + -- cgit v1.2.3 From 9a0189ccda40e366517c0690e0ea2833e533bf1a Mon Sep 17 00:00:00 2001 From: Scott Moser Date: Thu, 14 Jul 2016 14:17:52 -0400 Subject: flake8 --- tests/unittests/test_distros/test_netconfig.py | 1 - 1 file changed, 1 deletion(-) (limited to 'tests') diff --git a/tests/unittests/test_distros/test_netconfig.py b/tests/unittests/test_distros/test_netconfig.py index e3168a4a..36eae2dc 100644 --- a/tests/unittests/test_distros/test_netconfig.py +++ b/tests/unittests/test_distros/test_netconfig.py @@ -379,4 +379,3 @@ ifconfig_vtnet0="DHCP" ''' self.assertCfgEquals(expected_buf, str(write_buf)) self.assertEqual(write_buf.mode, 0o644) - -- cgit v1.2.3