diff options
Diffstat (limited to 'openstackclient')
| -rw-r--r-- | openstackclient/tests/__init__.py | 14 | ||||
| -rw-r--r-- | openstackclient/tests/common/test_clientmanager.py | 35 | ||||
| -rw-r--r-- | openstackclient/tests/common/test_commandmanager.py | 71 | ||||
| -rw-r--r-- | openstackclient/tests/common/test_parseractions.py | 104 | ||||
| -rw-r--r-- | openstackclient/tests/compute/__init__.py | 14 | ||||
| -rw-r--r-- | openstackclient/tests/compute/test_compute.py | 50 | ||||
| -rw-r--r-- | openstackclient/tests/identity/__init__.py | 14 | ||||
| -rw-r--r-- | openstackclient/tests/identity/test_identity.py | 48 | ||||
| -rw-r--r-- | openstackclient/tests/image/__init__.py | 14 | ||||
| -rw-r--r-- | openstackclient/tests/image/test_image.py | 51 | ||||
| -rw-r--r-- | openstackclient/tests/test_shell.py | 293 | ||||
| -rw-r--r-- | openstackclient/tests/utils.py | 59 | ||||
| -rw-r--r-- | openstackclient/tests/volume/__init__.py | 14 | ||||
| -rw-r--r-- | openstackclient/tests/volume/test_volume.py | 51 |
14 files changed, 832 insertions, 0 deletions
diff --git a/openstackclient/tests/__init__.py b/openstackclient/tests/__init__.py new file mode 100644 index 00000000..85ac2501 --- /dev/null +++ b/openstackclient/tests/__init__.py @@ -0,0 +1,14 @@ +# Copyright 2012-2013 OpenStack, LLC. +# +# 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. +# diff --git a/openstackclient/tests/common/test_clientmanager.py b/openstackclient/tests/common/test_clientmanager.py new file mode 100644 index 00000000..395f6ec3 --- /dev/null +++ b/openstackclient/tests/common/test_clientmanager.py @@ -0,0 +1,35 @@ +# Copyright 2012-2013 OpenStack, LLC. +# +# 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 openstackclient.common import clientmanager +from openstackclient.tests import utils + + +class Container(object): + attr = clientmanager.ClientCache(lambda x: object()) + + def __init__(self): + pass + + +class TestClientManager(utils.TestCase): + def setUp(self): + super(TestClientManager, self).setUp() + + def test_singleton(self): + # NOTE(dtroyer): Verify that the ClientCache descriptor only invokes + # the factory one time and always returns the same value after that. + c = Container() + self.assertEqual(c.attr, c.attr) diff --git a/openstackclient/tests/common/test_commandmanager.py b/openstackclient/tests/common/test_commandmanager.py new file mode 100644 index 00000000..4953c297 --- /dev/null +++ b/openstackclient/tests/common/test_commandmanager.py @@ -0,0 +1,71 @@ +# Copyright 2012-2013 OpenStack, LLC. +# +# 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 mock + +from openstackclient.common import commandmanager +from openstackclient.tests import utils + + +class FakeCommand(object): + @classmethod + def load(cls): + return cls + + def __init__(self): + return + +FAKE_CMD_ONE = FakeCommand +FAKE_CMD_TWO = FakeCommand +FAKE_CMD_ALPHA = FakeCommand +FAKE_CMD_BETA = FakeCommand + + +class FakeCommandManager(commandmanager.CommandManager): + commands = {} + + def _load_commands(self, group=None): + if not group: + self.commands['one'] = FAKE_CMD_ONE + self.commands['two'] = FAKE_CMD_TWO + else: + self.commands['alpha'] = FAKE_CMD_ALPHA + self.commands['beta'] = FAKE_CMD_BETA + + +class TestCommandManager(utils.TestCase): + def test_add_command_group(self): + mgr = FakeCommandManager('test') + + # Make sure add_command() still functions + mock_cmd_one = mock.Mock() + mgr.add_command('mock', mock_cmd_one) + cmd_mock, name, args = mgr.find_command(['mock']) + self.assertEqual(cmd_mock, mock_cmd_one) + + # Find a command added in initialization + cmd_one, name, args = mgr.find_command(['one']) + self.assertEqual(cmd_one, FAKE_CMD_ONE) + + # Load another command group + mgr.add_command_group('latin') + + # Find a new command + cmd_alpha, name, args = mgr.find_command(['alpha']) + self.assertEqual(cmd_alpha, FAKE_CMD_ALPHA) + + # Ensure that the original commands were not overwritten + cmd_two, name, args = mgr.find_command(['two']) + self.assertEqual(cmd_two, FAKE_CMD_TWO) diff --git a/openstackclient/tests/common/test_parseractions.py b/openstackclient/tests/common/test_parseractions.py new file mode 100644 index 00000000..705e7e9c --- /dev/null +++ b/openstackclient/tests/common/test_parseractions.py @@ -0,0 +1,104 @@ +# Copyright 2012-2013 OpenStack Foundation +# +# 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 argparse + +from openstackclient.common import parseractions +from openstackclient.tests import utils + + +class TestKeyValueAction(utils.TestCase): + def test_good_values(self): + parser = argparse.ArgumentParser() + + # Set up our typical usage + parser.add_argument( + '--property', + metavar='<key=value>', + action=parseractions.KeyValueAction, + help='Property to store for this volume ' + '(repeat option to set multiple properties)', + ) + + results = parser.parse_args([ + '--property', 'red=', + '--property', 'green=100%', + '--property', 'blue=50%', + ]) + + actual = getattr(results, 'property', {}) + # All should pass through unmolested + expect = {'red': '', 'green': '100%', 'blue': '50%'} + self.assertDictEqual(expect, actual) + + def test_default_values(self): + parser = argparse.ArgumentParser() + + # Set up our typical usage + parser.add_argument( + '--property', + metavar='<key=value>', + action=parseractions.KeyValueAction, + default={'green': '20%', 'format': '#rgb'}, + help='Property to store for this volume ' + '(repeat option to set multiple properties)', + ) + + results = parser.parse_args([ + '--property', 'red=', + '--property', 'green=100%', + '--property', 'blue=50%', + ]) + + actual = getattr(results, 'property', {}) + # Verify green default is changed, format default is unchanged + expect = {'red': '', 'green': '100%', 'blue': '50%', 'format': '#rgb'} + self.assertDictEqual(expect, actual) + + def test_error_values(self): + parser = argparse.ArgumentParser() + + # Set up our typical usage + parser.add_argument( + '--property', + metavar='<key=value>', + action=parseractions.KeyValueAction, + default={'green': '20%', 'blue': '40%'}, + help='Property to store for this volume ' + '(repeat option to set multiple properties)', + ) + + results = parser.parse_args([ + '--property', 'red', + '--property', 'green=100%', + '--property', 'blue', + ]) + + failhere = None + actual = getattr(results, 'property', {}) + # Verify non-existant red key + try: + failhere = actual['red'] + except Exception as e: + self.assertTrue(type(e) == KeyError) + # Verify removal of blue key + try: + failhere = actual['blue'] + except Exception as e: + self.assertTrue(type(e) == KeyError) + # There should be no red or blue + expect = {'green': '100%'} + self.assertDictEqual(expect, actual) + self.assertEqual(failhere, None) diff --git a/openstackclient/tests/compute/__init__.py b/openstackclient/tests/compute/__init__.py new file mode 100644 index 00000000..ebf59b32 --- /dev/null +++ b/openstackclient/tests/compute/__init__.py @@ -0,0 +1,14 @@ +# Copyright 2013 OpenStack, LLC. +# +# 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. +# diff --git a/openstackclient/tests/compute/test_compute.py b/openstackclient/tests/compute/test_compute.py new file mode 100644 index 00000000..9d2061d2 --- /dev/null +++ b/openstackclient/tests/compute/test_compute.py @@ -0,0 +1,50 @@ +# Copyright 2013 OpenStack, LLC. +# +# 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 mock + +from openstackclient.common import clientmanager +from openstackclient.compute import client as compute_client +from openstackclient.tests import utils + + +AUTH_TOKEN = "foobar" +AUTH_URL = "http://0.0.0.0" + + +class FakeClient(object): + def __init__(self, endpoint=None, **kwargs): + self.client = mock.MagicMock() + self.client.auth_url = AUTH_URL + + +class TestCompute(utils.TestCase): + def setUp(self): + super(TestCompute, self).setUp() + + api_version = {"compute": "2"} + + compute_client.API_VERSIONS = { + "2": "openstackclient.tests.compute.test_compute.FakeClient" + } + + self.cm = clientmanager.ClientManager(token=AUTH_TOKEN, + url=AUTH_URL, + auth_url=AUTH_URL, + api_version=api_version) + + def test_make_client(self): + self.assertEqual(self.cm.compute.client.auth_token, AUTH_TOKEN) + self.assertEqual(self.cm.compute.client.auth_url, AUTH_URL) diff --git a/openstackclient/tests/identity/__init__.py b/openstackclient/tests/identity/__init__.py new file mode 100644 index 00000000..ebf59b32 --- /dev/null +++ b/openstackclient/tests/identity/__init__.py @@ -0,0 +1,14 @@ +# Copyright 2013 OpenStack, LLC. +# +# 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. +# diff --git a/openstackclient/tests/identity/test_identity.py b/openstackclient/tests/identity/test_identity.py new file mode 100644 index 00000000..04bbd20f --- /dev/null +++ b/openstackclient/tests/identity/test_identity.py @@ -0,0 +1,48 @@ +# Copyright 2013 OpenStack, LLC. +# +# 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 openstackclient.common import clientmanager +from openstackclient.identity import client as identity_client +from openstackclient.tests import utils + + +AUTH_TOKEN = "foobar" +AUTH_URL = "http://0.0.0.0" + + +class FakeClient(object): + def __init__(self, endpoint=None, **kwargs): + self.auth_token = AUTH_TOKEN + self.auth_url = AUTH_URL + + +class TestIdentity(utils.TestCase): + def setUp(self): + super(TestIdentity, self).setUp() + + api_version = {"identity": "2.0"} + + identity_client.API_VERSIONS = { + "2.0": "openstackclient.tests.identity.test_identity.FakeClient" + } + + self.cm = clientmanager.ClientManager(token=AUTH_TOKEN, + url=AUTH_URL, + auth_url=AUTH_URL, + api_version=api_version) + + def test_make_client(self): + self.assertEqual(self.cm.identity.auth_token, AUTH_TOKEN) + self.assertEqual(self.cm.identity.auth_url, AUTH_URL) diff --git a/openstackclient/tests/image/__init__.py b/openstackclient/tests/image/__init__.py new file mode 100644 index 00000000..ebf59b32 --- /dev/null +++ b/openstackclient/tests/image/__init__.py @@ -0,0 +1,14 @@ +# Copyright 2013 OpenStack, LLC. +# +# 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. +# diff --git a/openstackclient/tests/image/test_image.py b/openstackclient/tests/image/test_image.py new file mode 100644 index 00000000..f4c8d72e --- /dev/null +++ b/openstackclient/tests/image/test_image.py @@ -0,0 +1,51 @@ +# Copyright 2013 OpenStack, LLC. +# +# 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 mock + +from openstackclient.common import clientmanager +from openstackclient.image import client as image_client +from openstackclient.tests import utils + + +AUTH_TOKEN = "foobar" +AUTH_URL = "http://0.0.0.0" + + +class FakeClient(object): + def __init__(self, endpoint=None, **kwargs): + self.client = mock.MagicMock() + self.client.auth_token = AUTH_TOKEN + self.client.auth_url = AUTH_URL + + +class TestImage(utils.TestCase): + def setUp(self): + super(TestImage, self).setUp() + + api_version = {"image": "2"} + + image_client.API_VERSIONS = { + "2": "openstackclient.tests.image.test_image.FakeClient" + } + + self.cm = clientmanager.ClientManager(token=AUTH_TOKEN, + url=AUTH_URL, + auth_url=AUTH_URL, + api_version=api_version) + + def test_make_client(self): + self.assertEqual(self.cm.image.client.auth_token, AUTH_TOKEN) + self.assertEqual(self.cm.image.client.auth_url, AUTH_URL) diff --git a/openstackclient/tests/test_shell.py b/openstackclient/tests/test_shell.py new file mode 100644 index 00000000..f479b11e --- /dev/null +++ b/openstackclient/tests/test_shell.py @@ -0,0 +1,293 @@ +# Copyright 2012-2013 OpenStack, LLC. +# +# 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 mock +import os + +from openstackclient import shell +from openstackclient.tests import utils + + +DEFAULT_USERNAME = "username" +DEFAULT_PASSWORD = "password" +DEFAULT_TENANT_ID = "xxxx-yyyy-zzzz" +DEFAULT_TENANT_NAME = "tenant" +DEFAULT_TOKEN = "token" +DEFAULT_REGION_NAME = "ZZ9_Plural_Z_Alpha" +DEFAULT_AUTH_URL = "http://127.0.0.1:5000/v2.0/" +DEFAULT_SERVICE_URL = "http://127.0.0.1:8771/v3.0/" + +DEFAULT_COMPUTE_API_VERSION = "2" +DEFAULT_IDENTITY_API_VERSION = "2.0" +DEFAULT_IMAGE_API_VERSION = "v2" +DEFAULT_VOLUME_API_VERSION = "1" + +LIB_COMPUTE_API_VERSION = "2" +LIB_IDENTITY_API_VERSION = "2.0" +LIB_IMAGE_API_VERSION = "2" +LIB_VOLUME_API_VERSION = "1" + + +def make_shell(): + """Create a new command shell and mock out some bits.""" + _shell = shell.OpenStackShell() + _shell.command_manager = mock.Mock() + + return _shell + + +def fake_execute(shell, cmd): + """Pretend to execute shell commands.""" + return shell.run(cmd.split()) + + +class TestShell(utils.TestCase): + def setUp(self): + super(TestShell, self).setUp() + patch = "openstackclient.shell.OpenStackShell.run_subcommand" + self.cmd_patch = mock.patch(patch) + self.cmd_save = self.cmd_patch.start() + self.app = mock.Mock("Test Shell") + + def tearDown(self): + super(TestShell, self).tearDown() + self.cmd_patch.stop() + + def _assert_password_auth(self, cmd_options, default_args): + with mock.patch("openstackclient.shell.OpenStackShell.initialize_app", + self.app): + _shell, _cmd = make_shell(), cmd_options + " list tenant" + fake_execute(_shell, _cmd) + + self.app.assert_called_with(["list", "tenant"]) + self.assertEqual(_shell.options.os_auth_url, + default_args["auth_url"]) + self.assertEqual(_shell.options.os_tenant_id, + default_args["tenant_id"]) + self.assertEqual(_shell.options.os_tenant_name, + default_args["tenant_name"]) + self.assertEqual(_shell.options.os_username, + default_args["username"]) + self.assertEqual(_shell.options.os_password, + default_args["password"]) + self.assertEqual(_shell.options.os_region_name, + default_args["region_name"]) + + def _assert_token_auth(self, cmd_options, default_args): + with mock.patch("openstackclient.shell.OpenStackShell.initialize_app", + self.app): + _shell, _cmd = make_shell(), cmd_options + " list role" + fake_execute(_shell, _cmd) + + self.app.assert_called_with(["list", "role"]) + self.assertEqual(_shell.options.os_token, default_args["os_token"]) + self.assertEqual(_shell.options.os_url, default_args["os_url"]) + + def _assert_cli(self, cmd_options, default_args): + with mock.patch("openstackclient.shell.OpenStackShell.initialize_app", + self.app): + _shell, _cmd = make_shell(), cmd_options + " list server" + fake_execute(_shell, _cmd) + + self.app.assert_called_with(["list", "server"]) + self.assertEqual(_shell.options.os_compute_api_version, + default_args["compute_api_version"]) + self.assertEqual(_shell.options.os_identity_api_version, + default_args["identity_api_version"]) + self.assertEqual(_shell.options.os_image_api_version, + default_args["image_api_version"]) + self.assertEqual(_shell.options.os_volume_api_version, + default_args["volume_api_version"]) + + +class TestShellHelp(TestShell): + """Test the deferred help flag""" + def setUp(self): + super(TestShellHelp, self).setUp() + self.orig_env, os.environ = os.environ, {} + + def tearDown(self): + super(TestShellHelp, self).tearDown() + os.environ = self.orig_env + + def test_help_options(self): + flag = "-h list server" + kwargs = { + "deferred_help": True, + } + with mock.patch("openstackclient.shell.OpenStackShell.initialize_app", + self.app): + _shell, _cmd = make_shell(), flag + fake_execute(_shell, _cmd) + + self.assertEqual(_shell.options.deferred_help, + kwargs["deferred_help"]) + + +class TestShellPasswordAuth(TestShell): + def setUp(self): + super(TestShellPasswordAuth, self).setUp() + self.orig_env, os.environ = os.environ, {} + + def tearDown(self): + super(TestShellPasswordAuth, self).tearDown() + os.environ = self.orig_env + + def test_only_url_flow(self): + flag = "--os-auth-url " + DEFAULT_AUTH_URL + kwargs = { + "auth_url": DEFAULT_AUTH_URL, + "tenant_id": "", + "tenant_name": "", + "username": "", + "password": "", + "region_name": "" + } + self._assert_password_auth(flag, kwargs) + + def test_only_tenant_id_flow(self): + flag = "--os-tenant-id " + DEFAULT_TENANT_ID + kwargs = { + "auth_url": "", + "tenant_id": DEFAULT_TENANT_ID, + "tenant_name": "", + "username": "", + "password": "", + "region_name": "" + } + self._assert_password_auth(flag, kwargs) + + def test_only_tenant_name_flow(self): + flag = "--os-tenant-name " + DEFAULT_TENANT_NAME + kwargs = { + "auth_url": "", + "tenant_id": "", + "tenant_name": DEFAULT_TENANT_NAME, + "username": "", + "password": "", + "region_name": "" + } + self._assert_password_auth(flag, kwargs) + + def test_only_username_flow(self): + flag = "--os-username " + DEFAULT_USERNAME + kwargs = { + "auth_url": "", + "tenant_id": "", + "tenant_name": "", + "username": DEFAULT_USERNAME, + "password": "", + "region_name": "" + } + self._assert_password_auth(flag, kwargs) + + def test_only_password_flow(self): + flag = "--os-password " + DEFAULT_PASSWORD + kwargs = { + "auth_url": "", + "tenant_id": "", + "tenant_name": "", + "username": "", + "password": DEFAULT_PASSWORD, + "region_name": "" + } + self._assert_password_auth(flag, kwargs) + + def test_only_region_name_flow(self): + flag = "--os-region-name " + DEFAULT_REGION_NAME + kwargs = { + "auth_url": "", + "tenant_id": "", + "tenant_name": "", + "username": "", + "password": "", + "region_name": DEFAULT_REGION_NAME + } + self._assert_password_auth(flag, kwargs) + + +class TestShellTokenAuth(TestShell): + def setUp(self): + super(TestShellTokenAuth, self).setUp() + env = { + "OS_TOKEN": DEFAULT_TOKEN, + "OS_URL": DEFAULT_SERVICE_URL, + } + self.orig_env, os.environ = os.environ, env.copy() + + def tearDown(self): + super(TestShellTokenAuth, self).tearDown() + os.environ = self.orig_env + + def test_default_auth(self): + flag = "" + kwargs = { + "os_token": DEFAULT_TOKEN, + "os_url": DEFAULT_SERVICE_URL + } + self._assert_token_auth(flag, kwargs) + + def test_empty_auth(self): + os.environ = {} + flag = "" + kwargs = { + "os_token": "", + "os_url": "" + } + self._assert_token_auth(flag, kwargs) + + +class TestShellCli(TestShell): + def setUp(self): + super(TestShellCli, self).setUp() + env = { + "OS_COMPUTE_API_VERSION": DEFAULT_COMPUTE_API_VERSION, + "OS_IDENTITY_API_VERSION": DEFAULT_IDENTITY_API_VERSION, + "OS_IMAGE_API_VERSION": DEFAULT_IMAGE_API_VERSION, + "OS_VOLUME_API_VERSION": DEFAULT_VOLUME_API_VERSION, + } + self.orig_env, os.environ = os.environ, env.copy() + + def tearDown(self): + super(TestShellCli, self).tearDown() + os.environ = self.orig_env + + def test_shell_args(self): + _shell = make_shell() + with mock.patch("openstackclient.shell.OpenStackShell.initialize_app", + self.app): + fake_execute(_shell, "list user") + self.app.assert_called_with(["list", "user"]) + + def test_default_env(self): + flag = "" + kwargs = { + "compute_api_version": DEFAULT_COMPUTE_API_VERSION, + "identity_api_version": DEFAULT_IDENTITY_API_VERSION, + "image_api_version": DEFAULT_IMAGE_API_VERSION, + "volume_api_version": DEFAULT_VOLUME_API_VERSION + } + self._assert_cli(flag, kwargs) + + def test_empty_env(self): + os.environ = {} + flag = "" + kwargs = { + "compute_api_version": LIB_COMPUTE_API_VERSION, + "identity_api_version": LIB_IDENTITY_API_VERSION, + "image_api_version": LIB_IMAGE_API_VERSION, + "volume_api_version": LIB_VOLUME_API_VERSION + } + self._assert_cli(flag, kwargs) diff --git a/openstackclient/tests/utils.py b/openstackclient/tests/utils.py new file mode 100644 index 00000000..3e24ff4a --- /dev/null +++ b/openstackclient/tests/utils.py @@ -0,0 +1,59 @@ +# Copyright 2012-2013 OpenStack, LLC. +# +# 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 os + +import fixtures +import sys +import testtools + + +class TestCase(testtools.TestCase): + def setUp(self): + testtools.TestCase.setUp(self) + + if (os.environ.get("OS_STDOUT_NOCAPTURE") == "True" and + os.environ.get("OS_STDOUT_NOCAPTURE") == "1"): + stdout = self.useFixture(fixtures.StringStream("stdout")).stream + self.useFixture(fixtures.MonkeyPatch("sys.stdout", stdout)) + + if (os.environ.get("OS_STDERR_NOCAPTURE") == "True" and + os.environ.get("OS_STDERR_NOCAPTURE") == "1"): + stderr = self.useFixture(fixtures.StringStream("stderr")).stream + self.useFixture(fixtures.MonkeyPatch("sys.stderr", stderr)) + + # 2.6 doesn't have the assert dict equals so make sure that it exists + if tuple(sys.version_info)[0:2] < (2, 7): + + def assertIsInstance(self, obj, cls, msg=None): + """Same as self.assertTrue(isinstance(obj, cls)), with a nicer + default message + """ + if not isinstance(obj, cls): + standardMsg = '%s is not an instance of %r' % (obj, cls) + self.fail(self._formatMessage(msg, standardMsg)) + + def assertDictEqual(self, d1, d2, msg=None): + # Simple version taken from 2.7 + self.assertIsInstance(d1, dict, + 'First argument is not a dictionary') + self.assertIsInstance(d2, dict, + 'Second argument is not a dictionary') + if d1 != d2: + if msg: + self.fail(msg) + else: + standardMsg = '%r != %r' % (d1, d2) + self.fail(standardMsg) diff --git a/openstackclient/tests/volume/__init__.py b/openstackclient/tests/volume/__init__.py new file mode 100644 index 00000000..ebf59b32 --- /dev/null +++ b/openstackclient/tests/volume/__init__.py @@ -0,0 +1,14 @@ +# Copyright 2013 OpenStack, LLC. +# +# 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. +# diff --git a/openstackclient/tests/volume/test_volume.py b/openstackclient/tests/volume/test_volume.py new file mode 100644 index 00000000..548fbf74 --- /dev/null +++ b/openstackclient/tests/volume/test_volume.py @@ -0,0 +1,51 @@ +# Copyright 2013 OpenStack, LLC. +# +# 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 mock + +from openstackclient.common import clientmanager +from openstackclient.tests import utils +from openstackclient.volume import client as volume_client + + +AUTH_TOKEN = "foobar" +AUTH_URL = "http://0.0.0.0" + + +class FakeClient(object): + def __init__(self, endpoint=None, **kwargs): + self.client = mock.MagicMock() + self.client.auth_token = AUTH_TOKEN + self.client.auth_url = AUTH_URL + + +class TestVolume(utils.TestCase): + def setUp(self): + super(TestVolume, self).setUp() + + api_version = {"volume": "1"} + + volume_client.API_VERSIONS = { + "1": "openstackclient.tests.volume.test_volume.FakeClient" + } + + self.cm = clientmanager.ClientManager(token=AUTH_TOKEN, + url=AUTH_URL, + auth_url=AUTH_URL, + api_version=api_version) + + def test_make_client(self): + self.assertEqual(self.cm.volume.client.auth_token, AUTH_TOKEN) + self.assertEqual(self.cm.volume.client.auth_url, AUTH_URL) |
