Merge lp:~gandelman-a/charm-helpers/service_running into lp:charm-helpers

Proposed by Adam Gandelman
Status: Merged
Merged at revision: 52
Proposed branch: lp:~gandelman-a/charm-helpers/service_running
Merge into: lp:charm-helpers
Diff against target: 49 lines (+28/-0)
2 files modified
charmhelpers/core/host.py (+12/-0)
tests/core/test_host.py (+16/-0)
To merge this branch: bzr merge lp:~gandelman-a/charm-helpers/service_running
Reviewer Review Type Date Requested Status
James Page Approve
Review via email: mp+174261@code.launchpad.net

Description of the change

Adds a basic service_running() util to query whether or a service is running.

To post a comment you must log in.
Revision history for this message
James Page (james-page) :
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 2013-07-09 10:43:41 +0000
3+++ charmhelpers/core/host.py 2013-07-11 17:52:24 +0000
4@@ -39,6 +39,18 @@
5 return subprocess.call(cmd) == 0
6
7
8+def service_running(service):
9+ try:
10+ output = subprocess.check_output(['service', service, 'status'])
11+ except subprocess.CalledProcessError:
12+ return False
13+ else:
14+ if ("start/running" in output or "is running" in output):
15+ return True
16+ else:
17+ return False
18+
19+
20 def adduser(username, password=None, shell='/bin/bash', system_user=False):
21 """Add a user"""
22 try:
23
24=== modified file 'tests/core/test_host.py'
25--- tests/core/test_host.py 2013-07-09 10:43:41 +0000
26+++ tests/core/test_host.py 2013-07-11 17:52:24 +0000
27@@ -152,6 +152,22 @@
28
29 service.assert_called_with('reload', service_name)
30
31+ @patch('subprocess.check_output')
32+ def test_service_running_on_stopped_service(self, check_output):
33+ check_output.return_value = 'foo stop/waiting'
34+ self.assertFalse(host.service_running('foo'))
35+
36+ @patch('subprocess.check_output')
37+ def test_service_running_on_running_service(self, check_output):
38+ check_output.return_value = 'foo start/running, process 23871'
39+ self.assertTrue(host.service_running('foo'))
40+
41+ @patch('subprocess.check_output')
42+ def test_service_running_on_unknown_service(self, check_output):
43+ exc = subprocess.CalledProcessError(1, ['status'])
44+ check_output.side_effect = exc
45+ self.assertFalse(host.service_running('foo'))
46+
47 @patch('pwd.getpwnam')
48 @patch('subprocess.check_call')
49 @patch.object(host, 'log')

Subscribers

People subscribed via source and target branches