blob: 888518747c86a2065199fe7ff797658c7bf6748d [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
vrovachev95a16cc2014-02-04 15:29:48 +040015from tempest.common.utils import data_utils
Yassine Lamgarchal05638762013-12-27 20:10:32 +010016from tempest import config
vrovachev95a16cc2014-02-04 15:29:48 +040017from tempest import exceptions
Vadim Rovachev7bcea352013-12-26 15:56:17 +040018from tempest.openstack.common import timeutils
Yassine Lamgarchal05638762013-12-27 20:10:32 +010019import tempest.test
20
21CONF = config.CONF
22
23
24class BaseTelemetryTest(tempest.test.BaseTestCase):
25
26 """Base test case class for all Telemetry API tests."""
27
28 @classmethod
Andrea Frittolic76d04d2014-09-15 13:14:54 +010029 def resource_setup(cls):
Yassine Lamgarchal05638762013-12-27 20:10:32 +010030 if not CONF.service_available.ceilometer:
31 raise cls.skipException("Ceilometer support is required")
gordon chungf59ac3b2014-08-20 11:35:13 -040032 cls.set_network_resources()
Andrea Frittolic76d04d2014-09-15 13:14:54 +010033 super(BaseTelemetryTest, cls).resource_setup()
vrovachev95a16cc2014-02-04 15:29:48 +040034 os = cls.get_client_manager()
35 cls.telemetry_client = os.telemetry_client
Vadim Rovachev7bcea352013-12-26 15:56:17 +040036 cls.servers_client = os.servers_client
37 cls.flavors_client = os.flavors_client
Vadim Rovachev80ee4e12014-02-05 16:59:07 +040038 cls.image_client = os.image_client
39 cls.image_client_v2 = os.image_client_v2
Vadim Rovachev7bcea352013-12-26 15:56:17 +040040
41 cls.nova_notifications = ['memory', 'vcpus', 'disk.root.size',
42 'disk.ephemeral.size']
Vadim Rovachev80ee4e12014-02-05 16:59:07 +040043
44 cls.glance_notifications = ['image.update', 'image.upload',
45 'image.delete']
46
47 cls.glance_v2_notifications = ['image.download', 'image.serve']
48
Vadim Rovachev7bcea352013-12-26 15:56:17 +040049 cls.server_ids = []
vrovachev95a16cc2014-02-04 15:29:48 +040050 cls.alarm_ids = []
Vadim Rovachev80ee4e12014-02-05 16:59:07 +040051 cls.image_ids = []
vrovachev95a16cc2014-02-04 15:29:48 +040052
53 @classmethod
54 def create_alarm(cls, **kwargs):
David Kranz20d06f42015-02-09 14:54:15 -050055 body = cls.telemetry_client.create_alarm(
vrovachev95a16cc2014-02-04 15:29:48 +040056 name=data_utils.rand_name('telemetry_alarm'),
57 type='threshold', **kwargs)
Vadim Rovachev80ee4e12014-02-05 16:59:07 +040058 cls.alarm_ids.append(body['alarm_id'])
David Kranz20d06f42015-02-09 14:54:15 -050059 return body
vrovachev95a16cc2014-02-04 15:29:48 +040060
61 @classmethod
Vadim Rovachev7bcea352013-12-26 15:56:17 +040062 def create_server(cls):
63 resp, body = cls.servers_client.create_server(
64 data_utils.rand_name('ceilometer-instance'),
65 CONF.compute.image_ref, CONF.compute.flavor_ref,
66 wait_until='ACTIVE')
Vadim Rovachev80ee4e12014-02-05 16:59:07 +040067 cls.server_ids.append(body['id'])
68 return resp, body
69
70 @classmethod
71 def create_image(cls, client):
David Kranza5299eb2015-01-15 17:24:05 -050072 body = client.create_image(
Vadim Rovachev80ee4e12014-02-05 16:59:07 +040073 data_utils.rand_name('image'), container_format='bare',
74 disk_format='raw', visibility='private')
75 cls.image_ids.append(body['id'])
David Kranza5299eb2015-01-15 17:24:05 -050076 return body
Vadim Rovachev7bcea352013-12-26 15:56:17 +040077
78 @staticmethod
79 def cleanup_resources(method, list_of_ids):
80 for resource_id in list_of_ids:
vrovachev95a16cc2014-02-04 15:29:48 +040081 try:
Vadim Rovachev7bcea352013-12-26 15:56:17 +040082 method(resource_id)
vrovachev95a16cc2014-02-04 15:29:48 +040083 except exceptions.NotFound:
84 pass
Vadim Rovachev7bcea352013-12-26 15:56:17 +040085
86 @classmethod
Andrea Frittolic76d04d2014-09-15 13:14:54 +010087 def resource_cleanup(cls):
Vadim Rovachev7bcea352013-12-26 15:56:17 +040088 cls.cleanup_resources(cls.telemetry_client.delete_alarm, cls.alarm_ids)
89 cls.cleanup_resources(cls.servers_client.delete_server, cls.server_ids)
Vadim Rovachev80ee4e12014-02-05 16:59:07 +040090 cls.cleanup_resources(cls.image_client.delete_image, cls.image_ids)
vrovachev95a16cc2014-02-04 15:29:48 +040091 cls.clear_isolated_creds()
Andrea Frittolic76d04d2014-09-15 13:14:54 +010092 super(BaseTelemetryTest, cls).resource_cleanup()
Vadim Rovachev7bcea352013-12-26 15:56:17 +040093
94 def await_samples(self, metric, query):
95 """
96 This method is to wait for sample to add it to database.
97 There are long time delays when using Postgresql (or Mysql)
98 database as ceilometer backend
99 """
100 timeout = CONF.compute.build_timeout
101 start = timeutils.utcnow()
102 while timeutils.delta_seconds(start, timeutils.utcnow()) < timeout:
David Kranz20d06f42015-02-09 14:54:15 -0500103 body = self.telemetry_client.list_samples(metric, query)
Vadim Rovachev7bcea352013-12-26 15:56:17 +0400104 if body:
David Kranz20d06f42015-02-09 14:54:15 -0500105 return body
Vadim Rovachev7bcea352013-12-26 15:56:17 +0400106 time.sleep(CONF.compute.build_interval)
107
108 raise exceptions.TimeoutException(
109 'Sample for metric:%s with query:%s has not been added to the '
110 'database within %d seconds' % (metric, query,
111 CONF.compute.build_timeout))