From 53aee5cf4b66c98c1142a57244d7466249e44f1f Mon Sep 17 00:00:00 2001 From: Monty Taylor Date: Mon, 24 Dec 2012 19:11:38 -0600 Subject: [PATCH] Move from untitest2 to testtools. Use testtools as the base testclass. Use fixtures library for managing fixtures. Part of blueprint grizzly-testtools Change-Id: Iac5af286b988787acf7049344641aadf140b9398 --- tests/test_auth_plugins.py | 7 +++--- tests/test_client.py | 3 --- tests/test_shell.py | 47 ++++++++++++++++++++----------------- tests/test_utils.py | 1 + tests/utils.py | 4 ++-- tests/v1_1/test_shell.py | 48 ++++++++++++++++++++++---------------- tools/test-requires | 3 ++- 7 files changed, 62 insertions(+), 51 deletions(-) diff --git a/tests/test_auth_plugins.py b/tests/test_auth_plugins.py index 00b3a7597..c4950af87 100644 --- a/tests/test_auth_plugins.py +++ b/tests/test_auth_plugins.py @@ -174,8 +174,9 @@ class AuthPluginTest(utils.TestCase): @mock.patch.object(pkg_resources, "iter_entry_points", mock_iter_entry_points) def test_auth_call(): - with self.assertRaises(exceptions.EndpointNotFound): - cs = client.Client("username", "password", "project_id", - auth_system="fakewithauthurl") + self.assertRaises( + exceptions.EndpointNotFound, + client.Client, "username", "password", "project_id", + auth_system="fakewithauthurl") test_auth_call() diff --git a/tests/test_client.py b/tests/test_client.py index e2fc9d965..48d511a81 100644 --- a/tests/test_client.py +++ b/tests/test_client.py @@ -6,9 +6,6 @@ from tests import utils class ClientTest(utils.TestCase): - def setUp(self): - pass - def test_get_client_class_v2(self): output = novaclient.client.get_client_class('2') self.assertEqual(output, novaclient.v1_1.client.Client) diff --git a/tests/test_shell.py b/tests/test_shell.py index 79c3d2f48..63c7244a2 100644 --- a/tests/test_shell.py +++ b/tests/test_shell.py @@ -1,7 +1,10 @@ import cStringIO -import os +import re import sys +import fixtures +from testtools import matchers + from novaclient import exceptions import novaclient.shell from tests import utils @@ -9,16 +12,18 @@ from tests import utils class ShellTest(utils.TestCase): - # Patch os.environ to avoid required auth info. + FAKE_ENV = { + 'OS_USERNAME': 'username', + 'OS_PASSWORD': 'password', + 'OS_TENANT_NAME': 'tenant_name', + 'OS_AUTH_URL': 'http://no.where', + } + def setUp(self): - global _old_env - fake_env = { - 'OS_USERNAME': 'username', - 'OS_PASSWORD': 'password', - 'OS_TENANT_NAME': 'tenant_name', - 'OS_AUTH_URL': 'http://no.where', - } - _old_env, os.environ = os.environ, fake_env.copy() + super(ShellTest, self).setUp() + for var in self.FAKE_ENV: + self.useFixture(fixtures.EnvironmentVariable(var, + self.FAKE_ENV[var])) def shell(self, argstr): orig = sys.stdout @@ -36,29 +41,27 @@ class ShellTest(utils.TestCase): return out - def tearDown(self): - global _old_env - os.environ = _old_env - def test_help_unknown_command(self): self.assertRaises(exceptions.CommandError, self.shell, 'help foofoo') def test_help(self): required = [ - '^usage: ', - '(?m)^\s+root-password\s+Change the root password', - '(?m)^See "nova help COMMAND" for help on a specific command', + '.*?^usage: ', + '.*?^\s+root-password\s+Change the root password', + '.*?^See "nova help COMMAND" for help on a specific command', ] help_text = self.shell('help') for r in required: - self.assertRegexpMatches(help_text, r) + self.assertThat(help_text, + matchers.MatchesRegex(r, re.DOTALL | re.MULTILINE)) def test_help_on_subcommand(self): required = [ - '^usage: nova root-password', - '(?m)^Change the root password', - '(?m)^Positional arguments:', + '.*?^usage: nova root-password', + '.*?^Change the root password', + '.*?^Positional arguments:', ] help_text = self.shell('help root-password') for r in required: - self.assertRegexpMatches(help_text, r) + self.assertThat(help_text, + matchers.MatchesRegex(r, re.DOTALL | re.MULTILINE)) diff --git a/tests/test_utils.py b/tests/test_utils.py index 0eb64cff2..c619fe14d 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -61,6 +61,7 @@ class FakeDisplayManager(FakeManager): class FindResourceTestCase(test_utils.TestCase): def setUp(self): + super(FindResourceTestCase, self).setUp() self.manager = FakeManager(None) def test_find_none(self): diff --git a/tests/utils.py b/tests/utils.py index 3bbe8bbad..ef231a775 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -1,8 +1,8 @@ import requests -import unittest2 +import testtools -class TestCase(unittest2.TestCase): +class TestCase(testtools.TestCase): TEST_REQUEST_BASE = { 'config': {'danger_mode': False}, 'verify': True, diff --git a/tests/v1_1/test_shell.py b/tests/v1_1/test_shell.py index de85a3e57..375fb8118 100644 --- a/tests/v1_1/test_shell.py +++ b/tests/v1_1/test_shell.py @@ -21,6 +21,8 @@ import mock import sys import tempfile +import fixtures + import novaclient.shell import novaclient.client from novaclient import exceptions @@ -29,39 +31,45 @@ from tests.v1_1 import fakes from tests import utils -class ShellTest(utils.TestCase): +class ShellFixture(fixtures.Fixture): - # Patch os.environ to avoid required auth info. def setUp(self): - """Run before each test.""" - self.old_environment = os.environ.copy() - os.environ = { - 'NOVA_USERNAME': 'username', - 'NOVA_PASSWORD': 'password', - 'NOVA_PROJECT_ID': 'project_id', - 'OS_COMPUTE_API_VERSION': '1.1', - 'NOVA_URL': 'http://no.where', - } - + super(ShellFixture, self).setUp() self.shell = novaclient.shell.OpenStackComputeShell() - #HACK(bcwaldon): replace this when we start using stubs - self.old_get_client_class = novaclient.client.get_client_class - novaclient.client.get_client_class = lambda *_: fakes.FakeClient - def tearDown(self): - os.environ = self.old_environment # For some method like test_image_meta_bad_action we are # testing a SystemExit to be thrown and object self.shell has # no time to get instantatiated which is OK in this case, so # we make sure the method is there before launching it. if hasattr(self.shell, 'cs'): self.shell.cs.clear_callstack() + super(ShellFixture, self).tearDown() - #HACK(bcwaldon): replace this when we start using stubs - novaclient.client.get_client_class = self.old_get_client_class - timeutils.clear_time_override() +class ShellTest(utils.TestCase): + + FAKE_ENV = { + 'NOVA_USERNAME': 'username', + 'NOVA_PASSWORD': 'password', + 'NOVA_PROJECT_ID': 'project_id', + 'OS_COMPUTE_API_VERSION': '1.1', + 'NOVA_URL': 'http://no.where', + } + + def setUp(self): + """Run before each test.""" + super(ShellTest, self).setUp() + + for var in self.FAKE_ENV: + self.useFixture(fixtures.EnvironmentVariable(var, + self.FAKE_ENV[var])) + self.shell = self.useFixture(ShellFixture()).shell + + self.useFixture(fixtures.MonkeyPatch( + 'novaclient.client.get_client_class', + lambda *_: fakes.FakeClient)) + self.addCleanup(timeutils.clear_time_override) def run_command(self, cmd): self.shell.main(cmd.split()) diff --git a/tools/test-requires b/tools/test-requires index 43a946955..f69ef390d 100644 --- a/tools/test-requires +++ b/tools/test-requires @@ -1,5 +1,6 @@ distribute>=0.6.24 +fixtures mock nose nose-exclude @@ -8,4 +9,4 @@ openstack.nose_plugin nosehtmloutput pep8==1.1 sphinx>=1.1.2 -unittest2 +testtools