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
This commit is contained in:
Monty Taylor 2012-12-24 19:11:38 -06:00
parent e31023fee1
commit 53aee5cf4b
7 changed files with 62 additions and 51 deletions

@ -174,8 +174,9 @@ class AuthPluginTest(utils.TestCase):
@mock.patch.object(pkg_resources, "iter_entry_points", @mock.patch.object(pkg_resources, "iter_entry_points",
mock_iter_entry_points) mock_iter_entry_points)
def test_auth_call(): def test_auth_call():
with self.assertRaises(exceptions.EndpointNotFound): self.assertRaises(
cs = client.Client("username", "password", "project_id", exceptions.EndpointNotFound,
client.Client, "username", "password", "project_id",
auth_system="fakewithauthurl") auth_system="fakewithauthurl")
test_auth_call() test_auth_call()

@ -6,9 +6,6 @@ from tests import utils
class ClientTest(utils.TestCase): class ClientTest(utils.TestCase):
def setUp(self):
pass
def test_get_client_class_v2(self): def test_get_client_class_v2(self):
output = novaclient.client.get_client_class('2') output = novaclient.client.get_client_class('2')
self.assertEqual(output, novaclient.v1_1.client.Client) self.assertEqual(output, novaclient.v1_1.client.Client)

@ -1,7 +1,10 @@
import cStringIO import cStringIO
import os import re
import sys import sys
import fixtures
from testtools import matchers
from novaclient import exceptions from novaclient import exceptions
import novaclient.shell import novaclient.shell
from tests import utils from tests import utils
@ -9,16 +12,18 @@ from tests import utils
class ShellTest(utils.TestCase): class ShellTest(utils.TestCase):
# Patch os.environ to avoid required auth info. FAKE_ENV = {
def setUp(self):
global _old_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()
def setUp(self):
super(ShellTest, self).setUp()
for var in self.FAKE_ENV:
self.useFixture(fixtures.EnvironmentVariable(var,
self.FAKE_ENV[var]))
def shell(self, argstr): def shell(self, argstr):
orig = sys.stdout orig = sys.stdout
@ -36,29 +41,27 @@ class ShellTest(utils.TestCase):
return out return out
def tearDown(self):
global _old_env
os.environ = _old_env
def test_help_unknown_command(self): def test_help_unknown_command(self):
self.assertRaises(exceptions.CommandError, self.shell, 'help foofoo') self.assertRaises(exceptions.CommandError, self.shell, 'help foofoo')
def test_help(self): def test_help(self):
required = [ required = [
'^usage: ', '.*?^usage: ',
'(?m)^\s+root-password\s+Change the root password', '.*?^\s+root-password\s+Change the root password',
'(?m)^See "nova help COMMAND" for help on a specific command', '.*?^See "nova help COMMAND" for help on a specific command',
] ]
help_text = self.shell('help') help_text = self.shell('help')
for r in required: 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): def test_help_on_subcommand(self):
required = [ required = [
'^usage: nova root-password', '.*?^usage: nova root-password',
'(?m)^Change the root password', '.*?^Change the root password',
'(?m)^Positional arguments:', '.*?^Positional arguments:',
] ]
help_text = self.shell('help root-password') help_text = self.shell('help root-password')
for r in required: for r in required:
self.assertRegexpMatches(help_text, r) self.assertThat(help_text,
matchers.MatchesRegex(r, re.DOTALL | re.MULTILINE))

@ -61,6 +61,7 @@ class FakeDisplayManager(FakeManager):
class FindResourceTestCase(test_utils.TestCase): class FindResourceTestCase(test_utils.TestCase):
def setUp(self): def setUp(self):
super(FindResourceTestCase, self).setUp()
self.manager = FakeManager(None) self.manager = FakeManager(None)
def test_find_none(self): def test_find_none(self):

@ -1,8 +1,8 @@
import requests import requests
import unittest2 import testtools
class TestCase(unittest2.TestCase): class TestCase(testtools.TestCase):
TEST_REQUEST_BASE = { TEST_REQUEST_BASE = {
'config': {'danger_mode': False}, 'config': {'danger_mode': False},
'verify': True, 'verify': True,

@ -21,6 +21,8 @@ import mock
import sys import sys
import tempfile import tempfile
import fixtures
import novaclient.shell import novaclient.shell
import novaclient.client import novaclient.client
from novaclient import exceptions from novaclient import exceptions
@ -29,13 +31,25 @@ from tests.v1_1 import fakes
from tests import utils from tests import utils
class ShellFixture(fixtures.Fixture):
def setUp(self):
super(ShellFixture, self).setUp()
self.shell = novaclient.shell.OpenStackComputeShell()
def tearDown(self):
# 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()
class ShellTest(utils.TestCase): class ShellTest(utils.TestCase):
# Patch os.environ to avoid required auth info. FAKE_ENV = {
def setUp(self):
"""Run before each test."""
self.old_environment = os.environ.copy()
os.environ = {
'NOVA_USERNAME': 'username', 'NOVA_USERNAME': 'username',
'NOVA_PASSWORD': 'password', 'NOVA_PASSWORD': 'password',
'NOVA_PROJECT_ID': 'project_id', 'NOVA_PROJECT_ID': 'project_id',
@ -43,25 +57,19 @@ class ShellTest(utils.TestCase):
'NOVA_URL': 'http://no.where', 'NOVA_URL': 'http://no.where',
} }
self.shell = novaclient.shell.OpenStackComputeShell() def setUp(self):
"""Run before each test."""
super(ShellTest, self).setUp()
#HACK(bcwaldon): replace this when we start using stubs for var in self.FAKE_ENV:
self.old_get_client_class = novaclient.client.get_client_class self.useFixture(fixtures.EnvironmentVariable(var,
novaclient.client.get_client_class = lambda *_: fakes.FakeClient self.FAKE_ENV[var]))
self.shell = self.useFixture(ShellFixture()).shell
def tearDown(self): self.useFixture(fixtures.MonkeyPatch(
os.environ = self.old_environment 'novaclient.client.get_client_class',
# For some method like test_image_meta_bad_action we are lambda *_: fakes.FakeClient))
# testing a SystemExit to be thrown and object self.shell has self.addCleanup(timeutils.clear_time_override)
# 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()
#HACK(bcwaldon): replace this when we start using stubs
novaclient.client.get_client_class = self.old_get_client_class
timeutils.clear_time_override()
def run_command(self, cmd): def run_command(self, cmd):
self.shell.main(cmd.split()) self.shell.main(cmd.split())

@ -1,5 +1,6 @@
distribute>=0.6.24 distribute>=0.6.24
fixtures
mock mock
nose nose
nose-exclude nose-exclude
@ -8,4 +9,4 @@ openstack.nose_plugin
nosehtmloutput nosehtmloutput
pep8==1.1 pep8==1.1
sphinx>=1.1.2 sphinx>=1.1.2
unittest2 testtools