]> git.apps.os.sepia.ceph.com Git - ceph-ci.git/commitdiff
test/pybind/rbd: make timestamp tests meaningful
authorIlya Dryomov <idryomov@gmail.com>
Tue, 4 Jun 2024 19:37:49 +0000 (21:37 +0200)
committerIlya Dryomov <idryomov@gmail.com>
Thu, 20 Jun 2024 10:52:39 +0000 (12:52 +0200)
The existing asserts don't really test anything, with some of them
being for inequality against a literal of a mismatching type.  As
a result, a bug in access_timestamp() and modify_timestamp() went
unnoticed for years.

Signed-off-by: Ilya Dryomov <idryomov@gmail.com>
(cherry picked from commit a1b6ca863a384fe32e750b9ee8f10688f88dd1e3)

src/test/pybind/test_rbd.py

index 935e584bd6ac309c67022bb80a1d04e75a3cede2..e5618fd66f5945f884cb19592fa69bcf75fa2510 100644 (file)
@@ -592,7 +592,11 @@ class TestImage(object):
 
     def setup_method(self, method):
         self.rbd = RBD()
+        # {create,access,modify}_timestamp() have second precision,
+        # allow for rounding
+        self.time_before_create = datetime.utcnow() - timedelta(seconds=1)
         create_image()
+        self.time_after_create = datetime.utcnow() + timedelta(seconds=1)
         self.image = Image(ioctx, image_name)
 
     def teardown_method(self, method):
@@ -640,19 +644,19 @@ class TestImage(object):
         assert_greater_equal(self.image.data_pool_id(), 0)
 
     def test_create_timestamp(self):
-        timestamp = self.image.create_timestamp()
-        assert_not_equal(0, timestamp.year)
-        assert_not_equal(1970, timestamp.year)
+        time = self.image.create_timestamp()
+        assert self.time_before_create < time
+        assert time < self.time_after_create
 
     def test_access_timestamp(self):
-        timestamp = self.image.access_timestamp()
-        assert_not_equal(0, timestamp.year)
-        assert_not_equal(1970, timestamp.year)
+        time = self.image.access_timestamp()
+        assert self.time_before_create < time
+        assert time < self.time_after_create
 
     def test_modify_timestamp(self):
-        timestamp = self.image.modify_timestamp()
-        assert_not_equal(0, timestamp.year)
-        assert_not_equal(1970, timestamp.year)
+        time = self.image.modify_timestamp()
+        assert self.time_before_create < time
+        assert time < self.time_after_create
 
     def test_invalidate_cache(self):
         self.image.write(b'abc', 0)
@@ -1113,14 +1117,16 @@ class TestImage(object):
         eq(self.image.snap_exists('snap1'), False)
 
     def test_snap_timestamp(self):
+        # get_snap_timestamp() has second precision, allow for rounding
+        time_before = datetime.utcnow() - timedelta(seconds=1)
         self.image.create_snap('snap1')
+        time_after = datetime.utcnow() + timedelta(seconds=1)
         eq(['snap1'], [snap['name'] for snap in self.image.list_snaps()])
         for snap in self.image.list_snaps():
             snap_id = snap["id"]
         time = self.image.get_snap_timestamp(snap_id)
-        assert_not_equal(b'', time.year)
-        assert_not_equal(0, time.year)
-        assert_not_equal(time.year, '1970')
+        assert time_before < time
+        assert time < time_after
         self.image.remove_snap('snap1')
 
     def test_limit_snaps(self):