blob: 3be807b59394bb8c8ee5aa4730f2d75fdf6f7aef [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
Fei Long Wangd39431f2015-05-14 11:30:48 +120018from tempest.common.utils import data_utils
Yassine Lamgarchal05638762013-12-27 20:10:32 +010019from tempest import config
vrovachev95a16cc2014-02-04 15:29:48 +040020from tempest import exceptions
Yassine Lamgarchal05638762013-12-27 20:10:32 +010021import tempest.test
22
23CONF = config.CONF
24
25
26class BaseTelemetryTest(tempest.test.BaseTestCase):
27
28 """Base test case class for all Telemetry API tests."""
29
Andrea Frittolib21de6c2015-02-06 20:12:38 +000030 credentials = ['primary']
31
Yassine Lamgarchal05638762013-12-27 20:10:32 +010032 @classmethod
Rohan Kanaded114a132015-02-07 11:11:16 +053033 def skip_checks(cls):
34 super(BaseTelemetryTest, cls).skip_checks()
Yassine Lamgarchal05638762013-12-27 20:10:32 +010035 if not CONF.service_available.ceilometer:
36 raise cls.skipException("Ceilometer support is required")
Vadim Rovachev7bcea352013-12-26 15:56:17 +040037
Rohan Kanaded114a132015-02-07 11:11:16 +053038 @classmethod
39 def setup_credentials(cls):
40 cls.set_network_resources()
41 super(BaseTelemetryTest, cls).setup_credentials()
Rohan Kanaded114a132015-02-07 11:11:16 +053042
43 @classmethod
44 def setup_clients(cls):
45 super(BaseTelemetryTest, cls).setup_clients()
46 cls.telemetry_client = cls.os.telemetry_client
47 cls.servers_client = cls.os.servers_client
48 cls.flavors_client = cls.os.flavors_client
49 cls.image_client = cls.os.image_client
50 cls.image_client_v2 = cls.os.image_client_v2
51
52 @classmethod
53 def resource_setup(cls):
54 super(BaseTelemetryTest, cls).resource_setup()
Vadim Rovachev7bcea352013-12-26 15:56:17 +040055 cls.nova_notifications = ['memory', 'vcpus', 'disk.root.size',
56 'disk.ephemeral.size']
Vadim Rovachev80ee4e12014-02-05 16:59:07 +040057
gord chunge10a3e42015-06-13 00:36:59 -040058 cls.glance_notifications = ['image.size']
Vadim Rovachev80ee4e12014-02-05 16:59:07 +040059
60 cls.glance_v2_notifications = ['image.download', 'image.serve']
61
Vadim Rovachev7bcea352013-12-26 15:56:17 +040062 cls.server_ids = []
vrovachev95a16cc2014-02-04 15:29:48 +040063 cls.alarm_ids = []
Vadim Rovachev80ee4e12014-02-05 16:59:07 +040064 cls.image_ids = []
vrovachev95a16cc2014-02-04 15:29:48 +040065
66 @classmethod
67 def create_alarm(cls, **kwargs):
David Kranz20d06f42015-02-09 14:54:15 -050068 body = cls.telemetry_client.create_alarm(
vrovachev95a16cc2014-02-04 15:29:48 +040069 name=data_utils.rand_name('telemetry_alarm'),
70 type='threshold', **kwargs)
Vadim Rovachev80ee4e12014-02-05 16:59:07 +040071 cls.alarm_ids.append(body['alarm_id'])
David Kranz20d06f42015-02-09 14:54:15 -050072 return body
vrovachev95a16cc2014-02-04 15:29:48 +040073
74 @classmethod
Vadim Rovachev7bcea352013-12-26 15:56:17 +040075 def create_server(cls):
David Kranz0fb14292015-02-11 15:55:20 -050076 body = cls.servers_client.create_server(
Vadim Rovachev7bcea352013-12-26 15:56:17 +040077 data_utils.rand_name('ceilometer-instance'),
78 CONF.compute.image_ref, CONF.compute.flavor_ref,
79 wait_until='ACTIVE')
Vadim Rovachev80ee4e12014-02-05 16:59:07 +040080 cls.server_ids.append(body['id'])
David Kranz0fb14292015-02-11 15:55:20 -050081 return body
Vadim Rovachev80ee4e12014-02-05 16:59:07 +040082
83 @classmethod
84 def create_image(cls, client):
David Kranza5299eb2015-01-15 17:24:05 -050085 body = client.create_image(
Vadim Rovachev80ee4e12014-02-05 16:59:07 +040086 data_utils.rand_name('image'), container_format='bare',
87 disk_format='raw', visibility='private')
88 cls.image_ids.append(body['id'])
David Kranza5299eb2015-01-15 17:24:05 -050089 return body
Vadim Rovachev7bcea352013-12-26 15:56:17 +040090
91 @staticmethod
92 def cleanup_resources(method, list_of_ids):
93 for resource_id in list_of_ids:
vrovachev95a16cc2014-02-04 15:29:48 +040094 try:
Vadim Rovachev7bcea352013-12-26 15:56:17 +040095 method(resource_id)
Masayuki Igawabfa07602015-01-20 18:47:17 +090096 except lib_exc.NotFound:
vrovachev95a16cc2014-02-04 15:29:48 +040097 pass
Vadim Rovachev7bcea352013-12-26 15:56:17 +040098
99 @classmethod
Andrea Frittolic76d04d2014-09-15 13:14:54 +0100100 def resource_cleanup(cls):
Vadim Rovachev7bcea352013-12-26 15:56:17 +0400101 cls.cleanup_resources(cls.telemetry_client.delete_alarm, cls.alarm_ids)
102 cls.cleanup_resources(cls.servers_client.delete_server, cls.server_ids)
Vadim Rovachev80ee4e12014-02-05 16:59:07 +0400103 cls.cleanup_resources(cls.image_client.delete_image, cls.image_ids)
Andrea Frittolic76d04d2014-09-15 13:14:54 +0100104 super(BaseTelemetryTest, cls).resource_cleanup()
Vadim Rovachev7bcea352013-12-26 15:56:17 +0400105
106 def await_samples(self, metric, query):
107 """
108 This method is to wait for sample to add it to database.
109 There are long time delays when using Postgresql (or Mysql)
110 database as ceilometer backend
111 """
112 timeout = CONF.compute.build_timeout
113 start = timeutils.utcnow()
114 while timeutils.delta_seconds(start, timeutils.utcnow()) < timeout:
David Kranz20d06f42015-02-09 14:54:15 -0500115 body = self.telemetry_client.list_samples(metric, query)
Vadim Rovachev7bcea352013-12-26 15:56:17 +0400116 if body:
David Kranz20d06f42015-02-09 14:54:15 -0500117 return body
Vadim Rovachev7bcea352013-12-26 15:56:17 +0400118 time.sleep(CONF.compute.build_interval)
119
120 raise exceptions.TimeoutException(
121 'Sample for metric:%s with query:%s has not been added to the '
122 'database within %d seconds' % (metric, query,
123 CONF.compute.build_timeout))