~ubuntu-branches/ubuntu/trusty/python-ceilometerclient/trusty

« back to all changes in this revision

Viewing changes to ceilometerclient/tests/test_shell.py

  • Committer: Package Import Robot
  • Author(s): Chuck Short
  • Date: 2013-08-13 11:27:54 UTC
  • mfrom: (1.1.3)
  • Revision ID: package-import@ubuntu.com-20130813112754-fz58btmvrw9gsrhn
Tags: 1.0.3-0ubuntu1
* New upstream release. 
* debian/patches/fix-keystoneclient-version.patch: Dropped no longer needed. 

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
4
#
 
5
#       http://www.apache.org/licenses/LICENSE-2.0
 
6
#
 
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
 
11
#   under the License.
 
12
 
 
13
import cStringIO
 
14
import httplib2
 
15
import re
 
16
import sys
 
17
 
 
18
import fixtures
 
19
from testtools import matchers
 
20
 
 
21
from keystoneclient.v2_0 import client as ksclient
 
22
 
 
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
 
27
 
 
28
FAKE_ENV = {'OS_USERNAME': 'username',
 
29
            'OS_PASSWORD': 'password',
 
30
            'OS_TENANT_NAME': 'tenant_name',
 
31
            'OS_AUTH_URL': 'http://no.where'}
 
32
 
 
33
 
 
34
class ShellTest(utils.BaseTestCase):
 
35
    re_options = re.DOTALL | re.MULTILINE
 
36
 
 
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))
 
41
 
 
42
    def setUp(self):
 
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')
 
47
 
 
48
    def shell(self, argstr):
 
49
        orig = sys.stdout
 
50
        try:
 
51
            sys.stdout = cStringIO.StringIO()
 
52
            _shell = ceilometer_shell.CeilometerShell()
 
53
            _shell.main(argstr.split())
 
54
        except SystemExit:
 
55
            exc_type, exc_value, exc_traceback = sys.exc_info()
 
56
            self.assertEqual(exc_value.code, 0)
 
57
        finally:
 
58
            out = sys.stdout.getvalue()
 
59
            sys.stdout.close()
 
60
            sys.stdout = orig
 
61
 
 
62
        return out
 
63
 
 
64
    def test_help_unknown_command(self):
 
65
        self.assertRaises(exc.CommandError, self.shell, 'help foofoo')
 
66
 
 
67
    def test_debug(self):
 
68
        httplib2.debuglevel = 0
 
69
        self.shell('--debug help')
 
70
        self.assertEqual(httplib2.debuglevel, 1)
 
71
 
 
72
    def test_help(self):
 
73
        required = [
 
74
            '.*?^usage: ceilometer',
 
75
            '.*?^See "ceilometer help COMMAND" '
 
76
            'for help on a specific command',
 
77
        ]
 
78
        for argstr in ['--help', 'help']:
 
79
            help_text = self.shell(argstr)
 
80
            for r in required:
 
81
                self.assertThat(help_text,
 
82
                                matchers.MatchesRegex(r,
 
83
                                                      self.re_options))
 
84
 
 
85
    def test_help_on_subcommand(self):
 
86
        required = [
 
87
            '.*?^usage: ceilometer meter-list',
 
88
            ".*?^List the user's meter",
 
89
        ]
 
90
        argstrings = [
 
91
            'help meter-list',
 
92
        ]
 
93
        for argstr in argstrings:
 
94
            help_text = self.shell(argstr)
 
95
            for r in required:
 
96
                self.assertThat(help_text,
 
97
                                matchers.MatchesRegex(r, self.re_options))
 
98
 
 
99
    def test_auth_param(self):
 
100
        self.make_env(exclude='OS_USERNAME')
 
101
        self.test_help()