From b4c6d0374531fcb821febdde1e9d8f1f4e1b4bd9 Mon Sep 17 00:00:00 2001
From: Sergio Durigan Junior <sergio.durigan@canonical.com>
Date: Mon, 17 May 2021 19:09:14 -0400
Subject: [PATCH] Improve assertion when verifying paths for Python modules

In Ubuntu we're facing a problem where the 3 Python tests under
src/tests/*-test.py are failing due to cosmetical differences between
what the '.__file__' method returns and what 'MODPATH' ends up being.

I have not been able to pinpoint exactly what is causing this issue;
it only happens when SSSD is built inside a chroot environment (with
sbuild, for example).  The logs look like this:

F
======================================================================
FAIL: testImport (__main__.PyHbacImport)
Import the module and assert it comes from tree
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/<<PKGBUILDDIR>>/src/tests/pyhbac-test.py", line 91, in testImport
    self.assertEqual(pyhbac.__file__, MODPATH + "/pyhbac.so")
AssertionError: '/<<PKGBUILDDIR>>/build/./tp_pyhbac_xw2omut2/pyhbac.so' != './tp_pyhbac_xw2omut2/pyhbac.so'
- /<<PKGBUILDDIR>>/build/./tp_pyhbac_xw2omut2/pyhbac.so
+ ./tp_pyhbac_xw2omut2/pyhbac.so

Given that the intention of the test is to verify that the two paths
are equal, I suggest that we do this slight improvement and call
'os.path.realpath' before comparing both paths.  This way we guarantee
that they're both properly canonicalized.

I have verified that the tests still pass with this change.

Signed-off-by: Sergio Durigan Junior <sergio.durigan@canonical.com>
---
 src/tests/pyhbac-test.py       | 3 ++-
 src/tests/pysss-test.py        | 3 ++-
 src/tests/pysss_murmur-test.py | 3 ++-
 3 files changed, 6 insertions(+), 3 deletions(-)

diff --git a/src/tests/pyhbac-test.py b/src/tests/pyhbac-test.py
index 06163afc50..c8ce47fee4 100755
--- a/src/tests/pyhbac-test.py
+++ b/src/tests/pyhbac-test.py
@@ -88,7 +88,8 @@ def testImport(self):
             print("Could not load the pyhbac module. Please check if it is "
                   "compiled", file=sys.stderr)
             raise e
-        self.assertEqual(pyhbac.__file__, MODPATH + "/pyhbac.so")
+        self.assertEqual(os.path.realpath(pyhbac.__file__),
+                         os.path.realpath(MODPATH + "/pyhbac.so"))
 
 
 class PyHbacRuleElementTest(unittest.TestCase):
diff --git a/src/tests/pysss-test.py b/src/tests/pysss-test.py
index 30bc074abd..20ef0abc65 100755
--- a/src/tests/pysss-test.py
+++ b/src/tests/pysss-test.py
@@ -58,7 +58,8 @@ def test_import(self):
             print("Could not load the pysss module. Please check if it is "
                   "compiled", file=sys.stderr)
             raise ex
-        self.assertEqual(pysss.__file__, MODPATH + "/pysss.so")
+        self.assertEqual(os.path.realpath(pysss.__file__),
+                         os.path.realpath(MODPATH + "/pysss.so"))
 
 
 class PysssEncryptTest(unittest.TestCase):
diff --git a/src/tests/pysss_murmur-test.py b/src/tests/pysss_murmur-test.py
index 531f8b55f6..75b46518ac 100755
--- a/src/tests/pysss_murmur-test.py
+++ b/src/tests/pysss_murmur-test.py
@@ -59,7 +59,8 @@ def testImport(self):
             print("Could not load the pysss_murmur module. "
                   "Please check if it is compiled", file=sys.stderr)
             raise e
-        self.assertEqual(pysss_murmur.__file__, MODPATH + "/pysss_murmur.so")
+        self.assertEqual(os.path.realpath(pysss_murmur.__file__),
+                         os.path.realpath(MODPATH + "/pysss_murmur.so"))
 
 
 class PySssMurmurTestNeg(unittest.TestCase):
