Merge lp:~adam-collard/charm-helpers/status-get-message into lp:charm-helpers

Proposed by Adam Collard
Status: Merged
Merged at revision: 435
Proposed branch: lp:~adam-collard/charm-helpers/status-get-message
Merge into: lp:charm-helpers
Diff against target: 72 lines (+19/-15)
2 files modified
charmhelpers/core/hookenv.py (+11/-9)
tests/core/test_hookenv.py (+8/-6)
To merge this branch: bzr merge lp:~adam-collard/charm-helpers/status-get-message
Reviewer Review Type Date Requested Status
Chris Glass (community) Approve
Review via email: mp+269227@code.launchpad.net

Description of the change

Change status_get() to return both the status and the message associated with it. This makes status_set(*status_get()) end up with the unit having the same state and message.

I searched the whole charmstore for files in hooks/ which used status_get() and weren't in charmhelpers itself. Only the Cassandra charm was using it, and I have filed bug 1489120 pre-emptively to note the failure.

To post a comment you must log in.
Revision history for this message
Chris Glass (tribaal) wrote :

Thanks for your branch.

I think in this particular case it would be better to simply return a 2-tuple with the message every time. I see no reason not to, and adding switches that default to a non-ideal behavior seems broken to me.

Since none of the charms I grep'd use this method (yet?), I would go for breaking and not being backwards compatible.

In other words, make status_get() not take a parameter, and always pass --include-data, returning a 2-tuple. Bonus points for making it an explicit 2-tuple so future readers see it right away.

review: Needs Fixing
436. By Adam Collard

Switch to always returning status and message (tribaal's review)

Revision history for this message
Chris Glass (tribaal) wrote :

Ok looks good now, thanks!

As commented in the cassandra bug the failure this will introduce will be picked up and fixed next time they synv C-H.

+1!

review: Approve
Revision history for this message
Adam Collard (adam-collard) :

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'charmhelpers/core/hookenv.py'
2--- charmhelpers/core/hookenv.py 2015-08-04 20:02:59 +0000
3+++ charmhelpers/core/hookenv.py 2015-08-26 17:32:40 +0000
4@@ -767,21 +767,23 @@
5
6
7 def status_get():
8- """Retrieve the previously set juju workload state
9-
10- If the status-set command is not found then assume this is juju < 1.23 and
11- return 'unknown'
12+ """Retrieve the previously set juju workload state and message
13+
14+ If the status-get command is not found then assume this is juju < 1.23 and
15+ return 'unknown', ""
16+
17 """
18- cmd = ['status-get']
19+ cmd = ['status-get', "--format=json", "--include-data"]
20 try:
21- raw_status = subprocess.check_output(cmd, universal_newlines=True)
22- status = raw_status.rstrip()
23- return status
24+ raw_status = subprocess.check_output(cmd)
25 except OSError as e:
26 if e.errno == errno.ENOENT:
27- return 'unknown'
28+ return ('unknown', "")
29 else:
30 raise
31+ else:
32+ status = json.loads(raw_status.decode("UTF-8"))
33+ return (status["status"], status["message"])
34
35
36 def translate_exc(from_exc, to_exc):
37
38=== modified file 'tests/core/test_hookenv.py'
39--- tests/core/test_hookenv.py 2015-07-29 15:33:40 +0000
40+++ tests/core/test_hookenv.py 2015-08-26 17:32:40 +0000
41@@ -1354,23 +1354,25 @@
42
43 @patch('subprocess.check_output')
44 def test_status_get(self, check_output):
45- check_output.return_value = 'active\n'
46+ check_output.return_value = json.dumps(
47+ {"message": "foo",
48+ "status": "active",
49+ "status-data": {}}).encode("UTF-8")
50 result = hookenv.status_get()
51- self.assertEqual(result, 'active')
52- check_output.assert_called_with(['status-get'], universal_newlines=True)
53+ self.assertEqual(result, ('active', 'foo'))
54+ check_output.assert_called_with(
55+ ['status-get', "--format=json", "--include-data"])
56
57 @patch('subprocess.check_output')
58 def test_status_get_nostatus(self, check_output):
59 check_output.side_effect = OSError(2, 'fail')
60 result = hookenv.status_get()
61- self.assertEqual(result, 'unknown')
62- check_output.assert_called_with(['status-get'], universal_newlines=True)
63+ self.assertEqual(result, ('unknown', ''))
64
65 @patch('subprocess.check_output')
66 def test_status_get_status_error(self, check_output):
67 check_output.side_effect = OSError(3, 'fail')
68 self.assertRaises(OSError, hookenv.status_get)
69- check_output.assert_called_with(['status-get'], universal_newlines=True)
70
71 @patch('subprocess.check_output')
72 @patch('glob.glob')

Subscribers

People subscribed via source and target branches