summaryrefslogtreecommitdiff
path: root/heat/tests/test_cw_alarm.py
blob: e1943cc503f88b1fffebf2510ecb8ff4768a7d7a (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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#
#    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 copy

from heat.common import template_format
from heat.engine import resource
from heat.engine.resources.openstack.heat import cloud_watch
from heat.engine import rsrc_defn
from heat.engine import scheduler
from heat.engine import watchrule
from heat.tests import common
from heat.tests import utils


alarm_template = '''
{
  "AWSTemplateFormatVersion" : "2010-09-09",
  "Description" : "Alarm Test",
  "Parameters" : {},
  "Resources" : {
    "MEMAlarmHigh": {
     "Type": "AWS::CloudWatch::Alarm",
     "Properties": {
        "AlarmDescription": "Scale-up if MEM > 50% for 1 minute",
        "MetricName": "MemoryUtilization",
        "Namespace": "system/linux",
        "Statistic": "Average",
        "Period": "60",
        "EvaluationPeriods": "1",
        "Threshold": "50",
        "AlarmActions": [],
        "Dimensions": [],
        "ComparisonOperator": "GreaterThanThreshold"
      }
    }
  }
}
'''


class CloudWatchAlarmTest(common.HeatTestCase):
    def setUp(self):
        super(CloudWatchAlarmTest, self).setUp()

    def create_alarm(self, t, stack, resource_name):
        resource_defns = stack.t.resource_definitions(stack)
        rsrc = cloud_watch.CloudWatchAlarm(resource_name,
                                           resource_defns[resource_name],
                                           stack)
        self.assertIsNone(rsrc.validate())
        scheduler.TaskRunner(rsrc.create)()
        self.assertEqual((rsrc.CREATE, rsrc.COMPLETE), rsrc.state)
        return rsrc

    def test_mem_alarm_high_update_no_replace(self):
        '''
        Make sure that we can change the update-able properties
        without replacing the Alarm rsrc.
        '''
        t = template_format.parse(alarm_template)

        # short circuit the alarm's references
        properties = t['Resources']['MEMAlarmHigh']['Properties']
        properties['AlarmActions'] = ['a']
        properties['Dimensions'] = [{'a': 'v'}]

        stack = utils.parse_stack(t)
        # the watch rule needs a valid stack_id
        stack.store()

        self.m.ReplayAll()
        rsrc = self.create_alarm(t, stack, 'MEMAlarmHigh')
        props = copy.copy(rsrc.properties.data)
        props.update({
            'ComparisonOperator': 'LessThanThreshold',
            'AlarmDescription': 'fruity',
            'EvaluationPeriods': '2',
            'Period': '90',
            'Statistic': 'Maximum',
            'Threshold': '39',
        })
        snippet = rsrc_defn.ResourceDefinition(rsrc.name,
                                               rsrc.type(),
                                               props)

        scheduler.TaskRunner(rsrc.update, snippet)()

        scheduler.TaskRunner(rsrc.delete)()
        self.m.VerifyAll()

    def test_mem_alarm_high_update_replace(self):
        '''
        Make sure that the Alarm resource IS replaced when non-update-able
        properties are changed.
        '''
        t = template_format.parse(alarm_template)

        # short circuit the alarm's references
        properties = t['Resources']['MEMAlarmHigh']['Properties']
        properties['AlarmActions'] = ['a']
        properties['Dimensions'] = [{'a': 'v'}]

        stack = utils.parse_stack(t)
        # the watch rule needs a valid stack_id
        stack.store()

        self.m.ReplayAll()
        rsrc = self.create_alarm(t, stack, 'MEMAlarmHigh')
        props = copy.copy(rsrc.properties.data)
        props['MetricName'] = 'temp'
        snippet = rsrc_defn.ResourceDefinition(rsrc.name,
                                               rsrc.type(),
                                               props)

        updater = scheduler.TaskRunner(rsrc.update, snippet)
        self.assertRaises(resource.UpdateReplace, updater)

        scheduler.TaskRunner(rsrc.delete)()
        self.m.VerifyAll()

    def test_suspend_resume(self):
        t = template_format.parse(alarm_template)
        stack_name = "test_cw_alarm_sus_res_stack"
        stack = utils.parse_stack(t, stack_name=stack_name)
        # the watch rule needs a valid stack_id
        stack.store()

        self.m.ReplayAll()
        rsrc = self.create_alarm(t, stack, 'MEMAlarmHigh')
        scheduler.TaskRunner(rsrc.suspend)()
        self.assertEqual((rsrc.SUSPEND, rsrc.COMPLETE), rsrc.state)

        self.ctx = utils.dummy_context()

        wr = watchrule.WatchRule.load(
            self.ctx, watch_name="%s-MEMAlarmHigh" % stack_name)

        self.assertEqual(watchrule.WatchRule.SUSPENDED, wr.state)

        scheduler.TaskRunner(rsrc.resume)()
        self.assertEqual((rsrc.RESUME, rsrc.COMPLETE), rsrc.state)

        wr = watchrule.WatchRule.load(
            self.ctx, watch_name="%s-MEMAlarmHigh" % stack_name)

        self.assertEqual(watchrule.WatchRule.NODATA, wr.state)

        scheduler.TaskRunner(rsrc.delete)()
        self.m.VerifyAll()