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
=== modified file 'charmhelpers/contrib/openstack/context.py'
--- charmhelpers/contrib/openstack/context.py 2013-10-16 11:57:08 +0000
+++ charmhelpers/contrib/openstack/context.py 2013-12-04 14:24:54 +0000
@@ -433,25 +433,55 @@
433433
434434
435class OSConfigFlagContext(OSContextGenerator):435class OSConfigFlagContext(OSContextGenerator):
436 '''436 """
437 Responsible adding user-defined config-flags in charm config to a437 Responsible for adding user-defined config-flags in charm config to a
438 to a template context.438 template context.
439 '''439
440 NOTE: the value of config-flags may be a comma-separated list of
441 key=value pairs and some Openstack config files support
442 comma-separated lists as values.
443 """
440 def __call__(self):444 def __call__(self):
441 config_flags = config('config-flags')445 config_flags = config('config-flags')
442 if not config_flags or config_flags in ['None', '']:446 if not config_flags:
443 return {}447 return {}
444 config_flags = config_flags.split(',')448
449 if config_flags.find('==') >= 0:
450 log("config_flags is not in expected format (key=value)",
451 level=ERROR)
452 raise OSContextError
453
454 # strip the following from each value.
455 post_strippers = ' ,'
456 # we strip any leading/trailing '=' or ' ' from the string then
457 # split on '='.
458 split = config_flags.strip(' =').split('=')
459 limit = len(split)
445 flags = {}460 flags = {}
446 for flag in config_flags:461 for i in xrange(0, limit - 1):
447 if '=' not in flag:462 current = split[i]
448 log('Improperly formatted config-flag, expected k=v '463 next = split[i + 1]
449 'got %s' % flag, level=WARNING)464 vindex = next.rfind(',')
450 continue465 if (i == limit - 2) or (vindex < 0):
451 k, v = flag.split('=')466 value = next
452 flags[k.strip()] = v467 else:
453 ctxt = {'user_config_flags': flags}468 value = next[:vindex]
454 return ctxt469
470 if i == 0:
471 key = current
472 else:
473 # if this not the first entry, expect an embedded key.
474 index = current.rfind(',')
475 if index < 0:
476 log("invalid config value(s) at index %s" % (i),
477 level=ERROR)
478 raise OSContextError
479 key = current[index + 1:]
480
481 # Add to collection.
482 flags[key.strip(post_strippers)] = value.rstrip(post_strippers)
483
484 return {'user_config_flags': flags}
455485
456486
457class SubordinateConfigContext(OSContextGenerator):487class SubordinateConfigContext(OSContextGenerator):
458488
=== modified file 'tests/contrib/openstack/test_os_contexts.py'
--- tests/contrib/openstack/test_os_contexts.py 2013-10-22 23:00:14 +0000
+++ tests/contrib/openstack/test_os_contexts.py 2013-12-04 14:24:54 +0000
@@ -636,6 +636,15 @@
636 def test_os_configflag_context(self, config):636 def test_os_configflag_context(self, config):
637 flags = context.OSConfigFlagContext()637 flags = context.OSConfigFlagContext()
638638
639 # single
640 config.return_value = 'deadbeef=True'
641 self.assertEquals({
642 'user_config_flags': {
643 'deadbeef': 'True',
644 }
645 }, flags())
646
647 # multi
639 config.return_value = 'floating_ip=True,use_virtio=False,max=5'648 config.return_value = 'floating_ip=True,use_virtio=False,max=5'
640 self.assertEquals({649 self.assertEquals({
641 'user_config_flags': {650 'user_config_flags': {
@@ -649,14 +658,23 @@
649 config.return_value = empty658 config.return_value = empty
650 self.assertEquals({}, flags())659 self.assertEquals({}, flags())
651660
661 # multi with commas
652 config.return_value = 'good_flag=woot,badflag,great_flag=w00t'662 config.return_value = 'good_flag=woot,badflag,great_flag=w00t'
653 self.assertEquals({663 self.assertEquals({
654 'user_config_flags': {664 'user_config_flags': {
655 'good_flag': 'woot',665 'good_flag': 'woot,badflag',
656 'great_flag': 'w00t',666 'great_flag': 'w00t',
657 }667 }
658 }, flags())668 }, flags())
659669
670 # missing key
671 config.return_value = 'good_flag=woot=toow'
672 self.assertRaises(context.OSContextError, flags)
673
674 # bad value
675 config.return_value = 'good_flag=woot=='
676 self.assertRaises(context.OSContextError, flags)
677
660 def test_os_subordinate_config_context(self):678 def test_os_subordinate_config_context(self):
661 relation = FakeRelation(relation_data=SUB_CONFIG_RELATION)679 relation = FakeRelation(relation_data=SUB_CONFIG_RELATION)
662 self.relation_get.side_effect = relation.get680 self.relation_get.side_effect = relation.get

Subscribers

People subscribed via source and target branches