]> git.ipfire.org Git - thirdparty/samba.git/commitdiff
python:tests: make test_export_keytab_nochange_update() more reliable
authorStefan Metzmacher <metze@samba.org>
Wed, 17 Jul 2024 07:03:34 +0000 (07:03 +0000)
committerStefan Metzmacher <metze@samba.org>
Wed, 17 Jul 2024 14:21:30 +0000 (14:21 +0000)
net.export_keytab() includes the current timestamp into the resulting
keytab. So we need to make sure the two compared exports actually
run within the same second.

And klist may also print the keytab filename...

Signed-off-by: Stefan Metzmacher <metze@samba.org>
Reviewed-by: Andreas Schneider <asn@samba.org>
Autobuild-User(master): Stefan Metzmacher <metze@samba.org>
Autobuild-Date(master): Wed Jul 17 14:21:30 UTC 2024 on atb-devel-224

python/samba/tests/dckeytab.py

index 9dee64e0ceab09cbd9710cfe263273d82496f54c..56be897e0f008f440338ed08125a601147381912 100644 (file)
@@ -18,6 +18,7 @@
 
 import os
 import subprocess
+import time
 from samba.net import Net
 from samba import enable_net_export_keytab
 
@@ -148,10 +149,38 @@ class DCKeytabTests(TestCaseInTempDir):
         self.addCleanup(self.samdb.deleteuser, "keytab_testuser")
 
         net = Net(None, self.lp)
-        self.addCleanup(self.rm_files, self.ktfile)
-        net.export_keytab(keytab=self.ktfile, principal=new_principal)
-        self.assertTrue(os.path.exists(self.ktfile), 'keytab was not created')
 
+        self.addCleanup(self.rm_files, self.ktfile)
+        ktfile1 = self.ktfile + ".1"
+        self.addCleanup(self.rm_files, ktfile1, allow_missing=True)
+        ktfile2 = self.ktfile + ".2"
+        self.addCleanup(self.rm_files, ktfile2, allow_missing=True)
+
+        # The export includes the current timestamp
+        # so we better do both exports within the
+        # same second.
+        #
+        # First we sleep until we reach the next second
+        now = time.time()
+        next = float(int(now)+1)
+        sleep = next-now
+        time.sleep(sleep)
+        start = time.time()
+        net.export_keytab(keytab=ktfile1, principal=new_principal)
+        net.export_keytab(keytab=ktfile2, principal=new_principal)
+        end = time.time()
+        self.assertTrue(os.path.exists(ktfile1), 'keytab1 was not created')
+        self.assertTrue(os.path.exists(ktfile2), 'keytab2 was not created')
+        print("now: %f" % now)
+        print("next: %f" % next)
+        print("sleep: %f" % sleep)
+        print("start: %f" % start)
+        print("end: %f" % end)
+        self.assertEqual(int(end), int(start))
+
+        # The output may contain the file name
+        # so we have to use self.ktfile...
+        os.rename(ktfile1, self.ktfile)
         cmd = ['klist', '-K', '-C', '-t', '-k', self.ktfile]
         keytab_orig_content = subprocess.Popen(
             cmd,
@@ -163,9 +192,10 @@ class DCKeytabTests(TestCaseInTempDir):
         with open(self.ktfile, 'rb') as bytes_kt:
             keytab_orig_bytes = bytes_kt.read()
 
-        net.export_keytab(keytab=self.ktfile, principal=new_principal)
-        self.assertTrue(os.path.exists(self.ktfile), 'keytab was not created')
-
+        # The output may contain the file name
+        # so we have to use self.ktfile...
+        os.rename(ktfile2, self.ktfile)
+        cmd = ['klist', '-K', '-C', '-t', '-k', self.ktfile]
         keytab_content = subprocess.Popen(
             cmd,
             shell=False,