Merge lp:~stub/charm-helpers/bug-1214793-service-wrappers into lp:charm-helpers

Proposed by Stuart Bishop
Status: Merged
Merged at revision: 73
Proposed branch: lp:~stub/charm-helpers/bug-1214793-service-wrappers
Merge into: lp:charm-helpers
Diff against target: 140 lines (+64/-13)
2 files modified
charmhelpers/core/host.py (+7/-5)
tests/core/test_host.py (+57/-8)
To merge this branch: bzr merge lp:~stub/charm-helpers/bug-1214793-service-wrappers
Reviewer Review Type Date Requested Status
Jacek Nykis (community) Approve
Review via email: mp+181239@code.launchpad.net

Description of the change

Make wrappers around the service() call, such as service_start() and service_stop(), return the underlying result.

To post a comment you must log in.
Revision history for this message
Jacek Nykis (jacekn) wrote :

Looks fine, all tests check out.

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-08-07 23:14:14 +0000
3+++ charmhelpers/core/host.py 2013-08-21 10:12:28 +0000
4@@ -19,20 +19,22 @@
5
6
7 def service_start(service_name):
8- service('start', service_name)
9+ return service('start', service_name)
10
11
12 def service_stop(service_name):
13- service('stop', service_name)
14+ return service('stop', service_name)
15
16
17 def service_restart(service_name):
18- service('restart', service_name)
19+ return service('restart', service_name)
20
21
22 def service_reload(service_name, restart_on_failure=False):
23- if not service('reload', service_name) and restart_on_failure:
24- service('restart', service_name)
25+ service_result = service('reload', service_name)
26+ if not service_result and restart_on_failure:
27+ service_result = service('restart', service_name)
28+ return service_result
29
30
31 def service(action, service_name):
32
33=== modified file 'tests/core/test_host.py'
34--- tests/core/test_host.py 2013-08-07 23:14:14 +0000
35+++ tests/core/test_host.py 2013-08-21 10:12:28 +0000
36@@ -82,21 +82,24 @@
37 @patch.object(host, 'service')
38 def test_starts_a_service(self, service):
39 service_name = 'foo-service'
40- host.service_start(service_name)
41+ service.side_effect = [True]
42+ self.assertTrue(host.service_start(service_name))
43
44 service.assert_called_with('start', service_name)
45
46 @patch.object(host, 'service')
47 def test_stops_a_service(self, service):
48 service_name = 'foo-service'
49- host.service_stop(service_name)
50+ service.side_effect = [True]
51+ self.assertTrue(host.service_stop(service_name))
52
53 service.assert_called_with('stop', service_name)
54
55 @patch.object(host, 'service')
56 def test_restarts_a_service(self, service):
57 service_name = 'foo-service'
58- host.service_restart(service_name)
59+ service.side_effect = [True]
60+ self.assertTrue(host.service_restart(service_name))
61
62 service.assert_called_with('restart', service_name)
63
64@@ -104,7 +107,7 @@
65 def test_reloads_a_service(self, service):
66 service_name = 'foo-service'
67 service.side_effect = [True]
68- host.service_reload(service_name)
69+ self.assertTrue(host.service_reload(service_name))
70
71 service.assert_called_with('reload', service_name)
72
73@@ -112,7 +115,8 @@
74 def test_failed_reload_restarts_a_service(self, service):
75 service_name = 'foo-service'
76 service.side_effect = [False, True]
77- host.service_reload(service_name, restart_on_failure=True)
78+ self.assertTrue(
79+ host.service_reload(service_name, restart_on_failure=True))
80
81 service.assert_has_calls([
82 call('reload', service_name),
83@@ -123,9 +127,54 @@
84 def test_failed_reload_without_restart(self, service):
85 service_name = 'foo-service'
86 service.side_effect = [False]
87- host.service_reload(service_name)
88-
89- service.assert_called_with('reload', service_name)
90+ self.assertFalse(host.service_reload(service_name))
91+
92+ service.assert_called_with('reload', service_name)
93+
94+
95+ @patch.object(host, 'service')
96+ def test_start_a_service_fails(self, service):
97+ service_name = 'foo-service'
98+ service.side_effect = [False]
99+ self.assertFalse(host.service_start(service_name))
100+
101+ service.assert_called_with('start', service_name)
102+
103+ @patch.object(host, 'service')
104+ def test_stop_a_service_fails(self, service):
105+ service_name = 'foo-service'
106+ service.side_effect = [False]
107+ self.assertFalse(host.service_stop(service_name))
108+
109+ service.assert_called_with('stop', service_name)
110+
111+ @patch.object(host, 'service')
112+ def test_restart_a_service_fails(self, service):
113+ service_name = 'foo-service'
114+ service.side_effect = [False]
115+ self.assertFalse(host.service_restart(service_name))
116+
117+ service.assert_called_with('restart', service_name)
118+
119+ @patch.object(host, 'service')
120+ def test_reload_a_service_fails(self, service):
121+ service_name = 'foo-service'
122+ service.side_effect = [False]
123+ self.assertFalse(host.service_reload(service_name))
124+
125+ service.assert_called_with('reload', service_name)
126+
127+ @patch.object(host, 'service')
128+ def test_failed_reload_restarts_a_service_fails(self, service):
129+ service_name = 'foo-service'
130+ service.side_effect = [False, False]
131+ self.assertFalse(
132+ host.service_reload(service_name, restart_on_failure=True))
133+
134+ service.assert_has_calls([
135+ call('reload', service_name),
136+ call('restart', service_name)
137+ ])
138
139 @patch('subprocess.check_output')
140 def test_service_running_on_stopped_service(self, check_output):

Subscribers

People subscribed via source and target branches