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

Steve Beattie steve at nxnw.org
Wed Aug 26 20:51:02 UTC 2015


Hi,

In recent commits, Tyler fixed some problems with the caching behavior
of the parser, as well as adjusting and improving the caching test
script to verify these behaviors.

In doing so, the test script adjusts the mtime of various
files and ensures that the written files have the expected mtime
timestamp. Unfortunately, the os.utime() function used to adjust mtime
in python 3.2 (as included in Ubuntu 12.04 LTS) does not update with
nanosecond precision, even though the timestamps returned by os.stat()
do have precision to nanoseconds. This causes the tests to fail when
running under python 3.2 with errors like the following:

  ======================================================================
  FAIL: test_abstraction_newer_rewrites_cache (__main__.AAParserCachingTests)
  ----------------------------------------------------------------------
  Traceback (most recent call last):
    File "/��PKGBUILDDIR��/parser/tst/testlib.py", line 50, in new_unittest_func
      return unittest_func(self)
    File "./caching.py", line 424, in test_abstraction_newer_rewrites_cache
      self._set_mtime(self.abstraction, abstraction_mtime)
    File "./caching.py", line 238, in _set_mtime
      self.assertEquals(os.stat(path).st_mtime, mtime)
  AssertionError: 1440337039.40212 != 1440337039.4021206

The following patch creates a new time stamp equality assertion
function that detects if it's running on python 3.2 or earlier, and
loosens the equality bounds when comparing the passed timestamps. On
python 3.3 and newer, where writing timestamps with nanosecond precision
is supported, the strict equality assertion is used.

(Note: I did not convert all time stamp comparisons, just ones where
the timestamp written and checked could be based on a timestamp
derived from os.stat().)

Reference: https://bugs.python.org/issue12904

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) 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/5b43f1ca/attachment.pgp>


More information about the AppArmor mailing list