Merge lp:~stub/charm-helpers/fix-executution-environment into lp:charm-helpers

Proposed by Stuart Bishop
Status: Merged
Merged at revision: 42
Proposed branch: lp:~stub/charm-helpers/fix-executution-environment
Merge into: lp:charm-helpers
Prerequisite: lp:~stub/charm-helpers/hook-magic
Diff against target: 58 lines (+32/-3)
2 files modified
charmhelpers/core/hookenv.py (+4/-3)
tests/core/test_hookenv.py (+28/-0)
To merge this branch: bzr merge lp:~stub/charm-helpers/fix-executution-environment
Reviewer Review Type Date Requested Status
Charm Helper Maintainers Pending
Review via email: mp+172111@code.launchpad.net

Description of the change

execution_environment() explodes if invoked from a non-relation hook. Stop this happening by only adding the current relation context if there actually is current relation context.

Now we can call write_file() in an install hook.

To post a comment you must log in.

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-06-28 17:16:25 +0000
+++ charmhelpers/core/hookenv.py 2013-06-28 17:16:25 +0000
@@ -108,11 +108,12 @@
108 """A convenient bundling of the current execution context"""108 """A convenient bundling of the current execution context"""
109 context = {}109 context = {}
110 context['conf'] = config()110 context['conf'] = config()
111 context['reltype'] = relation_type()111 if relation_id():
112 context['relid'] = relation_id()112 context['reltype'] = relation_type()
113 context['relid'] = relation_id()
114 context['rel'] = relation_get()
113 context['unit'] = local_unit()115 context['unit'] = local_unit()
114 context['rels'] = relations()116 context['rels'] = relations()
115 context['rel'] = relation_get()
116 context['env'] = os.environ117 context['env'] = os.environ
117 return context118 return context
118119
119120
=== modified file 'tests/core/test_hookenv.py'
--- tests/core/test_hookenv.py 2013-06-28 17:16:25 +0000
+++ tests/core/test_hookenv.py 2013-06-28 17:16:25 +0000
@@ -496,6 +496,34 @@
496 'env': 'some-environment',496 'env': 'some-environment',
497 })497 })
498498
499 @patch('charmhelpers.core.hookenv.config')
500 @patch('charmhelpers.core.hookenv.relation_type')
501 @patch('charmhelpers.core.hookenv.local_unit')
502 @patch('charmhelpers.core.hookenv.relation_id')
503 @patch('charmhelpers.core.hookenv.relations')
504 @patch('charmhelpers.core.hookenv.relation_get')
505 @patch('charmhelpers.core.hookenv.os')
506 def test_gets_execution_environment_no_relation(
507 self, os_, relations_get, relations, relation_id,
508 local_unit, relation_type, config):
509 config.return_value = 'some-config'
510 relation_type.return_value = 'some-type'
511 local_unit.return_value = 'some-unit'
512 relation_id.return_value = None
513 relations.return_value = 'all-relations'
514 relations_get.return_value = 'some-relations'
515 os_.environ = 'some-environment'
516
517 result = hookenv.execution_environment()
518
519 self.assertEqual(result, {
520 'conf': 'some-config',
521 'unit': 'some-unit',
522 'rels': 'all-relations',
523 'env': 'some-environment',
524 })
525
526
499 @patch('charmhelpers.core.hookenv.os')527 @patch('charmhelpers.core.hookenv.os')
500 def test_gets_the_relation_id(self, os_):528 def test_gets_the_relation_id(self, os_):
501 os_.environ = {529 os_.environ = {

Subscribers

People subscribed via source and target branches