summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBarry Warsaw <barry@python.org>2015-01-22 14:46:21 -0500
committerBarry Warsaw <barry@python.org>2015-01-22 14:46:21 -0500
commit5d1edaff81874ebbaa011b73302ab80ecf0afeb7 (patch)
tree8807a5958b5f8ebe29643787cf29ef5f99d2700e
parentab0bff66f61c0c1d69a155d334bf6da81cf57699 (diff)
downloadcloud-init-5d1edaff81874ebbaa011b73302ab80ecf0afeb7.tar.gz
Use .addCleanup() instead of a .tearDown() where appropriate, although we
might have to rewrite this for Python 2.6. Disable Cepko tests (test_cs_util.py) since they are essentially worthless. Convert test_azure to unittest.mock.
-rw-r--r--tests/unittests/test__init__.py9
-rw-r--r--tests/unittests/test_cs_util.py25
-rw-r--r--tests/unittests/test_datasource/test_azure.py64
-rw-r--r--tox.ini2
4 files changed, 49 insertions, 51 deletions
diff --git a/tests/unittests/test__init__.py b/tests/unittests/test__init__.py
index ce4704d8..f5dc3435 100644
--- a/tests/unittests/test__init__.py
+++ b/tests/unittests/test__init__.py
@@ -57,14 +57,11 @@ class TestWalkerHandleHandler(unittest.TestCase):
# Mock the write_file() function. We'll assert that it got called as
# expected in each of the individual tests.
- self.resources = ExitStack()
- self.write_file_mock = self.resources.enter_context(
+ resources = ExitStack()
+ self.addCleanup(resources.close)
+ self.write_file_mock = resources.enter_context(
mock.patch('cloudinit.util.write_file'))
- def tearDown(self):
- self.resources.close()
- unittest.TestCase.tearDown(self)
-
def test_no_errors(self):
"""Payload gets written to file and added to C{pdata}."""
with mock.patch('cloudinit.importer.import_module',
diff --git a/tests/unittests/test_cs_util.py b/tests/unittests/test_cs_util.py
index 7d59222b..99fac84d 100644
--- a/tests/unittests/test_cs_util.py
+++ b/tests/unittests/test_cs_util.py
@@ -1,4 +1,4 @@
-from mocker import MockerTestCase
+import unittest
from cloudinit.cs_utils import Cepko
@@ -26,16 +26,21 @@ class CepkoMock(Cepko):
return SERVER_CONTEXT['tags']
-class CepkoResultTests(MockerTestCase):
+# 2015-01-22 BAW: This test is completely useless because it only ever tests
+# the CepkoMock object. Even in its original form, I don't think it ever
+# touched the underlying Cepko class methods.
+@unittest.skip('This test is completely useless')
+class CepkoResultTests(unittest.TestCase):
def setUp(self):
- self.mocked = self.mocker.replace("cloudinit.cs_utils.Cepko",
- spec=CepkoMock,
- count=False,
- passthrough=False)
- self.mocked()
- self.mocker.result(CepkoMock())
- self.mocker.replay()
- self.c = Cepko()
+ pass
+ ## self.mocked = self.mocker.replace("cloudinit.cs_utils.Cepko",
+ ## spec=CepkoMock,
+ ## count=False,
+ ## passthrough=False)
+ ## self.mocked()
+ ## self.mocker.result(CepkoMock())
+ ## self.mocker.replay()
+ ## self.c = Cepko()
def test_getitem(self):
result = self.c.all()
diff --git a/tests/unittests/test_datasource/test_azure.py b/tests/unittests/test_datasource/test_azure.py
index 6e007a95..2dbcd389 100644
--- a/tests/unittests/test_datasource/test_azure.py
+++ b/tests/unittests/test_datasource/test_azure.py
@@ -3,12 +3,23 @@ from cloudinit.util import load_file
from cloudinit.sources import DataSourceAzure
from ..helpers import populate_dir
+try:
+ from unittest import mock
+except ImportError:
+ import mock
+try:
+ from contextlib import ExitStack
+except ImportError:
+ from contextlib2 import ExitStack
+
import base64
import crypt
-from mocker import MockerTestCase
import os
import stat
import yaml
+import shutil
+import tempfile
+import unittest
def construct_valid_ovf_env(data=None, pubkeys=None, userdata=None):
@@ -66,26 +77,24 @@ def construct_valid_ovf_env(data=None, pubkeys=None, userdata=None):
return content
-class TestAzureDataSource(MockerTestCase):
+class TestAzureDataSource(unittest.TestCase):
def setUp(self):
- # makeDir comes from MockerTestCase
- self.tmp = self.makeDir()
+ self.tmp = tempfile.mkdtemp()
+ self.addCleanup(shutil.rmtree, self.tmp)
# patch cloud_dir, so our 'seed_dir' is guaranteed empty
self.paths = helpers.Paths({'cloud_dir': self.tmp})
self.waagent_d = os.path.join(self.tmp, 'var', 'lib', 'waagent')
- self.unapply = []
- super(TestAzureDataSource, self).setUp()
+ self.patches = ExitStack()
+ self.addCleanup(self.patches.close)
- def tearDown(self):
- apply_patches([i for i in reversed(self.unapply)])
- super(TestAzureDataSource, self).tearDown()
+ super(TestAzureDataSource, self).setUp()
def apply_patches(self, patches):
- ret = apply_patches(patches)
- self.unapply += ret
+ for module, name, new in patches:
+ self.patches.enter_context(mock.patch.object(module, name, new))
def _get_ds(self, data):
@@ -117,16 +126,14 @@ class TestAzureDataSource(MockerTestCase):
mod = DataSourceAzure
mod.BUILTIN_DS_CONFIG['data_dir'] = self.waagent_d
- self.apply_patches([(mod, 'list_possible_azure_ds_devs', dsdevs)])
-
- self.apply_patches([(mod, 'invoke_agent', _invoke_agent),
- (mod, 'wait_for_files', _wait_for_files),
- (mod, 'pubkeys_from_crt_files',
- _pubkeys_from_crt_files),
- (mod, 'iid_from_shared_config',
- _iid_from_shared_config),
- (mod, 'apply_hostname_bounce',
- _apply_hostname_bounce), ])
+ self.apply_patches([
+ (mod, 'list_possible_azure_ds_devs', dsdevs),
+ (mod, 'invoke_agent', _invoke_agent),
+ (mod, 'wait_for_files', _wait_for_files),
+ (mod, 'pubkeys_from_crt_files', _pubkeys_from_crt_files),
+ (mod, 'iid_from_shared_config', _iid_from_shared_config),
+ (mod, 'apply_hostname_bounce', _apply_hostname_bounce),
+ ])
dsrc = mod.DataSourceAzureNet(
data.get('sys_cfg', {}), distro=None, paths=self.paths)
@@ -402,7 +409,7 @@ class TestAzureDataSource(MockerTestCase):
load_file(os.path.join(self.waagent_d, 'ovf-env.xml')))
-class TestReadAzureOvf(MockerTestCase):
+class TestReadAzureOvf(unittest.TestCase):
def test_invalid_xml_raises_non_azure_ds(self):
invalid_xml = "<foo>" + construct_valid_ovf_env(data={})
self.assertRaises(DataSourceAzure.BrokenAzureDataSource,
@@ -417,7 +424,7 @@ class TestReadAzureOvf(MockerTestCase):
self.assertIn(mypk, cfg['_pubkeys'])
-class TestReadAzureSharedConfig(MockerTestCase):
+class TestReadAzureSharedConfig(unittest.TestCase):
def test_valid_content(self):
xml = """<?xml version="1.0" encoding="utf-8"?>
<SharedConfig>
@@ -429,14 +436,3 @@ class TestReadAzureSharedConfig(MockerTestCase):
</SharedConfig>"""
ret = DataSourceAzure.iid_from_shared_config_content(xml)
self.assertEqual("MY_INSTANCE_ID", 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
diff --git a/tox.ini b/tox.ini
index e547c693..d050d7c8 100644
--- a/tox.ini
+++ b/tox.ini
@@ -3,7 +3,7 @@ envlist = py26,py27,py34
recreate = True
[testenv]
-commands = python -m nose tests
+commands = python -m nose -v tests
deps =
contextlib2
httpretty>=0.7.1