Enable most unittests on python3 (just some vault unittests and a logging one left) (#17240)

Make some python3 fixes to make the unittests pass:

* galaxy imports
* dictionary iteration in role requirements
* swap_stdout helper for unittests
* Normalize to text string in a facts.py function
This commit is contained in:
Toshio Kuratomi 2016-08-25 07:30:03 -07:00 committed by GitHub
commit 44d979c8f5
11 changed files with 23 additions and 29 deletions

View file

@ -25,7 +25,6 @@ import tarfile
import tempfile
from mock import patch
from nose.plugins.skip import SkipTest
from ansible.compat.six import PY3
from ansible.compat.tests import unittest
@ -34,13 +33,9 @@ from mock import patch, call
import ansible
from ansible.errors import AnsibleError, AnsibleOptionsError
from nose.plugins.skip import SkipTest
if PY3:
raise SkipTest('galaxy is not ported to be py3 compatible yet')
from ansible.cli.galaxy import GalaxyCLI
class TestGalaxy(unittest.TestCase):
@classmethod
def setUpClass(cls):

View file

@ -54,7 +54,10 @@ def swap_stdout():
context manager that temporarily replaces stdout for tests that need to verify output
"""
old_stdout = sys.stdout
fake_stream = BytesIO()
if PY3:
fake_stream = StringIO()
else:
fake_stream = BytesIO()
sys.stdout = fake_stream
yield fake_stream
sys.stdout = old_stdout

View file

@ -32,7 +32,6 @@ from ansible.module_utils import basic
empty_invocation = {u'module_args': {}}
@unittest.skipIf(sys.version_info[0] >= 3, "Python 3 is not supported on targets (yet)")
class TestAnsibleModuleExitJson(unittest.TestCase):
def setUp(self):
args = json.dumps(dict(ANSIBLE_MODULE_ARGS={}))
@ -90,7 +89,6 @@ class TestAnsibleModuleExitJson(unittest.TestCase):
return_val = json.loads(self.fake_stream.getvalue())
self.assertEquals(return_val, dict(changed=True, msg='success', invocation=empty_invocation))
@unittest.skipIf(sys.version_info[0] >= 3, "Python 3 is not supported on targets (yet)")
class TestAnsibleModuleExitValuesRemoved(unittest.TestCase):
OMIT = 'VALUE_SPECIFIED_IN_NO_LOG_PARAMETER'
dataset = (

View file

@ -42,7 +42,6 @@ class OpenBytesIO(BytesIO):
pass
@unittest.skipIf(sys.version_info[0] >= 3, "Python 3 is not supported on targets (yet)")
class TestAnsibleModuleRunCommand(unittest.TestCase):
def setUp(self):
self.cmd_out = {

View file

@ -30,6 +30,7 @@ from ansible.compat.tests import unittest
from ansible.compat.tests.mock import patch
# the module we are actually testing
import ansible.module_utils.facts as facts
# to generate the testcase data, you can use the script gen_distribution_version_testcase.py in hacking/tests
@ -621,7 +622,7 @@ DISTRIB_DESCRIPTION="CoreOS 976.0.0 (Coeur Rouge)"
]
@unittest.skipIf(sys.version_info[0] >= 3, "Python 3 is not supported on targets (yet)")
def test_distribution_version():
"""tests the distribution parsing code of the Facts class
@ -634,9 +635,6 @@ def test_distribution_version():
* results for the ansible variables distribution*
"""
# needs to be in here, because the import fails with python3 still
import ansible.module_utils.facts as facts
from ansible.module_utils import basic
args = json.dumps(dict(ANSIBLE_MODULE_ARGS={}))

View file

@ -19,7 +19,6 @@
# Make coding more python3-ish
from __future__ import (absolute_import, division, print_function)
__metaclass__ = type
#!/usr/bin/env python
import sys
import os
@ -103,12 +102,13 @@ class TestVaultEditor(unittest.TestCase):
self.assertTrue(os.path.exists(tmp_file.name))
@unittest.skipIf(sys.version_info[0] >= 3, "VaultAES still needs to be ported to Python 3")
def test_decrypt_1_0(self):
"""
Skip testing decrypting 1.0 files if we don't have access to AES, KDF or
Counter, or we are running on python3 since VaultAES hasn't been backported.
"""
if not HAS_AES or not HAS_COUNTER or not HAS_PBKDF2 or sys.version > '3':
if not HAS_AES or not HAS_COUNTER or not HAS_PBKDF2:
raise SkipTest
v10_file = tempfile.NamedTemporaryFile(delete=False)
@ -163,12 +163,13 @@ class TestVaultEditor(unittest.TestCase):
assert fdata.strip() == "foo", "incorrect decryption of 1.0 file: %s" % fdata.strip()
@unittest.skipIf(sys.version_info[0] >= 3, "VaultAES still needs to be ported to Python 3")
def test_rekey_migration(self):
"""
Skip testing rekeying files if we don't have access to AES, KDF or
Counter, or we are running on python3 since VaultAES hasn't been backported.
"""
if not HAS_AES or not HAS_COUNTER or not HAS_PBKDF2 or sys.version > '3':
if not HAS_AES or not HAS_COUNTER or not HAS_PBKDF2:
raise SkipTest
v10_file = tempfile.NamedTemporaryFile(delete=False)