1
0
mirror of https://github.com/systemd/systemd.git synced 2024-12-26 03:22:00 +03:00

test: check for the output file in a loop

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
```
This commit is contained in:
Frantisek Sumsal 2022-09-02 20:06:12 +02:00
parent 81e327c42c
commit 4aa84ef9a3

View File

@ -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
self.assertEqual(output, expected_output)
time.sleep(1)
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]: