]>
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 # systemd is free software; you can redistribute it and/or modify it
7 # under the terms of the GNU Lesser General Public License as published by
8 # the Free Software Foundation; either version 2.1 of the License, or
9 # (at your option) any later version.
11 # systemd is distributed in the hope that it will be useful, but
12 # WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 # Lesser General Public License for more details.
16 # You should have received a copy of the GNU Lesser General Public License
17 # along with systemd; If not, see <http://www.gnu.org/licenses/>.
19 # ATTENTION: This uses the *installed* systemd, not the one from the built
30 class UnitFileChange(Enum
):
33 COMMAND_ADDED_BEFORE
= 2
34 COMMAND_ADDED_AFTER
= 3
35 COMMAND_INTERLEAVED
= 4
38 class ExecutionResumeTest(unittest
.TestCase
):
40 self
.unit
= 'test-issue-518.service'
41 self
.unitfile_path
= '/run/systemd/system/{0}'.format(self
.unit
)
42 self
.output_file
= tempfile
.mktemp()
45 unit_file_content
= '''
48 ExecStart=/bin/sleep 2
49 ExecStart=/bin/bash -c "echo foo >> {0}"
50 '''.format(self
.output_file
)
51 self
.unit_files
[UnitFileChange
.NO_CHANGE
] = unit_file_content
53 unit_file_content
= '''
56 ExecStart=/bin/bash -c "echo foo >> {0}"
57 ExecStart=/bin/sleep 2
58 '''.format(self
.output_file
)
59 self
.unit_files
[UnitFileChange
.LINES_SWAPPED
] = unit_file_content
61 unit_file_content
= '''
64 ExecStart=/bin/bash -c "echo bar >> {0}"
65 ExecStart=/bin/sleep 2
66 ExecStart=/bin/bash -c "echo foo >> {0}"
67 '''.format(self
.output_file
)
68 self
.unit_files
[UnitFileChange
.COMMAND_ADDED_BEFORE
] = unit_file_content
70 unit_file_content
= '''
73 ExecStart=/bin/sleep 2
74 ExecStart=/bin/bash -c "echo foo >> {0}"
75 ExecStart=/bin/bash -c "echo bar >> {0}"
76 '''.format(self
.output_file
)
77 self
.unit_files
[UnitFileChange
.COMMAND_ADDED_AFTER
] = unit_file_content
79 unit_file_content
= '''
82 ExecStart=/bin/bash -c "echo baz >> {0}"
83 ExecStart=/bin/sleep 2
84 ExecStart=/bin/bash -c "echo foo >> {0}"
85 ExecStart=/bin/bash -c "echo bar >> {0}"
86 '''.format(self
.output_file
)
87 self
.unit_files
[UnitFileChange
.COMMAND_INTERLEAVED
] = unit_file_content
89 unit_file_content
= '''
92 ExecStart=/bin/bash -c "echo bar >> {0}"
93 ExecStart=/bin/bash -c "echo baz >> {0}"
94 '''.format(self
.output_file
)
95 self
.unit_files
[UnitFileChange
.REMOVAL
] = unit_file_content
98 subprocess
.check_call(['systemctl', 'daemon-reload'])
100 def write_unit_file(self
, unit_file_change
):
101 if not isinstance(unit_file_change
, UnitFileChange
):
102 raise ValueError('Unknown unit file change')
104 content
= self
.unit_files
[unit_file_change
]
106 with
open(self
.unitfile_path
, 'w') as f
:
111 def check_output(self
, expected_output
):
113 with
open(self
.output_file
, 'r') as log
:
118 self
.assertEqual(output
, expected_output
)
120 def setup_unit(self
):
121 self
.write_unit_file(UnitFileChange
.NO_CHANGE
)
122 subprocess
.check_call(['systemctl', '--job-mode=replace', '--no-block', 'start', self
.unit
])
124 def test_no_change(self
):
125 expected_output
= 'foo\n'
131 self
.check_output(expected_output
)
133 def test_swapped(self
):
137 self
.write_unit_file(UnitFileChange
.LINES_SWAPPED
)
141 self
.assertTrue(not os
.path
.exists(self
.output_file
))
143 def test_added_before(self
):
144 expected_output
= 'foo\n'
147 self
.write_unit_file(UnitFileChange
.COMMAND_ADDED_BEFORE
)
151 self
.check_output(expected_output
)
153 def test_added_after(self
):
154 expected_output
= 'foo\nbar\n'
157 self
.write_unit_file(UnitFileChange
.COMMAND_ADDED_AFTER
)
161 self
.check_output(expected_output
)
163 def test_interleaved(self
):
164 expected_output
= 'foo\nbar\n'
167 self
.write_unit_file(UnitFileChange
.COMMAND_INTERLEAVED
)
171 self
.check_output(expected_output
)
173 def test_removal(self
):
175 self
.write_unit_file(UnitFileChange
.REMOVAL
)
179 self
.assertTrue(not os
.path
.exists(self
.output_file
))
181 def test_issue_6533(self
):
182 unit
= "test-issue-6533.service"
183 unitfile_path
= "/run/systemd/system/{}".format(unit
)
187 ExecStart=/bin/sleep 5
190 with
open(unitfile_path
, 'w') as f
:
195 subprocess
.check_call(['systemctl', '--job-mode=replace', '--no-block', 'start', unit
])
200 ExecStart=/bin/sleep 5
204 with
open(unitfile_path
, 'w') as f
:
210 self
.assertTrue(subprocess
.call("journalctl -b _PID=1 | grep -q 'Freezing execution'", shell
=True) != 0)
213 for f
in [self
.output_file
, self
.unitfile_path
]:
217 # ignore error if log file doesn't exist
222 if __name__
== '__main__':