python-novaclient/tests/test_shell.py
Monty Taylor 53aee5cf4b 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
2013-01-03 11:58:13 -08:00

68 lines
2.0 KiB
Python

import cStringIO
import re
import sys
import fixtures
from testtools import matchers
from novaclient import exceptions
import novaclient.shell
from tests import utils
class ShellTest(utils.TestCase):
FAKE_ENV = {
'OS_USERNAME': 'username',
'OS_PASSWORD': 'password',
'OS_TENANT_NAME': 'tenant_name',
'OS_AUTH_URL': 'http://no.where',
}
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):
orig = sys.stdout
try:
sys.stdout = cStringIO.StringIO()
_shell = novaclient.shell.OpenStackComputeShell()
_shell.main(argstr.split())
except SystemExit:
exc_type, exc_value, exc_traceback = sys.exc_info()
self.assertEqual(exc_value.code, 0)
finally:
out = sys.stdout.getvalue()
sys.stdout.close()
sys.stdout = orig
return out
def test_help_unknown_command(self):
self.assertRaises(exceptions.CommandError, self.shell, 'help foofoo')
def test_help(self):
required = [
'.*?^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.assertThat(help_text,
matchers.MatchesRegex(r, re.DOTALL | re.MULTILINE))
def test_help_on_subcommand(self):
required = [
'.*?^usage: nova root-password',
'.*?^Change the root password',
'.*?^Positional arguments:',
]
help_text = self.shell('help root-password')
for r in required:
self.assertThat(help_text,
matchers.MatchesRegex(r, re.DOTALL | re.MULTILINE))