blob: 5d1784f997a54dd1d67c607894b1d71a1aabd6f0 [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')
John Warren66207252015-07-31 15:51:02 -040088 # TODO(jswarren) Move ['image'] up to initial body value assignment
89 # once both v1 and v2 glance clients include the full response
90 # object.
91 if 'image' in body:
92 body = body['image']
Vadim Rovachev80ee4e12014-02-05 16:59:07 +040093 cls.image_ids.append(body['id'])
David Kranza5299eb2015-01-15 17:24:05 -050094 return body
Vadim Rovachev7bcea352013-12-26 15:56:17 +040095
96 @staticmethod
97 def cleanup_resources(method, list_of_ids):
98 for resource_id in list_of_ids:
vrovachev95a16cc2014-02-04 15:29:48 +040099 try:
Vadim Rovachev7bcea352013-12-26 15:56:17 +0400100 method(resource_id)
Masayuki Igawabfa07602015-01-20 18:47:17 +0900101 except lib_exc.NotFound:
vrovachev95a16cc2014-02-04 15:29:48 +0400102 pass
Vadim Rovachev7bcea352013-12-26 15:56:17 +0400103
104 @classmethod
Andrea Frittolic76d04d2014-09-15 13:14:54 +0100105 def resource_cleanup(cls):
Vadim Rovachev7bcea352013-12-26 15:56:17 +0400106 cls.cleanup_resources(cls.telemetry_client.delete_alarm, cls.alarm_ids)
107 cls.cleanup_resources(cls.servers_client.delete_server, cls.server_ids)
Vadim Rovachev80ee4e12014-02-05 16:59:07 +0400108 cls.cleanup_resources(cls.image_client.delete_image, cls.image_ids)
Andrea Frittolic76d04d2014-09-15 13:14:54 +0100109 super(BaseTelemetryTest, cls).resource_cleanup()
Vadim Rovachev7bcea352013-12-26 15:56:17 +0400110
111 def await_samples(self, metric, query):
112 """
113 This method is to wait for sample to add it to database.
114 There are long time delays when using Postgresql (or Mysql)
115 database as ceilometer backend
116 """
117 timeout = CONF.compute.build_timeout
118 start = timeutils.utcnow()
119 while timeutils.delta_seconds(start, timeutils.utcnow()) < timeout:
David Kranz20d06f42015-02-09 14:54:15 -0500120 body = self.telemetry_client.list_samples(metric, query)
Vadim Rovachev7bcea352013-12-26 15:56:17 +0400121 if body:
David Kranz20d06f42015-02-09 14:54:15 -0500122 return body
Vadim Rovachev7bcea352013-12-26 15:56:17 +0400123 time.sleep(CONF.compute.build_interval)
124
125 raise exceptions.TimeoutException(
126 'Sample for metric:%s with query:%s has not been added to the '
127 'database within %d seconds' % (metric, query,
128 CONF.compute.build_timeout))
gordon chungee23ddb2015-02-11 20:05:07 -0500129
130
131class BaseTelemetryAdminTest(BaseTelemetryTest):
132 """Base test case class for admin Telemetry API tests."""
133
134 credentials = ['primary', 'admin']
135
136 @classmethod
137 def setup_clients(cls):
138 super(BaseTelemetryAdminTest, cls).setup_clients()
139 cls.telemetry_admin_client = cls.os_adm.telemetry_client
140
141 def await_events(self, query):
142 timeout = CONF.compute.build_timeout
143 start = timeutils.utcnow()
144 while timeutils.delta_seconds(start, timeutils.utcnow()) < timeout:
145 body = self.telemetry_admin_client.list_events(query)
146 if body:
147 return body
148 time.sleep(CONF.compute.build_interval)
149
150 raise exceptions.TimeoutException(
151 'Event with query:%s has not been added to the '
152 'database within %d seconds' % (query, CONF.compute.build_timeout))