]>
git.ipfire.org Git - thirdparty/systemd.git/blob - test/test-exec-deserialization.py
2 # SPDX-License-Identifier: LGPL-2.1-or-later
3 # pylint: disable=line-too-long,too-many-lines,too-many-branches,too-many-statements,too-many-arguments
4 # pylint: disable=too-many-public-methods,too-many-boolean-expressions,invalid-name,no-self-use
5 # pylint: disable=missing-function-docstring,missing-class-docstring,missing-module-docstring
7 # Copyright © 2017 Michal Sekletar <msekleta@redhat.com>
9 # ATTENTION: This uses the *installed* systemd, not the one from the built
20 class InstallChange(Enum
):
23 COMMAND_ADDED_BEFORE
= 2
24 COMMAND_ADDED_AFTER
= 3
25 COMMAND_INTERLEAVED
= 4
28 class ExecutionResumeTest(unittest
.TestCase
):
30 self
.unit
= 'test-issue-518.service'
31 self
.unitfile_path
= f
'/run/systemd/system/{self.unit}'
32 self
.output_file
= f
"/tmp/test-issue-518-{uuid.uuid4()}"
35 unit_file_content
= f
'''
38 ExecStart=/bin/sleep 3
39 ExecStart=/bin/bash -c "echo foo >>{self.output_file}"
41 self
.unit_files
[InstallChange
.NO_CHANGE
] = unit_file_content
43 unit_file_content
= f
'''
46 ExecStart=/bin/bash -c "echo foo >>{self.output_file}"
47 ExecStart=/bin/sleep 3
49 self
.unit_files
[InstallChange
.LINES_SWAPPED
] = unit_file_content
51 unit_file_content
= f
'''
54 ExecStart=/bin/bash -c "echo bar >>{self.output_file}"
55 ExecStart=/bin/sleep 3
56 ExecStart=/bin/bash -c "echo foo >>{self.output_file}"
58 self
.unit_files
[InstallChange
.COMMAND_ADDED_BEFORE
] = unit_file_content
60 unit_file_content
= f
'''
63 ExecStart=/bin/sleep 3
64 ExecStart=/bin/bash -c "echo foo >>{self.output_file}"
65 ExecStart=/bin/bash -c "echo bar >>{self.output_file}"
67 self
.unit_files
[InstallChange
.COMMAND_ADDED_AFTER
] = unit_file_content
69 unit_file_content
= f
'''
72 ExecStart=/bin/bash -c "echo baz >>{self.output_file}"
73 ExecStart=/bin/sleep 3
74 ExecStart=/bin/bash -c "echo foo >>{self.output_file}"
75 ExecStart=/bin/bash -c "echo bar >>{self.output_file}"
77 self
.unit_files
[InstallChange
.COMMAND_INTERLEAVED
] = unit_file_content
79 unit_file_content
= f
'''
82 ExecStart=/bin/bash -c "echo bar >>{self.output_file}"
83 ExecStart=/bin/bash -c "echo baz >>{self.output_file}"
85 self
.unit_files
[InstallChange
.REMOVAL
] = unit_file_content
88 subprocess
.check_call(['systemctl', 'daemon-reload'])
90 def write_unit_file(self
, unit_file_change
):
91 if not isinstance(unit_file_change
, InstallChange
):
92 raise ValueError('Unknown unit file change')
94 content
= self
.unit_files
[unit_file_change
]
96 with
open(self
.unitfile_path
, 'w', encoding
='utf-8') as f
:
101 def check_output(self
, expected_output
):
103 # Wait until the unit finishes so we don't check an incomplete log
104 if subprocess
.call(['systemctl', '-q', 'is-active', self
.unit
]) == 0:
110 with
open(self
.output_file
, 'r', encoding
='utf-8') as log
:
112 self
.assertEqual(output
, expected_output
)
119 self
.fail(f
'Timed out while waiting for the output file {self.output_file} to appear')
121 def setup_unit(self
):
122 self
.write_unit_file(InstallChange
.NO_CHANGE
)
123 subprocess
.check_call(['systemctl', '--job-mode=replace', '--no-block', 'start', self
.unit
])
126 def test_no_change(self
):
127 expected_output
= 'foo\n'
132 self
.check_output(expected_output
)
134 def test_swapped(self
):
136 self
.write_unit_file(InstallChange
.LINES_SWAPPED
)
139 self
.assertTrue(not os
.path
.exists(self
.output_file
))
141 def test_added_before(self
):
142 expected_output
= 'foo\n'
145 self
.write_unit_file(InstallChange
.COMMAND_ADDED_BEFORE
)
148 self
.check_output(expected_output
)
150 def test_added_after(self
):
151 expected_output
= 'foo\nbar\n'
154 self
.write_unit_file(InstallChange
.COMMAND_ADDED_AFTER
)
157 self
.check_output(expected_output
)
159 def test_interleaved(self
):
160 expected_output
= 'foo\nbar\n'
163 self
.write_unit_file(InstallChange
.COMMAND_INTERLEAVED
)
166 self
.check_output(expected_output
)
168 def test_removal(self
):
170 self
.write_unit_file(InstallChange
.REMOVAL
)
173 self
.assertTrue(not os
.path
.exists(self
.output_file
))
175 def test_issue_6533(self
):
176 unit
= "test-issue-6533.service"
177 unitfile_path
= f
"/run/systemd/system/{unit}"
181 ExecStart=/bin/sleep 5
184 with
open(unitfile_path
, 'w', encoding
='utf-8') as f
:
189 subprocess
.check_call(['systemctl', '--job-mode=replace', '--no-block', 'start', unit
])
194 ExecStart=/bin/sleep 5
198 with
open(unitfile_path
, 'w', encoding
='utf-8') as f
:
204 self
.assertNotEqual(subprocess
.call("journalctl -b _PID=1 | grep -q 'Freezing execution'", shell
=True), 0)
207 for f
in [self
.output_file
, self
.unitfile_path
]:
211 # ignore error if log file doesn't exist
216 if __name__
== '__main__':
217 unittest
.main(testRunner
=unittest
.TextTestRunner(stream
=sys
.stdout
, verbosity
=3))