1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
|
#!/usr/bin/env python3
# SPDX-License-Identifier: LGPL-2.1+
#
# Copyright 2017 Michal Sekletar <msekleta@redhat.com>
# ATTENTION: This uses the *installed* systemd, not the one from the built
# source tree.
import unittest
import time
import os
import tempfile
import subprocess
from enum import Enum
class UnitFileChange(Enum):
NO_CHANGE = 0
LINES_SWAPPED = 1
COMMAND_ADDED_BEFORE = 2
COMMAND_ADDED_AFTER = 3
COMMAND_INTERLEAVED = 4
REMOVAL = 5
class ExecutionResumeTest(unittest.TestCase):
def setUp(self):
self.unit = 'test-issue-518.service'
self.unitfile_path = '/run/systemd/system/{0}'.format(self.unit)
self.output_file = tempfile.mktemp()
self.unit_files = {}
unit_file_content = '''
[Service]
Type=oneshot
ExecStart=/bin/sleep 2
ExecStart=/bin/bash -c "echo foo >> {0}"
'''.format(self.output_file)
self.unit_files[UnitFileChange.NO_CHANGE] = unit_file_content
unit_file_content = '''
[Service]
Type=oneshot
ExecStart=/bin/bash -c "echo foo >> {0}"
ExecStart=/bin/sleep 2
'''.format(self.output_file)
self.unit_files[UnitFileChange.LINES_SWAPPED] = unit_file_content
unit_file_content = '''
[Service]
Type=oneshot
ExecStart=/bin/bash -c "echo bar >> {0}"
ExecStart=/bin/sleep 2
ExecStart=/bin/bash -c "echo foo >> {0}"
'''.format(self.output_file)
self.unit_files[UnitFileChange.COMMAND_ADDED_BEFORE] = unit_file_content
unit_file_content = '''
[Service]
Type=oneshot
ExecStart=/bin/sleep 2
ExecStart=/bin/bash -c "echo foo >> {0}"
ExecStart=/bin/bash -c "echo bar >> {0}"
'''.format(self.output_file)
self.unit_files[UnitFileChange.COMMAND_ADDED_AFTER] = unit_file_content
unit_file_content = '''
[Service]
Type=oneshot
ExecStart=/bin/bash -c "echo baz >> {0}"
ExecStart=/bin/sleep 2
ExecStart=/bin/bash -c "echo foo >> {0}"
ExecStart=/bin/bash -c "echo bar >> {0}"
'''.format(self.output_file)
self.unit_files[UnitFileChange.COMMAND_INTERLEAVED] = unit_file_content
unit_file_content = '''
[Service]
Type=oneshot
ExecStart=/bin/bash -c "echo bar >> {0}"
ExecStart=/bin/bash -c "echo baz >> {0}"
'''.format(self.output_file)
self.unit_files[UnitFileChange.REMOVAL] = unit_file_content
def reload(self):
subprocess.check_call(['systemctl', 'daemon-reload'])
def write_unit_file(self, unit_file_change):
if not isinstance(unit_file_change, UnitFileChange):
raise ValueError('Unknown unit file change')
content = self.unit_files[unit_file_change]
with open(self.unitfile_path, 'w') as f:
f.write(content)
self.reload()
def check_output(self, expected_output):
try:
with open(self.output_file, 'r') as log:
output = log.read()
except IOError:
self.fail()
self.assertEqual(output, expected_output)
def setup_unit(self):
self.write_unit_file(UnitFileChange.NO_CHANGE)
subprocess.check_call(['systemctl', '--job-mode=replace', '--no-block', 'start', self.unit])
def test_no_change(self):
expected_output = 'foo\n'
self.setup_unit()
self.reload()
time.sleep(4)
self.check_output(expected_output)
def test_swapped(self):
expected_output = ''
self.setup_unit()
self.write_unit_file(UnitFileChange.LINES_SWAPPED)
self.reload()
time.sleep(4)
self.assertTrue(not os.path.exists(self.output_file))
def test_added_before(self):
expected_output = 'foo\n'
self.setup_unit()
self.write_unit_file(UnitFileChange.COMMAND_ADDED_BEFORE)
self.reload()
time.sleep(4)
self.check_output(expected_output)
def test_added_after(self):
expected_output = 'foo\nbar\n'
self.setup_unit()
self.write_unit_file(UnitFileChange.COMMAND_ADDED_AFTER)
self.reload()
time.sleep(4)
self.check_output(expected_output)
def test_interleaved(self):
expected_output = 'foo\nbar\n'
self.setup_unit()
self.write_unit_file(UnitFileChange.COMMAND_INTERLEAVED)
self.reload()
time.sleep(4)
self.check_output(expected_output)
def test_removal(self):
self.setup_unit()
self.write_unit_file(UnitFileChange.REMOVAL)
self.reload()
time.sleep(4)
self.assertTrue(not os.path.exists(self.output_file))
def test_issue_6533(self):
unit = "test-issue-6533.service"
unitfile_path = "/run/systemd/system/{}".format(unit)
content = '''
[Service]
ExecStart=/bin/sleep 5
'''
with open(unitfile_path, 'w') as f:
f.write(content)
self.reload()
subprocess.check_call(['systemctl', '--job-mode=replace', '--no-block', 'start', unit])
time.sleep(2)
content = '''
[Service]
ExecStart=/bin/sleep 5
ExecStart=/bin/true
'''
with open(unitfile_path, 'w') as f:
f.write(content)
self.reload()
time.sleep(5)
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]:
try:
os.remove(f)
except OSError:
# ignore error if log file doesn't exist
pass
self.reload()
if __name__ == '__main__':
unittest.main()
|