From 2e6e49f7fa49c977fb9e406e1a49a99792e80775 Mon Sep 17 00:00:00 2001 From: Monty Taylor Date: Sat, 25 May 2013 09:08:25 +0200 Subject: [PATCH] Migrate test base classes to testtools. This is in prep for testr changes, but also cleans up several things in the tests where state could leak. Change-Id: I7f53dcee2f1fcffb622dd475ea8cf433ce43dbb2 --- heatclient/tests/test_common_http.py | 11 +- heatclient/tests/test_shell.py | 211 +++++++++++++-------------- heatclient/tests/test_utils.py | 4 +- test-requirements.txt | 2 + 4 files changed, 108 insertions(+), 120 deletions(-) diff --git a/heatclient/tests/test_common_http.py b/heatclient/tests/test_common_http.py index 28109fde..faabf48d 100644 --- a/heatclient/tests/test_common_http.py +++ b/heatclient/tests/test_common_http.py @@ -1,22 +1,21 @@ import mox -import unittest +import testtools import fakes from heatclient.common import http from heatclient import exc -class HttpClientTest(unittest.TestCase): +class HttpClientTest(testtools.TestCase): # Patch os.environ to avoid required auth info. def setUp(self): + super(HttpClientTest, self).setUp() self.m = mox.Mox() self.m.StubOutClassWithMocks(http.httplib, 'HTTPConnection') self.m.StubOutClassWithMocks(http.httplib, 'HTTPSConnection') - - def tearDown(self): - self.m.UnsetStubs() - self.m.ResetAll() + self.addCleanup(self.m.UnsetStubs) + self.addCleanup(self.m.ResetAll) def test_http_raw_request(self): # Record a 200 diff --git a/heatclient/tests/test_shell.py b/heatclient/tests/test_shell.py index 32a1a1d5..e925a3f7 100644 --- a/heatclient/tests/test_shell.py +++ b/heatclient/tests/test_shell.py @@ -4,8 +4,9 @@ import os import re import sys +import fixtures import mox -import unittest +import testtools try: import json except ImportError: @@ -21,7 +22,12 @@ TEST_VAR_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__), 'var')) -class TestCase(unittest.TestCase): +class TestCase(testtools.TestCase): + + def set_fake_env(self, fake_env): + for key, value in fake_env.items(): + self.useFixture(fixtures.EnvironmentVariable(key, value)) + # required for testing with Python 2.6 def assertRegexpMatches(self, text, expected_regexp, msg=None): """Fail the test unless the text matches the regular expression.""" @@ -33,93 +39,6 @@ class TestCase(unittest.TestCase): msg, expected_regexp.pattern, text) raise self.failureException(msg) - -class ShellValidationTest(TestCase): - - def test_missing_auth(self): - _old_env, os.environ = os.environ, { - 'OS_PASSWORD': 'password', - 'OS_TENANT_NAME': 'tenant_name', - 'OS_AUTH_URL': 'http://no.where', - } - self.shell_error('list', 'You must provide a username') - - os.environ = _old_env - - _old_env, os.environ = os.environ, { - 'OS_USERNAME': 'username', - 'OS_TENANT_NAME': 'tenant_name', - 'OS_AUTH_URL': 'http://no.where', - } - self.shell_error('list', 'You must provide a password') - - os.environ = _old_env - - _old_env, os.environ = os.environ, { - 'OS_USERNAME': 'username', - 'OS_PASSWORD': 'password', - 'OS_AUTH_URL': 'http://no.where', - } - self.shell_error('list', 'You must provide a tenant_id') - - os.environ = _old_env - - _old_env, os.environ = os.environ, { - 'OS_USERNAME': 'username', - 'OS_PASSWORD': 'password', - 'OS_TENANT_NAME': 'tenant_name', - } - self.shell_error('list', 'You must provide an auth url') - - os.environ = _old_env - - def test_failed_auth(self): - m = mox.Mox() - m.StubOutWithMock(ksclient, 'Client') - m.StubOutWithMock(v1client.Client, 'json_request') - fakes.script_keystone_client() - v1client.Client.json_request( - 'GET', '/stacks?limit=20').AndRaise(exc.Unauthorized) - - m.ReplayAll() - _old_env, os.environ = os.environ, { - 'OS_USERNAME': 'username', - 'OS_PASSWORD': 'password', - 'OS_TENANT_NAME': 'tenant_name', - 'OS_AUTH_URL': 'http://no.where', - } - self.shell_error('list', 'Invalid OpenStack Identity credentials.') - - m.VerifyAll() - - os.environ = _old_env - m.UnsetStubs() - - def test_create_validation(self): - m = mox.Mox() - m.StubOutWithMock(ksclient, 'Client') - m.StubOutWithMock(v1client.Client, 'json_request') - fakes.script_keystone_client() - - m.ReplayAll() - _old_env, os.environ = os.environ, { - 'OS_USERNAME': 'username', - 'OS_PASSWORD': 'password', - 'OS_TENANT_NAME': 'tenant_name', - 'OS_AUTH_URL': 'http://no.where', - } - self.shell_error( - 'create teststack ' - '--parameters="InstanceType=m1.large;DBUsername=wp;' - 'DBPassword=verybadpassword;KeyName=heat_key;' - 'LinuxDistribution=F17"', - 'Need to specify exactly one of') - - m.VerifyAll() - - os.environ = _old_env - m.UnsetStubs() - def shell_error(self, argstr, error_match): orig = sys.stderr try: @@ -137,28 +56,110 @@ class ShellValidationTest(TestCase): return err -class ShellTest(TestCase): +class EnvVarTest(TestCase): + + def test_missing_auth(self): + fake_env = { + 'OS_USERNAME': None, + 'OS_PASSWORD': 'password', + 'OS_TENANT_NAME': 'tenant_name', + 'OS_AUTH_URL': 'http://no.where', + } + self.set_fake_env(fake_env) + self.shell_error('list', 'You must provide a username') + + fake_env = { + 'OS_USERNAME': 'username', + 'OS_PASSWORD': None, + 'OS_TENANT_NAME': 'tenant_name', + 'OS_AUTH_URL': 'http://no.where', + } + self.set_fake_env(fake_env) + self.shell_error('list', 'You must provide a password') + + fake_env = { + 'OS_USERNAME': 'username', + 'OS_PASSWORD': 'password', + 'OS_TENANT_NAME': None, + 'OS_AUTH_URL': 'http://no.where', + } + self.set_fake_env(fake_env) + self.shell_error('list', 'You must provide a tenant_id') + + fake_env = { + 'OS_USERNAME': 'username', + 'OS_PASSWORD': 'password', + 'OS_TENANT_NAME': 'tenant_name', + 'OS_AUTH_URL': None, + } + self.set_fake_env(fake_env) + self.shell_error('list', 'You must provide an auth url') + + +class ShellValidationTest(TestCase): - # Patch os.environ to avoid required auth info. def setUp(self): + super(ShellValidationTest, self).setUp() self.m = mox.Mox() + self.addCleanup(self.m.VerifyAll) + self.addCleanup(self.m.UnsetStubs) + + def test_failed_auth(self): self.m.StubOutWithMock(ksclient, 'Client') self.m.StubOutWithMock(v1client.Client, 'json_request') - self.m.StubOutWithMock(v1client.Client, 'raw_request') + fakes.script_keystone_client() + v1client.Client.json_request( + 'GET', '/stacks?limit=20').AndRaise(exc.Unauthorized) - global _old_env + self.m.ReplayAll() 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() + self.set_fake_env(fake_env) + self.shell_error('list', 'Invalid OpenStack Identity credentials.') - def tearDown(self): - self.m.UnsetStubs() - global _old_env - os.environ = _old_env + def test_create_validation(self): + self.m.StubOutWithMock(ksclient, 'Client') + self.m.StubOutWithMock(v1client.Client, 'json_request') + fakes.script_keystone_client() + + self.m.ReplayAll() + fake_env = { + 'OS_USERNAME': 'username', + 'OS_PASSWORD': 'password', + 'OS_TENANT_NAME': 'tenant_name', + 'OS_AUTH_URL': 'http://no.where', + } + self.set_fake_env(fake_env) + self.shell_error( + 'create teststack ' + '--parameters="InstanceType=m1.large;DBUsername=wp;' + 'DBPassword=verybadpassword;KeyName=heat_key;' + 'LinuxDistribution=F17"', + 'Need to specify exactly one of') + + +class ShellTest(TestCase): + + # Patch os.environ to avoid required auth info. + def setUp(self): + super(ShellTest, self).setUp() + self.m = mox.Mox() + self.m.StubOutWithMock(ksclient, 'Client') + self.m.StubOutWithMock(v1client.Client, 'json_request') + self.m.StubOutWithMock(v1client.Client, 'raw_request') + self.addCleanup(self.m.VerifyAll) + self.addCleanup(self.m.UnsetStubs) + fake_env = { + 'OS_USERNAME': 'username', + 'OS_PASSWORD': 'password', + 'OS_TENANT_NAME': 'tenant_name', + 'OS_AUTH_URL': 'http://no.where', + } + self.set_fake_env(fake_env) def shell(self, argstr): orig = sys.stdout @@ -227,8 +228,6 @@ class ShellTest(TestCase): for r in required: self.assertRegexpMatches(list_text, r) - self.m.VerifyAll() - def test_describe(self): fakes.script_keystone_client() resp_dict = {"stack": { @@ -261,8 +260,6 @@ class ShellTest(TestCase): for r in required: self.assertRegexpMatches(list_text, r) - self.m.VerifyAll() - def test_create(self): fakes.script_keystone_client() resp = fakes.FakeHTTPResponse( @@ -294,8 +291,6 @@ class ShellTest(TestCase): for r in required: self.assertRegexpMatches(create_text, r) - self.m.VerifyAll() - def test_create_url(self): fakes.script_keystone_client() @@ -326,8 +321,6 @@ class ShellTest(TestCase): for r in required: self.assertRegexpMatches(create_text, r) - self.m.VerifyAll() - def test_create_object(self): fakes.script_keystone_client() @@ -366,8 +359,6 @@ class ShellTest(TestCase): for r in required: self.assertRegexpMatches(create_text, r) - self.m.VerifyAll() - def test_update(self): fakes.script_keystone_client() resp = fakes.FakeHTTPResponse( @@ -399,8 +390,6 @@ class ShellTest(TestCase): for r in required: self.assertRegexpMatches(create_text, r) - self.m.VerifyAll() - def test_delete(self): fakes.script_keystone_client() resp = fakes.FakeHTTPResponse( @@ -425,5 +414,3 @@ class ShellTest(TestCase): ] for r in required: self.assertRegexpMatches(create_text, r) - - self.m.VerifyAll() diff --git a/heatclient/tests/test_utils.py b/heatclient/tests/test_utils.py index 8a8d08ad..d9f5b600 100644 --- a/heatclient/tests/test_utils.py +++ b/heatclient/tests/test_utils.py @@ -13,10 +13,10 @@ # License for the specific language governing permissions and limitations # under the License. from heatclient.common import utils -import unittest +import testtools -class shellTest(unittest.TestCase): +class shellTest(testtools.TestCase): def test_format_parameters(self): p = utils.format_parameters( diff --git a/test-requirements.txt b/test-requirements.txt index 994302a3..ce667727 100644 --- a/test-requirements.txt +++ b/test-requirements.txt @@ -6,6 +6,7 @@ pyflakes==0.7.2 flake8==2.0 hacking>=0.5.3,<0.6 +fixtures>=0.3.12 mox nose nose-exclude @@ -13,3 +14,4 @@ nosexcover openstack.nose_plugin nosehtmloutput sphinx>=1.1.2 +testtools>=0.9.29