summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorClint Byrum <clint@fewbar.com>2013-05-07 18:26:11 -0700
committerClint Byrum <clint@fewbar.com>2013-05-07 20:17:55 -0700
commit25c3f97159b428e3ded65a337a6931dcab7c41ee (patch)
tree77a0eb66007076a4b4650a9c4861873029749eaa
parentc84894f78ae2cd52f2fe4ad91a5fd60c82ed635d (diff)
downloadheat-cfntools-25c3f97159b428e3ded65a337a6931dcab7c41ee.tar.gz
Add tests for cfn_hup.
Change-Id: Ibf69f99171c2c8316a0ea0a377e2fb186d5837b7
-rw-r--r--heat_cfntools/tests/test_cfn_hup.py114
1 files changed, 114 insertions, 0 deletions
diff --git a/heat_cfntools/tests/test_cfn_hup.py b/heat_cfntools/tests/test_cfn_hup.py
new file mode 100644
index 0000000..9cfe98e
--- /dev/null
+++ b/heat_cfntools/tests/test_cfn_hup.py
@@ -0,0 +1,114 @@
+#
+# Copyright 2013 Hewlett-Packard Development Company, L.P.
+#
+# Licensed under the Apache License, Version 2.0 (the "License"); you may
+# not use this file except in compliance with the License. You may obtain
+# a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+# License for the specific language governing permissions and limitations
+# under the License.
+
+import fixtures
+import mox
+import tempfile
+import testtools
+
+from heat_cfntools.cfntools import cfn_helper
+
+
+class FakeServicesHandler(object):
+ def __init__(self, *args, **kwargs):
+ self.m = mox.Mox()
+ self.m.StubOutWithMock(self, 'monitor_services')
+ self.monitor_services()
+ self.m.ReplayAll()
+
+ def monitor_services(self):
+ raise Exception('Mock not called')
+
+ def __del__(self):
+ self.m.VerifyAll()
+ self.m.UnsetStubs()
+
+
+class TestCfnHup(testtools.TestCase):
+
+ def setUp(self):
+ super(TestCfnHup, self).setUp()
+ self.m = mox.Mox()
+ self.logger = self.useFixture(fixtures.FakeLogger())
+ self.stack_name = self.getUniqueString()
+ self.resource = self.getUniqueString()
+ self.region = self.getUniqueString()
+ self.creds = tempfile.NamedTemporaryFile()
+ self.metadata = cfn_helper.Metadata(self.stack_name,
+ self.resource,
+ credentials_file=self.creds.name,
+ region=self.region)
+ self.init_content = self.getUniqueString()
+ self.init_temp = tempfile.NamedTemporaryFile()
+ self.service_name = self.getUniqueString()
+ self.init_section = {'AWS::CloudFormation::Init': {
+ 'config': {
+ 'services': {
+ 'sysvinit': {
+ self.service_name: {
+ 'enabled': True,
+ 'ensureRunning': True,
+ }
+ }
+ },
+ 'files': {
+ self.init_temp.name: {
+ 'content': self.init_content
+ }
+ }
+ }
+ }
+ }
+
+ def _mock_retrieve_metadata(self, desired_metadata):
+ self.m.StubOutWithMock(self.metadata, 'remote_metadata')
+ self.metadata.remote_metadata().AndReturn(desired_metadata)
+ self.m.ReplayAll()
+
+ with tempfile.NamedTemporaryFile() as last_md:
+ self.metadata.retrieve(last_path=last_md.name)
+
+ def test_cfn_hup_empty_metadata(self):
+
+ self._mock_retrieve_metadata({})
+
+ hooks = []
+ self.metadata.cfn_hup(hooks)
+
+ self.assertIn('Metadata does not contain a', self.logger.output)
+ self.m.VerifyAll()
+ self.m.UnsetStubs()
+
+ def test_cfn_hup_hooks(self):
+
+ self._mock_retrieve_metadata(self.init_section)
+ self.useFixture(
+ fixtures.MonkeyPatch(
+ 'heat_cfntools.cfntools.cfn_helper.ServicesHandler',
+ FakeServicesHandler))
+
+ section = self.getUniqueString()
+ triggers = 'post.add,post.delete,post.update'
+ path = 'Resources.%s.Metadata' % self.resource
+ runas = 'root'
+ action = '/bin/sh -c "true"'
+ hook = cfn_helper.Hook(section, triggers, path, runas, action)
+ self.m.StubOutWithMock(hook, 'event')
+ hook.event('post.update', self.resource, self.resource).AndReturn(None)
+ self.m.ReplayAll()
+
+ self.metadata.cfn_hup([hook])
+ self.m.VerifyAll()
+ self.m.UnsetStubs()