Merge lp:~james-page/charm-helpers/systemd-support into lp:charm-helpers
- systemd-support
- Merge into devel
Proposed by
James Page
Status: | Merged |
---|---|
Merged at revision: | 513 |
Proposed branch: | lp:~james-page/charm-helpers/systemd-support |
Merge into: | lp:charm-helpers |
Diff against target: |
414 lines (+140/-36) 3 files modified
charmhelpers/core/host.py (+34/-17) tests/contrib/charmsupport/test_nrpe.py (+3/-3) tests/core/test_host.py (+103/-16) |
To merge this branch: | bzr merge lp:~james-page/charm-helpers/systemd-support |
Related bugs: |
Reviewer | Review Type | Date Requested | Status |
---|---|---|---|
Liam Young (community) | Approve | ||
Adam Collard (community) | Needs Fixing | ||
Review via email: mp+281742@code.launchpad.net |
Commit message
Description of the change
To post a comment you must log in.
- 516. By James Page
-
Drop else for System ValueError raise.
- 517. By James Page
-
Tweak
- 518. By James Page
-
Feedback from review
- 519. By James Page
-
optimize is systemd
Preview Diff
[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1 | === modified file 'charmhelpers/core/host.py' | |||
2 | --- charmhelpers/core/host.py 2015-11-27 20:57:05 +0000 | |||
3 | +++ charmhelpers/core/host.py 2016-01-06 14:04:36 +0000 | |||
4 | @@ -72,7 +72,9 @@ | |||
5 | 72 | stopped = service_stop(service_name) | 72 | stopped = service_stop(service_name) |
6 | 73 | upstart_file = os.path.join(init_dir, "{}.conf".format(service_name)) | 73 | upstart_file = os.path.join(init_dir, "{}.conf".format(service_name)) |
7 | 74 | sysv_file = os.path.join(initd_dir, service_name) | 74 | sysv_file = os.path.join(initd_dir, service_name) |
9 | 75 | if os.path.exists(upstart_file): | 75 | if init_is_systemd(): |
10 | 76 | service('disable', service_name) | ||
11 | 77 | elif os.path.exists(upstart_file): | ||
12 | 76 | override_path = os.path.join( | 78 | override_path = os.path.join( |
13 | 77 | init_dir, '{}.override'.format(service_name)) | 79 | init_dir, '{}.override'.format(service_name)) |
14 | 78 | with open(override_path, 'w') as fh: | 80 | with open(override_path, 'w') as fh: |
15 | @@ -80,9 +82,9 @@ | |||
16 | 80 | elif os.path.exists(sysv_file): | 82 | elif os.path.exists(sysv_file): |
17 | 81 | subprocess.check_call(["update-rc.d", service_name, "disable"]) | 83 | subprocess.check_call(["update-rc.d", service_name, "disable"]) |
18 | 82 | else: | 84 | else: |
19 | 83 | # XXX: Support SystemD too | ||
20 | 84 | raise ValueError( | 85 | raise ValueError( |
22 | 85 | "Unable to detect {0} as either Upstart {1} or SysV {2}".format( | 86 | "Unable to detect {0} as SystemD, Upstart {1} or" |
23 | 87 | " SysV {2}".format( | ||
24 | 86 | service_name, upstart_file, sysv_file)) | 88 | service_name, upstart_file, sysv_file)) |
25 | 87 | return stopped | 89 | return stopped |
26 | 88 | 90 | ||
27 | @@ -94,7 +96,9 @@ | |||
28 | 94 | Reenable starting again at boot. Start the service""" | 96 | Reenable starting again at boot. Start the service""" |
29 | 95 | upstart_file = os.path.join(init_dir, "{}.conf".format(service_name)) | 97 | upstart_file = os.path.join(init_dir, "{}.conf".format(service_name)) |
30 | 96 | sysv_file = os.path.join(initd_dir, service_name) | 98 | sysv_file = os.path.join(initd_dir, service_name) |
32 | 97 | if os.path.exists(upstart_file): | 99 | if init_is_systemd(): |
33 | 100 | service('enable', service_name) | ||
34 | 101 | elif os.path.exists(upstart_file): | ||
35 | 98 | override_path = os.path.join( | 102 | override_path = os.path.join( |
36 | 99 | init_dir, '{}.override'.format(service_name)) | 103 | init_dir, '{}.override'.format(service_name)) |
37 | 100 | if os.path.exists(override_path): | 104 | if os.path.exists(override_path): |
38 | @@ -102,9 +106,9 @@ | |||
39 | 102 | elif os.path.exists(sysv_file): | 106 | elif os.path.exists(sysv_file): |
40 | 103 | subprocess.check_call(["update-rc.d", service_name, "enable"]) | 107 | subprocess.check_call(["update-rc.d", service_name, "enable"]) |
41 | 104 | else: | 108 | else: |
42 | 105 | # XXX: Support SystemD too | ||
43 | 106 | raise ValueError( | 109 | raise ValueError( |
45 | 107 | "Unable to detect {0} as either Upstart {1} or SysV {2}".format( | 110 | "Unable to detect {0} as SystemD, Upstart {1} or" |
46 | 111 | " SysV {2}".format( | ||
47 | 108 | service_name, upstart_file, sysv_file)) | 112 | service_name, upstart_file, sysv_file)) |
48 | 109 | 113 | ||
49 | 110 | started = service_running(service_name) | 114 | started = service_running(service_name) |
50 | @@ -115,23 +119,29 @@ | |||
51 | 115 | 119 | ||
52 | 116 | def service(action, service_name): | 120 | def service(action, service_name): |
53 | 117 | """Control a system service""" | 121 | """Control a system service""" |
55 | 118 | cmd = ['service', service_name, action] | 122 | if init_is_systemd(): |
56 | 123 | cmd = ['systemctl', action, service_name] | ||
57 | 124 | else: | ||
58 | 125 | cmd = ['service', service_name, action] | ||
59 | 119 | return subprocess.call(cmd) == 0 | 126 | return subprocess.call(cmd) == 0 |
60 | 120 | 127 | ||
61 | 121 | 128 | ||
63 | 122 | def service_running(service): | 129 | def service_running(service_name): |
64 | 123 | """Determine whether a system service is running""" | 130 | """Determine whether a system service is running""" |
71 | 124 | try: | 131 | if init_is_systemd(): |
72 | 125 | output = subprocess.check_output( | 132 | return service('is-active', service_name) |
67 | 126 | ['service', service, 'status'], | ||
68 | 127 | stderr=subprocess.STDOUT).decode('UTF-8') | ||
69 | 128 | except subprocess.CalledProcessError: | ||
70 | 129 | return False | ||
73 | 130 | else: | 133 | else: |
77 | 131 | if ("start/running" in output or "is running" in output): | 134 | try: |
78 | 132 | return True | 135 | output = subprocess.check_output( |
79 | 133 | else: | 136 | ['service', service_name, 'status'], |
80 | 137 | stderr=subprocess.STDOUT).decode('UTF-8') | ||
81 | 138 | except subprocess.CalledProcessError: | ||
82 | 134 | return False | 139 | return False |
83 | 140 | else: | ||
84 | 141 | if ("start/running" in output or "is running" in output): | ||
85 | 142 | return True | ||
86 | 143 | else: | ||
87 | 144 | return False | ||
88 | 135 | 145 | ||
89 | 136 | 146 | ||
90 | 137 | def service_available(service_name): | 147 | def service_available(service_name): |
91 | @@ -146,6 +156,13 @@ | |||
92 | 146 | return True | 156 | return True |
93 | 147 | 157 | ||
94 | 148 | 158 | ||
95 | 159 | SYSTEMD_SYSTEM = '/run/systemd/system' | ||
96 | 160 | |||
97 | 161 | |||
98 | 162 | def init_is_systemd(): | ||
99 | 163 | return os.path.isdir(SYSTEMD_SYSTEM) | ||
100 | 164 | |||
101 | 165 | |||
102 | 149 | def adduser(username, password=None, shell='/bin/bash', system_user=False, | 166 | def adduser(username, password=None, shell='/bin/bash', system_user=False, |
103 | 150 | primary_group=None, secondary_groups=None): | 167 | primary_group=None, secondary_groups=None): |
104 | 151 | """ | 168 | """ |
105 | 152 | 169 | ||
106 | === modified file 'tests/contrib/charmsupport/test_nrpe.py' | |||
107 | --- tests/contrib/charmsupport/test_nrpe.py 2015-10-29 23:52:26 +0000 | |||
108 | +++ tests/contrib/charmsupport/test_nrpe.py 2016-01-06 14:04:36 +0000 | |||
109 | @@ -25,6 +25,7 @@ | |||
110 | 25 | 'relation_ids': {'object': nrpe}, | 25 | 'relation_ids': {'object': nrpe}, |
111 | 26 | 'relation_set': {'object': nrpe}, | 26 | 'relation_set': {'object': nrpe}, |
112 | 27 | 'relations_of_type': {'object': nrpe}, | 27 | 'relations_of_type': {'object': nrpe}, |
113 | 28 | 'service': {'object': nrpe}, | ||
114 | 28 | } | 29 | } |
115 | 29 | 30 | ||
116 | 30 | def setUp(self): | 31 | def setUp(self): |
117 | @@ -108,10 +109,9 @@ | |||
118 | 108 | 109 | ||
119 | 109 | self.assertEqual(None, checker.write()) | 110 | self.assertEqual(None, checker.write()) |
120 | 110 | 111 | ||
123 | 111 | expected = ['service', 'nagios-nrpe-server', 'restart'] | 112 | self.patched['service'].assert_called_with('restart', 'nagios-nrpe-server') |
122 | 112 | self.assertEqual(expected, self.patched['call'].call_args[0][0]) | ||
124 | 113 | self.check_call_counts(config=1, getpwnam=1, getgrnam=1, | 113 | self.check_call_counts(config=1, getpwnam=1, getgrnam=1, |
126 | 114 | exists=1, call=1) | 114 | exists=1, service=1) |
127 | 115 | 115 | ||
128 | 116 | def test_update_nrpe(self): | 116 | def test_update_nrpe(self): |
129 | 117 | self.patched['config'].return_value = {'nagios_context': 'a', | 117 | self.patched['config'].return_value = {'nagios_context': 'a', |
130 | 118 | 118 | ||
131 | === modified file 'tests/core/test_host.py' | |||
132 | --- tests/core/test_host.py 2015-11-27 20:57:05 +0000 | |||
133 | +++ tests/core/test_host.py 2016-01-06 14:04:36 +0000 | |||
134 | @@ -62,8 +62,25 @@ | |||
135 | 62 | 62 | ||
136 | 63 | 63 | ||
137 | 64 | class HelpersTest(TestCase): | 64 | class HelpersTest(TestCase): |
138 | 65 | |||
139 | 66 | @patch('os.path') | ||
140 | 67 | def test_init_is_systemd_upstart(self, path): | ||
141 | 68 | """Upstart based init is correctly detected""" | ||
142 | 69 | path.isdir.return_value = False | ||
143 | 70 | self.assertFalse(host.init_is_systemd()) | ||
144 | 71 | path.isdir.assert_called_with('/run/systemd/system') | ||
145 | 72 | |||
146 | 73 | @patch('os.path') | ||
147 | 74 | def test_init_is_systemd_system(self, path): | ||
148 | 75 | """Systemd based init is correctly detected""" | ||
149 | 76 | path.isdir.return_value = True | ||
150 | 77 | self.assertTrue(host.init_is_systemd()) | ||
151 | 78 | path.isdir.assert_called_with('/run/systemd/system') | ||
152 | 79 | |||
153 | 80 | @patch.object(host, 'init_is_systemd') | ||
154 | 65 | @patch('subprocess.call') | 81 | @patch('subprocess.call') |
156 | 66 | def test_runs_service_action(self, mock_call): | 82 | def test_runs_service_action(self, mock_call, systemd): |
157 | 83 | systemd.return_value = False | ||
158 | 67 | mock_call.return_value = 0 | 84 | mock_call.return_value = 0 |
159 | 68 | action = 'some-action' | 85 | action = 'some-action' |
160 | 69 | service_name = 'foo-service' | 86 | service_name = 'foo-service' |
161 | @@ -73,8 +90,24 @@ | |||
162 | 73 | self.assertTrue(result) | 90 | self.assertTrue(result) |
163 | 74 | mock_call.assert_called_with(['service', service_name, action]) | 91 | mock_call.assert_called_with(['service', service_name, action]) |
164 | 75 | 92 | ||
167 | 76 | @patch('subprocess.call') | 93 | @patch.object(host, 'init_is_systemd') |
168 | 77 | def test_returns_false_when_service_fails(self, mock_call): | 94 | @patch('subprocess.call') |
169 | 95 | def test_runs_systemctl_action(self, mock_call, systemd): | ||
170 | 96 | """Ensure that service calls under systemd call 'systemctl'.""" | ||
171 | 97 | systemd.return_value = True | ||
172 | 98 | mock_call.return_value = 0 | ||
173 | 99 | action = 'some-action' | ||
174 | 100 | service_name = 'foo-service' | ||
175 | 101 | |||
176 | 102 | result = host.service(action, service_name) | ||
177 | 103 | |||
178 | 104 | self.assertTrue(result) | ||
179 | 105 | mock_call.assert_called_with(['systemctl', action, service_name]) | ||
180 | 106 | |||
181 | 107 | @patch.object(host, 'init_is_systemd') | ||
182 | 108 | @patch('subprocess.call') | ||
183 | 109 | def test_returns_false_when_service_fails(self, mock_call, systemd): | ||
184 | 110 | systemd.return_value = False | ||
185 | 78 | mock_call.return_value = 1 | 111 | mock_call.return_value = 1 |
186 | 79 | action = 'some-action' | 112 | action = 'some-action' |
187 | 80 | service_name = 'foo-service' | 113 | service_name = 'foo-service' |
188 | @@ -108,13 +141,43 @@ | |||
189 | 108 | 141 | ||
190 | 109 | service.assert_called_with('restart', service_name) | 142 | service.assert_called_with('restart', service_name) |
191 | 110 | 143 | ||
192 | 144 | @patch.object(host, 'service_running') | ||
193 | 145 | @patch.object(host, 'init_is_systemd') | ||
194 | 146 | @patch.object(host, 'service') | ||
195 | 147 | def test_pauses_a_running_systemd_unit(self, service, systemd, | ||
196 | 148 | service_running): | ||
197 | 149 | """Pause on a running systemd unit will be stopped and disabled.""" | ||
198 | 150 | service_name = 'foo-service' | ||
199 | 151 | service_running.return_value = True | ||
200 | 152 | systemd.return_value = True | ||
201 | 153 | self.assertTrue(host.service_pause(service_name)) | ||
202 | 154 | service.assert_has_calls([ | ||
203 | 155 | call('stop', service_name), | ||
204 | 156 | call('disable', service_name)]) | ||
205 | 157 | |||
206 | 158 | @patch.object(host, 'service_running') | ||
207 | 159 | @patch.object(host, 'init_is_systemd') | ||
208 | 160 | @patch.object(host, 'service') | ||
209 | 161 | def test_resumes_a_stopped_systemd_unit(self, service, systemd, | ||
210 | 162 | service_running): | ||
211 | 163 | """Resume on a stopped systemd unit will be started and enabled.""" | ||
212 | 164 | service_name = 'foo-service' | ||
213 | 165 | service_running.return_value = False | ||
214 | 166 | systemd.return_value = True | ||
215 | 167 | self.assertTrue(host.service_resume(service_name)) | ||
216 | 168 | service.assert_has_calls([ | ||
217 | 169 | call('enable', service_name), | ||
218 | 170 | call('start', service_name)]) | ||
219 | 171 | |||
220 | 172 | @patch.object(host, 'init_is_systemd') | ||
221 | 111 | @patch('subprocess.check_output') | 173 | @patch('subprocess.check_output') |
222 | 112 | @patch.object(host, 'service') | 174 | @patch.object(host, 'service') |
224 | 113 | def test_pauses_a_running_upstart_service(self, service, check_output): | 175 | def test_pauses_a_running_upstart_service(self, service, check_output, systemd): |
225 | 114 | """Pause on a running service will call service stop.""" | 176 | """Pause on a running service will call service stop.""" |
226 | 115 | check_output.return_value = b'foo-service start/running, process 123' | 177 | check_output.return_value = b'foo-service start/running, process 123' |
227 | 116 | service_name = 'foo-service' | 178 | service_name = 'foo-service' |
228 | 117 | service.side_effect = [True] | 179 | service.side_effect = [True] |
229 | 180 | systemd.return_value = False | ||
230 | 118 | tempdir = mkdtemp(prefix="test_pauses_an_upstart_service") | 181 | tempdir = mkdtemp(prefix="test_pauses_an_upstart_service") |
231 | 119 | conf_path = os.path.join(tempdir, "{}.conf".format(service_name)) | 182 | conf_path = os.path.join(tempdir, "{}.conf".format(service_name)) |
232 | 120 | # Just needs to exist | 183 | # Just needs to exist |
233 | @@ -130,13 +193,15 @@ | |||
234 | 130 | override_contents = fh.read() | 193 | override_contents = fh.read() |
235 | 131 | self.assertEqual("manual\n", override_contents) | 194 | self.assertEqual("manual\n", override_contents) |
236 | 132 | 195 | ||
237 | 196 | @patch.object(host, 'init_is_systemd') | ||
238 | 133 | @patch('subprocess.check_output') | 197 | @patch('subprocess.check_output') |
239 | 134 | @patch.object(host, 'service') | 198 | @patch.object(host, 'service') |
241 | 135 | def test_pauses_a_stopped_upstart_service(self, service, check_output): | 199 | def test_pauses_a_stopped_upstart_service(self, service, check_output, systemd): |
242 | 136 | """Pause on a stopped service will not call service stop.""" | 200 | """Pause on a stopped service will not call service stop.""" |
243 | 137 | check_output.return_value = b'foo-service stop/waiting' | 201 | check_output.return_value = b'foo-service stop/waiting' |
244 | 138 | service_name = 'foo-service' | 202 | service_name = 'foo-service' |
245 | 139 | service.side_effect = [True] | 203 | service.side_effect = [True] |
246 | 204 | systemd.return_value = False | ||
247 | 140 | tempdir = mkdtemp(prefix="test_pauses_an_upstart_service") | 205 | tempdir = mkdtemp(prefix="test_pauses_an_upstart_service") |
248 | 141 | conf_path = os.path.join(tempdir, "{}.conf".format(service_name)) | 206 | conf_path = os.path.join(tempdir, "{}.conf".format(service_name)) |
249 | 142 | # Just needs to exist | 207 | # Just needs to exist |
250 | @@ -154,15 +219,17 @@ | |||
251 | 154 | override_contents = fh.read() | 219 | override_contents = fh.read() |
252 | 155 | self.assertEqual("manual\n", override_contents) | 220 | self.assertEqual("manual\n", override_contents) |
253 | 156 | 221 | ||
254 | 222 | @patch.object(host, 'init_is_systemd') | ||
255 | 157 | @patch('subprocess.check_output') | 223 | @patch('subprocess.check_output') |
256 | 158 | @patch('subprocess.check_call') | 224 | @patch('subprocess.check_call') |
257 | 159 | @patch.object(host, 'service') | 225 | @patch.object(host, 'service') |
258 | 160 | def test_pauses_a_running_sysv_service(self, service, check_call, | 226 | def test_pauses_a_running_sysv_service(self, service, check_call, |
260 | 161 | check_output): | 227 | check_output, systemd): |
261 | 162 | """Pause calls service stop on a running sysv service.""" | 228 | """Pause calls service stop on a running sysv service.""" |
262 | 163 | check_output.return_value = b'foo-service start/running, process 123' | 229 | check_output.return_value = b'foo-service start/running, process 123' |
263 | 164 | service_name = 'foo-service' | 230 | service_name = 'foo-service' |
264 | 165 | service.side_effect = [True] | 231 | service.side_effect = [True] |
265 | 232 | systemd.return_value = False | ||
266 | 166 | tempdir = mkdtemp(prefix="test_pauses_a_sysv_service") | 233 | tempdir = mkdtemp(prefix="test_pauses_a_sysv_service") |
267 | 167 | sysv_path = os.path.join(tempdir, service_name) | 234 | sysv_path = os.path.join(tempdir, service_name) |
268 | 168 | # Just needs to exist | 235 | # Just needs to exist |
269 | @@ -175,15 +242,17 @@ | |||
270 | 175 | service.assert_called_with('stop', service_name) | 242 | service.assert_called_with('stop', service_name) |
271 | 176 | check_call.assert_called_with(["update-rc.d", service_name, "disable"]) | 243 | check_call.assert_called_with(["update-rc.d", service_name, "disable"]) |
272 | 177 | 244 | ||
273 | 245 | @patch.object(host, 'init_is_systemd') | ||
274 | 178 | @patch('subprocess.check_output') | 246 | @patch('subprocess.check_output') |
275 | 179 | @patch('subprocess.check_call') | 247 | @patch('subprocess.check_call') |
276 | 180 | @patch.object(host, 'service') | 248 | @patch.object(host, 'service') |
277 | 181 | def test_pauses_a_stopped_sysv_service(self, service, check_call, | 249 | def test_pauses_a_stopped_sysv_service(self, service, check_call, |
279 | 182 | check_output): | 250 | check_output, systemd): |
280 | 183 | """Pause does not call service stop on a stopped sysv service.""" | 251 | """Pause does not call service stop on a stopped sysv service.""" |
281 | 184 | check_output.return_value = b'foo-service stop/waiting' | 252 | check_output.return_value = b'foo-service stop/waiting' |
282 | 185 | service_name = 'foo-service' | 253 | service_name = 'foo-service' |
283 | 186 | service.side_effect = [True] | 254 | service.side_effect = [True] |
284 | 255 | systemd.return_value = False | ||
285 | 187 | tempdir = mkdtemp(prefix="test_pauses_a_sysv_service") | 256 | tempdir = mkdtemp(prefix="test_pauses_a_sysv_service") |
286 | 188 | sysv_path = os.path.join(tempdir, service_name) | 257 | sysv_path = os.path.join(tempdir, service_name) |
287 | 189 | # Just needs to exist | 258 | # Just needs to exist |
288 | @@ -198,10 +267,12 @@ | |||
289 | 198 | AssertionError, service.assert_called_with, 'stop', service_name) | 267 | AssertionError, service.assert_called_with, 'stop', service_name) |
290 | 199 | check_call.assert_called_with(["update-rc.d", service_name, "disable"]) | 268 | check_call.assert_called_with(["update-rc.d", service_name, "disable"]) |
291 | 200 | 269 | ||
292 | 270 | @patch.object(host, 'init_is_systemd') | ||
293 | 201 | @patch.object(host, 'service') | 271 | @patch.object(host, 'service') |
295 | 202 | def test_pause_with_unknown_service(self, service): | 272 | def test_pause_with_unknown_service(self, service, systemd): |
296 | 203 | service_name = 'foo-service' | 273 | service_name = 'foo-service' |
297 | 204 | service.side_effect = [True] | 274 | service.side_effect = [True] |
298 | 275 | systemd.return_value = False | ||
299 | 205 | tempdir = mkdtemp(prefix="test_pauses_with_unknown_service") | 276 | tempdir = mkdtemp(prefix="test_pauses_with_unknown_service") |
300 | 206 | self.addCleanup(rmtree, tempdir) | 277 | self.addCleanup(rmtree, tempdir) |
301 | 207 | exception = self.assertRaises( | 278 | exception = self.assertRaises( |
302 | @@ -211,13 +282,15 @@ | |||
303 | 211 | "Unable to detect {0}".format(service_name), str(exception)) | 282 | "Unable to detect {0}".format(service_name), str(exception)) |
304 | 212 | self.assertIn(tempdir, str(exception)) | 283 | self.assertIn(tempdir, str(exception)) |
305 | 213 | 284 | ||
306 | 285 | @patch.object(host, 'init_is_systemd') | ||
307 | 214 | @patch('subprocess.check_output') | 286 | @patch('subprocess.check_output') |
308 | 215 | @patch.object(host, 'service') | 287 | @patch.object(host, 'service') |
310 | 216 | def test_resumes_a_running_upstart_service(self, service, check_output): | 288 | def test_resumes_a_running_upstart_service(self, service, check_output, systemd): |
311 | 217 | """When the service is already running, service start isn't called.""" | 289 | """When the service is already running, service start isn't called.""" |
312 | 218 | check_output.return_value = b'foo-service start/running, process 111' | 290 | check_output.return_value = b'foo-service start/running, process 111' |
313 | 219 | service_name = 'foo-service' | 291 | service_name = 'foo-service' |
314 | 220 | service.side_effect = [True] | 292 | service.side_effect = [True] |
315 | 293 | systemd.return_value = False | ||
316 | 221 | tempdir = mkdtemp(prefix="test_resumes_an_upstart_service") | 294 | tempdir = mkdtemp(prefix="test_resumes_an_upstart_service") |
317 | 222 | conf_path = os.path.join(tempdir, "{}.conf".format(service_name)) | 295 | conf_path = os.path.join(tempdir, "{}.conf".format(service_name)) |
318 | 223 | with open(conf_path, "w") as fh: | 296 | with open(conf_path, "w") as fh: |
319 | @@ -232,13 +305,15 @@ | |||
320 | 232 | tempdir, "{}.override".format(service_name)) | 305 | tempdir, "{}.override".format(service_name)) |
321 | 233 | self.assertFalse(os.path.exists(override_path)) | 306 | self.assertFalse(os.path.exists(override_path)) |
322 | 234 | 307 | ||
323 | 308 | @patch.object(host, 'init_is_systemd') | ||
324 | 235 | @patch('subprocess.check_output') | 309 | @patch('subprocess.check_output') |
325 | 236 | @patch.object(host, 'service') | 310 | @patch.object(host, 'service') |
327 | 237 | def test_resumes_a_stopped_upstart_service(self, service, check_output): | 311 | def test_resumes_a_stopped_upstart_service(self, service, check_output, systemd): |
328 | 238 | """When the service is stopped, service start is called.""" | 312 | """When the service is stopped, service start is called.""" |
329 | 239 | check_output.return_value = b'foo-service stop/waiting' | 313 | check_output.return_value = b'foo-service stop/waiting' |
330 | 240 | service_name = 'foo-service' | 314 | service_name = 'foo-service' |
331 | 241 | service.side_effect = [True] | 315 | service.side_effect = [True] |
332 | 316 | systemd.return_value = False | ||
333 | 242 | tempdir = mkdtemp(prefix="test_resumes_an_upstart_service") | 317 | tempdir = mkdtemp(prefix="test_resumes_an_upstart_service") |
334 | 243 | conf_path = os.path.join(tempdir, "{}.conf".format(service_name)) | 318 | conf_path = os.path.join(tempdir, "{}.conf".format(service_name)) |
335 | 244 | with open(conf_path, "w") as fh: | 319 | with open(conf_path, "w") as fh: |
336 | @@ -251,14 +326,16 @@ | |||
337 | 251 | tempdir, "{}.override".format(service_name)) | 326 | tempdir, "{}.override".format(service_name)) |
338 | 252 | self.assertFalse(os.path.exists(override_path)) | 327 | self.assertFalse(os.path.exists(override_path)) |
339 | 253 | 328 | ||
340 | 329 | @patch.object(host, 'init_is_systemd') | ||
341 | 254 | @patch('subprocess.check_output') | 330 | @patch('subprocess.check_output') |
342 | 255 | @patch('subprocess.check_call') | 331 | @patch('subprocess.check_call') |
343 | 256 | @patch.object(host, 'service') | 332 | @patch.object(host, 'service') |
345 | 257 | def test_resumes_a_sysv_service(self, service, check_call, check_output): | 333 | def test_resumes_a_sysv_service(self, service, check_call, check_output, systemd): |
346 | 258 | """When process is in a stop/waiting state, service start is called.""" | 334 | """When process is in a stop/waiting state, service start is called.""" |
347 | 259 | check_output.return_value = b'foo-service stop/waiting' | 335 | check_output.return_value = b'foo-service stop/waiting' |
348 | 260 | service_name = 'foo-service' | 336 | service_name = 'foo-service' |
349 | 261 | service.side_effect = [True] | 337 | service.side_effect = [True] |
350 | 338 | systemd.return_value = False | ||
351 | 262 | tempdir = mkdtemp(prefix="test_resumes_a_sysv_service") | 339 | tempdir = mkdtemp(prefix="test_resumes_a_sysv_service") |
352 | 263 | sysv_path = os.path.join(tempdir, service_name) | 340 | sysv_path = os.path.join(tempdir, service_name) |
353 | 264 | # Just needs to exist | 341 | # Just needs to exist |
354 | @@ -271,15 +348,17 @@ | |||
355 | 271 | service.assert_called_with('start', service_name) | 348 | service.assert_called_with('start', service_name) |
356 | 272 | check_call.assert_called_with(["update-rc.d", service_name, "enable"]) | 349 | check_call.assert_called_with(["update-rc.d", service_name, "enable"]) |
357 | 273 | 350 | ||
358 | 351 | @patch.object(host, 'init_is_systemd') | ||
359 | 274 | @patch('subprocess.check_output') | 352 | @patch('subprocess.check_output') |
360 | 275 | @patch('subprocess.check_call') | 353 | @patch('subprocess.check_call') |
361 | 276 | @patch.object(host, 'service') | 354 | @patch.object(host, 'service') |
362 | 277 | def test_resume_a_running_sysv_service(self, service, check_call, | 355 | def test_resume_a_running_sysv_service(self, service, check_call, |
364 | 278 | check_output): | 356 | check_output, systemd): |
365 | 279 | """When process is already running, service start isn't called.""" | 357 | """When process is already running, service start isn't called.""" |
366 | 280 | check_output.return_value = b'foo-service start/running, process 123' | 358 | check_output.return_value = b'foo-service start/running, process 123' |
367 | 281 | service_name = 'foo-service' | 359 | service_name = 'foo-service' |
368 | 282 | service.side_effect = [True] | 360 | service.side_effect = [True] |
369 | 361 | systemd.return_value = False | ||
370 | 283 | tempdir = mkdtemp(prefix="test_resumes_a_sysv_service") | 362 | tempdir = mkdtemp(prefix="test_resumes_a_sysv_service") |
371 | 284 | sysv_path = os.path.join(tempdir, service_name) | 363 | sysv_path = os.path.join(tempdir, service_name) |
372 | 285 | # Just needs to exist | 364 | # Just needs to exist |
373 | @@ -294,10 +373,12 @@ | |||
374 | 294 | AssertionError, service.assert_called_with, 'start', service_name) | 373 | AssertionError, service.assert_called_with, 'start', service_name) |
375 | 295 | check_call.assert_called_with(["update-rc.d", service_name, "enable"]) | 374 | check_call.assert_called_with(["update-rc.d", service_name, "enable"]) |
376 | 296 | 375 | ||
377 | 376 | @patch.object(host, 'init_is_systemd') | ||
378 | 297 | @patch.object(host, 'service') | 377 | @patch.object(host, 'service') |
380 | 298 | def test_resume_with_unknown_service(self, service): | 378 | def test_resume_with_unknown_service(self, service, systemd): |
381 | 299 | service_name = 'foo-service' | 379 | service_name = 'foo-service' |
382 | 300 | service.side_effect = [True] | 380 | service.side_effect = [True] |
383 | 381 | systemd.return_value = False | ||
384 | 301 | tempdir = mkdtemp(prefix="test_resumes_with_unknown_service") | 382 | tempdir = mkdtemp(prefix="test_resumes_with_unknown_service") |
385 | 302 | self.addCleanup(rmtree, tempdir) | 383 | self.addCleanup(rmtree, tempdir) |
386 | 303 | exception = self.assertRaises( | 384 | exception = self.assertRaises( |
387 | @@ -379,18 +460,24 @@ | |||
388 | 379 | call('restart', service_name) | 460 | call('restart', service_name) |
389 | 380 | ]) | 461 | ]) |
390 | 381 | 462 | ||
391 | 463 | @patch.object(host, 'init_is_systemd') | ||
392 | 382 | @patch('subprocess.check_output') | 464 | @patch('subprocess.check_output') |
394 | 383 | def test_service_running_on_stopped_service(self, check_output): | 465 | def test_service_running_on_stopped_service(self, check_output, systemd): |
395 | 466 | systemd.return_value = False | ||
396 | 384 | check_output.return_value = b'foo stop/waiting' | 467 | check_output.return_value = b'foo stop/waiting' |
397 | 385 | self.assertFalse(host.service_running('foo')) | 468 | self.assertFalse(host.service_running('foo')) |
398 | 386 | 469 | ||
399 | 470 | @patch.object(host, 'init_is_systemd') | ||
400 | 387 | @patch('subprocess.check_output') | 471 | @patch('subprocess.check_output') |
402 | 388 | def test_service_running_on_running_service(self, check_output): | 472 | def test_service_running_on_running_service(self, check_output, systemd): |
403 | 473 | systemd.return_value = False | ||
404 | 389 | check_output.return_value = b'foo start/running, process 23871' | 474 | check_output.return_value = b'foo start/running, process 23871' |
405 | 390 | self.assertTrue(host.service_running('foo')) | 475 | self.assertTrue(host.service_running('foo')) |
406 | 391 | 476 | ||
407 | 477 | @patch.object(host, 'init_is_systemd') | ||
408 | 392 | @patch('subprocess.check_output') | 478 | @patch('subprocess.check_output') |
410 | 393 | def test_service_running_on_unknown_service(self, check_output): | 479 | def test_service_running_on_unknown_service(self, check_output, systemd): |
411 | 480 | systemd.return_value = False | ||
412 | 394 | exc = subprocess.CalledProcessError(1, ['status']) | 481 | exc = subprocess.CalledProcessError(1, ['status']) |
413 | 395 | check_output.side_effect = exc | 482 | check_output.side_effect = exc |
414 | 396 | self.assertFalse(host.service_running('foo')) | 483 | self.assertFalse(host.service_running('foo')) |
First pass, several cleanups needed