[apparmor] [patch] parser: make caching tests not fail w/python <= 3.2

Steve Beattie steve at nxnw.org
Wed Aug 26 23:22:46 UTC 2015


On Wed, Aug 26, 2015 at 11:44:04PM +0200, Christian Boltz wrote:
> Am Mittwoch, 26. August 2015 schrieb Steve Beattie:
> > The following patch creates a new time stamp equality assertion
> > function that detects if it's running on python 3.2 or earlier, and
> ...
> > +        if (int(major) <= 3) and (int(minor) <= 2):
> > +            self.assertAlmostEquals(time1, time2, places=5)
> > +        else:
> > +            self.assertEquals(time1, time2)
> 
> Just curious - what about Python 2.x? Python 2.7 will hit the "else" 
> branch (because minor is > 2), and I'm not sure if that's what you want.
> 
> (Assuming we still want to support py2 - I know we do this officially, 
> but some code already disagrees ;-) and nobody (except you) complained 
> yet.)

This particular set of code depends on some features that are
explicitly python3 only (the metaclass bits, the syntax for which
changed incompatibly between python2 and python3), and since this
script is not user visible, nor does it exercise any of the user
visible python code, it was deemed okay to be python3 only.

That said, the logic above was intended to fall back to the looser
equality assert under python2, and clearly would not. v2 of the patch
follows:

v2: change logic for python2.x versions.

Signed-off-by: Steve Beattie <steve at nxnw.org>
---
 parser/tst/caching.py |   21 ++++++++++++++++++---
 1 file changed, 18 insertions(+), 3 deletions(-)

Index: b/parser/tst/caching.py
===================================================================
--- a/parser/tst/caching.py
+++ b/parser/tst/caching.py
@@ -17,6 +17,7 @@
 
 from argparse import ArgumentParser
 import os
+import platform
 import shutil
 import time
 import tempfile
@@ -232,10 +233,24 @@ class AAParserCachingTests(AAParserCachi
         self.run_cmd_check(cmd)
         self.assert_path_exists(self.cache_file)
 
+    def _assertTimeStampEquals(self, time1, time2):
+        '''Compare two timestamps to ensure equality'''
+
+        # python 3.2 and earlier don't support writing timestamps with
+        # nanosecond resolution, only microsecond. When comparing
+        # timestamps in such an environment, loosen the equality bounds
+        # to compensate
+        # Reference: https://bugs.python.org/issue12904
+        (major, minor, _) = platform.python_version_tuple()
+        if (int(major) < 3) or ((int(major) == 3) and (int(minor) <= 2)):
+            self.assertAlmostEquals(time1, time2, places=5)
+        else:
+            self.assertEquals(time1, time2)
+
     def _set_mtime(self, path, mtime):
         atime = os.stat(path).st_atime
         os.utime(path, (atime, mtime))
-        self.assertEquals(os.stat(path).st_mtime, mtime)
+        self._assertTimeStampEquals(os.stat(path).st_mtime, mtime)
 
     def test_cache_loaded_when_exists(self):
         '''test cache is loaded when it exists, is newer than profile,  and features match'''
@@ -414,7 +429,7 @@ class AAParserCachingTests(AAParserCachi
 
         stat = os.stat(self.cache_file)
         self.assertNotEquals(orig_stat.st_ino, stat.st_ino)
-        self.assertEquals(profile_mtime, stat.st_mtime)
+        self._assertTimeStampEquals(profile_mtime, stat.st_mtime)
 
     def test_abstraction_newer_rewrites_cache(self):
         '''test cache is rewritten if abstraction is newer'''
@@ -431,7 +446,7 @@ class AAParserCachingTests(AAParserCachi
 
         stat = os.stat(self.cache_file)
         self.assertNotEquals(orig_stat.st_ino, stat.st_ino)
-        self.assertEquals(abstraction_mtime, stat.st_mtime)
+        self._assertTimeStampEquals(abstraction_mtime, stat.st_mtime)
 
     def test_parser_newer_uses_cache(self):
         '''test cache is not skipped if parser is newer'''

-- 
Steve Beattie
<sbeattie at ubuntu.com>
http://NxNW.org/~steve/
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 819 bytes
Desc: Digital signature
URL: <https://lists.ubuntu.com/archives/apparmor/attachments/20150826/1ccac791/attachment.pgp>


More information about the AppArmor mailing list