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

View File

@ -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()

View File

@ -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)

View File

@ -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))

View File

@ -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):

View File

@ -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,

View File

@ -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())

View File

@ -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