Merge lp:~james-page/charm-helpers/fixup-service-running into lp:charm-helpers
- fixup-service-running
- Merge into devel
Proposed by
James Page
Status: | Merged | ||||
---|---|---|---|---|---|
Merged at revision: | 574 | ||||
Proposed branch: | lp:~james-page/charm-helpers/fixup-service-running | ||||
Merge into: | lp:charm-helpers | ||||
Diff against target: |
364 lines (+78/-94) 2 files modified
charmhelpers/core/host.py (+16/-23) tests/core/test_host.py (+62/-71) |
||||
To merge this branch: | bzr merge lp:~james-page/charm-helpers/fixup-service-running | ||||
Related bugs: |
|
Reviewer | Review Type | Date Requested | Status |
---|---|---|---|
Ryan Beisner (community) | Approve | ||
Edward Hope-Morley | Approve | ||
Review via email: mp+294942@code.launchpad.net |
Commit message
Refactor service_running to determine init approach
Use of "service --status-all" has some challenges on trusty where
a recent lsb-base update resulted in all calls being diverted to
upstart, where return codes for non-running daemons are still 0.
Detect whether to use upstart or the base init.d script to
determine whether a service is running, and look at either the
upstart output, or for a non-zero return code from an init.d
status call.
Description of the change
To post a comment you must log in.
- 574. By James Page
-
Avoid conditional for sysv init check
Revision history for this message
James Page (james-page) wrote : | # |
Test sync: https:/
Revision history for this message
Edward Hope-Morley (hopem) wrote : | # |
lgtm +1
review:
Needs Resubmitting
Revision history for this message
Edward Hope-Morley (hopem) : | # |
review:
Approve
Revision history for this message
Ryan Beisner (1chb1n) : | # |
review:
Approve
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 2016-05-13 16:04:36 +0000 | |||
3 | +++ charmhelpers/core/host.py 2016-05-17 17:50:07 +0000 | |||
4 | @@ -128,11 +128,8 @@ | |||
5 | 128 | return subprocess.call(cmd) == 0 | 128 | return subprocess.call(cmd) == 0 |
6 | 129 | 129 | ||
7 | 130 | 130 | ||
13 | 131 | def systemv_services_running(): | 131 | _UPSTART_CONF = "/etc/init/{}.conf" |
14 | 132 | output = subprocess.check_output( | 132 | _INIT_D_CONF = "/etc/init.d/{}" |
10 | 133 | ['service', '--status-all'], | ||
11 | 134 | stderr=subprocess.STDOUT).decode('UTF-8') | ||
12 | 135 | return [row.split()[-1] for row in output.split('\n') if '[ + ]' in row] | ||
15 | 136 | 133 | ||
16 | 137 | 134 | ||
17 | 138 | def service_running(service_name): | 135 | def service_running(service_name): |
18 | @@ -140,26 +137,22 @@ | |||
19 | 140 | if init_is_systemd(): | 137 | if init_is_systemd(): |
20 | 141 | return service('is-active', service_name) | 138 | return service('is-active', service_name) |
21 | 142 | else: | 139 | else: |
37 | 143 | try: | 140 | if os.path.exists(_UPSTART_CONF.format(service_name)): |
38 | 144 | output = subprocess.check_output( | 141 | try: |
39 | 145 | ['service', service_name, 'status'], | 142 | output = subprocess.check_output( |
40 | 146 | stderr=subprocess.STDOUT).decode('UTF-8') | 143 | ['status', service_name], |
41 | 147 | except subprocess.CalledProcessError: | 144 | stderr=subprocess.STDOUT).decode('UTF-8') |
42 | 148 | return False | 145 | except subprocess.CalledProcessError: |
28 | 149 | else: | ||
29 | 150 | # This works for upstart scripts where the 'service' command | ||
30 | 151 | # returns a consistent string to represent running 'start/running' | ||
31 | 152 | if ("start/running" in output or "is running" in output or | ||
32 | 153 | "up and running" in output): | ||
33 | 154 | return True | ||
34 | 155 | # Actively check for upstart stopped message(s) as the fall through | ||
35 | 156 | # SystemV check can return false positives | ||
36 | 157 | if ("stop/waiting" in output): | ||
43 | 158 | return False | 146 | return False |
44 | 147 | else: | ||
45 | 148 | # This works for upstart scripts where the 'service' command | ||
46 | 149 | # returns a consistent string to represent running 'start/running' | ||
47 | 150 | if "start/running" in output: | ||
48 | 151 | return True | ||
49 | 152 | elif os.path.exists(_INIT_D_CONF.format(service_name)): | ||
50 | 159 | # Check System V scripts init script return codes | 153 | # Check System V scripts init script return codes |
54 | 160 | if service_name in systemv_services_running(): | 154 | return service('status', service_name) |
55 | 161 | return True | 155 | return False |
53 | 162 | return False | ||
56 | 163 | 156 | ||
57 | 164 | 157 | ||
58 | 165 | def service_available(service_name): | 158 | def service_available(service_name): |
59 | 166 | 159 | ||
60 | === modified file 'tests/core/test_host.py' | |||
61 | --- tests/core/test_host.py 2016-05-13 16:29:19 +0000 | |||
62 | +++ tests/core/test_host.py 2016-05-17 17:50:07 +0000 | |||
63 | @@ -60,13 +60,6 @@ | |||
64 | 60 | link/ether 08:00:27:16:b9:5f brd ff:ff:ff:ff:ff:ff | 60 | link/ether 08:00:27:16:b9:5f brd ff:ff:ff:ff:ff:ff |
65 | 61 | """ | 61 | """ |
66 | 62 | 62 | ||
67 | 63 | SERVICE_STATUS_ALL = b""" | ||
68 | 64 | [ + ] rabbitmq-server | ||
69 | 65 | [ ? ] rc.local | ||
70 | 66 | [ + ] resolvconf | ||
71 | 67 | [ - ] rsync | ||
72 | 68 | """ | ||
73 | 69 | |||
74 | 70 | 63 | ||
75 | 71 | class HelpersTest(TestCase): | 64 | class HelpersTest(TestCase): |
76 | 72 | 65 | ||
77 | @@ -176,15 +169,16 @@ | |||
78 | 176 | call('enable', service_name), | 169 | call('enable', service_name), |
79 | 177 | call('start', service_name)]) | 170 | call('start', service_name)]) |
80 | 178 | 171 | ||
81 | 172 | @patch.object(host, 'service_running') | ||
82 | 179 | @patch.object(host, 'init_is_systemd') | 173 | @patch.object(host, 'init_is_systemd') |
83 | 180 | @patch('subprocess.check_output') | ||
84 | 181 | @patch.object(host, 'service') | 174 | @patch.object(host, 'service') |
86 | 182 | def test_pauses_a_running_upstart_service(self, service, check_output, systemd): | 175 | def test_pauses_a_running_upstart_service(self, service, systemd, |
87 | 176 | service_running): | ||
88 | 183 | """Pause on a running service will call service stop.""" | 177 | """Pause on a running service will call service stop.""" |
89 | 184 | check_output.return_value = b'foo-service start/running, process 123' | ||
90 | 185 | service_name = 'foo-service' | 178 | service_name = 'foo-service' |
91 | 186 | service.side_effect = [True] | 179 | service.side_effect = [True] |
92 | 187 | systemd.return_value = False | 180 | systemd.return_value = False |
93 | 181 | service_running.return_value = True | ||
94 | 188 | tempdir = mkdtemp(prefix="test_pauses_an_upstart_service") | 182 | tempdir = mkdtemp(prefix="test_pauses_an_upstart_service") |
95 | 189 | conf_path = os.path.join(tempdir, "{}.conf".format(service_name)) | 183 | conf_path = os.path.join(tempdir, "{}.conf".format(service_name)) |
96 | 190 | # Just needs to exist | 184 | # Just needs to exist |
97 | @@ -200,15 +194,16 @@ | |||
98 | 200 | override_contents = fh.read() | 194 | override_contents = fh.read() |
99 | 201 | self.assertEqual("manual\n", override_contents) | 195 | self.assertEqual("manual\n", override_contents) |
100 | 202 | 196 | ||
101 | 197 | @patch.object(host, 'service_running') | ||
102 | 203 | @patch.object(host, 'init_is_systemd') | 198 | @patch.object(host, 'init_is_systemd') |
103 | 204 | @patch('subprocess.check_output') | ||
104 | 205 | @patch.object(host, 'service') | 199 | @patch.object(host, 'service') |
106 | 206 | def test_pauses_a_stopped_upstart_service(self, service, check_output, systemd): | 200 | def test_pauses_a_stopped_upstart_service(self, service, systemd, |
107 | 201 | service_running): | ||
108 | 207 | """Pause on a stopped service will not call service stop.""" | 202 | """Pause on a stopped service will not call service stop.""" |
109 | 208 | check_output.return_value = b'foo-service stop/waiting' | ||
110 | 209 | service_name = 'foo-service' | 203 | service_name = 'foo-service' |
111 | 210 | service.side_effect = [True] | 204 | service.side_effect = [True] |
112 | 211 | systemd.return_value = False | 205 | systemd.return_value = False |
113 | 206 | service_running.return_value = False | ||
114 | 212 | tempdir = mkdtemp(prefix="test_pauses_an_upstart_service") | 207 | tempdir = mkdtemp(prefix="test_pauses_an_upstart_service") |
115 | 213 | conf_path = os.path.join(tempdir, "{}.conf".format(service_name)) | 208 | conf_path = os.path.join(tempdir, "{}.conf".format(service_name)) |
116 | 214 | # Just needs to exist | 209 | # Just needs to exist |
117 | @@ -226,17 +221,17 @@ | |||
118 | 226 | override_contents = fh.read() | 221 | override_contents = fh.read() |
119 | 227 | self.assertEqual("manual\n", override_contents) | 222 | self.assertEqual("manual\n", override_contents) |
120 | 228 | 223 | ||
121 | 224 | @patch.object(host, 'service_running') | ||
122 | 229 | @patch.object(host, 'init_is_systemd') | 225 | @patch.object(host, 'init_is_systemd') |
123 | 230 | @patch('subprocess.check_output') | ||
124 | 231 | @patch('subprocess.check_call') | 226 | @patch('subprocess.check_call') |
125 | 232 | @patch.object(host, 'service') | 227 | @patch.object(host, 'service') |
126 | 233 | def test_pauses_a_running_sysv_service(self, service, check_call, | 228 | def test_pauses_a_running_sysv_service(self, service, check_call, |
128 | 234 | check_output, systemd): | 229 | systemd, service_running): |
129 | 235 | """Pause calls service stop on a running sysv service.""" | 230 | """Pause calls service stop on a running sysv service.""" |
130 | 236 | check_output.return_value = b'foo-service start/running, process 123' | ||
131 | 237 | service_name = 'foo-service' | 231 | service_name = 'foo-service' |
132 | 238 | service.side_effect = [True] | 232 | service.side_effect = [True] |
133 | 239 | systemd.return_value = False | 233 | systemd.return_value = False |
134 | 234 | service_running.return_value = True | ||
135 | 240 | tempdir = mkdtemp(prefix="test_pauses_a_sysv_service") | 235 | tempdir = mkdtemp(prefix="test_pauses_a_sysv_service") |
136 | 241 | sysv_path = os.path.join(tempdir, service_name) | 236 | sysv_path = os.path.join(tempdir, service_name) |
137 | 242 | # Just needs to exist | 237 | # Just needs to exist |
138 | @@ -249,17 +244,17 @@ | |||
139 | 249 | service.assert_called_with('stop', service_name) | 244 | service.assert_called_with('stop', service_name) |
140 | 250 | check_call.assert_called_with(["update-rc.d", service_name, "disable"]) | 245 | check_call.assert_called_with(["update-rc.d", service_name, "disable"]) |
141 | 251 | 246 | ||
142 | 247 | @patch.object(host, 'service_running') | ||
143 | 252 | @patch.object(host, 'init_is_systemd') | 248 | @patch.object(host, 'init_is_systemd') |
144 | 253 | @patch('subprocess.check_output') | ||
145 | 254 | @patch('subprocess.check_call') | 249 | @patch('subprocess.check_call') |
146 | 255 | @patch.object(host, 'service') | 250 | @patch.object(host, 'service') |
147 | 256 | def test_pauses_a_stopped_sysv_service(self, service, check_call, | 251 | def test_pauses_a_stopped_sysv_service(self, service, check_call, |
149 | 257 | check_output, systemd): | 252 | systemd, service_running): |
150 | 258 | """Pause does not call service stop on a stopped sysv service.""" | 253 | """Pause does not call service stop on a stopped sysv service.""" |
151 | 259 | check_output.return_value = b'foo-service stop/waiting' | ||
152 | 260 | service_name = 'foo-service' | 254 | service_name = 'foo-service' |
153 | 261 | service.side_effect = [True] | 255 | service.side_effect = [True] |
154 | 262 | systemd.return_value = False | 256 | systemd.return_value = False |
155 | 257 | service_running.return_value = False | ||
156 | 263 | tempdir = mkdtemp(prefix="test_pauses_a_sysv_service") | 258 | tempdir = mkdtemp(prefix="test_pauses_a_sysv_service") |
157 | 264 | sysv_path = os.path.join(tempdir, service_name) | 259 | sysv_path = os.path.join(tempdir, service_name) |
158 | 265 | # Just needs to exist | 260 | # Just needs to exist |
159 | @@ -289,15 +284,17 @@ | |||
160 | 289 | "Unable to detect {0}".format(service_name), str(exception)) | 284 | "Unable to detect {0}".format(service_name), str(exception)) |
161 | 290 | self.assertIn(tempdir, str(exception)) | 285 | self.assertIn(tempdir, str(exception)) |
162 | 291 | 286 | ||
163 | 287 | @patch.object(host, 'service_running') | ||
164 | 292 | @patch.object(host, 'init_is_systemd') | 288 | @patch.object(host, 'init_is_systemd') |
165 | 293 | @patch('subprocess.check_output') | 289 | @patch('subprocess.check_output') |
166 | 294 | @patch.object(host, 'service') | 290 | @patch.object(host, 'service') |
168 | 295 | def test_resumes_a_running_upstart_service(self, service, check_output, systemd): | 291 | def test_resumes_a_running_upstart_service(self, service, check_output, systemd, |
169 | 292 | service_running): | ||
170 | 296 | """When the service is already running, service start isn't called.""" | 293 | """When the service is already running, service start isn't called.""" |
171 | 297 | check_output.return_value = b'foo-service start/running, process 111' | ||
172 | 298 | service_name = 'foo-service' | 294 | service_name = 'foo-service' |
173 | 299 | service.side_effect = [True] | 295 | service.side_effect = [True] |
174 | 300 | systemd.return_value = False | 296 | systemd.return_value = False |
175 | 297 | service_running.return_value = True | ||
176 | 301 | tempdir = mkdtemp(prefix="test_resumes_an_upstart_service") | 298 | tempdir = mkdtemp(prefix="test_resumes_an_upstart_service") |
177 | 302 | conf_path = os.path.join(tempdir, "{}.conf".format(service_name)) | 299 | conf_path = os.path.join(tempdir, "{}.conf".format(service_name)) |
178 | 303 | with open(conf_path, "w") as fh: | 300 | with open(conf_path, "w") as fh: |
179 | @@ -306,21 +303,23 @@ | |||
180 | 306 | self.assertTrue(host.service_resume(service_name, init_dir=tempdir)) | 303 | self.assertTrue(host.service_resume(service_name, init_dir=tempdir)) |
181 | 307 | 304 | ||
182 | 308 | # Start isn't called because service is already running | 305 | # Start isn't called because service is already running |
185 | 309 | self.assertRaises( | 306 | self.assertFalse(service.called) |
184 | 310 | AssertionError, service.assert_called_with, 'start', service_name) | ||
186 | 311 | override_path = os.path.join( | 307 | override_path = os.path.join( |
187 | 312 | tempdir, "{}.override".format(service_name)) | 308 | tempdir, "{}.override".format(service_name)) |
188 | 313 | self.assertFalse(os.path.exists(override_path)) | 309 | self.assertFalse(os.path.exists(override_path)) |
189 | 314 | 310 | ||
190 | 311 | @patch.object(host, 'service_running') | ||
191 | 315 | @patch.object(host, 'init_is_systemd') | 312 | @patch.object(host, 'init_is_systemd') |
192 | 316 | @patch('subprocess.check_output') | 313 | @patch('subprocess.check_output') |
193 | 317 | @patch.object(host, 'service') | 314 | @patch.object(host, 'service') |
195 | 318 | def test_resumes_a_stopped_upstart_service(self, service, check_output, systemd): | 315 | def test_resumes_a_stopped_upstart_service(self, service, check_output, systemd, |
196 | 316 | service_running): | ||
197 | 319 | """When the service is stopped, service start is called.""" | 317 | """When the service is stopped, service start is called.""" |
198 | 320 | check_output.return_value = b'foo-service stop/waiting' | 318 | check_output.return_value = b'foo-service stop/waiting' |
199 | 321 | service_name = 'foo-service' | 319 | service_name = 'foo-service' |
200 | 322 | service.side_effect = [True] | 320 | service.side_effect = [True] |
201 | 323 | systemd.return_value = False | 321 | systemd.return_value = False |
202 | 322 | service_running.return_value = False | ||
203 | 324 | tempdir = mkdtemp(prefix="test_resumes_an_upstart_service") | 323 | tempdir = mkdtemp(prefix="test_resumes_an_upstart_service") |
204 | 325 | conf_path = os.path.join(tempdir, "{}.conf".format(service_name)) | 324 | conf_path = os.path.join(tempdir, "{}.conf".format(service_name)) |
205 | 326 | with open(conf_path, "w") as fh: | 325 | with open(conf_path, "w") as fh: |
206 | @@ -333,16 +332,17 @@ | |||
207 | 333 | tempdir, "{}.override".format(service_name)) | 332 | tempdir, "{}.override".format(service_name)) |
208 | 334 | self.assertFalse(os.path.exists(override_path)) | 333 | self.assertFalse(os.path.exists(override_path)) |
209 | 335 | 334 | ||
210 | 335 | @patch.object(host, 'service_running') | ||
211 | 336 | @patch.object(host, 'init_is_systemd') | 336 | @patch.object(host, 'init_is_systemd') |
212 | 337 | @patch('subprocess.check_output') | ||
213 | 338 | @patch('subprocess.check_call') | 337 | @patch('subprocess.check_call') |
214 | 339 | @patch.object(host, 'service') | 338 | @patch.object(host, 'service') |
216 | 340 | def test_resumes_a_sysv_service(self, service, check_call, check_output, systemd): | 339 | def test_resumes_a_sysv_service(self, service, check_call, systemd, |
217 | 340 | service_running): | ||
218 | 341 | """When process is in a stop/waiting state, service start is called.""" | 341 | """When process is in a stop/waiting state, service start is called.""" |
219 | 342 | check_output.return_value = b'foo-service stop/waiting' | ||
220 | 343 | service_name = 'foo-service' | 342 | service_name = 'foo-service' |
221 | 344 | service.side_effect = [True] | 343 | service.side_effect = [True] |
222 | 345 | systemd.return_value = False | 344 | systemd.return_value = False |
223 | 345 | service_running.return_value = False | ||
224 | 346 | tempdir = mkdtemp(prefix="test_resumes_a_sysv_service") | 346 | tempdir = mkdtemp(prefix="test_resumes_a_sysv_service") |
225 | 347 | sysv_path = os.path.join(tempdir, service_name) | 347 | sysv_path = os.path.join(tempdir, service_name) |
226 | 348 | # Just needs to exist | 348 | # Just needs to exist |
227 | @@ -355,17 +355,16 @@ | |||
228 | 355 | service.assert_called_with('start', service_name) | 355 | service.assert_called_with('start', service_name) |
229 | 356 | check_call.assert_called_with(["update-rc.d", service_name, "enable"]) | 356 | check_call.assert_called_with(["update-rc.d", service_name, "enable"]) |
230 | 357 | 357 | ||
231 | 358 | @patch.object(host, 'service_running') | ||
232 | 358 | @patch.object(host, 'init_is_systemd') | 359 | @patch.object(host, 'init_is_systemd') |
233 | 359 | @patch('subprocess.check_output') | ||
234 | 360 | @patch('subprocess.check_call') | 360 | @patch('subprocess.check_call') |
235 | 361 | @patch.object(host, 'service') | 361 | @patch.object(host, 'service') |
236 | 362 | def test_resume_a_running_sysv_service(self, service, check_call, | 362 | def test_resume_a_running_sysv_service(self, service, check_call, |
238 | 363 | check_output, systemd): | 363 | systemd, service_running): |
239 | 364 | """When process is already running, service start isn't called.""" | 364 | """When process is already running, service start isn't called.""" |
240 | 365 | check_output.return_value = b'foo-service start/running, process 123' | ||
241 | 366 | service_name = 'foo-service' | 365 | service_name = 'foo-service' |
242 | 367 | service.side_effect = [True] | ||
243 | 368 | systemd.return_value = False | 366 | systemd.return_value = False |
244 | 367 | service_running.return_value = True | ||
245 | 369 | tempdir = mkdtemp(prefix="test_resumes_a_sysv_service") | 368 | tempdir = mkdtemp(prefix="test_resumes_a_sysv_service") |
246 | 370 | sysv_path = os.path.join(tempdir, service_name) | 369 | sysv_path = os.path.join(tempdir, service_name) |
247 | 371 | # Just needs to exist | 370 | # Just needs to exist |
248 | @@ -376,16 +375,18 @@ | |||
249 | 376 | service_name, init_dir=tempdir, initd_dir=tempdir)) | 375 | service_name, init_dir=tempdir, initd_dir=tempdir)) |
250 | 377 | 376 | ||
251 | 378 | # Start isn't called because service is already running | 377 | # Start isn't called because service is already running |
254 | 379 | self.assertRaises( | 378 | self.assertFalse(service.called) |
253 | 380 | AssertionError, service.assert_called_with, 'start', service_name) | ||
255 | 381 | check_call.assert_called_with(["update-rc.d", service_name, "enable"]) | 379 | check_call.assert_called_with(["update-rc.d", service_name, "enable"]) |
256 | 382 | 380 | ||
257 | 381 | @patch.object(host, 'service_running') | ||
258 | 383 | @patch.object(host, 'init_is_systemd') | 382 | @patch.object(host, 'init_is_systemd') |
259 | 384 | @patch.object(host, 'service') | 383 | @patch.object(host, 'service') |
261 | 385 | def test_resume_with_unknown_service(self, service, systemd): | 384 | def test_resume_with_unknown_service(self, service, systemd, |
262 | 385 | service_running): | ||
263 | 386 | service_name = 'foo-service' | 386 | service_name = 'foo-service' |
264 | 387 | service.side_effect = [True] | 387 | service.side_effect = [True] |
265 | 388 | systemd.return_value = False | 388 | systemd.return_value = False |
266 | 389 | service_running.return_value = False | ||
267 | 389 | tempdir = mkdtemp(prefix="test_resumes_with_unknown_service") | 390 | tempdir = mkdtemp(prefix="test_resumes_with_unknown_service") |
268 | 390 | self.addCleanup(rmtree, tempdir) | 391 | self.addCleanup(rmtree, tempdir) |
269 | 391 | exception = self.assertRaises( | 392 | exception = self.assertRaises( |
270 | @@ -467,67 +468,57 @@ | |||
271 | 467 | call('restart', service_name) | 468 | call('restart', service_name) |
272 | 468 | ]) | 469 | ]) |
273 | 469 | 470 | ||
275 | 470 | @patch.object(host, 'systemv_services_running') | 471 | @patch.object(host, 'os') |
276 | 471 | @patch.object(host, 'init_is_systemd') | 472 | @patch.object(host, 'init_is_systemd') |
277 | 472 | @patch('subprocess.check_output') | 473 | @patch('subprocess.check_output') |
278 | 473 | def test_service_running_on_stopped_service(self, check_output, systemd, | 474 | def test_service_running_on_stopped_service(self, check_output, systemd, |
280 | 474 | systemv_services_running): | 475 | os): |
281 | 475 | systemd.return_value = False | 476 | systemd.return_value = False |
282 | 477 | os.path.exists.return_value = True | ||
283 | 476 | check_output.return_value = b'foo stop/waiting' | 478 | check_output.return_value = b'foo stop/waiting' |
284 | 477 | self.assertFalse(host.service_running('foo')) | 479 | self.assertFalse(host.service_running('foo')) |
285 | 478 | self.assertFalse(systemv_services_running.called) | ||
286 | 479 | 480 | ||
287 | 481 | @patch.object(host, 'os') | ||
288 | 480 | @patch.object(host, 'init_is_systemd') | 482 | @patch.object(host, 'init_is_systemd') |
289 | 481 | @patch('subprocess.check_output') | 483 | @patch('subprocess.check_output') |
291 | 482 | def test_service_running_on_running_service(self, check_output, systemd): | 484 | def test_service_running_on_running_service(self, check_output, systemd, |
292 | 485 | os): | ||
293 | 483 | systemd.return_value = False | 486 | systemd.return_value = False |
294 | 487 | os.path.exists.return_value = True | ||
295 | 484 | check_output.return_value = b'foo start/running, process 23871' | 488 | check_output.return_value = b'foo start/running, process 23871' |
296 | 485 | self.assertTrue(host.service_running('foo')) | 489 | self.assertTrue(host.service_running('foo')) |
297 | 486 | 490 | ||
309 | 487 | @patch.object(host, 'init_is_systemd') | 491 | @patch.object(host, 'os') |
310 | 488 | @patch('subprocess.check_output') | 492 | @patch.object(host, 'init_is_systemd') |
311 | 489 | def test_service_running_on_running_service_pxc(self, check_output, systemd): | 493 | @patch('subprocess.check_output') |
312 | 490 | systemd.return_value = False | 494 | def test_service_running_on_unknown_service(self, check_output, systemd, |
313 | 491 | check_output.return_value = b' * Percona XtraDB Cluster up and running' | 495 | os): |
314 | 492 | self.assertTrue(host.service_running('foo')) | 496 | systemd.return_value = False |
315 | 493 | 497 | os.path.exists.return_value = True | |
305 | 494 | @patch.object(host, 'init_is_systemd') | ||
306 | 495 | @patch('subprocess.check_output') | ||
307 | 496 | def test_service_running_on_unknown_service(self, check_output, systemd): | ||
308 | 497 | systemd.return_value = False | ||
316 | 498 | exc = subprocess.CalledProcessError(1, ['status']) | 498 | exc = subprocess.CalledProcessError(1, ['status']) |
317 | 499 | check_output.side_effect = exc | 499 | check_output.side_effect = exc |
318 | 500 | self.assertFalse(host.service_running('foo')) | 500 | self.assertFalse(host.service_running('foo')) |
319 | 501 | 501 | ||
321 | 502 | @patch.object(host, 'systemv_services_running') | 502 | @patch.object(host, 'os') |
322 | 503 | @patch.object(host, 'service') | ||
323 | 503 | @patch.object(host, 'init_is_systemd') | 504 | @patch.object(host, 'init_is_systemd') |
327 | 504 | @patch('subprocess.check_output') | 505 | def test_service_systemv_running(self, systemd, service, os): |
325 | 505 | def test_service_systemv_running(self, check_output, systemd, | ||
326 | 506 | systemv_services_running): | ||
328 | 507 | systemd.return_value = False | 506 | systemd.return_value = False |
331 | 508 | check_output.return_value = b' * Unhelpfull guff, thanks a lot rabbit' | 507 | service.return_value = True |
332 | 509 | systemv_services_running.return_value = [u'udev', u'rabbitmq-server'] | 508 | os.path.exists.side_effect = [False, True] |
333 | 510 | self.assertTrue(host.service_running('rabbitmq-server')) | 509 | self.assertTrue(host.service_running('rabbitmq-server')) |
335 | 511 | self.assertTrue(systemv_services_running.called) | 510 | service.assert_called_with('status', 'rabbitmq-server') |
336 | 512 | 511 | ||
338 | 513 | @patch.object(host, 'systemv_services_running') | 512 | @patch.object(host, 'os') |
339 | 513 | @patch.object(host, 'service') | ||
340 | 514 | @patch.object(host, 'init_is_systemd') | 514 | @patch.object(host, 'init_is_systemd') |
344 | 515 | @patch('subprocess.check_output') | 515 | def test_service_systemv_not_running(self, systemd, service, |
345 | 516 | def test_service_systemv_not_running(self, check_output, systemd, | 516 | os): |
343 | 517 | systemv_services_running): | ||
346 | 518 | systemd.return_value = False | 517 | systemd.return_value = False |
349 | 519 | check_output.return_value = b' * Unhelpfull guff, thanks a lot rabbit' | 518 | service.return_value = False |
350 | 520 | systemv_services_running.return_value = [u'udev', u'rabbitmq-server'] | 519 | os.path.exists.side_effect = [False, True] |
351 | 521 | self.assertFalse(host.service_running('keystone')) | 520 | self.assertFalse(host.service_running('keystone')) |
361 | 522 | self.assertTrue(systemv_services_running.called) | 521 | service.assert_called_with('status', 'keystone') |
353 | 523 | |||
354 | 524 | @patch('subprocess.check_output') | ||
355 | 525 | def test_systemv_services_running(self, check_output): | ||
356 | 526 | check_output.return_value = SERVICE_STATUS_ALL | ||
357 | 527 | self.assertEqual( | ||
358 | 528 | host.systemv_services_running(), | ||
359 | 529 | [u'rabbitmq-server', u'resolvconf'] | ||
360 | 530 | ) | ||
362 | 531 | 522 | ||
363 | 532 | @patch('grp.getgrnam') | 523 | @patch('grp.getgrnam') |
364 | 533 | @patch('pwd.getpwnam') | 524 | @patch('pwd.getpwnam') |
I'm syncing this into a few charms @master and stable/16.04 to exercise. Will report back here.