From 0dadf9ae06241db0fa83e16295675f814315fd55 Mon Sep 17 00:00:00 2001 From: Monty Taylor Date: Sun, 30 Jun 2013 23:01:17 -0400 Subject: [PATCH] Move tests into project package. There are several reasons for this. One is that the majority of OpenStack packages behave this way. The second is that it makes writing software that extends something easier to test (which is a clear usecase for openstackclient) And third, tests/__init__.py implies a global package named "tests" - which I'm pretty sure we're not providing. Change-Id: Ic708ffd92aea78c2ffc1a8579af0587af4fca4ff --- .testr.conf | 2 +- openstackclient/tests/__init__.py | 14 + .../tests/common/test_clientmanager.py | 35 +++ .../tests/common/test_commandmanager.py | 71 +++++ .../tests/common/test_parseractions.py | 104 +++++++ openstackclient/tests/test_shell.py | 293 ++++++++++++++++++ openstackclient/tests/utils.py | 59 ++++ 7 files changed, 577 insertions(+), 1 deletion(-) create mode 100644 openstackclient/tests/__init__.py create mode 100644 openstackclient/tests/common/test_clientmanager.py create mode 100644 openstackclient/tests/common/test_commandmanager.py create mode 100644 openstackclient/tests/common/test_parseractions.py create mode 100644 openstackclient/tests/test_shell.py create mode 100644 openstackclient/tests/utils.py diff --git a/.testr.conf b/.testr.conf index 2109af6..d152a5a 100644 --- a/.testr.conf +++ b/.testr.conf @@ -1,4 +1,4 @@ [DEFAULT] -test_command=OS_STDOUT_CAPTURE=1 OS_STDERR_CAPTURE=1 ${PYTHON:-python} -m subunit.run discover -t ./ ./tests $LISTOPT $IDOPTION +test_command=OS_STDOUT_CAPTURE=1 OS_STDERR_CAPTURE=1 ${PYTHON:-python} -m subunit.run discover -t ./ . $LISTOPT $IDOPTION test_id_option=--load-list $IDFILE test_list_option=--list diff --git a/openstackclient/tests/__init__.py b/openstackclient/tests/__init__.py new file mode 100644 index 0000000..85ac250 --- /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 0000000..395f6ec --- /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 0000000..4953c29 --- /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 0000000..705e7e9 --- /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='', + 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='', + 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='', + 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/test_shell.py b/openstackclient/tests/test_shell.py new file mode 100644 index 0000000..f479b11 --- /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 0000000..3e24ff4 --- /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)