]> git.ipfire.org Git - thirdparty/samba.git/commitdiff
pytests: dns_aging get informative assertions
authorDouglas Bagnall <douglas.bagnall@catalyst.net.nz>
Wed, 9 Jun 2021 23:58:43 +0000 (11:58 +1200)
committerAndrew Bartlett <abartlet@samba.org>
Sun, 20 Jun 2021 23:26:32 +0000 (23:26 +0000)
When trying to understand the results of these tests, it is not very
helpful to have messages like:

AssertionError: 3685511 != 3685343

when the only thing you want to know is the difference between these two
numbers. So here we make timestamp specific assertions.

Signed-off-by: Douglas Bagnall <douglas.bagnall@catalyst.net.nz>
Reviewed-by: Andrew Bartlett <abartlet@samba.org>
python/samba/tests/dns_aging.py

index f9bcdb2eed238e414d55e1f4b1ab04b4844a20be..deb43687c45f4b8696fc22f76abca593d36b266d 100644 (file)
@@ -500,8 +500,53 @@ class TestDNSAging(DNSTest):
         we allow one more hour in case it happens during a daylight
         savings transition or something.
         """
-        self.assertGreaterEqual(timestamp, reference)
-        self.assertLess(timestamp, reference + 3)
+        if hasattr(timestamp, 'dwTimeStamp'):
+            timestamp = timestamp.dwTimeStamp
+        if hasattr(reference, 'dwTimeStamp'):
+            reference = reference.dwTimeStamp
+
+        diff = timestamp - reference
+        days = abs(diff / 24.0)
+
+        if diff < 0:
+            msg = f"timestamp is {days} days ({abs(diff)} hours) before reference"
+        elif diff > 2:
+            msg = f"timestamp is {days} days ({diff} hours) after reference"
+        else:
+            return
+        raise AssertionError(msg)
+
+    def assert_timestamps_equal(self, ts1, ts2):
+        """Just like assertEqual(), but tells us the difference, not the
+        absolute values. e.g:
+
+        self.assertEqual(a, b)
+        AssertionError: 3685491 != 3685371
+
+        self.assert_timestamps_equal(a, b)
+        AssertionError: -120 (first is 5.0 days earlier than second)
+
+        Also, we turn a record into a timestamp if we need
+        """
+        if hasattr(ts1, 'dwTimeStamp'):
+            ts1 = ts1.dwTimeStamp
+        if hasattr(ts2, 'dwTimeStamp'):
+            ts2 = ts2.dwTimeStamp
+
+        if ts1 == ts2:
+            return
+
+        diff = ts1 - ts2
+        days = abs(diff / 24.0)
+        if ts1 == 0 or ts2 == 0:
+            # when comparing to zero we don't want the number of days.
+            msg = f"timestamp {ts1} != {ts2}"
+        elif diff > 0:
+            msg = f"{ts1} is {days} days ({diff} hours) after {ts2}"
+        else:
+            msg = f"{ts1} is {days} days ({abs(diff)} hours) before {ts2}"
+
+        raise AssertionError(msg)
 
     def test_update_timestamps_aging_off_then_on(self):
         # we will add a record with aging off