Merge lp:~james-page/charm-helpers/fix_hookenv_misc into lp:charm-helpers

Proposed by James Page
Status: Merged
Merged at revision: 51
Proposed branch: lp:~james-page/charm-helpers/fix_hookenv_misc
Merge into: lp:charm-helpers
Diff against target: 67 lines (+30/-2)
2 files modified
charmhelpers/core/hookenv.py (+2/-2)
tests/core/test_hookenv.py (+28/-0)
To merge this branch: bzr merge lp:~james-page/charm-helpers/fix_hookenv_misc
Reviewer Review Type Date Requested Status
Adam Gandelman (community) Approve
Review via email: mp+173929@code.launchpad.net

Description of the change

Fixup misc edge cases with relation_ids and related_units

peer and container scoped relations behave a little differently to inter
container relations in that the JSON output can return null which results
in None being returned to the caller of relation_ids and related_units.

This fix works around this by returning an empty array in these instances.

To post a comment you must log in.
Revision history for this message
Adam Gandelman (gandelman-a) :
review: Approve

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 2013-07-05 18:22:23 +0000
3+++ charmhelpers/core/hookenv.py 2013-07-10 12:33:54 +0000
4@@ -197,7 +197,7 @@
5 relid_cmd_line = ['relation-ids', '--format=json']
6 if reltype is not None:
7 relid_cmd_line.append(reltype)
8- return json.loads(subprocess.check_output(relid_cmd_line))
9+ return json.loads(subprocess.check_output(relid_cmd_line)) or []
10 return []
11
12
13@@ -208,7 +208,7 @@
14 units_cmd_line = ['relation-list', '--format=json']
15 if relid is not None:
16 units_cmd_line.extend(('-r', relid))
17- return json.loads(subprocess.check_output(units_cmd_line))
18+ return json.loads(subprocess.check_output(units_cmd_line)) or []
19
20
21 @cached
22
23=== modified file 'tests/core/test_hookenv.py'
24--- tests/core/test_hookenv.py 2013-07-05 18:43:41 +0000
25+++ tests/core/test_hookenv.py 2013-07-10 12:33:54 +0000
26@@ -223,6 +223,20 @@
27
28 @patch('subprocess.check_output')
29 @patch('charmhelpers.core.hookenv.relation_type')
30+ def test_gets_relation_ids_empty_array(self, relation_type, check_output):
31+ ids = []
32+ check_output.return_value = json.dumps(None)
33+ reltype = 'foo'
34+ relation_type.return_value = reltype
35+
36+ result = hookenv.relation_ids()
37+
38+ self.assertEqual(result, ids)
39+ check_output.assert_called_with(['relation-ids', '--format=json',
40+ reltype])
41+
42+ @patch('subprocess.check_output')
43+ @patch('charmhelpers.core.hookenv.relation_type')
44 def test_relation_ids_no_relation_type(self, relation_type, check_output):
45 ids = [1, 2, 3]
46 check_output.return_value = json.dumps(ids)
47@@ -262,6 +276,20 @@
48
49 @patch('subprocess.check_output')
50 @patch('charmhelpers.core.hookenv.relation_id')
51+ def test_gets_related_units_empty_array(self, relation_id, check_output):
52+ relid = 123
53+ units = []
54+ relation_id.return_value = relid
55+ check_output.return_value = json.dumps(None)
56+
57+ result = hookenv.related_units()
58+
59+ self.assertEqual(result, units)
60+ check_output.assert_called_with(['relation-list', '--format=json',
61+ '-r', relid])
62+
63+ @patch('subprocess.check_output')
64+ @patch('charmhelpers.core.hookenv.relation_id')
65 def test_related_units_no_relation(self, relation_id, check_output):
66 units = ['foo', 'bar']
67 relation_id.return_value = None

Subscribers

People subscribed via source and target branches