forked from openstack-archive/python-ceilometerclient
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_shell.py
More file actions
122 lines (103 loc) · 3.53 KB
/
Copy pathtest_shell.py
File metadata and controls
122 lines (103 loc) · 3.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import cStringIO
import os
import httplib2
import sys
import mox
import unittest
import unittest2
try:
import json
except ImportError:
import simplejson as json
from keystoneclient.v2_0 import client as ksclient
from ceilometerclient import exc
from ceilometerclient.v1 import client as v1client
import ceilometerclient.shell
class ShellValidationTest(unittest.TestCase):
def shell_error(self, argstr, error_match):
orig = sys.stderr
try:
sys.stderr = cStringIO.StringIO()
_shell = ceilometerclient.shell.CeilometerShell()
_shell.main(argstr.split())
except exc.CommandError as e:
self.assertRegexpMatches(e.__str__(), error_match)
else:
self.fail('Expected error matching: %s' % error_match)
finally:
err = sys.stderr.getvalue()
sys.stderr.close()
sys.stderr = orig
return err
class ShellTest(unittest2.TestCase):
# Patch os.environ to avoid required auth info.
def setUp(self):
self.m = mox.Mox()
self.m.StubOutWithMock(ksclient, 'Client')
self.m.StubOutWithMock(v1client.Client, 'json_request')
self.m.StubOutWithMock(v1client.Client, 'raw_request')
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()
def tearDown(self):
self.m.UnsetStubs()
global _old_env
os.environ = _old_env
def shell(self, argstr):
orig = sys.stdout
try:
sys.stdout = cStringIO.StringIO()
_shell = ceilometerclient.shell.CeilometerShell()
_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(exc.CommandError, self.shell, 'help foofoo')
def test_debug(self):
httplib2.debuglevel = 0
self.shell('--debug help')
self.assertEqual(httplib2.debuglevel, 1)
def test_help(self):
required = [
'^usage: ceilometer',
'(?m)^See "ceilometer help COMMAND" for help on a specific command',
]
for argstr in ['--help', 'help']:
help_text = self.shell(argstr)
for r in required:
self.assertRegexpMatches(help_text, r)
def test_help_on_subcommand(self):
required = [
'^usage: ceilometer meter-list',
"(?m)^List the user's meter",
]
argstrings = [
'help meter-list',
]
for argstr in argstrings:
help_text = self.shell(argstr)
for r in required:
self.assertRegexpMatches(help_text, r)
def test_auth_param(self):
class TokenContext(object):
def __enter__(self):
fake_env = {
'OS_AUTH_TOKEN': 'token',
'CEILOMETER_URL': 'http://no.where'
}
self.old_env, os.environ = os.environ, fake_env.copy()
def __exit__(self, exc_type, exc_value, traceback):
os.environ = self.old_env
with TokenContext():
self.test_help()