1
# Licensed under the Apache License, Version 2.0 (the "License"); you may
2
# not use this file except in compliance with the License. You may obtain
3
# a copy of the License at
5
# http://www.apache.org/licenses/LICENSE-2.0
7
# Unless required by applicable law or agreed to in writing, software
8
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10
# License for the specific language governing permissions and limitations
19
from testtools import matchers
21
from keystoneclient.v2_0 import client as ksclient
23
from ceilometerclient import exc
24
from ceilometerclient import shell as ceilometer_shell
25
from ceilometerclient.tests import utils
26
from ceilometerclient.v1 import client as v1client
28
FAKE_ENV = {'OS_USERNAME': 'username',
29
'OS_PASSWORD': 'password',
30
'OS_TENANT_NAME': 'tenant_name',
31
'OS_AUTH_URL': 'http://no.where'}
34
class ShellTest(utils.BaseTestCase):
35
re_options = re.DOTALL | re.MULTILINE
37
# Patch os.environ to avoid required auth info.
38
def make_env(self, exclude=None):
39
env = dict((k, v) for k, v in FAKE_ENV.items() if k != exclude)
40
self.useFixture(fixtures.MonkeyPatch('os.environ', env))
43
super(ShellTest, self).setUp()
44
self.m.StubOutWithMock(ksclient, 'Client')
45
self.m.StubOutWithMock(v1client.Client, 'json_request')
46
self.m.StubOutWithMock(v1client.Client, 'raw_request')
48
def shell(self, argstr):
51
sys.stdout = cStringIO.StringIO()
52
_shell = ceilometer_shell.CeilometerShell()
53
_shell.main(argstr.split())
55
exc_type, exc_value, exc_traceback = sys.exc_info()
56
self.assertEqual(exc_value.code, 0)
58
out = sys.stdout.getvalue()
64
def test_help_unknown_command(self):
65
self.assertRaises(exc.CommandError, self.shell, 'help foofoo')
68
httplib2.debuglevel = 0
69
self.shell('--debug help')
70
self.assertEqual(httplib2.debuglevel, 1)
74
'.*?^usage: ceilometer',
75
'.*?^See "ceilometer help COMMAND" '
76
'for help on a specific command',
78
for argstr in ['--help', 'help']:
79
help_text = self.shell(argstr)
81
self.assertThat(help_text,
82
matchers.MatchesRegex(r,
85
def test_help_on_subcommand(self):
87
'.*?^usage: ceilometer meter-list',
88
".*?^List the user's meter",
93
for argstr in argstrings:
94
help_text = self.shell(argstr)
96
self.assertThat(help_text,
97
matchers.MatchesRegex(r, self.re_options))
99
def test_auth_param(self):
100
self.make_env(exclude='OS_USERNAME')