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
This commit is contained in:
Victor Stinner 2016-06-20 12:24:04 +02:00
parent 6e78df9954
commit 9c45ec7e6f
3 changed files with 23 additions and 55 deletions

View File

@ -41,9 +41,6 @@ class TestCaseShell(testtools.TestCase):
k = '--os-' + k.replace('_', '-') k = '--os-' + k.replace('_', '-')
self.args.extend([k, v]) self.args.extend([k, v])
sys.stdout = six.StringIO()
sys.stderr = six.StringIO()
self.useFixture(fixtures.MonkeyPatch('keystoneclient.v3.client.Client', self.useFixture(fixtures.MonkeyPatch('keystoneclient.v3.client.Client',
mock.MagicMock)) mock.MagicMock))
dirs = [os.path.dirname(__file__), dirs = [os.path.dirname(__file__),
@ -60,24 +57,20 @@ class TestCaseShell(testtools.TestCase):
self.addCleanup(CONF.clear_override, name, group) self.addCleanup(CONF.clear_override, name, group)
def shell(self, cmd_args=None, exitcode=0): def shell(self, cmd_args=None, exitcode=0):
orig = sys.stdout stdout = six.StringIO()
orig_stderr = sys.stderr stderr = six.StringIO()
sys.stdout = six.StringIO()
sys.stderr = six.StringIO()
args = self.args args = self.args
if cmd_args: if cmd_args:
cmd_args = cmd_args.split() cmd_args = cmd_args.split()
args.extend(cmd_args) args.extend(cmd_args)
with mock.patch.object(sys, 'argv', args): with mock.patch.object(sys, 'stdout', stdout):
result = self.assertRaises(SystemExit, test_runner.main) with mock.patch.object(sys, 'stderr', stderr):
self.assertEqual(result.code, exitcode, with mock.patch.object(sys, 'argv', args):
'Command finished with error.') result = self.assertRaises(SystemExit, test_runner.main)
stdout = sys.stdout.getvalue() self.assertEqual(result.code, exitcode,
sys.stdout.close() 'Command finished with error.')
sys.stdout = orig stdout = stdout.getvalue()
stderr = sys.stderr.getvalue() stderr = stderr.getvalue()
sys.stderr.close()
sys.stderr = orig_stderr
return (stdout, stderr) return (stdout, stderr)
def test_help(self): def test_help(self):
@ -94,8 +87,12 @@ class TestCaseShell(testtools.TestCase):
self.assertIn(usage, stdout) self.assertIn(usage, stdout)
def test_version(self): def test_version(self):
_, stderr = self.shell('--version') stdout, stderr = self.shell('--version')
self.assertIn(version.version_string, stderr) if six.PY3:
output = stdout
else:
output = stderr
self.assertIn(version.version_string, output)
@mock.patch.object(test_runner, 'LOG') @mock.patch.object(test_runner, 'LOG')
def test_increase_verbosity(self, mock_log): def test_increase_verbosity(self, mock_log):
@ -157,7 +154,11 @@ class TestCaseShell(testtools.TestCase):
def test_package_is_not_provided(self): def test_package_is_not_provided(self):
_, stderr = self.shell(exitcode=2) _, 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): def test_wrong_parent(self):
_, stderr = self.shell( _, stderr = self.shell(

View File

@ -105,7 +105,7 @@ class MuranoMigrationsCheckers(object):
package = { package = {
'id': str(uuid.uuid4()), 'id': str(uuid.uuid4()),
'archive': "archive blob here", 'archive': b"archive blob here",
'fully_qualified_name': 'com.example.package', 'fully_qualified_name': 'com.example.package',
'type': 'class', 'type': 'class',
'author': 'OpenStack', 'author': 'OpenStack',
@ -114,7 +114,7 @@ class MuranoMigrationsCheckers(object):
'description': 'some text', 'description': 'some text',
'is_public': False, 'is_public': False,
'tags': ['tag1', 'tag2'], 'tags': ['tag1', 'tag2'],
'logo': "logo blob here", 'logo': b"logo blob here",
'ui_definition': '{}', 'ui_definition': '{}',
'owner_id': '123', 'owner_id': '123',
'created': datetime.datetime.now(), 'created': datetime.datetime.now(),

33
tox.ini
View File

@ -15,39 +15,6 @@ deps = -r{toxinidir}/requirements.txt
commands = bash tools/pretty_tox.sh '{posargs}' commands = bash tools/pretty_tox.sh '{posargs}'
whitelist_externals = bash 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] [testenv:murano-test-runner]
commands = murano-test-runner {posargs} commands = murano-test-runner {posargs}