blob: a9b6cf18db9e300772bf3a4d4e706f90eed9205e [file] [log] [blame]
Oleksii Chuprykovd9cd9dc2015-02-03 10:34:55 -05001# Licensed under the Apache License, Version 2.0 (the "License"); you may
2# not use this file except in compliance with the License. You may obtain
3# a copy of the License at
4#
5# http://www.apache.org/licenses/LICENSE-2.0
6#
7# Unless required by applicable law or agreed to in writing, software
8# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10# License for the specific language governing permissions and limitations
11# under the License.
12
13import kombu
14from oslo_config import cfg
15from oslo_messaging._drivers import common
16from oslo_messaging import transport
17import requests
18
19from heat_integrationtests.common import test
20
21
22BASIC_NOTIFICATIONS = [
23 'orchestration.stack.create.start',
24 'orchestration.stack.create.end',
25 'orchestration.stack.update.start',
26 'orchestration.stack.update.end',
27 'orchestration.stack.suspend.start',
28 'orchestration.stack.suspend.end',
29 'orchestration.stack.resume.start',
30 'orchestration.stack.resume.end',
31 'orchestration.stack.delete.start',
32 'orchestration.stack.delete.end'
33]
34
35ASG_NOTIFICATIONS = [
36 'orchestration.autoscaling.start',
37 'orchestration.autoscaling.end'
38]
39
40
41def get_url(conf):
42 conf = conf.oslo_messaging_rabbit
43 return 'amqp://%s:%s@%s:%s/' % (conf.rabbit_userid,
44 conf.rabbit_password,
45 conf.rabbit_host,
46 conf.rabbit_port)
47
48
49class NotificationHandler(object):
50 def __init__(self, stack_id, events=None):
51 self._notifications = []
52 self.stack_id = stack_id
53 self.events = events
54
55 def process_message(self, body, message):
56 notification = common.deserialize_msg(body)
57 if notification['payload']['stack_name'] == self.stack_id:
58 if self.events is not None:
59 if notification['event_type'] in self.events:
60 self.notifications.append(notification['event_type'])
61 else:
62 self.notifications.append(notification['event_type'])
63 message.ack()
64
65 def clear(self):
66 self._notifications = []
67
68 @property
69 def notifications(self):
70 return self._notifications
71
72
73class NotificationTest(test.HeatIntegrationTest):
74
75 basic_template = '''
76heat_template_version: 2013-05-23
77resources:
78 random1:
79 type: OS::Heat::RandomString
80'''
81 update_basic_template = '''
82heat_template_version: 2013-05-23
83resources:
84 random1:
85 type: OS::Heat::RandomString
86 random2:
87 type: OS::Heat::RandomString
88'''
89
90 asg_template = '''
91heat_template_version: 2013-05-23
92resources:
93 asg:
94 type: OS::Heat::AutoScalingGroup
95 properties:
96 resource:
97 type: OS::Heat::RandomString
98 min_size: 1
99 desired_capacity: 2
100 max_size: 3
101
102 scale_up_policy:
103 type: OS::Heat::ScalingPolicy
104 properties:
105 adjustment_type: change_in_capacity
106 auto_scaling_group_id: {get_resource: asg}
107 cooldown: 0
108 scaling_adjustment: 1
109
110 scale_down_policy:
111 type: OS::Heat::ScalingPolicy
112 properties:
113 adjustment_type: change_in_capacity
114 auto_scaling_group_id: {get_resource: asg}
115 cooldown: 0
116 scaling_adjustment: '-1'
117
118outputs:
119 scale_up_url:
120 value: {get_attr: [scale_up_policy, alarm_url]}
121 scale_dn_url:
122 value: {get_attr: [scale_down_policy, alarm_url]}
123'''
124
125 def setUp(self):
126 super(NotificationTest, self).setUp()
127
128 self.client = self.orchestration_client
129 self.exchange = kombu.Exchange('heat', 'topic', durable=False)
130 queue = kombu.Queue(exchange=self.exchange,
131 routing_key='notifications.info',
132 exclusive=True)
133 self.conn = kombu.Connection(get_url(
134 transport.get_transport(cfg.CONF).conf))
135 self.ch = self.conn.channel()
136 self.queue = queue(self.ch)
137 self.queue.declare()
138
139 def consume_events(self, handler, count):
140 self.conn.drain_events()
141 return len(handler.notifications) == count
142
143 def test_basic_notifications(self):
144 stack_identifier = self._stack_rand_name()
145 # do this manually so we can call _stack_delete() directly.
146 self.client.stacks.create(
147 stack_name=stack_identifier,
148 template=self.basic_template,
149 files={},
150 disable_rollback=True,
151 parameters={},
152 environment={}
153 )
154 self._wait_for_stack_status(stack_identifier, 'CREATE_COMPLETE')
155 self.update_stack(stack_identifier,
156 template=self.update_basic_template)
157 self.stack_suspend(stack_identifier)
158 self.stack_resume(stack_identifier)
159 self._stack_delete(stack_identifier)
160
161 handler = NotificationHandler(stack_identifier.split('/')[0])
162
163 with self.conn.Consumer(self.queue,
164 callbacks=[handler.process_message],
165 auto_declare=False):
166 try:
167 while True:
168 self.conn.drain_events(timeout=1)
169 except Exception:
170 pass
171
172 for n in BASIC_NOTIFICATIONS:
173 self.assertIn(n, handler.notifications)
174
175 def test_asg_notifications(self):
176 stack_identifier = self.stack_create(template=self.asg_template)
177
178 for output in self.client.stacks.get(stack_identifier).outputs:
179 if output['output_key'] == 'scale_dn_url':
180 scale_down_url = output['output_value']
181 else:
182 scale_up_url = output['output_value']
183
184 notifications = []
185 handler = NotificationHandler(stack_identifier.split('/')[0],
186 ASG_NOTIFICATIONS)
187
188 with self.conn.Consumer(self.queue,
189 callbacks=[handler.process_message],
190 auto_declare=False):
191
192 requests.post(scale_up_url)
193 test.call_until_true(20, 0, self.consume_events, handler, 2)
194 notifications += handler.notifications
195
196 handler.clear()
197 requests.post(scale_down_url)
198 test.call_until_true(20, 0, self.consume_events, handler, 2)
199 notifications += handler.notifications
200
201 self.assertEqual(2, notifications.count(ASG_NOTIFICATIONS[0]))
202 self.assertEqual(2, notifications.count(ASG_NOTIFICATIONS[1]))