blob: 43180e5dd9c840c11b48660c6e471eaa30a32f00 [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
Matthew Treinish01472ff2015-02-20 17:26:52 -050016from tempest_lib.common.utils import data_utils
Masayuki Igawabfa07602015-01-20 18:47:17 +090017from tempest_lib import exceptions as lib_exc
18
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
58 cls.glance_notifications = ['image.update', 'image.upload',
59 'image.delete']
60
61 cls.glance_v2_notifications = ['image.download', 'image.serve']
62
Vadim Rovachev7bcea352013-12-26 15:56:17 +040063 cls.server_ids = []
vrovachev95a16cc2014-02-04 15:29:48 +040064 cls.alarm_ids = []
Vadim Rovachev80ee4e12014-02-05 16:59:07 +040065 cls.image_ids = []
vrovachev95a16cc2014-02-04 15:29:48 +040066
67 @classmethod
68 def create_alarm(cls, **kwargs):
David Kranz20d06f42015-02-09 14:54:15 -050069 body = cls.telemetry_client.create_alarm(
vrovachev95a16cc2014-02-04 15:29:48 +040070 name=data_utils.rand_name('telemetry_alarm'),
71 type='threshold', **kwargs)
Vadim Rovachev80ee4e12014-02-05 16:59:07 +040072 cls.alarm_ids.append(body['alarm_id'])
David Kranz20d06f42015-02-09 14:54:15 -050073 return body
vrovachev95a16cc2014-02-04 15:29:48 +040074
75 @classmethod
Vadim Rovachev7bcea352013-12-26 15:56:17 +040076 def create_server(cls):
David Kranz0fb14292015-02-11 15:55:20 -050077 body = cls.servers_client.create_server(
Vadim Rovachev7bcea352013-12-26 15:56:17 +040078 data_utils.rand_name('ceilometer-instance'),
79 CONF.compute.image_ref, CONF.compute.flavor_ref,
80 wait_until='ACTIVE')
Vadim Rovachev80ee4e12014-02-05 16:59:07 +040081 cls.server_ids.append(body['id'])
David Kranz0fb14292015-02-11 15:55:20 -050082 return body
Vadim Rovachev80ee4e12014-02-05 16:59:07 +040083
84 @classmethod
85 def create_image(cls, client):
David Kranza5299eb2015-01-15 17:24:05 -050086 body = client.create_image(
Vadim Rovachev80ee4e12014-02-05 16:59:07 +040087 data_utils.rand_name('image'), container_format='bare',
88 disk_format='raw', visibility='private')
89 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
Andrea Frittolic76d04d2014-09-15 13:14:54 +0100101 def resource_cleanup(cls):
Vadim Rovachev7bcea352013-12-26 15:56:17 +0400102 cls.cleanup_resources(cls.telemetry_client.delete_alarm, cls.alarm_ids)
103 cls.cleanup_resources(cls.servers_client.delete_server, cls.server_ids)
Vadim Rovachev80ee4e12014-02-05 16:59:07 +0400104 cls.cleanup_resources(cls.image_client.delete_image, cls.image_ids)
Andrea Frittolic76d04d2014-09-15 13:14:54 +0100105 super(BaseTelemetryTest, cls).resource_cleanup()
Vadim Rovachev7bcea352013-12-26 15:56:17 +0400106
107 def await_samples(self, metric, query):
108 """
109 This method is to wait for sample to add it to database.
110 There are long time delays when using Postgresql (or Mysql)
111 database as ceilometer backend
112 """
113 timeout = CONF.compute.build_timeout
114 start = timeutils.utcnow()
115 while timeutils.delta_seconds(start, timeutils.utcnow()) < timeout:
David Kranz20d06f42015-02-09 14:54:15 -0500116 body = self.telemetry_client.list_samples(metric, query)
Vadim Rovachev7bcea352013-12-26 15:56:17 +0400117 if body:
David Kranz20d06f42015-02-09 14:54:15 -0500118 return body
Vadim Rovachev7bcea352013-12-26 15:56:17 +0400119 time.sleep(CONF.compute.build_interval)
120
121 raise exceptions.TimeoutException(
122 'Sample for metric:%s with query:%s has not been added to the '
123 'database within %d seconds' % (metric, query,
124 CONF.compute.build_timeout))