File: test_shell.py

package info (click to toggle)
python-blazarclient 4.3.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 460 kB
  • sloc: python: 2,742; makefile: 18; sh: 2
file content (97 lines) | stat: -rw-r--r-- 3,196 bytes parent folder | download | duplicates (3)
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
# Copyright (c) 2014 Mirantis Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import io
import re
import sys

import fixtures
#note(n.s.): you may need it later
#import mock
import testtools

#note(n.s.): you may need it later
#from blazarclient import client as blazar_client
#from blazarclient import exception
from blazarclient import shell
from blazarclient import tests

FAKE_ENV = {'OS_USERNAME': 'username',
            'OS_USER_DOMAIN_ID': 'user_domain_id',
            'OS_PASSWORD': 'password',
            'OS_TENANT_NAME': 'tenant_name',
            'OS_PROJECT_NAME': 'project_name',
            'OS_PROJECT_DOMAIN_ID': 'project_domain_id',
            'OS_AUTH_URL': 'http://no.where'}


class BlazarShellTestCase(tests.TestCase):

    def make_env(self, exclude=None, fake_env=FAKE_ENV):
        env = dict((k, v) for k, v in fake_env.items() if k != exclude)
        self.useFixture(fixtures.MonkeyPatch('os.environ', env))

    def setUp(self):
        super(BlazarShellTestCase, self).setUp()

        #Create shell for non-specific tests
        self.blazar_shell = shell.BlazarShell()

    def shell(self, argstr, exitcodes=(0,)):
        orig = sys.stdout
        orig_stderr = sys.stderr
        try:
            sys.stdout = io.StringIO()
            sys.stderr = io.StringIO()
            _shell = shell.BlazarShell()
            _shell.initialize_app(argstr.split())
        except SystemExit:
            exc_type, exc_value, exc_traceback = sys.exc_info()
            self.assertIn(exc_value.code, exitcodes)
        finally:
            stdout = sys.stdout.getvalue()
            sys.stdout.close()
            sys.stdout = orig
            stderr = sys.stderr.getvalue()
            sys.stderr.close()
            sys.stderr = orig_stderr
        return (stdout, stderr)

    def test_help_unknown_command(self):
        self.assertRaises(ValueError, self.shell, 'bash-completion')

    @testtools.skip('lol')
    def test_bash_completion(self):
        stdout, stderr = self.shell('bash-completion')
        # just check we have some output
        required = [
            '.*--matching',
            '.*--wrap',
            '.*help',
            '.*secgroup-delete-rule',
            '.*--priority']
        for r in required:
            self.assertThat((stdout + stderr),
                            testtools.matchers.MatchesRegex(
                                r, re.DOTALL | re.MULTILINE))

    @testtools.skip('lol')
    def test_authenticate_user(self):
        obj = shell.BlazarShell()
        obj.initialize_app('list-leases')
        obj.options.os_token = 'aaaa-bbbb-cccc'
        obj.options.os_cacert = 'cert'

        obj.authenticate_user()