blob: a4c419c2446c561cde1d9549aca25102c6f93365 [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):
Sergey Kraynevbf67ce32015-04-17 10:54:20 -0400144 # disable cleanup so we can call _stack_delete() directly.
145 stack_identifier = self.stack_create(template=self.basic_template,
146 enable_cleanup=False)
Oleksii Chuprykovd9cd9dc2015-02-03 10:34:55 -0500147 self.update_stack(stack_identifier,
148 template=self.update_basic_template)
149 self.stack_suspend(stack_identifier)
150 self.stack_resume(stack_identifier)
151 self._stack_delete(stack_identifier)
152
153 handler = NotificationHandler(stack_identifier.split('/')[0])
154
155 with self.conn.Consumer(self.queue,
156 callbacks=[handler.process_message],
157 auto_declare=False):
158 try:
159 while True:
160 self.conn.drain_events(timeout=1)
161 except Exception:
162 pass
163
164 for n in BASIC_NOTIFICATIONS:
165 self.assertIn(n, handler.notifications)
166
167 def test_asg_notifications(self):
168 stack_identifier = self.stack_create(template=self.asg_template)
169
170 for output in self.client.stacks.get(stack_identifier).outputs:
171 if output['output_key'] == 'scale_dn_url':
172 scale_down_url = output['output_value']
173 else:
174 scale_up_url = output['output_value']
175
176 notifications = []
177 handler = NotificationHandler(stack_identifier.split('/')[0],
178 ASG_NOTIFICATIONS)
179
180 with self.conn.Consumer(self.queue,
181 callbacks=[handler.process_message],
182 auto_declare=False):
183
184 requests.post(scale_up_url)
185 test.call_until_true(20, 0, self.consume_events, handler, 2)
186 notifications += handler.notifications
187
188 handler.clear()
189 requests.post(scale_down_url)
190 test.call_until_true(20, 0, self.consume_events, handler, 2)
191 notifications += handler.notifications
192
193 self.assertEqual(2, notifications.count(ASG_NOTIFICATIONS[0]))
194 self.assertEqual(2, notifications.count(ASG_NOTIFICATIONS[1]))