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
This commit is contained in:
Monty Taylor 2013-05-25 09:08:25 +02:00
parent 56bfe7541d
commit 2e6e49f7fa
4 changed files with 108 additions and 120 deletions

View File

@ -1,22 +1,21 @@
import mox import mox
import unittest import testtools
import fakes import fakes
from heatclient.common import http from heatclient.common import http
from heatclient import exc from heatclient import exc
class HttpClientTest(unittest.TestCase): class HttpClientTest(testtools.TestCase):
# Patch os.environ to avoid required auth info. # Patch os.environ to avoid required auth info.
def setUp(self): def setUp(self):
super(HttpClientTest, self).setUp()
self.m = mox.Mox() self.m = mox.Mox()
self.m.StubOutClassWithMocks(http.httplib, 'HTTPConnection') self.m.StubOutClassWithMocks(http.httplib, 'HTTPConnection')
self.m.StubOutClassWithMocks(http.httplib, 'HTTPSConnection') self.m.StubOutClassWithMocks(http.httplib, 'HTTPSConnection')
self.addCleanup(self.m.UnsetStubs)
def tearDown(self): self.addCleanup(self.m.ResetAll)
self.m.UnsetStubs()
self.m.ResetAll()
def test_http_raw_request(self): def test_http_raw_request(self):
# Record a 200 # Record a 200

View File

@ -4,8 +4,9 @@ import os
import re import re
import sys import sys
import fixtures
import mox import mox
import unittest import testtools
try: try:
import json import json
except ImportError: except ImportError:
@ -21,7 +22,12 @@ TEST_VAR_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__),
'var')) '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 # required for testing with Python 2.6
def assertRegexpMatches(self, text, expected_regexp, msg=None): def assertRegexpMatches(self, text, expected_regexp, msg=None):
"""Fail the test unless the text matches the regular expression.""" """Fail the test unless the text matches the regular expression."""
@ -33,93 +39,6 @@ class TestCase(unittest.TestCase):
msg, expected_regexp.pattern, text) msg, expected_regexp.pattern, text)
raise self.failureException(msg) 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): def shell_error(self, argstr, error_match):
orig = sys.stderr orig = sys.stderr
try: try:
@ -137,28 +56,110 @@ class ShellValidationTest(TestCase):
return err 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): def setUp(self):
super(ShellValidationTest, self).setUp()
self.m = mox.Mox() 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(ksclient, 'Client')
self.m.StubOutWithMock(v1client.Client, 'json_request') 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 = { fake_env = {
'OS_USERNAME': 'username', 'OS_USERNAME': 'username',
'OS_PASSWORD': 'password', 'OS_PASSWORD': 'password',
'OS_TENANT_NAME': 'tenant_name', 'OS_TENANT_NAME': 'tenant_name',
'OS_AUTH_URL': 'http://no.where', '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): def test_create_validation(self):
self.m.UnsetStubs() self.m.StubOutWithMock(ksclient, 'Client')
global _old_env self.m.StubOutWithMock(v1client.Client, 'json_request')
os.environ = _old_env 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): def shell(self, argstr):
orig = sys.stdout orig = sys.stdout
@ -227,8 +228,6 @@ class ShellTest(TestCase):
for r in required: for r in required:
self.assertRegexpMatches(list_text, r) self.assertRegexpMatches(list_text, r)
self.m.VerifyAll()
def test_describe(self): def test_describe(self):
fakes.script_keystone_client() fakes.script_keystone_client()
resp_dict = {"stack": { resp_dict = {"stack": {
@ -261,8 +260,6 @@ class ShellTest(TestCase):
for r in required: for r in required:
self.assertRegexpMatches(list_text, r) self.assertRegexpMatches(list_text, r)
self.m.VerifyAll()
def test_create(self): def test_create(self):
fakes.script_keystone_client() fakes.script_keystone_client()
resp = fakes.FakeHTTPResponse( resp = fakes.FakeHTTPResponse(
@ -294,8 +291,6 @@ class ShellTest(TestCase):
for r in required: for r in required:
self.assertRegexpMatches(create_text, r) self.assertRegexpMatches(create_text, r)
self.m.VerifyAll()
def test_create_url(self): def test_create_url(self):
fakes.script_keystone_client() fakes.script_keystone_client()
@ -326,8 +321,6 @@ class ShellTest(TestCase):
for r in required: for r in required:
self.assertRegexpMatches(create_text, r) self.assertRegexpMatches(create_text, r)
self.m.VerifyAll()
def test_create_object(self): def test_create_object(self):
fakes.script_keystone_client() fakes.script_keystone_client()
@ -366,8 +359,6 @@ class ShellTest(TestCase):
for r in required: for r in required:
self.assertRegexpMatches(create_text, r) self.assertRegexpMatches(create_text, r)
self.m.VerifyAll()
def test_update(self): def test_update(self):
fakes.script_keystone_client() fakes.script_keystone_client()
resp = fakes.FakeHTTPResponse( resp = fakes.FakeHTTPResponse(
@ -399,8 +390,6 @@ class ShellTest(TestCase):
for r in required: for r in required:
self.assertRegexpMatches(create_text, r) self.assertRegexpMatches(create_text, r)
self.m.VerifyAll()
def test_delete(self): def test_delete(self):
fakes.script_keystone_client() fakes.script_keystone_client()
resp = fakes.FakeHTTPResponse( resp = fakes.FakeHTTPResponse(
@ -425,5 +414,3 @@ class ShellTest(TestCase):
] ]
for r in required: for r in required:
self.assertRegexpMatches(create_text, r) self.assertRegexpMatches(create_text, r)
self.m.VerifyAll()

View File

@ -13,10 +13,10 @@
# License for the specific language governing permissions and limitations # License for the specific language governing permissions and limitations
# under the License. # under the License.
from heatclient.common import utils from heatclient.common import utils
import unittest import testtools
class shellTest(unittest.TestCase): class shellTest(testtools.TestCase):
def test_format_parameters(self): def test_format_parameters(self):
p = utils.format_parameters( p = utils.format_parameters(

View File

@ -6,6 +6,7 @@ pyflakes==0.7.2
flake8==2.0 flake8==2.0
hacking>=0.5.3,<0.6 hacking>=0.5.3,<0.6
fixtures>=0.3.12
mox mox
nose nose
nose-exclude nose-exclude
@ -13,3 +14,4 @@ nosexcover
openstack.nose_plugin openstack.nose_plugin
nosehtmloutput nosehtmloutput
sphinx>=1.1.2 sphinx>=1.1.2
testtools>=0.9.29