blob: ff068108e301c008ab15704379f952a90d084897 [file] [log] [blame]
Yassine Lamgarchal05638762013-12-27 20:10:32 +01001# 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
Vadim Rovachev7bcea352013-12-26 15:56:17 +040013import time
14
Doug Hellmann583ce2c2015-03-11 14:55:46 +000015from oslo_utils import timeutils
Masayuki Igawabfa07602015-01-20 18:47:17 +090016from tempest_lib import exceptions as lib_exc
17
Joseph Lanoux2a15fe62015-07-31 14:09:58 +000018from tempest.common import compute
Fei Long Wangd39431f2015-05-14 11:30:48 +120019from tempest.common.utils import data_utils
Vincent Untz66bc0f52016-01-16 14:28:28 +010020from tempest.common import waiters
Yassine Lamgarchal05638762013-12-27 20:10:32 +010021from tempest import config
vrovachev95a16cc2014-02-04 15:29:48 +040022from tempest import exceptions
Yassine Lamgarchal05638762013-12-27 20:10:32 +010023import tempest.test
24
25CONF = config.CONF
26
27
28class BaseTelemetryTest(tempest.test.BaseTestCase):
29
30 """Base test case class for all Telemetry API tests."""
31
Andrea Frittolib21de6c2015-02-06 20:12:38 +000032 credentials = ['primary']
33
Yassine Lamgarchal05638762013-12-27 20:10:32 +010034 @classmethod
Rohan Kanaded114a132015-02-07 11:11:16 +053035 def skip_checks(cls):
36 super(BaseTelemetryTest, cls).skip_checks()
Yassine Lamgarchal05638762013-12-27 20:10:32 +010037 if not CONF.service_available.ceilometer:
38 raise cls.skipException("Ceilometer support is required")
Vadim Rovachev7bcea352013-12-26 15:56:17 +040039
Rohan Kanaded114a132015-02-07 11:11:16 +053040 @classmethod
41 def setup_credentials(cls):
42 cls.set_network_resources()
43 super(BaseTelemetryTest, cls).setup_credentials()
Rohan Kanaded114a132015-02-07 11:11:16 +053044
45 @classmethod
46 def setup_clients(cls):
47 super(BaseTelemetryTest, cls).setup_clients()
48 cls.telemetry_client = cls.os.telemetry_client
49 cls.servers_client = cls.os.servers_client
50 cls.flavors_client = cls.os.flavors_client
51 cls.image_client = cls.os.image_client
52 cls.image_client_v2 = cls.os.image_client_v2
53
54 @classmethod
55 def resource_setup(cls):
56 super(BaseTelemetryTest, cls).resource_setup()
Vadim Rovachev7bcea352013-12-26 15:56:17 +040057 cls.nova_notifications = ['memory', 'vcpus', 'disk.root.size',
58 'disk.ephemeral.size']
Vadim Rovachev80ee4e12014-02-05 16:59:07 +040059
gord chunge10a3e42015-06-13 00:36:59 -040060 cls.glance_notifications = ['image.size']
Vadim Rovachev80ee4e12014-02-05 16:59:07 +040061
62 cls.glance_v2_notifications = ['image.download', 'image.serve']
63
Vadim Rovachev7bcea352013-12-26 15:56:17 +040064 cls.server_ids = []
Vadim Rovachev80ee4e12014-02-05 16:59:07 +040065 cls.image_ids = []
vrovachev95a16cc2014-02-04 15:29:48 +040066
67 @classmethod
Vadim Rovachev7bcea352013-12-26 15:56:17 +040068 def create_server(cls):
Joseph Lanoux2a15fe62015-07-31 14:09:58 +000069 tenant_network = cls.get_tenant_network()
70 body, server = compute.create_test_server(
71 cls.os,
72 tenant_network=tenant_network,
73 name=data_utils.rand_name('ceilometer-instance'),
Vadim Rovachev7bcea352013-12-26 15:56:17 +040074 wait_until='ACTIVE')
Vadim Rovachev80ee4e12014-02-05 16:59:07 +040075 cls.server_ids.append(body['id'])
David Kranz0fb14292015-02-11 15:55:20 -050076 return body
Vadim Rovachev80ee4e12014-02-05 16:59:07 +040077
78 @classmethod
Ghanshyamf603b052015-12-16 15:06:55 +090079 def create_image(cls, client, **kwargs):
80 body = client.create_image(name=data_utils.rand_name('image'),
81 container_format='bare',
82 disk_format='raw',
83 **kwargs)
John Warren66207252015-07-31 15:51:02 -040084 # TODO(jswarren) Move ['image'] up to initial body value assignment
85 # once both v1 and v2 glance clients include the full response
86 # object.
87 if 'image' in body:
88 body = body['image']
Vadim Rovachev80ee4e12014-02-05 16:59:07 +040089 cls.image_ids.append(body['id'])
David Kranza5299eb2015-01-15 17:24:05 -050090 return body
Vadim Rovachev7bcea352013-12-26 15:56:17 +040091
92 @staticmethod
93 def cleanup_resources(method, list_of_ids):
94 for resource_id in list_of_ids:
vrovachev95a16cc2014-02-04 15:29:48 +040095 try:
Vadim Rovachev7bcea352013-12-26 15:56:17 +040096 method(resource_id)
Masayuki Igawabfa07602015-01-20 18:47:17 +090097 except lib_exc.NotFound:
vrovachev95a16cc2014-02-04 15:29:48 +040098 pass
Vadim Rovachev7bcea352013-12-26 15:56:17 +040099
100 @classmethod
Vincent Untz66bc0f52016-01-16 14:28:28 +0100101 def wait_for_server_termination(cls, server_id):
102 waiters.wait_for_server_termination(cls.servers_client,
103 server_id)
104
105 @classmethod
Andrea Frittolic76d04d2014-09-15 13:14:54 +0100106 def resource_cleanup(cls):
Vadim Rovachev7bcea352013-12-26 15:56:17 +0400107 cls.cleanup_resources(cls.servers_client.delete_server, cls.server_ids)
Vincent Untz66bc0f52016-01-16 14:28:28 +0100108 cls.cleanup_resources(cls.wait_for_server_termination, cls.server_ids)
Vadim Rovachev80ee4e12014-02-05 16:59:07 +0400109 cls.cleanup_resources(cls.image_client.delete_image, cls.image_ids)
Andrea Frittolic76d04d2014-09-15 13:14:54 +0100110 super(BaseTelemetryTest, cls).resource_cleanup()
Vadim Rovachev7bcea352013-12-26 15:56:17 +0400111
112 def await_samples(self, metric, query):
Ken'ichi Ohmichi9e3dac02015-11-19 07:01:07 +0000113 """This method is to wait for sample to add it to database.
114
Vadim Rovachev7bcea352013-12-26 15:56:17 +0400115 There are long time delays when using Postgresql (or Mysql)
116 database as ceilometer backend
117 """
118 timeout = CONF.compute.build_timeout
119 start = timeutils.utcnow()
120 while timeutils.delta_seconds(start, timeutils.utcnow()) < timeout:
David Kranz20d06f42015-02-09 14:54:15 -0500121 body = self.telemetry_client.list_samples(metric, query)
Vadim Rovachev7bcea352013-12-26 15:56:17 +0400122 if body:
David Kranz20d06f42015-02-09 14:54:15 -0500123 return body
Vadim Rovachev7bcea352013-12-26 15:56:17 +0400124 time.sleep(CONF.compute.build_interval)
125
126 raise exceptions.TimeoutException(
127 'Sample for metric:%s with query:%s has not been added to the '
128 'database within %d seconds' % (metric, query,
129 CONF.compute.build_timeout))
gordon chungee23ddb2015-02-11 20:05:07 -0500130
131
132class BaseTelemetryAdminTest(BaseTelemetryTest):
133 """Base test case class for admin Telemetry API tests."""
134
135 credentials = ['primary', 'admin']
136
137 @classmethod
138 def setup_clients(cls):
139 super(BaseTelemetryAdminTest, cls).setup_clients()
140 cls.telemetry_admin_client = cls.os_adm.telemetry_client
141
142 def await_events(self, query):
143 timeout = CONF.compute.build_timeout
144 start = timeutils.utcnow()
145 while timeutils.delta_seconds(start, timeutils.utcnow()) < timeout:
146 body = self.telemetry_admin_client.list_events(query)
147 if body:
148 return body
149 time.sleep(CONF.compute.build_interval)
150
151 raise exceptions.TimeoutException(
152 'Event with query:%s has not been added to the '
153 'database within %d seconds' % (query, CONF.compute.build_timeout))
liu-sheng67b730e2015-07-16 15:19:33 +0800154
155
156class BaseAlarmingTest(tempest.test.BaseTestCase):
157 """Base test case class for all Alarming API tests."""
158
159 credentials = ['primary']
160
161 @classmethod
162 def skip_checks(cls):
163 super(BaseAlarmingTest, cls).skip_checks()
164 if not CONF.service_available.aodh:
165 raise cls.skipException("Aodh support is required")
166
167 @classmethod
168 def setup_clients(cls):
169 super(BaseAlarmingTest, cls).setup_clients()
170 cls.alarming_client = cls.os.alarming_client
171
172 @classmethod
173 def resource_setup(cls):
174 super(BaseAlarmingTest, cls).resource_setup()
175 cls.alarm_ids = []
176
177 @classmethod
178 def create_alarm(cls, **kwargs):
179 body = cls.alarming_client.create_alarm(
180 name=data_utils.rand_name('telemetry_alarm'),
181 type='threshold', **kwargs)
182 cls.alarm_ids.append(body['alarm_id'])
183 return body
184
185 @staticmethod
186 def cleanup_resources(method, list_of_ids):
187 for resource_id in list_of_ids:
188 try:
189 method(resource_id)
190 except lib_exc.NotFound:
191 pass
192
193 @classmethod
194 def resource_cleanup(cls):
195 cls.cleanup_resources(cls.alarming_client.delete_alarm, cls.alarm_ids)
196 super(BaseAlarmingTest, cls).resource_cleanup()