]>
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
18 class UnitFileChange(Enum
):
21 COMMAND_ADDED_BEFORE
= 2
22 COMMAND_ADDED_AFTER
= 3
23 COMMAND_INTERLEAVED
= 4
26 class ExecutionResumeTest(unittest
.TestCase
):
28 self
.unit
= 'test-issue-518.service'
29 self
.unitfile_path
= '/run/systemd/system/{0}'.format(self
.unit
)
30 self
.output_file
= tempfile
.mktemp()
33 unit_file_content
= '''
36 ExecStart=/bin/sleep 3
37 ExecStart=/bin/bash -c "echo foo >> {0}"
38 '''.format(self
.output_file
)
39 self
.unit_files
[UnitFileChange
.NO_CHANGE
] = unit_file_content
41 unit_file_content
= '''
44 ExecStart=/bin/bash -c "echo foo >> {0}"
45 ExecStart=/bin/sleep 3
46 '''.format(self
.output_file
)
47 self
.unit_files
[UnitFileChange
.LINES_SWAPPED
] = unit_file_content
49 unit_file_content
= '''
52 ExecStart=/bin/bash -c "echo bar >> {0}"
53 ExecStart=/bin/sleep 3
54 ExecStart=/bin/bash -c "echo foo >> {0}"
55 '''.format(self
.output_file
)
56 self
.unit_files
[UnitFileChange
.COMMAND_ADDED_BEFORE
] = unit_file_content
58 unit_file_content
= '''
61 ExecStart=/bin/sleep 3
62 ExecStart=/bin/bash -c "echo foo >> {0}"
63 ExecStart=/bin/bash -c "echo bar >> {0}"
64 '''.format(self
.output_file
)
65 self
.unit_files
[UnitFileChange
.COMMAND_ADDED_AFTER
] = unit_file_content
67 unit_file_content
= '''
70 ExecStart=/bin/bash -c "echo baz >> {0}"
71 ExecStart=/bin/sleep 3
72 ExecStart=/bin/bash -c "echo foo >> {0}"
73 ExecStart=/bin/bash -c "echo bar >> {0}"
74 '''.format(self
.output_file
)
75 self
.unit_files
[UnitFileChange
.COMMAND_INTERLEAVED
] = unit_file_content
77 unit_file_content
= '''
80 ExecStart=/bin/bash -c "echo bar >> {0}"
81 ExecStart=/bin/bash -c "echo baz >> {0}"
82 '''.format(self
.output_file
)
83 self
.unit_files
[UnitFileChange
.REMOVAL
] = unit_file_content
86 subprocess
.check_call(['systemctl', 'daemon-reload'])
88 def write_unit_file(self
, unit_file_change
):
89 if not isinstance(unit_file_change
, UnitFileChange
):
90 raise ValueError('Unknown unit file change')
92 content
= self
.unit_files
[unit_file_change
]
94 with
open(self
.unitfile_path
, 'w') as f
:
99 def check_output(self
, expected_output
):
101 with
open(self
.output_file
, 'r') as log
:
106 self
.assertEqual(output
, expected_output
)
108 def setup_unit(self
):
109 self
.write_unit_file(UnitFileChange
.NO_CHANGE
)
110 subprocess
.check_call(['systemctl', '--job-mode=replace', '--no-block', 'start', self
.unit
])
113 def test_no_change(self
):
114 expected_output
= 'foo\n'
120 self
.check_output(expected_output
)
122 def test_swapped(self
):
126 self
.write_unit_file(UnitFileChange
.LINES_SWAPPED
)
130 self
.assertTrue(not os
.path
.exists(self
.output_file
))
132 def test_added_before(self
):
133 expected_output
= 'foo\n'
136 self
.write_unit_file(UnitFileChange
.COMMAND_ADDED_BEFORE
)
140 self
.check_output(expected_output
)
142 def test_added_after(self
):
143 expected_output
= 'foo\nbar\n'
146 self
.write_unit_file(UnitFileChange
.COMMAND_ADDED_AFTER
)
150 self
.check_output(expected_output
)
152 def test_interleaved(self
):
153 expected_output
= 'foo\nbar\n'
156 self
.write_unit_file(UnitFileChange
.COMMAND_INTERLEAVED
)
160 self
.check_output(expected_output
)
162 def test_removal(self
):
164 self
.write_unit_file(UnitFileChange
.REMOVAL
)
168 self
.assertTrue(not os
.path
.exists(self
.output_file
))
170 def test_issue_6533(self
):
171 unit
= "test-issue-6533.service"
172 unitfile_path
= "/run/systemd/system/{}".format(unit
)
176 ExecStart=/bin/sleep 5
179 with
open(unitfile_path
, 'w') as f
:
184 subprocess
.check_call(['systemctl', '--job-mode=replace', '--no-block', 'start', unit
])
189 ExecStart=/bin/sleep 5
193 with
open(unitfile_path
, 'w') as f
:
199 self
.assertTrue(subprocess
.call("journalctl -b _PID=1 | grep -q 'Freezing execution'", shell
=True) != 0)
202 for f
in [self
.output_file
, self
.unitfile_path
]:
206 # ignore error if log file doesn't exist
211 if __name__
== '__main__':
212 unittest
.main(testRunner
=unittest
.TextTestRunner(stream
=sys
.stdout
, verbosity
=3))