1c61b41081c3cb57e4faaf6834f5c51bbf88a998
[snaps.git] / snaps / openstack / tests / conf / os_credentials_tests.py
1 # Copyright (c) 2017 Cable Television Laboratories, Inc. ("CableLabs")
2 #                    and others.  All rights reserved.
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at:
7 #
8 #     http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15
16 import logging
17 import unittest
18
19 from snaps.openstack.os_credentials import OSCredsError, OSCreds, \
20     ProxySettings, ProxySettingsError
21
22 __author__ = 'spisarski'
23
24 logger = logging.getLogger('os_credentials_test')
25
26
27 class ProxySettingsUnitTests(unittest.TestCase):
28     """
29     Tests the construction of the ProxySettings class
30     """
31
32     def test_no_params(self):
33         with self.assertRaises(ProxySettingsError):
34             ProxySettings()
35
36     def test_empty_kwargs(self):
37         with self.assertRaises(ProxySettingsError):
38             ProxySettings(**dict())
39
40     def test_host_only(self):
41         with self.assertRaises(ProxySettingsError):
42             ProxySettings(host='foo')
43
44     def test_host_only_kwargs(self):
45         with self.assertRaises(ProxySettingsError):
46             ProxySettings(**{'host': 'foo'})
47
48     def test_port_only(self):
49         with self.assertRaises(ProxySettingsError):
50             ProxySettings(port=1234)
51
52     def test_port_only_kwargs(self):
53         with self.assertRaises(ProxySettingsError):
54             ProxySettings(**{'port': 1234})
55
56     def test_minimum(self):
57         proxy_settings = ProxySettings(host='foo', port=1234)
58         self.assertEqual('foo', proxy_settings.host)
59         self.assertEqual(1234, proxy_settings.port)
60         self.assertIsNone(proxy_settings.ssh_proxy_cmd)
61
62     def test_minimum_kwargs(self):
63         proxy_settings = ProxySettings(**{'host': 'foo', 'port': 1234})
64         self.assertEqual('foo', proxy_settings.host)
65         self.assertEqual(1234, proxy_settings.port)
66         self.assertIsNone(proxy_settings.ssh_proxy_cmd)
67
68     def test_all(self):
69         proxy_settings = ProxySettings(host='foo', port=1234,
70                                        ssh_proxy_cmd='proxy command')
71         self.assertEqual('foo', proxy_settings.host)
72         self.assertEqual(1234, proxy_settings.port)
73         self.assertEqual('proxy command', proxy_settings.ssh_proxy_cmd)
74
75     def test_all_kwargs(self):
76         proxy_settings = ProxySettings(
77             **{'host': 'foo', 'port': 1234, 'ssh_proxy_cmd': 'proxy command'})
78         self.assertEqual('foo', proxy_settings.host)
79         self.assertEqual(1234, proxy_settings.port)
80         self.assertEqual('proxy command', proxy_settings.ssh_proxy_cmd)
81
82
83 class OSCredsUnitTests(unittest.TestCase):
84     """
85     Tests the construction of the OSCreds class
86     """
87
88     def test_no_params(self):
89         with self.assertRaises(OSCredsError):
90             OSCreds()
91
92     def test_empty_kwargs(self):
93         with self.assertRaises(OSCredsError):
94             OSCreds(**dict())
95
96     def test_username_only(self):
97         with self.assertRaises(OSCredsError):
98             OSCreds(username='foo')
99
100     def test_username_only_kwargs(self):
101         with self.assertRaises(OSCredsError):
102             OSCreds(**{'username': 'foo'})
103
104     def test_password_only(self):
105         with self.assertRaises(OSCredsError):
106             OSCreds(password='foo')
107
108     def test_password_only_kwargs(self):
109         with self.assertRaises(OSCredsError):
110             OSCreds(**{'password': 'foo'})
111
112     def test_auth_url_only(self):
113         with self.assertRaises(OSCredsError):
114             OSCreds(auth_url='foo')
115
116     def test_auth_url_only_kwargs(self):
117         with self.assertRaises(OSCredsError):
118             OSCreds(**{'auth_url': 'foo'})
119
120     def test_project_name_only(self):
121         with self.assertRaises(OSCredsError):
122             OSCreds(project_name='foo')
123
124     def test_project_name_only_kwargs(self):
125         with self.assertRaises(OSCredsError):
126             OSCreds(**{'project_name': 'foo'})
127
128     def test_invalid_auth_url(self):
129         with self.assertRaises(OSCredsError):
130             OSCreds(username='foo', password='bar',
131                     auth_url='http://foo.bar', project_name='hello')
132
133     def test_invalid_auth_url_kwargs(self):
134         with self.assertRaises(OSCredsError):
135             OSCreds(**{'username': 'foo', 'password': 'bar',
136                     'auth_url': 'http://foo.bar', 'project_name': 'hello'})
137
138     def test_minimal(self):
139         os_creds = OSCreds(
140             username='foo', password='bar', auth_url='http://foo.bar:5000/v2',
141             project_name='hello')
142         self.assertEqual('foo', os_creds.username)
143         self.assertEqual('bar', os_creds.password)
144         self.assertEqual('http://foo.bar:5000/v2', os_creds.auth_url)
145         self.assertEqual('hello', os_creds.project_name)
146         self.assertIsNone(os_creds.proxy_settings)
147
148     def test_minimal_kwargs(self):
149         os_creds = OSCreds(**{'username': 'foo', 'password': 'bar',
150                               'auth_url': 'http://foo.bar:5000/v2',
151                               'project_name': 'hello'})
152         self.assertEqual('foo', os_creds.username)
153         self.assertEqual('bar', os_creds.password)
154         self.assertEqual('http://foo.bar:5000/v2', os_creds.auth_url)
155         self.assertEqual('hello', os_creds.project_name)
156         self.assertIsNone(os_creds.proxy_settings)
157
158     def test_proxy_settings_obj(self):
159         proxy_settings = ProxySettings(host='foo', port=1234)
160         os_creds = OSCreds(
161             username='foo', password='bar', auth_url='http://foo.bar:5000/v2',
162             project_name='hello', proxy_settings=proxy_settings)
163         self.assertEqual('foo', os_creds.username)
164         self.assertEqual('bar', os_creds.password)
165         self.assertEqual('http://foo.bar:5000/v2', os_creds.auth_url)
166         self.assertEqual('hello', os_creds.project_name)
167         self.assertEqual('foo', os_creds.proxy_settings.host)
168         self.assertEqual(1234, os_creds.proxy_settings.port)
169         self.assertIsNone(os_creds.proxy_settings.ssh_proxy_cmd)
170
171     def test_proxy_settings_obj_kwargs(self):
172         proxy_settings = ProxySettings(host='foo', port=1234)
173         os_creds = OSCreds(**{'username': 'foo', 'password': 'bar',
174                               'auth_url': 'http://foo.bar:5000/v2',
175                               'project_name': 'hello',
176                               'proxy_settings': proxy_settings})
177         self.assertEqual('foo', os_creds.username)
178         self.assertEqual('bar', os_creds.password)
179         self.assertEqual('http://foo.bar:5000/v2', os_creds.auth_url)
180         self.assertEqual('hello', os_creds.project_name)
181         self.assertEqual('foo', os_creds.proxy_settings.host)
182         self.assertEqual(1234, os_creds.proxy_settings.port)
183         self.assertIsNone(os_creds.proxy_settings.ssh_proxy_cmd)
184
185     def test_proxy_settings_dict(self):
186         os_creds = OSCreds(
187             username='foo', password='bar', auth_url='http://foo.bar:5000/v2',
188             project_name='hello', proxy_settings={'host': 'foo', 'port': 1234})
189         self.assertEqual('foo', os_creds.username)
190         self.assertEqual('bar', os_creds.password)
191         self.assertEqual('http://foo.bar:5000/v2', os_creds.auth_url)
192         self.assertEqual('hello', os_creds.project_name)
193         self.assertEqual('foo', os_creds.proxy_settings.host)
194         self.assertEqual(1234, os_creds.proxy_settings.port)
195         self.assertIsNone(os_creds.proxy_settings.ssh_proxy_cmd)
196
197     def test_proxy_settings_dict_kwargs(self):
198         os_creds = OSCreds(**{'username': 'foo', 'password': 'bar',
199                               'auth_url': 'http://foo.bar:5000/v2',
200                               'project_name': 'hello',
201                               'proxy_settings': {'host': 'foo', 'port': 1234}})
202         self.assertEqual('foo', os_creds.username)
203         self.assertEqual('bar', os_creds.password)
204         self.assertEqual('http://foo.bar:5000/v2', os_creds.auth_url)
205         self.assertEqual('hello', os_creds.project_name)
206         self.assertEqual('foo', os_creds.proxy_settings.host)
207         self.assertEqual(1234, os_creds.proxy_settings.port)
208         self.assertIsNone(os_creds.proxy_settings.ssh_proxy_cmd)