summaryrefslogtreecommitdiff
path: root/os_client_config/tests/test_environ.py
blob: 35ce2f2bf4a2b066a28b2dc4185d06d2193a4b51 (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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# Copyright (c) 2014 Hewlett-Packard Development Company, L.P.
#
# 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 os_client_config import cloud_config
from os_client_config import config
from os_client_config import exceptions
from os_client_config.tests import base

import fixtures


class TestEnviron(base.TestCase):

    def setUp(self):
        super(TestEnviron, self).setUp()
        self.useFixture(
            fixtures.EnvironmentVariable('OS_AUTH_URL', 'https://example.com'))
        self.useFixture(
            fixtures.EnvironmentVariable('OS_USERNAME', 'testuser'))
        self.useFixture(
            fixtures.EnvironmentVariable('OS_PASSWORD', 'testpass'))
        self.useFixture(
            fixtures.EnvironmentVariable('OS_PROJECT_NAME', 'testproject'))
        self.useFixture(
            fixtures.EnvironmentVariable('NOVA_PROJECT_ID', 'testnova'))

    def test_get_one_cloud(self):
        c = config.OpenStackConfig(config_files=[self.cloud_yaml],
                                   vendor_files=[self.vendor_yaml])
        self.assertIsInstance(c.get_one_cloud(), cloud_config.CloudConfig)

    def test_no_fallthrough(self):
        c = config.OpenStackConfig(config_files=[self.cloud_yaml],
                                   vendor_files=[self.vendor_yaml])
        self.assertRaises(
            exceptions.OpenStackConfigException, c.get_one_cloud, 'openstack')

    def test_envvar_name_override(self):
        self.useFixture(
            fixtures.EnvironmentVariable('OS_CLOUD_NAME', 'override'))
        c = config.OpenStackConfig(config_files=[self.cloud_yaml],
                                   vendor_files=[self.vendor_yaml])
        cc = c.get_one_cloud('override')
        self._assert_cloud_details(cc)

    def test_envvar_prefer_ipv6_override(self):
        self.useFixture(
            fixtures.EnvironmentVariable('OS_PREFER_IPV6', 'false'))
        c = config.OpenStackConfig(config_files=[self.cloud_yaml],
                                   vendor_files=[self.vendor_yaml],
                                   secure_files=[self.secure_yaml])
        cc = c.get_one_cloud('_test-cloud_')
        self.assertFalse(cc.prefer_ipv6)

    def test_environ_exists(self):
        c = config.OpenStackConfig(config_files=[self.cloud_yaml],
                                   vendor_files=[self.vendor_yaml],
                                   secure_files=[self.secure_yaml])
        cc = c.get_one_cloud('envvars')
        self._assert_cloud_details(cc)
        self.assertNotIn('auth_url', cc.config)
        self.assertIn('auth_url', cc.config['auth'])
        self.assertNotIn('project_id', cc.config['auth'])
        self.assertNotIn('auth_url', cc.config)
        cc = c.get_one_cloud('_test-cloud_')
        self._assert_cloud_details(cc)
        cc = c.get_one_cloud('_test_cloud_no_vendor')
        self._assert_cloud_details(cc)

    def test_environ_prefix(self):
        c = config.OpenStackConfig(config_files=[self.cloud_yaml],
                                   vendor_files=[self.vendor_yaml],
                                   envvar_prefix='NOVA_',
                                   secure_files=[self.secure_yaml])
        cc = c.get_one_cloud('envvars')
        self._assert_cloud_details(cc)
        self.assertNotIn('auth_url', cc.config)
        self.assertIn('auth_url', cc.config['auth'])
        self.assertIn('project_id', cc.config['auth'])
        self.assertNotIn('auth_url', cc.config)
        cc = c.get_one_cloud('_test-cloud_')
        self._assert_cloud_details(cc)
        cc = c.get_one_cloud('_test_cloud_no_vendor')
        self._assert_cloud_details(cc)

    def test_get_one_cloud_with_config_files(self):
        c = config.OpenStackConfig(config_files=[self.cloud_yaml],
                                   vendor_files=[self.vendor_yaml],
                                   secure_files=[self.secure_yaml])
        self.assertIsInstance(c.cloud_config, dict)
        self.assertIn('cache', c.cloud_config)
        self.assertIsInstance(c.cloud_config['cache'], dict)
        self.assertIn('max_age', c.cloud_config['cache'])
        self.assertIn('path', c.cloud_config['cache'])
        cc = c.get_one_cloud('_test-cloud_')
        self._assert_cloud_details(cc)
        cc = c.get_one_cloud('_test_cloud_no_vendor')
        self._assert_cloud_details(cc)

    def test_config_file_override(self):
        self.useFixture(
            fixtures.EnvironmentVariable(
                'OS_CLIENT_CONFIG_FILE', self.cloud_yaml))
        c = config.OpenStackConfig(config_files=[],
                                   vendor_files=[self.vendor_yaml])
        cc = c.get_one_cloud('_test-cloud_')
        self._assert_cloud_details(cc)


class TestEnvvars(base.TestCase):

    def test_no_envvars(self):
        self.useFixture(
            fixtures.EnvironmentVariable('NOVA_USERNAME', 'nova'))
        c = config.OpenStackConfig(config_files=[self.cloud_yaml],
                                   vendor_files=[self.vendor_yaml])
        self.assertRaises(
            exceptions.OpenStackConfigException, c.get_one_cloud, 'envvars')

    def test_test_envvars(self):
        self.useFixture(
            fixtures.EnvironmentVariable('NOVA_USERNAME', 'nova'))
        self.useFixture(
            fixtures.EnvironmentVariable('OS_STDERR_CAPTURE', 'True'))
        c = config.OpenStackConfig(config_files=[self.cloud_yaml],
                                   vendor_files=[self.vendor_yaml])
        self.assertRaises(
            exceptions.OpenStackConfigException, c.get_one_cloud, 'envvars')

    def test_incomplete_envvars(self):
        self.useFixture(
            fixtures.EnvironmentVariable('NOVA_USERNAME', 'nova'))
        self.useFixture(
            fixtures.EnvironmentVariable('OS_USERNAME', 'user'))
        config.OpenStackConfig(config_files=[self.cloud_yaml],
                               vendor_files=[self.vendor_yaml])
        # This is broken due to an issue that's fixed in a subsequent patch
        # commenting it out in this patch to keep the patch size reasonable
        # self.assertRaises(
        #     keystoneauth1.exceptions.auth_plugins.MissingRequiredOptions,
        #     c.get_one_cloud, 'envvars')

    def test_have_envvars(self):
        self.useFixture(
            fixtures.EnvironmentVariable('NOVA_USERNAME', 'nova'))
        self.useFixture(
            fixtures.EnvironmentVariable('OS_AUTH_URL', 'http://example.com'))
        self.useFixture(
            fixtures.EnvironmentVariable('OS_USERNAME', 'user'))
        self.useFixture(
            fixtures.EnvironmentVariable('OS_PASSWORD', 'password'))
        self.useFixture(
            fixtures.EnvironmentVariable('OS_PROJECT_NAME', 'project'))
        c = config.OpenStackConfig(config_files=[self.cloud_yaml],
                                   vendor_files=[self.vendor_yaml])
        cc = c.get_one_cloud('envvars')
        self.assertEqual(cc.config['auth']['username'], 'user')

    def test_old_envvars(self):
        self.useFixture(
            fixtures.EnvironmentVariable('NOVA_USERNAME', 'nova'))
        self.useFixture(
            fixtures.EnvironmentVariable(
                'NOVA_AUTH_URL', 'http://example.com'))
        self.useFixture(
            fixtures.EnvironmentVariable('NOVA_PASSWORD', 'password'))
        self.useFixture(
            fixtures.EnvironmentVariable('NOVA_PROJECT_NAME', 'project'))
        c = config.OpenStackConfig(config_files=[self.cloud_yaml],
                                   vendor_files=[self.vendor_yaml],
                                   envvar_prefix='NOVA_')
        cc = c.get_one_cloud('envvars')
        self.assertEqual(cc.config['auth']['username'], 'nova')