Merge lp:~gnuoy/charm-helpers/sysv-service-running into lp:charm-helpers

Proposed by Liam Young
Status: Merged
Merged at revision: 558
Proposed branch: lp:~gnuoy/charm-helpers/sysv-service-running
Merge into: lp:charm-helpers
Diff against target: 88 lines (+48/-2)
2 files modified
charmhelpers/core/host.py (+13/-2)
tests/core/test_host.py (+35/-0)
To merge this branch: bzr merge lp:~gnuoy/charm-helpers/sysv-service-running
Reviewer Review Type Date Requested Status
charmers Pending
Review via email: mp+290729@code.launchpad.net
To post a comment you must log in.

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-03-02 13:55:54 +0000
3+++ charmhelpers/core/host.py 2016-04-01 13:11:41 +0000
4@@ -128,6 +128,13 @@
5 return subprocess.call(cmd) == 0
6
7
8+def systemv_services_running():
9+ output = subprocess.check_output(
10+ ['service', '--status-all'],
11+ stderr=subprocess.STDOUT).decode('UTF-8')
12+ return [row.split()[-1] for row in output.split('\n') if '[ + ]' in row]
13+
14+
15 def service_running(service_name):
16 """Determine whether a system service is running"""
17 if init_is_systemd():
18@@ -140,11 +147,15 @@
19 except subprocess.CalledProcessError:
20 return False
21 else:
22+ # This works for upstart scripts where the 'service' command
23+ # returns a consistent string to represent running 'start/running'
24 if ("start/running" in output or "is running" in output or
25 "up and running" in output):
26 return True
27- else:
28- return False
29+ # Check System V scripts init script return codes
30+ if service_name in systemv_services_running():
31+ return True
32+ return False
33
34
35 def service_available(service_name):
36
37=== modified file 'tests/core/test_host.py'
38--- tests/core/test_host.py 2016-01-20 09:43:17 +0000
39+++ tests/core/test_host.py 2016-04-01 13:11:41 +0000
40@@ -60,6 +60,13 @@
41 link/ether 08:00:27:16:b9:5f brd ff:ff:ff:ff:ff:ff
42 """
43
44+SERVICE_STATUS_ALL = b"""
45+ [ + ] rabbitmq-server
46+ [ ? ] rc.local
47+ [ + ] resolvconf
48+ [ - ] rsync
49+"""
50+
51
52 class HelpersTest(TestCase):
53
54@@ -489,6 +496,34 @@
55 check_output.side_effect = exc
56 self.assertFalse(host.service_running('foo'))
57
58+ @patch.object(host, 'systemv_services_running')
59+ @patch.object(host, 'init_is_systemd')
60+ @patch('subprocess.check_output')
61+ def test_service_systemv_running(self, check_output, systemd,
62+ systemv_services_running):
63+ systemd.return_value = False
64+ check_output.return_value = b' * Unhelpfull guff, thanks a lot rabbit'
65+ systemv_services_running.return_value = [u'udev', u'rabbitmq-server']
66+ self.assertTrue(host.service_running('rabbitmq-server'))
67+
68+ @patch.object(host, 'systemv_services_running')
69+ @patch.object(host, 'init_is_systemd')
70+ @patch('subprocess.check_output')
71+ def test_service_systemv_not_running(self, check_output, systemd,
72+ systemv_services_running):
73+ systemd.return_value = False
74+ check_output.return_value = b' * Unhelpfull guff, thanks a lot rabbit'
75+ systemv_services_running.return_value = [u'udev', u'rabbitmq-server']
76+ self.assertFalse(host.service_running('keystone'))
77+
78+ @patch('subprocess.check_output')
79+ def test_systemv_services_running(self, check_output):
80+ check_output.return_value = SERVICE_STATUS_ALL
81+ self.assertEqual(
82+ host.systemv_services_running(),
83+ [u'rabbitmq-server', u'resolvconf']
84+ )
85+
86 @patch('grp.getgrnam')
87 @patch('pwd.getpwnam')
88 @patch('subprocess.check_call')

Subscribers

People subscribed via source and target branches