diff options
author | spisarski <s.pisarski@cablelabs.com> | 2017-06-28 09:20:13 -0600 |
---|---|---|
committer | spisarski <s.pisarski@cablelabs.com> | 2017-07-05 08:47:29 -0600 |
commit | 139af8603aa484c1ba08997f30204fb7be146606 (patch) | |
tree | d15a7b71a1a5780e5b4447071a9fb886d38f5d5e /snaps/openstack/tests | |
parent | 2a4dc5493763a241c9895652bf1c71ac3ef0e5b3 (diff) |
Refactor OSCreds to leverage kwargs instead of named parameters.
JIRA: SNAPS-109
Change-Id: I423ede964cce9fc3b4e5b27f1e8f0dd7603d9ff1
Signed-off-by: spisarski <s.pisarski@cablelabs.com>
Diffstat (limited to 'snaps/openstack/tests')
-rw-r--r-- | snaps/openstack/tests/conf/os_credentials_tests.py | 208 | ||||
-rw-r--r-- | snaps/openstack/tests/openstack_tests.py | 10 |
2 files changed, 214 insertions, 4 deletions
diff --git a/snaps/openstack/tests/conf/os_credentials_tests.py b/snaps/openstack/tests/conf/os_credentials_tests.py new file mode 100644 index 0000000..1c61b41 --- /dev/null +++ b/snaps/openstack/tests/conf/os_credentials_tests.py @@ -0,0 +1,208 @@ +# Copyright (c) 2017 Cable Television Laboratories, Inc. ("CableLabs") +# and others. All rights reserved. +# +# 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 logging +import unittest + +from snaps.openstack.os_credentials import OSCredsError, OSCreds, \ + ProxySettings, ProxySettingsError + +__author__ = 'spisarski' + +logger = logging.getLogger('os_credentials_test') + + +class ProxySettingsUnitTests(unittest.TestCase): + """ + Tests the construction of the ProxySettings class + """ + + def test_no_params(self): + with self.assertRaises(ProxySettingsError): + ProxySettings() + + def test_empty_kwargs(self): + with self.assertRaises(ProxySettingsError): + ProxySettings(**dict()) + + def test_host_only(self): + with self.assertRaises(ProxySettingsError): + ProxySettings(host='foo') + + def test_host_only_kwargs(self): + with self.assertRaises(ProxySettingsError): + ProxySettings(**{'host': 'foo'}) + + def test_port_only(self): + with self.assertRaises(ProxySettingsError): + ProxySettings(port=1234) + + def test_port_only_kwargs(self): + with self.assertRaises(ProxySettingsError): + ProxySettings(**{'port': 1234}) + + def test_minimum(self): + proxy_settings = ProxySettings(host='foo', port=1234) + self.assertEqual('foo', proxy_settings.host) + self.assertEqual(1234, proxy_settings.port) + self.assertIsNone(proxy_settings.ssh_proxy_cmd) + + def test_minimum_kwargs(self): + proxy_settings = ProxySettings(**{'host': 'foo', 'port': 1234}) + self.assertEqual('foo', proxy_settings.host) + self.assertEqual(1234, proxy_settings.port) + self.assertIsNone(proxy_settings.ssh_proxy_cmd) + + def test_all(self): + proxy_settings = ProxySettings(host='foo', port=1234, + ssh_proxy_cmd='proxy command') + self.assertEqual('foo', proxy_settings.host) + self.assertEqual(1234, proxy_settings.port) + self.assertEqual('proxy command', proxy_settings.ssh_proxy_cmd) + + def test_all_kwargs(self): + proxy_settings = ProxySettings( + **{'host': 'foo', 'port': 1234, 'ssh_proxy_cmd': 'proxy command'}) + self.assertEqual('foo', proxy_settings.host) + self.assertEqual(1234, proxy_settings.port) + self.assertEqual('proxy command', proxy_settings.ssh_proxy_cmd) + + +class OSCredsUnitTests(unittest.TestCase): + """ + Tests the construction of the OSCreds class + """ + + def test_no_params(self): + with self.assertRaises(OSCredsError): + OSCreds() + + def test_empty_kwargs(self): + with self.assertRaises(OSCredsError): + OSCreds(**dict()) + + def test_username_only(self): + with self.assertRaises(OSCredsError): + OSCreds(username='foo') + + def test_username_only_kwargs(self): + with self.assertRaises(OSCredsError): + OSCreds(**{'username': 'foo'}) + + def test_password_only(self): + with self.assertRaises(OSCredsError): + OSCreds(password='foo') + + def test_password_only_kwargs(self): + with self.assertRaises(OSCredsError): + OSCreds(**{'password': 'foo'}) + + def test_auth_url_only(self): + with self.assertRaises(OSCredsError): + OSCreds(auth_url='foo') + + def test_auth_url_only_kwargs(self): + with self.assertRaises(OSCredsError): + OSCreds(**{'auth_url': 'foo'}) + + def test_project_name_only(self): + with self.assertRaises(OSCredsError): + OSCreds(project_name='foo') + + def test_project_name_only_kwargs(self): + with self.assertRaises(OSCredsError): + OSCreds(**{'project_name': 'foo'}) + + def test_invalid_auth_url(self): + with self.assertRaises(OSCredsError): + OSCreds(username='foo', password='bar', + auth_url='http://foo.bar', project_name='hello') + + def test_invalid_auth_url_kwargs(self): + with self.assertRaises(OSCredsError): + OSCreds(**{'username': 'foo', 'password': 'bar', + 'auth_url': 'http://foo.bar', 'project_name': 'hello'}) + + def test_minimal(self): + os_creds = OSCreds( + username='foo', password='bar', auth_url='http://foo.bar:5000/v2', + project_name='hello') + self.assertEqual('foo', os_creds.username) + self.assertEqual('bar', os_creds.password) + self.assertEqual('http://foo.bar:5000/v2', os_creds.auth_url) + self.assertEqual('hello', os_creds.project_name) + self.assertIsNone(os_creds.proxy_settings) + + def test_minimal_kwargs(self): + os_creds = OSCreds(**{'username': 'foo', 'password': 'bar', + 'auth_url': 'http://foo.bar:5000/v2', + 'project_name': 'hello'}) + self.assertEqual('foo', os_creds.username) + self.assertEqual('bar', os_creds.password) + self.assertEqual('http://foo.bar:5000/v2', os_creds.auth_url) + self.assertEqual('hello', os_creds.project_name) + self.assertIsNone(os_creds.proxy_settings) + + def test_proxy_settings_obj(self): + proxy_settings = ProxySettings(host='foo', port=1234) + os_creds = OSCreds( + username='foo', password='bar', auth_url='http://foo.bar:5000/v2', + project_name='hello', proxy_settings=proxy_settings) + self.assertEqual('foo', os_creds.username) + self.assertEqual('bar', os_creds.password) + self.assertEqual('http://foo.bar:5000/v2', os_creds.auth_url) + self.assertEqual('hello', os_creds.project_name) + self.assertEqual('foo', os_creds.proxy_settings.host) + self.assertEqual(1234, os_creds.proxy_settings.port) + self.assertIsNone(os_creds.proxy_settings.ssh_proxy_cmd) + + def test_proxy_settings_obj_kwargs(self): + proxy_settings = ProxySettings(host='foo', port=1234) + os_creds = OSCreds(**{'username': 'foo', 'password': 'bar', + 'auth_url': 'http://foo.bar:5000/v2', + 'project_name': 'hello', + 'proxy_settings': proxy_settings}) + self.assertEqual('foo', os_creds.username) + self.assertEqual('bar', os_creds.password) + self.assertEqual('http://foo.bar:5000/v2', os_creds.auth_url) + self.assertEqual('hello', os_creds.project_name) + self.assertEqual('foo', os_creds.proxy_settings.host) + self.assertEqual(1234, os_creds.proxy_settings.port) + self.assertIsNone(os_creds.proxy_settings.ssh_proxy_cmd) + + def test_proxy_settings_dict(self): + os_creds = OSCreds( + username='foo', password='bar', auth_url='http://foo.bar:5000/v2', + project_name='hello', proxy_settings={'host': 'foo', 'port': 1234}) + self.assertEqual('foo', os_creds.username) + self.assertEqual('bar', os_creds.password) + self.assertEqual('http://foo.bar:5000/v2', os_creds.auth_url) + self.assertEqual('hello', os_creds.project_name) + self.assertEqual('foo', os_creds.proxy_settings.host) + self.assertEqual(1234, os_creds.proxy_settings.port) + self.assertIsNone(os_creds.proxy_settings.ssh_proxy_cmd) + + def test_proxy_settings_dict_kwargs(self): + os_creds = OSCreds(**{'username': 'foo', 'password': 'bar', + 'auth_url': 'http://foo.bar:5000/v2', + 'project_name': 'hello', + 'proxy_settings': {'host': 'foo', 'port': 1234}}) + self.assertEqual('foo', os_creds.username) + self.assertEqual('bar', os_creds.password) + self.assertEqual('http://foo.bar:5000/v2', os_creds.auth_url) + self.assertEqual('hello', os_creds.project_name) + self.assertEqual('foo', os_creds.proxy_settings.host) + self.assertEqual(1234, os_creds.proxy_settings.port) + self.assertIsNone(os_creds.proxy_settings.ssh_proxy_cmd) diff --git a/snaps/openstack/tests/openstack_tests.py b/snaps/openstack/tests/openstack_tests.py index d0c250f..59c7395 100644 --- a/snaps/openstack/tests/openstack_tests.py +++ b/snaps/openstack/tests/openstack_tests.py @@ -83,7 +83,8 @@ def get_credentials(os_env_file=None, proxy_settings_str=None, proxy_settings = None if proxy_settings_str: tokens = re.split(':', proxy_settings_str) - proxy_settings = ProxySettings(tokens[0], tokens[1], ssh_proxy_cmd) + proxy_settings = ProxySettings(host=tokens[0], port=tokens[1], + ssh_proxy_cmd=ssh_proxy_cmd) if config.get('OS_CACERT'): https_cacert = config.get('OS_CACERT') @@ -121,8 +122,9 @@ def get_credentials(os_env_file=None, proxy_settings_str=None, proxy_str = config.get('http_proxy') if proxy_str: tokens = re.split(':', proxy_str) - proxy_settings = ProxySettings(tokens[0], tokens[1], - config.get('ssh_proxy_cmd')) + proxy_settings = ProxySettings( + host=tokens[0], port=tokens[1], + ssh_proxy_cmd=config.get('ssh_proxy_cmd')) os_creds = OSCreds(username=config['username'], password=config['password'], @@ -132,7 +134,7 @@ def get_credentials(os_env_file=None, proxy_settings_str=None, image_api_version=image_api_version, proxy_settings=proxy_settings) - logger.info('OS Credentials = ' + str(os_creds)) + logger.info('OS Credentials = %s', os_creds) return os_creds |