From 9c45ec7e6f038cda1b4ae9dd3d5f56b3ba774c53 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Mon, 20 Jun 2016 12:24:04 +0200 Subject: [PATCH] Finish porting unit tests to Python 3 Port the last unit tests to Python 3, test_test_runner: * TestCaseShell constructor: don't replace sys.stdout and sys.stderr by StringIO, it's useless and prevent using print() to debug failing unit tests. * shell(): use mock.patch() to replace sys.stdout and sys.stderr * test_version(): on Python 3, the version is written into stdout, not stderr. * test_package_is_not_provided(): the exact error message changes on Python 3. test_migrations: * Use a byte string for archive and logo BLOBs Remove whitelist of tests running on Python 3 from tox.ini since all unit tests now pass on Python 3! Blueprint: murano-python-3-support Change-Id: Ic795b99526746ac0c06427001e4da27275b76d84 --- murano/tests/unit/api/cmd/test_test_runner.py | 41 ++++++++++--------- .../unit/db/migration/test_migrations.py | 4 +- tox.ini | 33 --------------- 3 files changed, 23 insertions(+), 55 deletions(-) diff --git a/murano/tests/unit/api/cmd/test_test_runner.py b/murano/tests/unit/api/cmd/test_test_runner.py index 7fe40aff..5b1e4f18 100644 --- a/murano/tests/unit/api/cmd/test_test_runner.py +++ b/murano/tests/unit/api/cmd/test_test_runner.py @@ -41,9 +41,6 @@ class TestCaseShell(testtools.TestCase): k = '--os-' + k.replace('_', '-') self.args.extend([k, v]) - sys.stdout = six.StringIO() - sys.stderr = six.StringIO() - self.useFixture(fixtures.MonkeyPatch('keystoneclient.v3.client.Client', mock.MagicMock)) dirs = [os.path.dirname(__file__), @@ -60,24 +57,20 @@ class TestCaseShell(testtools.TestCase): self.addCleanup(CONF.clear_override, name, group) def shell(self, cmd_args=None, exitcode=0): - orig = sys.stdout - orig_stderr = sys.stderr - sys.stdout = six.StringIO() - sys.stderr = six.StringIO() + stdout = six.StringIO() + stderr = six.StringIO() args = self.args if cmd_args: cmd_args = cmd_args.split() args.extend(cmd_args) - with mock.patch.object(sys, 'argv', args): - result = self.assertRaises(SystemExit, test_runner.main) - self.assertEqual(result.code, exitcode, - 'Command finished with error.') - stdout = sys.stdout.getvalue() - sys.stdout.close() - sys.stdout = orig - stderr = sys.stderr.getvalue() - sys.stderr.close() - sys.stderr = orig_stderr + with mock.patch.object(sys, 'stdout', stdout): + with mock.patch.object(sys, 'stderr', stderr): + with mock.patch.object(sys, 'argv', args): + result = self.assertRaises(SystemExit, test_runner.main) + self.assertEqual(result.code, exitcode, + 'Command finished with error.') + stdout = stdout.getvalue() + stderr = stderr.getvalue() return (stdout, stderr) def test_help(self): @@ -94,8 +87,12 @@ class TestCaseShell(testtools.TestCase): self.assertIn(usage, stdout) def test_version(self): - _, stderr = self.shell('--version') - self.assertIn(version.version_string, stderr) + stdout, stderr = self.shell('--version') + if six.PY3: + output = stdout + else: + output = stderr + self.assertIn(version.version_string, output) @mock.patch.object(test_runner, 'LOG') def test_increase_verbosity(self, mock_log): @@ -157,7 +154,11 @@ class TestCaseShell(testtools.TestCase): def test_package_is_not_provided(self): _, stderr = self.shell(exitcode=2) - self.assertIn('murano-test-runner: error: too few arguments', stderr) + if six.PY3: + err = 'the following arguments are required: ' + else: + err = 'too few arguments' + self.assertIn('murano-test-runner: error: %s' % err, stderr) def test_wrong_parent(self): _, stderr = self.shell( diff --git a/murano/tests/unit/db/migration/test_migrations.py b/murano/tests/unit/db/migration/test_migrations.py index 48d64fd3..8b9a40c1 100644 --- a/murano/tests/unit/db/migration/test_migrations.py +++ b/murano/tests/unit/db/migration/test_migrations.py @@ -105,7 +105,7 @@ class MuranoMigrationsCheckers(object): package = { 'id': str(uuid.uuid4()), - 'archive': "archive blob here", + 'archive': b"archive blob here", 'fully_qualified_name': 'com.example.package', 'type': 'class', 'author': 'OpenStack', @@ -114,7 +114,7 @@ class MuranoMigrationsCheckers(object): 'description': 'some text', 'is_public': False, 'tags': ['tag1', 'tag2'], - 'logo': "logo blob here", + 'logo': b"logo blob here", 'ui_definition': '{}', 'owner_id': '123', 'created': datetime.datetime.now(), diff --git a/tox.ini b/tox.ini index 59a86f0f..e606b8c6 100644 --- a/tox.ini +++ b/tox.ini @@ -15,39 +15,6 @@ deps = -r{toxinidir}/requirements.txt commands = bash tools/pretty_tox.sh '{posargs}' whitelist_externals = bash -[testenv:py34] -commands = python -m testtools.run \ - murano/tests/unit/api/middleware/test_ssl.py \ - murano/tests/unit/api/v1/cloudfoundry/test_cfapi.py \ - murano/tests/unit/api/v1/test_actions.py \ - murano/tests/unit/api/v1/test_catalog.py \ - murano/tests/unit/api/v1/test_env_templates.py \ - murano/tests/unit/api/v1/test_environments.py \ - murano/tests/unit/api/v1/test_sessions.py \ - murano/tests/unit/cmd/test_engine_workers.py \ - murano/tests/unit/common/helpers/test_token_sanitizer.py \ - murano/tests/unit/common/test_is_different.py \ - murano/tests/unit/common/test_plugin_loader.py \ - murano/tests/unit/common/test_traverse_helper.py \ - murano/tests/unit/core_library/instance/test_destroy/test_destroy.py \ - murano/tests/unit/db/services/test_core_service.py \ - murano/tests/unit/db/services/test_environments.py \ - murano/tests/unit/db/services/test_templates_service.py \ - murano/tests/unit/db/test_catalog.py \ - murano/tests/unit/db/test_models.py \ - murano/tests/unit/dsl/test_helpers.py \ - murano/tests/unit/engine/system/test_agent.py \ - murano/tests/unit/engine/test_package_loader.py \ - murano/tests/unit/packages/hot_package/test_hot_package.py \ - murano/tests/unit/packages/versions/test_hot_v1.py \ - murano/tests/unit/packages/versions/test_mpl_v1.py \ - murano/tests/unit/policy/test_congress_rules.py \ - murano/tests/unit/policy/test_model_policy_enforcer.py \ - murano/tests/unit/test_actions.py \ - murano/tests/unit/test_engine.py \ - murano/tests/unit/test_hacking.py \ - murano/tests/unit/test_heat_stack.py - [testenv:murano-test-runner] commands = murano-test-runner {posargs}