[Piglit] [PATCH 18/44] base_tests.py: make tests for timeout run on python 3.x

baker.dylan.c at gmail.com baker.dylan.c at gmail.com
Wed Jan 27 16:06:26 PST 2016


From: Dylan Baker <baker.dylan.c at gmail.com>

These tests are always valid on python 3, but only valid on python 2 if
subprocess32 is installed.

One unittest on python3 raises a warning. I've tracked it back to somewhere
in the python stdlib. I think that for some reason a file is being
unlinked twice, but I'm having trouble tracking it down, it works fine
under python 2.7, and I think it's safe to ignore.

Signed-off-by: Dylan Baker <dylanx.c.baker at intel.com>
---
 tox.ini                 |  2 +-
 unittests/base_tests.py | 24 +++++++++++++++---------
 2 files changed, 16 insertions(+), 10 deletions(-)

diff --git a/tox.ini b/tox.ini
index 1914d1e..3177fef 100644
--- a/tox.ini
+++ b/tox.ini
@@ -9,13 +9,13 @@ deps =
     mako
     nose
     coverage
+    six
     {accel,noaccel}: mock
     accel: simplejson
     accel: lxml
     accel: backports.lzma
     accel: subprocess32
     accel: psutil
-    generator: six
     generator: numpy
 commands = 
     {accel,noaccel}: nosetests unittests []
diff --git a/unittests/base_tests.py b/unittests/base_tests.py
index 3235cc6..59c1c8e 100644
--- a/unittests/base_tests.py
+++ b/unittests/base_tests.py
@@ -32,6 +32,7 @@ except ImportError:
 
 import nose.tools as nt
 from nose.plugins.attrib import attr
+import six
 
 try:
     import psutil
@@ -93,10 +94,12 @@ def test_timeout_kill_children():
     This test could leave processes running if it fails.
 
     """
-    utils.module_check('subprocess32')
     utils.module_check('psutil')
-
-    import subprocess32  # pylint: disable=import-error
+    if six.PY2:
+        utils.module_check('subprocess32')
+        import subprocess32 as subprocess  # pylint: disable=import-error
+    elif six.PY3:
+        import subprocess
 
     class PopenProxy(object):
         """An object that proxies Popen, and saves the Popen instance as an
@@ -109,7 +112,7 @@ def test_timeout_kill_children():
             self.popen = None
 
         def __call__(self, *args, **kwargs):
-            self.popen = subprocess32.Popen(*args, **kwargs)
+            self.popen = subprocess.Popen(*args, **kwargs)
 
             # if commuincate cis called successfully then the proc will be
             # reset to None, whic will make the test fail.
@@ -117,7 +120,7 @@ def test_timeout_kill_children():
 
             return self.popen
 
-    with tempfile.NamedTemporaryFile() as f:
+    with tempfile.NamedTemporaryFile(mode='w+') as f:
         # Create a file that will be executed as a python script
         # Create a process with two subproccesses (not threads) that will run
         # for a long time.
@@ -148,7 +151,7 @@ def test_timeout_kill_children():
         # mock out subprocess.Popen with our proxy object
         with mock.patch('framework.test.base.subprocess') as mock_subp:
             mock_subp.Popen = proxy
-            mock_subp.TimeoutExpired = subprocess32.TimeoutExpired
+            mock_subp.TimeoutExpired = subprocess.TimeoutExpired
             test.run()
 
         # Check to see if the Popen has children, even after it should have
@@ -175,7 +178,8 @@ def test_timeout():
     if the test runs 5 seconds it's run too long
 
     """
-    utils.module_check('subprocess32')
+    if six.PY2:
+        utils.module_check('subprocess32')
     utils.binary_check('sleep', 1)
 
     test = TimeoutTest(['sleep', '60'])
@@ -187,7 +191,8 @@ def test_timeout():
 @nt.timed(6)
 def test_timeout_timeout():
     """test.base.Test: Sets status to 'timeout' when timeout exceeded"""
-    utils.module_check('subprocess32')
+    if six.PY2:
+        utils.module_check('subprocess32')
     utils.binary_check('sleep', 1)
 
     test = TimeoutTest(['sleep', '60'])
@@ -200,7 +205,8 @@ def test_timeout_timeout():
 def test_timeout_pass():
     """test.base.Test: Doesn't change status when timeout not exceeded
     """
-    utils.module_check('subprocess32')
+    if six.PY2:
+        utils.module_check('subprocess32')
     utils.binary_check('true')
 
     test = TimeoutTest(['true'])
-- 
2.7.0



More information about the Piglit mailing list