]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
test: check for the output file in a loop
authorFrantisek Sumsal <frantisek@sumsal.cz>
Fri, 2 Sep 2022 18:06:12 +0000 (20:06 +0200)
committerFrantisek Sumsal <frantisek@sumsal.cz>
Fri, 2 Sep 2022 18:06:12 +0000 (20:06 +0200)
This should make the test faster on fast machines and more reliable on
slower/under-load machines, where the 4 sec sleep wasn't sometimes enough.

Spotted on C8S machines under load:

```
test_added_after (__main__.ExecutionResumeTest) ... FAIL
test_added_before (__main__.ExecutionResumeTest) ... ok
test_interleaved (__main__.ExecutionResumeTest) ... ok
test_issue_6533 (__main__.ExecutionResumeTest) ... ok
test_no_change (__main__.ExecutionResumeTest) ... ok
test_removal (__main__.ExecutionResumeTest) ... ok
test_swapped (__main__.ExecutionResumeTest) ... ok

======================================================================
FAIL: test_added_after (__main__.ExecutionResumeTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "./test/test-exec-deserialization.py", line 101, in check_output
    with open(self.output_file, 'r') as log:
FileNotFoundError: [Errno 2] No such file or directory: '/tmp/tmpjnec1dj4'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "./test/test-exec-deserialization.py", line 150, in test_added_after
    self.check_output(expected_output)
  File "./test/test-exec-deserialization.py", line 104, in check_output
    self.fail()
AssertionError: None

----------------------------------------------------------------------
Ran 7 tests in 44.270s
```

test/test-exec-deserialization.py

index 9491e4081e4191002b94ec00ba8cfa09f1086280..ce118c8add6eaa5e89f2ba976369f52d7b303ed2 100755 (executable)
@@ -97,13 +97,18 @@ class ExecutionResumeTest(unittest.TestCase):
         self.reload()
 
     def check_output(self, expected_output):
-        try:
-            with open(self.output_file, 'r') as log:
-                output = log.read()
-        except IOError:
-            self.fail()
+        for _ in range(15):
+            try:
+                with open(self.output_file, 'r') as log:
+                    output = log.read()
+                    self.assertEqual(output, expected_output)
+                    return
+            except IOError:
+                pass
+
+            time.sleep(1)
 
-        self.assertEqual(output, expected_output)
+        self.fail("Time out while waiting for the output file {} to appear".format(self.output_file))
 
     def setup_unit(self):
         self.write_unit_file(UnitFileChange.NO_CHANGE)
@@ -115,7 +120,6 @@ class ExecutionResumeTest(unittest.TestCase):
 
         self.setup_unit()
         self.reload()
-        time.sleep(4)
 
         self.check_output(expected_output)
 
@@ -125,7 +129,6 @@ class ExecutionResumeTest(unittest.TestCase):
         self.setup_unit()
         self.write_unit_file(UnitFileChange.LINES_SWAPPED)
         self.reload()
-        time.sleep(4)
 
         self.assertTrue(not os.path.exists(self.output_file))
 
@@ -135,7 +138,6 @@ class ExecutionResumeTest(unittest.TestCase):
         self.setup_unit()
         self.write_unit_file(UnitFileChange.COMMAND_ADDED_BEFORE)
         self.reload()
-        time.sleep(4)
 
         self.check_output(expected_output)
 
@@ -145,7 +147,6 @@ class ExecutionResumeTest(unittest.TestCase):
         self.setup_unit()
         self.write_unit_file(UnitFileChange.COMMAND_ADDED_AFTER)
         self.reload()
-        time.sleep(4)
 
         self.check_output(expected_output)
 
@@ -155,7 +156,6 @@ class ExecutionResumeTest(unittest.TestCase):
         self.setup_unit()
         self.write_unit_file(UnitFileChange.COMMAND_INTERLEAVED)
         self.reload()
-        time.sleep(4)
 
         self.check_output(expected_output)
 
@@ -163,7 +163,6 @@ class ExecutionResumeTest(unittest.TestCase):
         self.setup_unit()
         self.write_unit_file(UnitFileChange.REMOVAL)
         self.reload()
-        time.sleep(4)
 
         self.assertTrue(not os.path.exists(self.output_file))
 
@@ -196,7 +195,7 @@ class ExecutionResumeTest(unittest.TestCase):
         self.reload()
         time.sleep(5)
 
-        self.assertTrue(subprocess.call("journalctl -b _PID=1  | grep -q 'Freezing execution'", shell=True) != 0)
+        self.assertTrue(subprocess.call("journalctl -b _PID=1 | grep -q 'Freezing execution'", shell=True) != 0)
 
     def tearDown(self):
         for f in [self.output_file, self.unitfile_path]: