2012-04-05 13:24:50 +01:00
|
|
|
import cStringIO
|
2011-08-03 17:41:33 -04:00
|
|
|
import os
|
2012-04-05 13:24:50 +01:00
|
|
|
import sys
|
2011-08-03 17:41:33 -04:00
|
|
|
|
|
|
|
from novaclient import exceptions
|
2011-12-09 14:26:06 -05:00
|
|
|
import novaclient.shell
|
2011-08-03 17:41:33 -04:00
|
|
|
from tests import utils
|
|
|
|
|
|
|
|
|
|
|
|
class ShellTest(utils.TestCase):
|
|
|
|
|
|
|
|
# Patch os.environ to avoid required auth info.
|
|
|
|
def setUp(self):
|
|
|
|
global _old_env
|
|
|
|
fake_env = {
|
2012-02-02 10:32:30 -06:00
|
|
|
'OS_USERNAME': 'username',
|
|
|
|
'OS_PASSWORD': 'password',
|
|
|
|
'OS_TENANT_NAME': 'tenant_name',
|
|
|
|
'OS_AUTH_URL': 'http://no.where',
|
2011-08-03 17:41:33 -04:00
|
|
|
}
|
|
|
|
_old_env, os.environ = os.environ, fake_env.copy()
|
|
|
|
|
2012-04-05 13:24:50 +01:00
|
|
|
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
|
2011-08-03 17:41:33 -04:00
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
global _old_env
|
|
|
|
os.environ = _old_env
|
|
|
|
|
|
|
|
def test_help_unknown_command(self):
|
2012-04-05 13:24:50 +01:00
|
|
|
self.assertRaises(exceptions.CommandError, self.shell, 'help foofoo')
|
2011-08-03 17:41:33 -04:00
|
|
|
|
2012-04-05 13:24:50 +01:00
|
|
|
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',
|
|
|
|
]
|
2012-11-08 21:03:13 +01:00
|
|
|
help_text = self.shell('help')
|
|
|
|
for r in required:
|
|
|
|
self.assertRegexpMatches(help_text, r)
|
2012-04-05 13:24:50 +01:00
|
|
|
|
|
|
|
def test_help_on_subcommand(self):
|
|
|
|
required = [
|
|
|
|
'^usage: nova root-password',
|
|
|
|
'(?m)^Change the root password',
|
|
|
|
'(?m)^Positional arguments:',
|
|
|
|
]
|
2012-11-08 21:03:13 +01:00
|
|
|
help_text = self.shell('help root-password')
|
|
|
|
for r in required:
|
|
|
|
self.assertRegexpMatches(help_text, r)
|