From c587035876f03b60770bc6109b7caa679f950653 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 | 33 ++++++++++++++++----------------- src/tests/pysss_murmur-test.py | 33 ++++++++++++++++----------------- 2 files changed, 32 insertions(+), 34 deletions(-) diff --git a/src/tests/pyhbac-test.py b/src/tests/pyhbac-test.py index 8fa5169e0d7fd8f56870115769b7dbf24ebbd573..e34f05596d496525d5349d073bf28cfa156db1af 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,15 @@ 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" + + src_module_path = os.path.abspath(src_module_path) + os.symlink(src_module_path, dest_module_path) import pyhbac except ImportError as e: @@ -470,6 +464,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..7237c95b015c6a66917ee9a2604ef54ef2ad9718 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,15 @@ 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" + + src_module_path = os.path.abspath(src_module_path) + os.symlink(src_module_path, dest_module_path) import pysss_murmur except ImportError as e: @@ -67,6 +61,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