Merge lp:~mpontillo/maas/fix-1469305-service-monitor-ignore-sudo-output into lp:~maas-committers/maas/trunk

Proposed by Mike Pontillo
Status: Merged
Approved by: Mike Pontillo
Approved revision: no longer in the source branch.
Merged at revision: 4051
Proposed branch: lp:~mpontillo/maas/fix-1469305-service-monitor-ignore-sudo-output
Merge into: lp:~maas-committers/maas/trunk
Diff against target: 87 lines (+45/-7)
2 files modified
src/provisioningserver/service_monitor.py (+15/-7)
src/provisioningserver/tests/test_service_monitor.py (+30/-0)
To merge this branch: bzr merge lp:~mpontillo/maas/fix-1469305-service-monitor-ignore-sudo-output
Reviewer Review Type Date Requested Status
Raphaël Badin (community) Approve
Review via email: mp+263177@code.launchpad.net

Commit message

Ignore sudo output inside service monitor. (This fixes upstart service status parsing errors that occur when, for example, an invalid hostname is set.)

To post a comment you must log in.
Revision history for this message
Raphaël Badin (rvb) wrote :

Looks good. Thanks for fixing this.

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'src/provisioningserver/service_monitor.py'
2--- src/provisioningserver/service_monitor.py 2015-06-02 09:36:59 +0000
3+++ src/provisioningserver/service_monitor.py 2015-06-27 00:30:22 +0000
4@@ -269,6 +269,20 @@
5 if exit_code != 0:
6 raise UnknownServiceError("'%s' is unknown to upstart." % (
7 service_name))
8+ for line in output.splitlines():
9+ if not line.startswith('sudo:'):
10+ active_state, process_state = self._parse_upstart_status_line(
11+ line, service_name)
12+ break
13+ active_state_enum = self.UPSTART_TO_STATE.get(active_state)
14+ if active_state_enum is None:
15+ raise ServiceParsingError(
16+ "Unable to parse the active state from upstart for "
17+ "service '%s', active state reported as '%s'." % (
18+ service_name, active_state))
19+ return active_state_enum, process_state
20+
21+ def _parse_upstart_status_line(self, output, service_name):
22 # output looks like:
23 # tgt start/running, process 29993
24 # split to get the active_state/process_state
25@@ -279,13 +293,7 @@
26 raise ServiceParsingError(
27 "Unable to parse the output from upstart for service '%s'." % (
28 service_name))
29- active_state_enum = self.UPSTART_TO_STATE.get(active_state)
30- if active_state_enum is None:
31- raise ServiceParsingError(
32- "Unable to parse the active state from upstart for "
33- "service '%s', active state reported as '%s'." % (
34- service_name, active_state))
35- return active_state_enum, process_state
36+ return active_state, process_state
37
38 def _get_expected_process_state(self, active_state):
39 """Return the expected process state for the `active_state` based
40
41=== modified file 'src/provisioningserver/tests/test_service_monitor.py'
42--- src/provisioningserver/tests/test_service_monitor.py 2015-06-02 09:36:59 +0000
43+++ src/provisioningserver/tests/test_service_monitor.py 2015-06-27 00:30:22 +0000
44@@ -436,6 +436,24 @@
45 self.assertEquals(SERVICE_STATE.ON, active_state)
46 self.assertEquals("running", process_state)
47
48+ def test__get_systemd_service_status_ignores_sudo_output(self):
49+ systemd_status_output = dedent("""\
50+ sudo: unable to resolve host sub-etha-sens-o-matic
51+ tgt.service - LSB: iscsi target daemon
52+ Loaded: loaded (/etc/init.d/tgt)
53+ Active: active (running) since Fri 2015-05-15 15:08:26 UTC;
54+ Docs: man:systemd-sysv-generator(8)
55+ """)
56+
57+ service_monitor = ServiceMonitor()
58+ mock_exec_service_action = self.patch(
59+ service_monitor, "_exec_service_action")
60+ mock_exec_service_action.return_value = (0, systemd_status_output)
61+ active_state, process_state = (
62+ service_monitor._get_systemd_service_status("tgt"))
63+ self.assertEquals(SERVICE_STATE.ON, active_state)
64+ self.assertEquals("running", process_state)
65+
66 def test__get_systemd_service_status_raise_error_for_invalid_active(self):
67 systemd_status_output = dedent("""\
68 tgt.service - LSB: iscsi target daemon
69@@ -494,6 +512,18 @@
70 self.assertEquals(SERVICE_STATE.ON, active_state)
71 self.assertEquals("running", process_state)
72
73+ def test__get_upstart_service_status_parsing_ignores_sudo_output(self):
74+ service_monitor = ServiceMonitor()
75+ mock_exec_service_action = self.patch(
76+ service_monitor, "_exec_service_action")
77+ mock_exec_service_action.return_value = (0, dedent("""\
78+ sudo: unable to resolve host infinite-improbability
79+ tgt start/running, process 23239"""))
80+ active_state, process_state = (
81+ service_monitor._get_upstart_service_status("tgt"))
82+ self.assertEquals(SERVICE_STATE.ON, active_state)
83+ self.assertEquals("running", process_state)
84+
85 def test__get_upstart_service_status_raise_error_for_invalid_active(self):
86 service_monitor = ServiceMonitor()
87 mock_exec_service_action = self.patch(