summaryrefslogtreecommitdiff
path: root/openstackclient/tests/common/test_clientmanager.py
blob: 5a25fa2c9b3ab2a695bb6a79aa288b795501a8c1 (plain)
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#   Copyright 2012-2013 OpenStack Foundation
#
#   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.
#

from openstackclient.common import clientmanager
from openstackclient.common import restapi
from openstackclient.tests import utils


AUTH_REF = {'a': 1}
AUTH_TOKEN = "foobar"
AUTH_URL = "http://0.0.0.0"
USERNAME = "itchy"
PASSWORD = "scratchy"
SERVICE_CATALOG = {'sc': '123'}


def FakeMakeClient(instance):
    return FakeClient()


class FakeClient(object):
    auth_ref = AUTH_REF
    auth_token = AUTH_TOKEN
    service_catalog = SERVICE_CATALOG


class Container(object):
    attr = clientmanager.ClientCache(lambda x: object())

    def __init__(self):
        pass


class TestClientCache(utils.TestCase):

    def test_singleton(self):
        # NOTE(dtroyer): Verify that the ClientCache descriptor only invokes
        # the factory one time and always returns the same value after that.
        c = Container()
        self.assertEqual(c.attr, c.attr)


class TestClientManager(utils.TestCase):
    def setUp(self):
        super(TestClientManager, self).setUp()

        clientmanager.ClientManager.identity = \
            clientmanager.ClientCache(FakeMakeClient)

    def test_client_manager_token(self):

        client_manager = clientmanager.ClientManager(
            token=AUTH_TOKEN,
            url=AUTH_URL,
            verify=True,
        )

        self.assertEqual(
            AUTH_TOKEN,
            client_manager._token,
        )
        self.assertEqual(
            AUTH_URL,
            client_manager._url,
        )
        self.assertIsInstance(
            client_manager.session,
            restapi.RESTApi,
        )
        self.assertFalse(client_manager._insecure)
        self.assertTrue(client_manager._verify)

    def test_client_manager_password(self):

        client_manager = clientmanager.ClientManager(
            auth_url=AUTH_URL,
            username=USERNAME,
            password=PASSWORD,
            verify=False,
        )

        self.assertEqual(
            AUTH_URL,
            client_manager._auth_url,
        )
        self.assertEqual(
            USERNAME,
            client_manager._username,
        )
        self.assertEqual(
            PASSWORD,
            client_manager._password,
        )
        self.assertIsInstance(
            client_manager.session,
            restapi.RESTApi,
        )
        self.assertTrue(client_manager._insecure)
        self.assertFalse(client_manager._verify)

        # These need to stick around until the old-style clients are gone
        self.assertEqual(
            AUTH_REF,
            client_manager.auth_ref,
        )
        self.assertEqual(
            AUTH_TOKEN,
            client_manager._token,
        )
        self.assertEqual(
            SERVICE_CATALOG,
            client_manager._service_catalog,
        )

    def test_client_manager_password_verify_ca(self):

        client_manager = clientmanager.ClientManager(
            auth_url=AUTH_URL,
            username=USERNAME,
            password=PASSWORD,
            verify='cafile',
        )

        self.assertFalse(client_manager._insecure)
        self.assertTrue(client_manager._verify)
        self.assertEqual('cafile', client_manager._cacert)