summaryrefslogtreecommitdiff
path: root/heat/tests/test_cloudwatch.py
blob: 34af2d3e331c91d5287f037ce9a4bd413436bea4 (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
#
#    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.

from mock import patch

from heat.common import exception
from heat.common import template_format
from heat.tests import common
from heat.tests import utils
from heat.engine import scheduler
from heat.engine import watchrule


AWS_CloudWatch_Alarm = '''
HeatTemplateFormatVersion: '2012-12-12'
Description: Template which tests alarms
Resources:
  test_me:
    Type: AWS::CloudWatch::Alarm
    Properties:
      MetricName: cpu_util
      Namespace: AWS/EC2
      Statistic: Average
      Period: '60'
      EvaluationPeriods: '1'
      Threshold: '50'
      ComparisonOperator: GreaterThanThreshold
'''


class CloudWatchAlarmTest(common.HeatTestCase):

    def setUp(self):
        super(CloudWatchAlarmTest, self).setUp()
        utils.setup_dummy_db()
        self.ctx = utils.dummy_context()

    def parse_stack(self):
        t = template_format.parse(AWS_CloudWatch_Alarm)
        self.stack = utils.parse_stack(t)
        return self.stack

    @utils.stack_delete_after
    def test_resource_create_good(self):
        s = self.parse_stack()
        self.assertIsNone(scheduler.TaskRunner(s['test_me'].create)())

    @utils.stack_delete_after
    def test_resource_create_failed(self):
        s = self.parse_stack()
        with patch.object(watchrule.WatchRule, 'store') as bad_store:
            bad_store.side_effect = KeyError('any random failure')
            task_func = scheduler.TaskRunner(s['test_me'].create)
            self.assertRaises(exception.ResourceFailure, task_func)

    @utils.stack_delete_after
    def test_resource_delete_good(self):
        s = self.parse_stack()
        self.assertIsNone(scheduler.TaskRunner(s['test_me'].create)())
        self.assertIsNone(scheduler.TaskRunner(s['test_me'].delete)())

    @utils.stack_delete_after
    @utils.wr_delete_after
    def test_resource_delete_notfound(self):
        # if a resource is not found, handle_delete() should not raise
        # an exception.
        s = self.parse_stack()
        self.assertIsNone(scheduler.TaskRunner(s['test_me'].create)())
        res_name = self.stack['test_me'].physical_resource_name()
        self.wr = watchrule.WatchRule.load(self.ctx,
                                           watch_name=res_name)

        with patch.object(watchrule.WatchRule, 'destroy') as bad_destroy:
            watch_exc = exception.WatchRuleNotFound(watch_name='test')
            bad_destroy.side_effect = watch_exc
            self.assertIsNone(scheduler.TaskRunner(s['test_me'].delete)())