From e750e0c0eca29d45ea8daf96f1f07e484ebc97a4 Mon Sep 17 00:00:00 2001 From: Lukas Slebodnik Date: Wed, 13 Jan 2016 15:06:06 +0100 Subject: [PATCH] TESTS: Fix race condition in python test Python tests for pyhbac and pysss_murmur created symbolic links in shared directory ".libs". It happened that both tests created symbolic link in the same time and therefore python2 test could try to import link to python3 module which caused failures in tests. --- src/tests/pyhbac-test.py | 32 +++++++++++++++----------------- src/tests/pysss_murmur-test.py | 32 +++++++++++++++----------------- 2 files changed, 30 insertions(+), 34 deletions(-) diff --git a/src/tests/pyhbac-test.py b/src/tests/pyhbac-test.py index 8fa5169e0d7fd8f56870115769b7dbf24ebbd573..7baaf9498a86be8ccf36cf2e323daf0ae2857e49 100755 --- a/src/tests/pyhbac-test.py +++ b/src/tests/pyhbac-test.py @@ -5,11 +5,12 @@ import unittest import sys import os import copy -import sys -import errno +import tempfile + +BUILD_DIR = os.getenv('builddir') or "." +TEST_DIR = os.getenv('SSS_TEST_DIR') or "." +MODPATH = tempfile.mkdtemp(prefix="tp_pyhbac_", dir=TEST_DIR) -srcdir = os.getenv('builddir') or "." -MODPATH = srcdir + "/.libs" #FIXME - is there a way to get this from libtool? if sys.version_info[0] > 2: unicode = str @@ -52,22 +53,14 @@ class PyHbacImport(unittest.TestCase): def testImport(self): " Import the module and assert it comes from tree " try: - cwd_backup = os.getcwd() + dest_module_path = MODPATH + "/pyhbac.so" - try: - os.unlink(MODPATH + "/pyhbac.so") - except OSError as e: - if e.errno == errno.ENOENT: - pass - else: - raise e - - os.chdir(MODPATH) if sys.version_info[0] > 2: - os.symlink("_py3hbac.so", "pyhbac.so") + src_module_path = BUILD_DIR + "/.libs/_py3hbac.so" else: - os.symlink("_py2hbac.so", "pyhbac.so") - os.chdir(cwd_backup) + src_module_path = BUILD_DIR + "/.libs/_py2hbac.so" + + os.link(src_module_path, dest_module_path) import pyhbac except ImportError as e: @@ -470,6 +463,11 @@ class PyHbacRequestTest(unittest.TestCase): self.assertRaises(TypeError, req.evaluate, (allow_rule, None)) class PyHbacModuleTest(unittest.TestCase): + @classmethod + def tearDownClass(cls): + os.unlink(MODPATH + "/pyhbac.so") + os.rmdir(MODPATH) + def testHasResultTypes(self): assert hasattr(pyhbac, "HBAC_EVAL_ALLOW") assert hasattr(pyhbac, "HBAC_EVAL_DENY") diff --git a/src/tests/pysss_murmur-test.py b/src/tests/pysss_murmur-test.py index d21450a2644adee610f5da071fbed547cfa95a9b..4a5c293f1e36d14102851de12f006158454ebb34 100755 --- a/src/tests/pysss_murmur-test.py +++ b/src/tests/pysss_murmur-test.py @@ -22,11 +22,12 @@ from __future__ import print_function import unittest import sys import os -import copy -import errno +import tempfile + +BUILD_DIR = os.getenv('builddir') or "." +TEST_DIR = os.getenv('SSS_TEST_DIR') or "." +MODPATH = tempfile.mkdtemp(prefix="tp_pysss_murmur_", dir=TEST_DIR) -srcdir = os.getenv('builddir') or "." -MODPATH = srcdir + "/.libs" #FIXME - is there a way to get this from libtool? class PySssMurmurImport(unittest.TestCase): def setUp(self): @@ -43,22 +44,14 @@ class PySssMurmurImport(unittest.TestCase): def testImport(self): " Import the module and assert it comes from tree " try: - cwd_backup = os.getcwd() + dest_module_path = MODPATH + "/pysss_murmur.so" - try: - os.unlink(MODPATH + "/pysss_murmur.so") - except OSError as e: - if e.errno == errno.ENOENT: - pass - else: - raise e - - os.chdir(MODPATH) if sys.version_info[0] > 2: - os.symlink("_py3sss_murmur.so", "pysss_murmur.so") + src_module_path = BUILD_DIR + "/.libs/_py3sss_murmur.so" else: - os.symlink("_py2sss_murmur.so", "pysss_murmur.so") - os.chdir(cwd_backup) + src_module_path = BUILD_DIR + "/.libs/_py2sss_murmur.so" + + os.link(src_module_path, dest_module_path) import pysss_murmur except ImportError as e: @@ -67,6 +60,11 @@ class PySssMurmurImport(unittest.TestCase): self.assertEqual(pysss_murmur.__file__, MODPATH + "/pysss_murmur.so") class PySssMurmurTest(unittest.TestCase): + @classmethod + def tearDownClass(cls): + os.unlink(MODPATH + "/pysss_murmur.so") + os.rmdir(MODPATH) + def testExpectedHash(self): hash = pysss_murmur.murmurhash3("S-1-5-21-2153326666-2176343378-3404031434", 41, 0xdeadbeef) self.assertEqual(hash, 93103853) -- 2.5.0