diff --git a/python-portasclient/portasclient/common/base.py b/python-portasclient/portasclient/common/base.py index 4115a84..e92def9 100644 --- a/python-portasclient/portasclient/common/base.py +++ b/python-portasclient/portasclient/common/base.py @@ -49,14 +49,16 @@ class Manager(object): def __init__(self, api): self.api = api - def _list(self, url, response_key=None, obj_class=None, body=None, headers={}): + def _list(self, url, response_key=None, obj_class=None, + body=None, headers={}): + resp, body = self.api.json_request('GET', url, headers=headers) if obj_class is None: obj_class = self.resource_class if response_key: - if not body.has_key(response_key): + if not response_key in body: body[response_key] = [] data = body[response_key] else: @@ -74,9 +76,12 @@ class Manager(object): return self.resource_class(self, body[response_key]) return self.resource_class(self, body) - def _create(self, url, body=None, response_key=None, return_raw=False, headers={}): + def _create(self, url, body=None, response_key=None, + return_raw=False, headers={}): + if body: - resp, body = self.api.json_request('POST', url, body=body, headers=headers) + resp, body = self.api.json_request('POST', url, + body=body, headers=headers) else: resp, body = self.api.json_request('POST', url, headers=headers) if return_raw: diff --git a/python-portasclient/portasclient/common/utils.py b/python-portasclient/portasclient/common/utils.py index 549a3b4..3b4d49d 100644 --- a/python-portasclient/portasclient/common/utils.py +++ b/python-portasclient/portasclient/common/utils.py @@ -22,7 +22,6 @@ import prettytable from portasclient.openstack.common import importutils - # Decorator for cli-args def arg(*args, **kwargs): def _decorator(func): diff --git a/python-portasclient/portasclient/shell.py b/python-portasclient/portasclient/shell.py index d57c0f4..124f38b 100644 --- a/python-portasclient/portasclient/shell.py +++ b/python-portasclient/portasclient/shell.py @@ -41,8 +41,7 @@ class PortasShell(object): # Global arguments parser.add_argument('-h', '--help', action='store_true', - help=argparse.SUPPRESS, - ) + help=argparse.SUPPRESS,) parser.add_argument('-d', '--debug', default=bool(utils.env('PORTASCLIENT_DEBUG')), @@ -60,24 +59,24 @@ class PortasShell(object): "\"insecure\" SSL (https) requests. " "The server's certificate will " "not be verified against any certificate " - "authorities. This option should be used with " - "caution.") + "authorities. This option should be used " + "with caution.") parser.add_argument('--cert-file', help='Path of certificate file to use in SSL ' - 'connection. This file can optionally be prepended' - ' with the private key.') + 'connection. This file can optionally be ' + 'prepended with the private key.') parser.add_argument('--key-file', help='Path of client key to use in SSL connection.' - ' This option is not necessary if your key is ' - 'prepended to your cert file.') + ' This option is not necessary if your ' + 'key is prepended to your cert file.') parser.add_argument('--ca-file', help='Path of CA SSL certificate(s) used to verify' - ' the remote server certificate. Without this ' - 'option glance looks for the default system ' - 'CA certificates.') + ' the remote server certificate. Without ' + 'this option glance looks for the default ' + 'system CA certificates.') parser.add_argument('--timeout', default=600, @@ -226,24 +225,24 @@ class PortasShell(object): endpoint = args.portas_url else: if not args.os_username: - raise exceptions.CommandError("You must provide a username via " - "either --os-username or via " - "env[OS_USERNAME]") + raise exceptions.CommandError("You must provide a username " + "via either --os-username " + "or via env[OS_USERNAME]") if not args.os_password: - raise exceptions.CommandError("You must provide a password via " - "either --os-password or via " - "env[OS_PASSWORD]") + raise exceptions.CommandError("You must provide a password " + "via either --os-password " + "or via env[OS_PASSWORD]") if not (args.os_tenant_id or args.os_tenant_name): - raise exceptions.CommandError("You must provide a tenant_id via " - "either --os-tenant-id or via " - "env[OS_TENANT_ID]") + raise exceptions.CommandError("You must provide a tenant_id " + "via either --os-tenant-id " + "or via env[OS_TENANT_ID]") if not args.os_auth_url: - raise exceptions.CommandError("You must provide an auth url via " - "either --os-auth-url or via " - "env[OS_AUTH_URL]") + raise exceptions.CommandError("You must provide an auth url " + "via either --os-auth-url or " + "via env[OS_AUTH_URL]") kwargs = { 'username': args.os_username, 'password': args.os_password, @@ -257,8 +256,8 @@ class PortasShell(object): _ksclient = self._get_ksclient(**kwargs) token = args.os_auth_token or _ksclient.auth_token - endpoint = args.portas_url or \ - self._get_endpoint(_ksclient, **kwargs) + url = args.portas_url + endpoint = url or self._get_endpoint(_ksclient, **kwargs) kwargs = { 'token': token, @@ -274,7 +273,8 @@ class PortasShell(object): try: args.func(client, args) except exceptions.Unauthorized: - raise exceptions.CommandError("Invalid OpenStack Identity credentials.") + msg = "Invalid OpenStack Identity credentials." + raise exceptions.CommandError(msg) @utils.arg('command', metavar='', nargs='?', help='Display help for ') @@ -286,8 +286,8 @@ class PortasShell(object): if args.command in self.subcommands: self.subcommands[args.command].print_help() else: - raise exceptions.CommandError("'%s' is not a valid subcommand" % - args.command) + msg = "'%s' is not a valid subcommand" + raise exceptions.CommandError(msg % args.command) else: self.parser.print_help() diff --git a/python-portasclient/portasclient/v1/services.py b/python-portasclient/portasclient/v1/services.py index dd65448..d195c6c 100644 --- a/python-portasclient/portasclient/v1/services.py +++ b/python-portasclient/portasclient/v1/services.py @@ -50,8 +50,8 @@ class ActiveDirectoryManager(base.Manager): headers = {'X-Configuration-Session': session_id} path = 'environments/{id}/activeDirectories/{active_directory_id}' - return self._delete(patch.format(id=environment_id, - active_directory_id=service_id), + return self._delete(path.format(id=environment_id, + active_directory_id=service_id), headers=headers) diff --git a/python-portasclient/tests/test_sanity.py b/python-portasclient/tests/test_sanity.py new file mode 100644 index 0000000..63d9e37 --- /dev/null +++ b/python-portasclient/tests/test_sanity.py @@ -0,0 +1,169 @@ +import unittest +import logging +from mock import MagicMock + +from portasclient.v1 import Client +import portasclient.v1.environments as environments +import portasclient.v1.services as services +import portasclient.v1.sessions as sessions + +LOG = logging.getLogger('Unit tests') + +def my_mock(*a, **b): + return [a, b] + +api = MagicMock(json_request=my_mock) + +class SanityUnitTests(unittest.TestCase): + + def test_create_client_instance(self): + + endpoint = 'http://localhost:8001' + test_client = Client(endpoint=endpoint, token='1', timeout=10) + + assert test_client.environments is not None + assert test_client.sessions is not None + assert test_client.activeDirectories is not None + assert test_client.webServers is not None + + def test_env_manager_list(self): + + manager = environments.EnvironmentManager(api) + + result = manager.list() + + assert result == [] + + def test_env_manager_create(self): + + manager = environments.EnvironmentManager(api) + + result = manager.create('test') + + assert result.headers == {} + assert result.body == {'name': 'test'} + + def test_env_manager_delete(self): + + manager = environments.EnvironmentManager(api) + + result = manager.delete('test') + + assert result is None + + def test_env_manager_update(self): + + manager = environments.EnvironmentManager(api) + + result = manager.update('1', 'test') + + assert result.body == {'name': 'test'} + + def test_env_manager_get(self): + + manager = environments.EnvironmentManager(api) + + result = manager.get('test') + ## WTF? + assert result.manager is not None + + def test_ad_manager_list(self): + + manager = services.ActiveDirectoryManager(api) + + result = manager.list('datacenter1') + + assert result == [] + + def test_ad_manager_create(self): + + manager = services.ActiveDirectoryManager(api) + + result = manager.create('datacenter1', 'session1', 'test') + + assert result.headers == {'X-Configuration-Session': 'session1'} + assert result.body == 'test' + + #@unittest.skip("https://mirantis.jira.com/browse/KEERO-218") + def test_ad_manager_delete(self): + + manager = services.ActiveDirectoryManager(api) + + result = manager.delete('datacenter1', 'session1', 'test') + + assert result is None + + def test_iis_manager_list(self): + + manager = services.WebServerManager(api) + + result = manager.list('datacenter1') + + assert result == [] + + def test_iis_manager_create(self): + + manager = services.WebServerManager(api) + + result = manager.create('datacenter1', 'session1', 'test') + + assert result.headers == {'X-Configuration-Session': 'session1'} + assert result.body == 'test' + + #@unittest.skip("https://mirantis.jira.com/browse/KEERO-218") + def test_iis_manager_delete(self): + + manager = services.WebServerManager(api) + + result = manager.delete('datacenter1', 'session1', 'test') + + assert result is None + + def test_session_manager_list(self): + + manager = sessions.SessionManager(api) + + result = manager.list('datacenter1') + + assert result == [] + + def test_session_manager_delete(self): + + manager = sessions.SessionManager(api) + + result = manager.delete('datacenter1', 'session1') + + assert result is None + + def test_session_manager_get(self): + + manager = sessions.SessionManager(api) + + result = manager.get('datacenter1', 'session1') + # WTF? + assert result.manager is not None + + def test_session_manager_configure(self): + + manager = sessions.SessionManager(api) + + result = manager.configure('datacenter1') + + assert result.headers == {} + + def test_session_manager_deploy(self): + + manager = sessions.SessionManager(api) + + result = manager.deploy('datacenter1', '1') + + assert result is None + + @unittest.skip("https://mirantis.jira.com/browse/KEERO-219") + def test_session_manager_reports(self): + + manager = sessions.SessionManager(api) + + result = manager.reports('datacenter1', '1') + + assert result == [] \ No newline at end of file diff --git a/python-portasclient/tools/test-requires b/python-portasclient/tools/test-requires index c39eaf1..6114c87 100644 --- a/python-portasclient/tools/test-requires +++ b/python-portasclient/tools/test-requires @@ -1,5 +1,7 @@ distribute>=0.6.24 +mock +anyjson mox nose nose-exclude