diff options
author | Jenkins <jenkins@review.openstack.org> | 2013-03-08 07:53:54 +0000 |
---|---|---|
committer | Gerrit Code Review <review@openstack.org> | 2013-03-08 07:53:54 +0000 |
commit | d1e536102616079460fd7ffd4041f22daa387cd5 (patch) | |
tree | 67c4af9cbdc3275dc3be1dbf01082f68f4ab9c86 | |
parent | 22d0fa738d8b061fe81ff6df1aa58644d2af69d4 (diff) | |
parent | d069c09428c38e4b6a6041e8e69683b674d4e30a (diff) | |
download | heat-cfntools-d1e536102616079460fd7ffd4041f22daa387cd5.tar.gz |
Merge "Store HupConfig hooks in a list, not a dict"
-rw-r--r-- | heat_cfntools/cfntools/cfn_helper.py | 25 | ||||
-rw-r--r-- | heat_cfntools/tests/test_cfn_helper.py | 126 |
2 files changed, 138 insertions, 13 deletions
diff --git a/heat_cfntools/cfntools/cfn_helper.py b/heat_cfntools/cfntools/cfn_helper.py index a84e3c5..fc15077 100644 --- a/heat_cfntools/cfntools/cfn_helper.py +++ b/heat_cfntools/cfntools/cfn_helper.py @@ -75,14 +75,15 @@ class HupConfig(object): self.load_main_section() - self.hooks = {} + self.hooks = [] for s in self.config.sections(): if s != 'main': - self.hooks[s] = Hook(s, - self.config.get(s, 'triggers'), - self.config.get(s, 'path'), - self.config.get(s, 'runas'), - self.config.get(s, 'action')) + self.hooks.append(Hook( + s, + self.config.get(s, 'triggers'), + self.config.get(s, 'path'), + self.config.get(s, 'runas'), + self.config.get(s, 'action'))) def load_main_section(self): # required values @@ -113,9 +114,9 @@ class HupConfig(object): def unique_resources_get(self): resources = [] for h in self.hooks: - r = self.hooks[h].resource_name_get() - if r not in resources: - resources.append(self.hooks[h].resource_name_get()) + r = h.resource_name_get() + if not r in resources: + resources.append(h.resource_name_get()) return resources @@ -715,8 +716,7 @@ class ServicesHandler(object): (service, start_cmd.stderr)) return for h in self.hooks: - self.hooks[h].event('service.restarted', - service, self.resource) + h.event('service.restarted', service, self.resource) def _monitor_services(self, handler, services): for service, properties in services.iteritems(): @@ -1209,5 +1209,4 @@ class Metadata(object): if self._has_changed: for h in hooks: - hooks[h].event('post.update', - self.resource, self.resource) + h.event('post.update', self.resource, self.resource) diff --git a/heat_cfntools/tests/test_cfn_helper.py b/heat_cfntools/tests/test_cfn_helper.py index 10e9251..3656d62 100644 --- a/heat_cfntools/tests/test_cfn_helper.py +++ b/heat_cfntools/tests/test_cfn_helper.py @@ -72,6 +72,132 @@ class TestCommandRunner(MockPopenTestCase): self.m.VerifyAll() +class TestHupConfig(MockPopenTestCase): + + def test_load_main_section(self): + fcreds = tempfile.NamedTemporaryFile() + fcreds.write('AWSAccessKeyId=foo\nAWSSecretKey=bar\n') + fcreds.flush() + + main_conf = tempfile.NamedTemporaryFile() + main_conf.write('''[main] +stack=teststack +credential-file=%s''' % fcreds.name) + main_conf.flush() + mainconfig = cfn_helper.HupConfig([open(main_conf.name)]) + self.assertEqual( + '{stack: teststack, credential_file: %s, ' + 'region: nova, interval:10}' % fcreds.name, + str(mainconfig)) + main_conf.close() + + main_conf = tempfile.NamedTemporaryFile() + main_conf.write('''[main] +stack=teststack +region=region1 +credential-file=%s-invalid +interval=120''' % fcreds.name) + main_conf.flush() + self.assertRaisesRegexp( + Exception, + 'invalid credentials file', + cfn_helper.HupConfig, + [open(main_conf.name)]) + + fcreds.close() + + def test_hup_config(self): + self.mock_cmd_run(['su', 'root', '-c', '/bin/hook2']).AndReturn( + FakePOpen('All good')) + self.mock_cmd_run(['su', 'root', '-c', '/bin/hook1']).AndReturn( + FakePOpen('All good')) + self.mock_cmd_run(['su', 'root', '-c', '/bin/hook3']).AndReturn( + FakePOpen('All good')) + self.mock_cmd_run( + ['su', 'root', '-c', '/bin/cfn-http-restarted']).AndReturn( + FakePOpen('All good')) + self.m.ReplayAll() + + hooks_conf = tempfile.NamedTemporaryFile() + + def write_hook_conf(f, name, triggers, path, action): + f.write( + '[%s]\ntriggers=%s\npath=%s\naction=%s\nrunas=root\n\n' % ( + name, triggers, path, action)) + + write_hook_conf( + hooks_conf, + 'hook2', + 'service2.restarted', + 'Resources.resource2.Metadata', + '/bin/hook2') + write_hook_conf( + hooks_conf, + 'hook1', + 'service1.restarted', + 'Resources.resource1.Metadata', + '/bin/hook1') + write_hook_conf( + hooks_conf, + 'hook3', + 'service3.restarted', + 'Resources.resource3.Metadata', + '/bin/hook3') + write_hook_conf( + hooks_conf, + 'cfn-http-restarted', + 'service.restarted', + 'Resources.resource.Metadata', + '/bin/cfn-http-restarted') + hooks_conf.flush() + + fcreds = tempfile.NamedTemporaryFile() + fcreds.write('AWSAccessKeyId=foo\nAWSSecretKey=bar\n') + fcreds.flush() + + main_conf = tempfile.NamedTemporaryFile() + main_conf.write('''[main] +stack=teststack +credential-file=%s +region=region1 +interval=120''' % fcreds.name) + main_conf.flush() + + mainconfig = cfn_helper.HupConfig([ + open(main_conf.name), + open(hooks_conf.name)]) + unique_resources = mainconfig.unique_resources_get() + self.assertSequenceEqual([ + 'resource2', + 'resource1', + 'resource3', + 'resource' + ], unique_resources) + + hooks = mainconfig.hooks + self.assertEqual( + '{hook2, service2.restarted, Resources.resource2.Metadata,' + ' root, /bin/hook2}', str(hooks[0])) + self.assertEqual( + '{hook1, service1.restarted, Resources.resource1.Metadata,' + ' root, /bin/hook1}', str(hooks[1])) + self.assertEqual( + '{hook3, service3.restarted, Resources.resource3.Metadata,' + ' root, /bin/hook3}', str(hooks[2])) + self.assertEqual( + '{cfn-http-restarted, service.restarted,' + ' Resources.resource.Metadata, root, /bin/cfn-http-restarted}', + str(hooks[3])) + + for hook in mainconfig.hooks: + hook.event(hook.triggers, None, hook.resource_name_get()) + + hooks_conf.close() + fcreds.close() + main_conf.close() + self.m.VerifyAll() + + class TestCfnHelper(testtools.TestCase): def _check_metadata_content(self, content, value): |