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
1=== modified file 'charmhelpers/core/hookenv.py'
2--- charmhelpers/core/hookenv.py 2013-06-28 17:16:25 +0000
3+++ charmhelpers/core/hookenv.py 2013-06-28 17:16:25 +0000
4@@ -108,11 +108,12 @@
5 """A convenient bundling of the current execution context"""
6 context = {}
7 context['conf'] = config()
8- context['reltype'] = relation_type()
9- context['relid'] = relation_id()
10+ if relation_id():
11+ context['reltype'] = relation_type()
12+ context['relid'] = relation_id()
13+ context['rel'] = relation_get()
14 context['unit'] = local_unit()
15 context['rels'] = relations()
16- context['rel'] = relation_get()
17 context['env'] = os.environ
18 return context
19
20
21=== modified file 'tests/core/test_hookenv.py'
22--- tests/core/test_hookenv.py 2013-06-28 17:16:25 +0000
23+++ tests/core/test_hookenv.py 2013-06-28 17:16:25 +0000
24@@ -496,6 +496,34 @@
25 'env': 'some-environment',
26 })
27
28+ @patch('charmhelpers.core.hookenv.config')
29+ @patch('charmhelpers.core.hookenv.relation_type')
30+ @patch('charmhelpers.core.hookenv.local_unit')
31+ @patch('charmhelpers.core.hookenv.relation_id')
32+ @patch('charmhelpers.core.hookenv.relations')
33+ @patch('charmhelpers.core.hookenv.relation_get')
34+ @patch('charmhelpers.core.hookenv.os')
35+ def test_gets_execution_environment_no_relation(
36+ self, os_, relations_get, relations, relation_id,
37+ local_unit, relation_type, config):
38+ config.return_value = 'some-config'
39+ relation_type.return_value = 'some-type'
40+ local_unit.return_value = 'some-unit'
41+ relation_id.return_value = None
42+ relations.return_value = 'all-relations'
43+ relations_get.return_value = 'some-relations'
44+ os_.environ = 'some-environment'
45+
46+ result = hookenv.execution_environment()
47+
48+ self.assertEqual(result, {
49+ 'conf': 'some-config',
50+ 'unit': 'some-unit',
51+ 'rels': 'all-relations',
52+ 'env': 'some-environment',
53+ })
54+
55+
56 @patch('charmhelpers.core.hookenv.os')
57 def test_gets_the_relation_id(self, os_):
58 os_.environ = {

Subscribers

People subscribed via source and target branches