summaryrefslogtreecommitdiff
path: root/heat_cfntools/tests/test_cfn_hup.py
blob: b182f923135ba44a32b0c1b887e1d3cb9bc617b6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#
# 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 tempfile

import fixtures
import mock
import testtools

from heat_cfntools.cfntools import cfn_helper


class TestCfnHup(testtools.TestCase):

    def setUp(self):
        super(TestCfnHup, self).setUp()
        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):
        with mock.patch.object(
                cfn_helper.Metadata, 'remote_metadata') as mock_method:
            mock_method.return_value = desired_metadata
            with tempfile.NamedTemporaryFile() as last_md:
                self.metadata.retrieve(last_path=last_md.name)

    def _test_cfn_hup_metadata(self, metadata):

        self._mock_retrieve_metadata(metadata)
        FakeServicesHandler = mock.Mock()
        FakeServicesHandler.monitor_services.return_value = None
        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)

        with mock.patch.object(cfn_helper.Hook, 'event') as mock_method:
            mock_method.return_value = None
            self.metadata.cfn_hup([hook])

    def test_cfn_hup_empty_metadata(self):
        self._test_cfn_hup_metadata({})

    def test_cfn_hup_cfn_init_metadata(self):
        self._test_cfn_hup_metadata(self.init_section)