]>
git.ipfire.org Git - thirdparty/systemd.git/blob - test/test-exec-deserialization.py
2 # SPDX-License-Identifier: LGPL-2.1+
4 # Copyright © 2017 Michal Sekletar <msekleta@redhat.com>
6 # ATTENTION: This uses the *installed* systemd, not the one from the built
17 class UnitFileChange(Enum
):
20 COMMAND_ADDED_BEFORE
= 2
21 COMMAND_ADDED_AFTER
= 3
22 COMMAND_INTERLEAVED
= 4
25 class ExecutionResumeTest(unittest
.TestCase
):
27 self
.unit
= 'test-issue-518.service'
28 self
.unitfile_path
= '/run/systemd/system/{0}'.format(self
.unit
)
29 self
.output_file
= tempfile
.mktemp()
32 unit_file_content
= '''
35 ExecStart=/bin/sleep 2
36 ExecStart=/bin/bash -c "echo foo >> {0}"
37 '''.format(self
.output_file
)
38 self
.unit_files
[UnitFileChange
.NO_CHANGE
] = unit_file_content
40 unit_file_content
= '''
43 ExecStart=/bin/bash -c "echo foo >> {0}"
44 ExecStart=/bin/sleep 2
45 '''.format(self
.output_file
)
46 self
.unit_files
[UnitFileChange
.LINES_SWAPPED
] = unit_file_content
48 unit_file_content
= '''
51 ExecStart=/bin/bash -c "echo bar >> {0}"
52 ExecStart=/bin/sleep 2
53 ExecStart=/bin/bash -c "echo foo >> {0}"
54 '''.format(self
.output_file
)
55 self
.unit_files
[UnitFileChange
.COMMAND_ADDED_BEFORE
] = unit_file_content
57 unit_file_content
= '''
60 ExecStart=/bin/sleep 2
61 ExecStart=/bin/bash -c "echo foo >> {0}"
62 ExecStart=/bin/bash -c "echo bar >> {0}"
63 '''.format(self
.output_file
)
64 self
.unit_files
[UnitFileChange
.COMMAND_ADDED_AFTER
] = unit_file_content
66 unit_file_content
= '''
69 ExecStart=/bin/bash -c "echo baz >> {0}"
70 ExecStart=/bin/sleep 2
71 ExecStart=/bin/bash -c "echo foo >> {0}"
72 ExecStart=/bin/bash -c "echo bar >> {0}"
73 '''.format(self
.output_file
)
74 self
.unit_files
[UnitFileChange
.COMMAND_INTERLEAVED
] = unit_file_content
76 unit_file_content
= '''
79 ExecStart=/bin/bash -c "echo bar >> {0}"
80 ExecStart=/bin/bash -c "echo baz >> {0}"
81 '''.format(self
.output_file
)
82 self
.unit_files
[UnitFileChange
.REMOVAL
] = unit_file_content
85 subprocess
.check_call(['systemctl', 'daemon-reload'])
87 def write_unit_file(self
, unit_file_change
):
88 if not isinstance(unit_file_change
, UnitFileChange
):
89 raise ValueError('Unknown unit file change')
91 content
= self
.unit_files
[unit_file_change
]
93 with
open(self
.unitfile_path
, 'w') as f
:
98 def check_output(self
, expected_output
):
100 with
open(self
.output_file
, 'r') as log
:
105 self
.assertEqual(output
, expected_output
)
107 def setup_unit(self
):
108 self
.write_unit_file(UnitFileChange
.NO_CHANGE
)
109 subprocess
.check_call(['systemctl', '--job-mode=replace', '--no-block', 'start', self
.unit
])
111 def test_no_change(self
):
112 expected_output
= 'foo\n'
118 self
.check_output(expected_output
)
120 def test_swapped(self
):
124 self
.write_unit_file(UnitFileChange
.LINES_SWAPPED
)
128 self
.assertTrue(not os
.path
.exists(self
.output_file
))
130 def test_added_before(self
):
131 expected_output
= 'foo\n'
134 self
.write_unit_file(UnitFileChange
.COMMAND_ADDED_BEFORE
)
138 self
.check_output(expected_output
)
140 def test_added_after(self
):
141 expected_output
= 'foo\nbar\n'
144 self
.write_unit_file(UnitFileChange
.COMMAND_ADDED_AFTER
)
148 self
.check_output(expected_output
)
150 def test_interleaved(self
):
151 expected_output
= 'foo\nbar\n'
154 self
.write_unit_file(UnitFileChange
.COMMAND_INTERLEAVED
)
158 self
.check_output(expected_output
)
160 def test_removal(self
):
162 self
.write_unit_file(UnitFileChange
.REMOVAL
)
166 self
.assertTrue(not os
.path
.exists(self
.output_file
))
168 def test_issue_6533(self
):
169 unit
= "test-issue-6533.service"
170 unitfile_path
= "/run/systemd/system/{}".format(unit
)
174 ExecStart=/bin/sleep 5
177 with
open(unitfile_path
, 'w') as f
:
182 subprocess
.check_call(['systemctl', '--job-mode=replace', '--no-block', 'start', unit
])
187 ExecStart=/bin/sleep 5
191 with
open(unitfile_path
, 'w') as f
:
197 self
.assertTrue(subprocess
.call("journalctl -b _PID=1 | grep -q 'Freezing execution'", shell
=True) != 0)
200 for f
in [self
.output_file
, self
.unitfile_path
]:
204 # ignore error if log file doesn't exist
209 if __name__
== '__main__':