summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorBarry Warsaw <barry@python.org>2015-01-22 19:53:04 -0500
committerBarry Warsaw <barry@python.org>2015-01-22 19:53:04 -0500
commit8329ad5bad5d6b0fcb8f4736fb308914ca3600c7 (patch)
tree0add38939898c14cea23596c3fc15225e188b888 /tests
parent508670bdaf9545b6bcc8e2009e8bd3f08d6f8796 (diff)
downloadvyos-cloud-init-8329ad5bad5d6b0fcb8f4736fb308914ca3600c7.tar.gz
vyos-cloud-init-8329ad5bad5d6b0fcb8f4736fb308914ca3600c7.zip
More conversions from mocker to mock.
Diffstat (limited to 'tests')
-rw-r--r--tests/unittests/test_datasource/test_nocloud.py52
-rw-r--r--tests/unittests/test_datasource/test_opennebula.py14
-rw-r--r--tests/unittests/test_distros/test_generic.py1
-rw-r--r--tests/unittests/test_distros/test_hostname.py4
-rw-r--r--tests/unittests/test_distros/test_hosts.py4
-rw-r--r--tests/unittests/test_distros/test_netconfig.py265
6 files changed, 151 insertions, 189 deletions
diff --git a/tests/unittests/test_datasource/test_nocloud.py b/tests/unittests/test_datasource/test_nocloud.py
index ae9e6c22..480a4012 100644
--- a/tests/unittests/test_datasource/test_nocloud.py
+++ b/tests/unittests/test_datasource/test_nocloud.py
@@ -3,33 +3,38 @@ from cloudinit.sources import DataSourceNoCloud
from cloudinit import util
from ..helpers import populate_dir
-from mocker import MockerTestCase
import os
import yaml
+import shutil
+import tempfile
+import unittest
+try:
+ from unittest import mock
+except ImportError:
+ import mock
+try:
+ from contextlib import ExitStack
+except ImportError:
+ from contextlib2 import ExitStack
-class TestNoCloudDataSource(MockerTestCase):
+
+class TestNoCloudDataSource(unittest.TestCase):
def setUp(self):
- self.tmp = self.makeDir()
+ self.tmp = tempfile.mkdtemp()
+ self.addCleanup(shutil.rmtree, self.tmp)
self.paths = helpers.Paths({'cloud_dir': self.tmp})
self.cmdline = "root=TESTCMDLINE"
- self.unapply = []
- self.apply_patches([(util, 'get_cmdline', self._getcmdline)])
- super(TestNoCloudDataSource, self).setUp()
-
- def tearDown(self):
- apply_patches([i for i in reversed(self.unapply)])
- super(TestNoCloudDataSource, self).tearDown()
+ self.mocks = ExitStack()
+ self.addCleanup(self.mocks.close)
- def apply_patches(self, patches):
- ret = apply_patches(patches)
- self.unapply += ret
+ self.mocks.enter_context(
+ mock.patch.object(util, 'get_cmdline', return_value=self.cmdline))
- def _getcmdline(self):
- return self.cmdline
+ super(TestNoCloudDataSource, self).setUp()
def test_nocloud_seed_dir(self):
md = {'instance-id': 'IID', 'dsmode': 'local'}
@@ -59,7 +64,9 @@ class TestNoCloudDataSource(MockerTestCase):
def my_find_devs_with(*args, **kwargs):
raise PsuedoException
- self.apply_patches([(util, 'find_devs_with', my_find_devs_with)])
+ self.mocks.enter_context(
+ mock.patch.object(util, 'find_devs_with',
+ side_effect=PsuedoException))
# by default, NoCloud should search for filesystems by label
sys_cfg = {'datasource': {'NoCloud': {}}}
@@ -133,7 +140,7 @@ class TestNoCloudDataSource(MockerTestCase):
self.assertTrue(ret)
-class TestParseCommandLineData(MockerTestCase):
+class TestParseCommandLineData(unittest.TestCase):
def test_parse_cmdline_data_valid(self):
ds_id = "ds=nocloud"
@@ -178,15 +185,4 @@ class TestParseCommandLineData(MockerTestCase):
self.assertFalse(ret)
-def apply_patches(patches):
- ret = []
- for (ref, name, replace) in patches:
- if replace is None:
- continue
- orig = getattr(ref, name)
- setattr(ref, name, replace)
- ret.append((ref, name, orig))
- return ret
-
-
# vi: ts=4 expandtab
diff --git a/tests/unittests/test_datasource/test_opennebula.py b/tests/unittests/test_datasource/test_opennebula.py
index b4fd1f4d..ddf77265 100644
--- a/tests/unittests/test_datasource/test_opennebula.py
+++ b/tests/unittests/test_datasource/test_opennebula.py
@@ -1,12 +1,15 @@
from cloudinit import helpers
from cloudinit.sources import DataSourceOpenNebula as ds
from cloudinit import util
-from mocker import MockerTestCase
from ..helpers import populate_dir
from base64 import b64encode
import os
import pwd
+import shutil
+import tempfile
+import unittest
+
TEST_VARS = {
'VAR1': 'single',
@@ -37,12 +40,13 @@ CMD_IP_OUT = '''\
'''
-class TestOpenNebulaDataSource(MockerTestCase):
+class TestOpenNebulaDataSource(unittest.TestCase):
parsed_user = None
def setUp(self):
super(TestOpenNebulaDataSource, self).setUp()
- self.tmp = self.makeDir()
+ self.tmp = tempfile.mkdtemp()
+ self.addCleanup(shutil.rmtree, self.tmp)
self.paths = helpers.Paths({'cloud_dir': self.tmp})
# defaults for few tests
@@ -228,7 +232,7 @@ class TestOpenNebulaDataSource(MockerTestCase):
util.find_devs_with = orig_find_devs_with
-class TestOpenNebulaNetwork(MockerTestCase):
+class TestOpenNebulaNetwork(unittest.TestCase):
def setUp(self):
super(TestOpenNebulaNetwork, self).setUp()
@@ -280,7 +284,7 @@ iface eth0 inet static
''')
-class TestParseShellConfig(MockerTestCase):
+class TestParseShellConfig(unittest.TestCase):
def test_no_seconds(self):
cfg = '\n'.join(["foo=bar", "SECONDS=2", "xx=foo"])
# we could test 'sleep 2', but that would make the test run slower.
diff --git a/tests/unittests/test_distros/test_generic.py b/tests/unittests/test_distros/test_generic.py
index 2c85cbdb..35153f0d 100644
--- a/tests/unittests/test_distros/test_generic.py
+++ b/tests/unittests/test_distros/test_generic.py
@@ -67,7 +67,6 @@ class TestGenericDistro(helpers.FilesystemMockingTestCase):
self.patchUtils(self.tmp)
d.write_sudo_rules("harlowja", rules)
contents = util.load_file(d.ci_sudoers_fn)
- self.restore()
return contents
def _count_in(self, lines_look_for, text_content):
diff --git a/tests/unittests/test_distros/test_hostname.py b/tests/unittests/test_distros/test_hostname.py
index 8e644f4d..143e29a9 100644
--- a/tests/unittests/test_distros/test_hostname.py
+++ b/tests/unittests/test_distros/test_hostname.py
@@ -1,4 +1,4 @@
-from mocker import MockerTestCase
+import unittest
from cloudinit.distros.parsers import hostname
@@ -12,7 +12,7 @@ blahblah
BASE_HOSTNAME = BASE_HOSTNAME.strip()
-class TestHostnameHelper(MockerTestCase):
+class TestHostnameHelper(unittest.TestCase):
def test_parse_same(self):
hn = hostname.HostnameConf(BASE_HOSTNAME)
self.assertEquals(str(hn).strip(), BASE_HOSTNAME)
diff --git a/tests/unittests/test_distros/test_hosts.py b/tests/unittests/test_distros/test_hosts.py
index 687a0dab..fc701eaa 100644
--- a/tests/unittests/test_distros/test_hosts.py
+++ b/tests/unittests/test_distros/test_hosts.py
@@ -1,4 +1,4 @@
-from mocker import MockerTestCase
+import unittest
from cloudinit.distros.parsers import hosts
@@ -14,7 +14,7 @@ BASE_ETC = '''
BASE_ETC = BASE_ETC.strip()
-class TestHostsHelper(MockerTestCase):
+class TestHostsHelper(unittest.TestCase):
def test_parse(self):
eh = hosts.HostsConf(BASE_ETC)
self.assertEquals(eh.get_entry('127.0.0.1'), [['localhost']])
diff --git a/tests/unittests/test_distros/test_netconfig.py b/tests/unittests/test_distros/test_netconfig.py
index 6e1a0b69..91e630ae 100644
--- a/tests/unittests/test_distros/test_netconfig.py
+++ b/tests/unittests/test_distros/test_netconfig.py
@@ -1,8 +1,14 @@
-from mocker import MockerTestCase
-
-import mocker
-
import os
+import unittest
+
+try:
+ from unittest import mock
+except ImportError:
+ import mock
+try:
+ from contextlib import ExitStack
+except ImportError:
+ from contextlib2 import ExitStack
from six import StringIO
@@ -74,7 +80,7 @@ class WriteBuffer(object):
return self.buffer.getvalue()
-class TestNetCfgDistro(MockerTestCase):
+class TestNetCfgDistro(unittest.TestCase):
def _get_distro(self, dname):
cls = distros.fetch(dname)
@@ -85,34 +91,28 @@ class TestNetCfgDistro(MockerTestCase):
def test_simple_write_ub(self):
ub_distro = self._get_distro('ubuntu')
- util_mock = self.mocker.replace(util.write_file,
- spec=False, passthrough=False)
- exists_mock = self.mocker.replace(os.path.isfile,
- spec=False, passthrough=False)
+ with ExitStack() as mocks:
+ write_bufs = {}
- exists_mock(mocker.ARGS)
- self.mocker.count(0, None)
- self.mocker.result(False)
+ def replace_write(filename, content, mode=0o644, omode="wb"):
+ buf = WriteBuffer()
+ buf.mode = mode
+ buf.omode = omode
+ buf.write(content)
+ write_bufs[filename] = buf
- write_bufs = {}
-
- def replace_write(filename, content, mode=0o644, omode="wb"):
- buf = WriteBuffer()
- buf.mode = mode
- buf.omode = omode
- buf.write(content)
- write_bufs[filename] = buf
+ mocks.enter_context(
+ mock.patch.object(util, 'write_file', replace_write))
+ mocks.enter_context(
+ mock.patch.object(os.path, 'isfile', return_value=False))
- util_mock(mocker.ARGS)
- self.mocker.call(replace_write)
- self.mocker.replay()
- ub_distro.apply_network(BASE_NET_CFG, False)
+ ub_distro.apply_network(BASE_NET_CFG, False)
- self.assertEquals(len(write_bufs), 1)
- self.assertIn('/etc/network/interfaces', write_bufs)
- write_buf = write_bufs['/etc/network/interfaces']
- self.assertEquals(str(write_buf).strip(), BASE_NET_CFG.strip())
- self.assertEquals(write_buf.mode, 0o644)
+ self.assertEquals(len(write_bufs), 1)
+ self.assertIn('/etc/network/interfaces', write_bufs)
+ write_buf = write_bufs['/etc/network/interfaces']
+ self.assertEquals(str(write_buf).strip(), BASE_NET_CFG.strip())
+ self.assertEquals(write_buf.mode, 0o644)
def assertCfgEquals(self, blob1, blob2):
b1 = dict(SysConf(blob1.strip().splitlines()))
@@ -127,12 +127,6 @@ class TestNetCfgDistro(MockerTestCase):
def test_simple_write_rh(self):
rh_distro = self._get_distro('rhel')
- write_mock = self.mocker.replace(util.write_file,
- spec=False, passthrough=False)
- load_mock = self.mocker.replace(util.load_file,
- spec=False, passthrough=False)
- exists_mock = self.mocker.replace(os.path.isfile,
- spec=False, passthrough=False)
write_bufs = {}
@@ -143,37 +137,31 @@ class TestNetCfgDistro(MockerTestCase):
buf.write(content)
write_bufs[filename] = buf
- exists_mock(mocker.ARGS)
- self.mocker.count(0, None)
- self.mocker.result(False)
-
- load_mock(mocker.ARGS)
- self.mocker.count(0, None)
- self.mocker.result('')
-
- for _i in range(0, 3):
- write_mock(mocker.ARGS)
- self.mocker.call(replace_write)
-
- write_mock(mocker.ARGS)
- self.mocker.call(replace_write)
-
- self.mocker.replay()
- rh_distro.apply_network(BASE_NET_CFG, False)
-
- self.assertEquals(len(write_bufs), 4)
- self.assertIn('/etc/sysconfig/network-scripts/ifcfg-lo', write_bufs)
- write_buf = write_bufs['/etc/sysconfig/network-scripts/ifcfg-lo']
- expected_buf = '''
+ with ExitStack() as mocks:
+ mocks.enter_context(
+ mock.patch.object(util, 'write_file', replace_write))
+ mocks.enter_context(
+ mock.patch.object(util, 'load_file', return_value=''))
+ mocks.enter_context(
+ mock.patch.object(os.path, 'isfile', return_value=False))
+
+ rh_distro.apply_network(BASE_NET_CFG, False)
+
+ self.assertEquals(len(write_bufs), 4)
+ self.assertIn('/etc/sysconfig/network-scripts/ifcfg-lo',
+ write_bufs)
+ write_buf = write_bufs['/etc/sysconfig/network-scripts/ifcfg-lo']
+ expected_buf = '''
DEVICE="lo"
ONBOOT=yes
'''
- self.assertCfgEquals(expected_buf, str(write_buf))
- self.assertEquals(write_buf.mode, 0o644)
+ self.assertCfgEquals(expected_buf, str(write_buf))
+ self.assertEquals(write_buf.mode, 0o644)
- self.assertIn('/etc/sysconfig/network-scripts/ifcfg-eth0', write_bufs)
- write_buf = write_bufs['/etc/sysconfig/network-scripts/ifcfg-eth0']
- expected_buf = '''
+ self.assertIn('/etc/sysconfig/network-scripts/ifcfg-eth0',
+ write_bufs)
+ write_buf = write_bufs['/etc/sysconfig/network-scripts/ifcfg-eth0']
+ expected_buf = '''
DEVICE="eth0"
BOOTPROTO="static"
NETMASK="255.255.255.0"
@@ -182,36 +170,31 @@ ONBOOT=yes
GATEWAY="192.168.1.254"
BROADCAST="192.168.1.0"
'''
- self.assertCfgEquals(expected_buf, str(write_buf))
- self.assertEquals(write_buf.mode, 0o644)
+ self.assertCfgEquals(expected_buf, str(write_buf))
+ self.assertEquals(write_buf.mode, 0o644)
- self.assertIn('/etc/sysconfig/network-scripts/ifcfg-eth1', write_bufs)
- write_buf = write_bufs['/etc/sysconfig/network-scripts/ifcfg-eth1']
- expected_buf = '''
+ self.assertIn('/etc/sysconfig/network-scripts/ifcfg-eth1',
+ write_bufs)
+ write_buf = write_bufs['/etc/sysconfig/network-scripts/ifcfg-eth1']
+ expected_buf = '''
DEVICE="eth1"
BOOTPROTO="dhcp"
ONBOOT=yes
'''
- self.assertCfgEquals(expected_buf, str(write_buf))
- self.assertEquals(write_buf.mode, 0o644)
+ self.assertCfgEquals(expected_buf, str(write_buf))
+ self.assertEquals(write_buf.mode, 0o644)
- self.assertIn('/etc/sysconfig/network', write_bufs)
- write_buf = write_bufs['/etc/sysconfig/network']
- expected_buf = '''
+ self.assertIn('/etc/sysconfig/network', write_bufs)
+ write_buf = write_bufs['/etc/sysconfig/network']
+ expected_buf = '''
# Created by cloud-init v. 0.7
NETWORKING=yes
'''
- self.assertCfgEquals(expected_buf, str(write_buf))
- self.assertEquals(write_buf.mode, 0o644)
+ self.assertCfgEquals(expected_buf, str(write_buf))
+ self.assertEquals(write_buf.mode, 0o644)
def test_write_ipv6_rhel(self):
rh_distro = self._get_distro('rhel')
- write_mock = self.mocker.replace(util.write_file,
- spec=False, passthrough=False)
- load_mock = self.mocker.replace(util.load_file,
- spec=False, passthrough=False)
- exists_mock = self.mocker.replace(os.path.isfile,
- spec=False, passthrough=False)
write_bufs = {}
@@ -222,37 +205,31 @@ NETWORKING=yes
buf.write(content)
write_bufs[filename] = buf
- exists_mock(mocker.ARGS)
- self.mocker.count(0, None)
- self.mocker.result(False)
-
- load_mock(mocker.ARGS)
- self.mocker.count(0, None)
- self.mocker.result('')
-
- for _i in range(0, 3):
- write_mock(mocker.ARGS)
- self.mocker.call(replace_write)
-
- write_mock(mocker.ARGS)
- self.mocker.call(replace_write)
-
- self.mocker.replay()
- rh_distro.apply_network(BASE_NET_CFG_IPV6, False)
-
- self.assertEquals(len(write_bufs), 4)
- self.assertIn('/etc/sysconfig/network-scripts/ifcfg-lo', write_bufs)
- write_buf = write_bufs['/etc/sysconfig/network-scripts/ifcfg-lo']
- expected_buf = '''
+ with ExitStack() as mocks:
+ mocks.enter_context(
+ mock.patch.object(util, 'write_file', replace_write))
+ mocks.enter_context(
+ mock.patch.object(util, 'load_file', return_value=''))
+ mocks.enter_context(
+ mock.patch.object(os.path, 'isfile', return_value=False))
+
+ rh_distro.apply_network(BASE_NET_CFG_IPV6, False)
+
+ self.assertEquals(len(write_bufs), 4)
+ self.assertIn('/etc/sysconfig/network-scripts/ifcfg-lo',
+ write_bufs)
+ write_buf = write_bufs['/etc/sysconfig/network-scripts/ifcfg-lo']
+ expected_buf = '''
DEVICE="lo"
ONBOOT=yes
'''
- self.assertCfgEquals(expected_buf, str(write_buf))
- self.assertEquals(write_buf.mode, 0o644)
+ self.assertCfgEquals(expected_buf, str(write_buf))
+ self.assertEquals(write_buf.mode, 0o644)
- self.assertIn('/etc/sysconfig/network-scripts/ifcfg-eth0', write_bufs)
- write_buf = write_bufs['/etc/sysconfig/network-scripts/ifcfg-eth0']
- expected_buf = '''
+ self.assertIn('/etc/sysconfig/network-scripts/ifcfg-eth0',
+ write_bufs)
+ write_buf = write_bufs['/etc/sysconfig/network-scripts/ifcfg-eth0']
+ expected_buf = '''
DEVICE="eth0"
BOOTPROTO="static"
NETMASK="255.255.255.0"
@@ -264,11 +241,12 @@ IPV6INIT=yes
IPV6ADDR="2607:f0d0:1002:0011::2"
IPV6_DEFAULTGW="2607:f0d0:1002:0011::1"
'''
- self.assertCfgEquals(expected_buf, str(write_buf))
- self.assertEquals(write_buf.mode, 0o644)
- self.assertIn('/etc/sysconfig/network-scripts/ifcfg-eth1', write_bufs)
- write_buf = write_bufs['/etc/sysconfig/network-scripts/ifcfg-eth1']
- expected_buf = '''
+ self.assertCfgEquals(expected_buf, str(write_buf))
+ self.assertEquals(write_buf.mode, 0o644)
+ self.assertIn('/etc/sysconfig/network-scripts/ifcfg-eth1',
+ write_bufs)
+ write_buf = write_bufs['/etc/sysconfig/network-scripts/ifcfg-eth1']
+ expected_buf = '''
DEVICE="eth1"
BOOTPROTO="static"
NETMASK="255.255.255.0"
@@ -280,38 +258,22 @@ IPV6INIT=yes
IPV6ADDR="2607:f0d0:1002:0011::3"
IPV6_DEFAULTGW="2607:f0d0:1002:0011::1"
'''
- self.assertCfgEquals(expected_buf, str(write_buf))
- self.assertEquals(write_buf.mode, 0o644)
+ self.assertCfgEquals(expected_buf, str(write_buf))
+ self.assertEquals(write_buf.mode, 0o644)
- self.assertIn('/etc/sysconfig/network', write_bufs)
- write_buf = write_bufs['/etc/sysconfig/network']
- expected_buf = '''
+ self.assertIn('/etc/sysconfig/network', write_bufs)
+ write_buf = write_bufs['/etc/sysconfig/network']
+ expected_buf = '''
# Created by cloud-init v. 0.7
NETWORKING=yes
NETWORKING_IPV6=yes
IPV6_AUTOCONF=no
'''
- self.assertCfgEquals(expected_buf, str(write_buf))
- self.assertEquals(write_buf.mode, 0o644)
+ self.assertCfgEquals(expected_buf, str(write_buf))
+ self.assertEquals(write_buf.mode, 0o644)
def test_simple_write_freebsd(self):
fbsd_distro = self._get_distro('freebsd')
- util_mock = self.mocker.replace(util.write_file,
- spec=False, passthrough=False)
- exists_mock = self.mocker.replace(os.path.isfile,
- spec=False, passthrough=False)
- load_mock = self.mocker.replace(util.load_file,
- spec=False, passthrough=False)
- subp_mock = self.mocker.replace(util.subp,
- spec=False, passthrough=False)
-
- subp_mock(['ifconfig', '-a'])
- self.mocker.count(0, None)
- self.mocker.result(('vtnet0', ''))
-
- exists_mock(mocker.ARGS)
- self.mocker.count(0, None)
- self.mocker.result(False)
write_bufs = {}
read_bufs = {
@@ -336,23 +298,24 @@ IPV6_AUTOCONF=no
return str(write_bufs[fname])
return read_bufs[fname]
- util_mock(mocker.ARGS)
- self.mocker.call(replace_write)
- self.mocker.count(0, None)
-
- load_mock(mocker.ARGS)
- self.mocker.call(replace_read)
- self.mocker.count(0, None)
-
- self.mocker.replay()
- fbsd_distro.apply_network(BASE_NET_CFG, False)
-
- self.assertIn('/etc/rc.conf', write_bufs)
- write_buf = write_bufs['/etc/rc.conf']
- expected_buf = '''
+ 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(BASE_NET_CFG, False)
+
+ self.assertIn('/etc/rc.conf', write_bufs)
+ write_buf = write_bufs['/etc/rc.conf']
+ expected_buf = '''
ifconfig_vtnet0="192.168.1.5 netmask 255.255.255.0"
ifconfig_vtnet1="DHCP"
defaultrouter="192.168.1.254"
'''
- self.assertCfgEquals(expected_buf, str(write_buf))
- self.assertEquals(write_buf.mode, 0o644)
+ self.assertCfgEquals(expected_buf, str(write_buf))
+ self.assertEquals(write_buf.mode, 0o644)