1 # Copyright (c) 2017 Cable Television Laboratories, Inc. ("CableLabs")
2 # and others. All rights reserved.
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:
8 # http://www.apache.org/licenses/LICENSE-2.0
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.
19 from snaps.openstack.os_credentials import OSCredsError, OSCreds, \
20 ProxySettings, ProxySettingsError
22 __author__ = 'spisarski'
24 logger = logging.getLogger('os_credentials_test')
27 class ProxySettingsUnitTests(unittest.TestCase):
29 Tests the construction of the ProxySettings class
32 def test_no_params(self):
33 with self.assertRaises(ProxySettingsError):
36 def test_empty_kwargs(self):
37 with self.assertRaises(ProxySettingsError):
38 ProxySettings(**dict())
40 def test_host_only(self):
41 with self.assertRaises(ProxySettingsError):
42 ProxySettings(host='foo')
44 def test_host_only_kwargs(self):
45 with self.assertRaises(ProxySettingsError):
46 ProxySettings(**{'host': 'foo'})
48 def test_port_only(self):
49 with self.assertRaises(ProxySettingsError):
50 ProxySettings(port=1234)
52 def test_port_only_kwargs(self):
53 with self.assertRaises(ProxySettingsError):
54 ProxySettings(**{'port': 1234})
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)
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)
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)
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)
83 class OSCredsUnitTests(unittest.TestCase):
85 Tests the construction of the OSCreds class
88 def test_no_params(self):
89 with self.assertRaises(OSCredsError):
92 def test_empty_kwargs(self):
93 with self.assertRaises(OSCredsError):
96 def test_username_only(self):
97 with self.assertRaises(OSCredsError):
98 OSCreds(username='foo')
100 def test_username_only_kwargs(self):
101 with self.assertRaises(OSCredsError):
102 OSCreds(**{'username': 'foo'})
104 def test_password_only(self):
105 with self.assertRaises(OSCredsError):
106 OSCreds(password='foo')
108 def test_password_only_kwargs(self):
109 with self.assertRaises(OSCredsError):
110 OSCreds(**{'password': 'foo'})
112 def test_auth_url_only(self):
113 with self.assertRaises(OSCredsError):
114 OSCreds(auth_url='foo')
116 def test_auth_url_only_kwargs(self):
117 with self.assertRaises(OSCredsError):
118 OSCreds(**{'auth_url': 'foo'})
120 def test_project_name_only(self):
121 with self.assertRaises(OSCredsError):
122 OSCreds(project_name='foo')
124 def test_project_name_only_kwargs(self):
125 with self.assertRaises(OSCredsError):
126 OSCreds(**{'project_name': 'foo'})
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')
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'})
138 def test_minimal(self):
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)
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)
158 def test_proxy_settings_obj(self):
159 proxy_settings = ProxySettings(host='foo', port=1234)
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)
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)
185 def test_proxy_settings_dict(self):
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)
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)