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
=== modified file 'charmhelpers/core/hookenv.py'
--- charmhelpers/core/hookenv.py 2013-07-05 18:22:23 +0000
+++ charmhelpers/core/hookenv.py 2013-07-10 12:33:54 +0000
@@ -197,7 +197,7 @@
197 relid_cmd_line = ['relation-ids', '--format=json']197 relid_cmd_line = ['relation-ids', '--format=json']
198 if reltype is not None:198 if reltype is not None:
199 relid_cmd_line.append(reltype)199 relid_cmd_line.append(reltype)
200 return json.loads(subprocess.check_output(relid_cmd_line))200 return json.loads(subprocess.check_output(relid_cmd_line)) or []
201 return []201 return []
202202
203203
@@ -208,7 +208,7 @@
208 units_cmd_line = ['relation-list', '--format=json']208 units_cmd_line = ['relation-list', '--format=json']
209 if relid is not None:209 if relid is not None:
210 units_cmd_line.extend(('-r', relid))210 units_cmd_line.extend(('-r', relid))
211 return json.loads(subprocess.check_output(units_cmd_line))211 return json.loads(subprocess.check_output(units_cmd_line)) or []
212212
213213
214@cached214@cached
215215
=== modified file 'tests/core/test_hookenv.py'
--- tests/core/test_hookenv.py 2013-07-05 18:43:41 +0000
+++ tests/core/test_hookenv.py 2013-07-10 12:33:54 +0000
@@ -223,6 +223,20 @@
223223
224 @patch('subprocess.check_output')224 @patch('subprocess.check_output')
225 @patch('charmhelpers.core.hookenv.relation_type')225 @patch('charmhelpers.core.hookenv.relation_type')
226 def test_gets_relation_ids_empty_array(self, relation_type, check_output):
227 ids = []
228 check_output.return_value = json.dumps(None)
229 reltype = 'foo'
230 relation_type.return_value = reltype
231
232 result = hookenv.relation_ids()
233
234 self.assertEqual(result, ids)
235 check_output.assert_called_with(['relation-ids', '--format=json',
236 reltype])
237
238 @patch('subprocess.check_output')
239 @patch('charmhelpers.core.hookenv.relation_type')
226 def test_relation_ids_no_relation_type(self, relation_type, check_output):240 def test_relation_ids_no_relation_type(self, relation_type, check_output):
227 ids = [1, 2, 3]241 ids = [1, 2, 3]
228 check_output.return_value = json.dumps(ids)242 check_output.return_value = json.dumps(ids)
@@ -262,6 +276,20 @@
262276
263 @patch('subprocess.check_output')277 @patch('subprocess.check_output')
264 @patch('charmhelpers.core.hookenv.relation_id')278 @patch('charmhelpers.core.hookenv.relation_id')
279 def test_gets_related_units_empty_array(self, relation_id, check_output):
280 relid = 123
281 units = []
282 relation_id.return_value = relid
283 check_output.return_value = json.dumps(None)
284
285 result = hookenv.related_units()
286
287 self.assertEqual(result, units)
288 check_output.assert_called_with(['relation-list', '--format=json',
289 '-r', relid])
290
291 @patch('subprocess.check_output')
292 @patch('charmhelpers.core.hookenv.relation_id')
265 def test_related_units_no_relation(self, relation_id, check_output):293 def test_related_units_no_relation(self, relation_id, check_output):
266 units = ['foo', 'bar']294 units = ['foo', 'bar']
267 relation_id.return_value = None295 relation_id.return_value = None

Subscribers

People subscribed via source and target branches