Merge lp:~hopem/charm-helpers/lp1246796 into lp:charm-helpers

Proposed by Edward Hope-Morley
Status: Merged
Merged at revision: 108
Proposed branch: lp:~hopem/charm-helpers/lp1246796
Merge into: lp:charm-helpers
Diff against target: 119 lines (+64/-16)
2 files modified
charmhelpers/contrib/openstack/context.py (+45/-15)
tests/contrib/openstack/test_os_contexts.py (+19/-1)
To merge this branch: bzr merge lp:~hopem/charm-helpers/lp1246796
Reviewer Review Type Date Requested Status
James Page Approve
Review via email: mp+197722@code.launchpad.net
To post a comment you must log in.
Revision history for this message
James Page (james-page) :
review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'charmhelpers/contrib/openstack/context.py'
2--- charmhelpers/contrib/openstack/context.py 2013-10-16 11:57:08 +0000
3+++ charmhelpers/contrib/openstack/context.py 2013-12-04 14:24:54 +0000
4@@ -433,25 +433,55 @@
5
6
7 class OSConfigFlagContext(OSContextGenerator):
8- '''
9- Responsible adding user-defined config-flags in charm config to a
10- to a template context.
11- '''
12+ """
13+ Responsible for adding user-defined config-flags in charm config to a
14+ template context.
15+
16+ NOTE: the value of config-flags may be a comma-separated list of
17+ key=value pairs and some Openstack config files support
18+ comma-separated lists as values.
19+ """
20 def __call__(self):
21 config_flags = config('config-flags')
22- if not config_flags or config_flags in ['None', '']:
23+ if not config_flags:
24 return {}
25- config_flags = config_flags.split(',')
26+
27+ if config_flags.find('==') >= 0:
28+ log("config_flags is not in expected format (key=value)",
29+ level=ERROR)
30+ raise OSContextError
31+
32+ # strip the following from each value.
33+ post_strippers = ' ,'
34+ # we strip any leading/trailing '=' or ' ' from the string then
35+ # split on '='.
36+ split = config_flags.strip(' =').split('=')
37+ limit = len(split)
38 flags = {}
39- for flag in config_flags:
40- if '=' not in flag:
41- log('Improperly formatted config-flag, expected k=v '
42- 'got %s' % flag, level=WARNING)
43- continue
44- k, v = flag.split('=')
45- flags[k.strip()] = v
46- ctxt = {'user_config_flags': flags}
47- return ctxt
48+ for i in xrange(0, limit - 1):
49+ current = split[i]
50+ next = split[i + 1]
51+ vindex = next.rfind(',')
52+ if (i == limit - 2) or (vindex < 0):
53+ value = next
54+ else:
55+ value = next[:vindex]
56+
57+ if i == 0:
58+ key = current
59+ else:
60+ # if this not the first entry, expect an embedded key.
61+ index = current.rfind(',')
62+ if index < 0:
63+ log("invalid config value(s) at index %s" % (i),
64+ level=ERROR)
65+ raise OSContextError
66+ key = current[index + 1:]
67+
68+ # Add to collection.
69+ flags[key.strip(post_strippers)] = value.rstrip(post_strippers)
70+
71+ return {'user_config_flags': flags}
72
73
74 class SubordinateConfigContext(OSContextGenerator):
75
76=== modified file 'tests/contrib/openstack/test_os_contexts.py'
77--- tests/contrib/openstack/test_os_contexts.py 2013-10-22 23:00:14 +0000
78+++ tests/contrib/openstack/test_os_contexts.py 2013-12-04 14:24:54 +0000
79@@ -636,6 +636,15 @@
80 def test_os_configflag_context(self, config):
81 flags = context.OSConfigFlagContext()
82
83+ # single
84+ config.return_value = 'deadbeef=True'
85+ self.assertEquals({
86+ 'user_config_flags': {
87+ 'deadbeef': 'True',
88+ }
89+ }, flags())
90+
91+ # multi
92 config.return_value = 'floating_ip=True,use_virtio=False,max=5'
93 self.assertEquals({
94 'user_config_flags': {
95@@ -649,14 +658,23 @@
96 config.return_value = empty
97 self.assertEquals({}, flags())
98
99+ # multi with commas
100 config.return_value = 'good_flag=woot,badflag,great_flag=w00t'
101 self.assertEquals({
102 'user_config_flags': {
103- 'good_flag': 'woot',
104+ 'good_flag': 'woot,badflag',
105 'great_flag': 'w00t',
106 }
107 }, flags())
108
109+ # missing key
110+ config.return_value = 'good_flag=woot=toow'
111+ self.assertRaises(context.OSContextError, flags)
112+
113+ # bad value
114+ config.return_value = 'good_flag=woot=='
115+ self.assertRaises(context.OSContextError, flags)
116+
117 def test_os_subordinate_config_context(self):
118 relation = FakeRelation(relation_data=SUB_CONFIG_RELATION)
119 self.relation_get.side_effect = relation.get

Subscribers

People subscribed via source and target branches