blob: 8f076143828a46476f34f75675e56dff009b5f86 [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
Yassine Lamgarchal05638762013-12-27 20:10:32 +010020from tempest import config
vrovachev95a16cc2014-02-04 15:29:48 +040021from tempest import exceptions
Yassine Lamgarchal05638762013-12-27 20:10:32 +010022import tempest.test
23
24CONF = config.CONF
25
26
27class BaseTelemetryTest(tempest.test.BaseTestCase):
28
29 """Base test case class for all Telemetry API tests."""
30
Andrea Frittolib21de6c2015-02-06 20:12:38 +000031 credentials = ['primary']
32
Yassine Lamgarchal05638762013-12-27 20:10:32 +010033 @classmethod
Rohan Kanaded114a132015-02-07 11:11:16 +053034 def skip_checks(cls):
35 super(BaseTelemetryTest, cls).skip_checks()
Yassine Lamgarchal05638762013-12-27 20:10:32 +010036 if not CONF.service_available.ceilometer:
37 raise cls.skipException("Ceilometer support is required")
Vadim Rovachev7bcea352013-12-26 15:56:17 +040038
Rohan Kanaded114a132015-02-07 11:11:16 +053039 @classmethod
40 def setup_credentials(cls):
41 cls.set_network_resources()
42 super(BaseTelemetryTest, cls).setup_credentials()
Rohan Kanaded114a132015-02-07 11:11:16 +053043
44 @classmethod
45 def setup_clients(cls):
46 super(BaseTelemetryTest, cls).setup_clients()
47 cls.telemetry_client = cls.os.telemetry_client
48 cls.servers_client = cls.os.servers_client
49 cls.flavors_client = cls.os.flavors_client
50 cls.image_client = cls.os.image_client
51 cls.image_client_v2 = cls.os.image_client_v2
52
53 @classmethod
54 def resource_setup(cls):
55 super(BaseTelemetryTest, cls).resource_setup()
Vadim Rovachev7bcea352013-12-26 15:56:17 +040056 cls.nova_notifications = ['memory', 'vcpus', 'disk.root.size',
57 'disk.ephemeral.size']
Vadim Rovachev80ee4e12014-02-05 16:59:07 +040058
gord chunge10a3e42015-06-13 00:36:59 -040059 cls.glance_notifications = ['image.size']
Vadim Rovachev80ee4e12014-02-05 16:59:07 +040060
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):
Joseph Lanoux2a15fe62015-07-31 14:09:58 +000077 tenant_network = cls.get_tenant_network()
78 body, server = compute.create_test_server(
79 cls.os,
80 tenant_network=tenant_network,
81 name=data_utils.rand_name('ceilometer-instance'),
Vadim Rovachev7bcea352013-12-26 15:56:17 +040082 wait_until='ACTIVE')
Vadim Rovachev80ee4e12014-02-05 16:59:07 +040083 cls.server_ids.append(body['id'])
David Kranz0fb14292015-02-11 15:55:20 -050084 return body
Vadim Rovachev80ee4e12014-02-05 16:59:07 +040085
86 @classmethod
87 def create_image(cls, client):
David Kranza5299eb2015-01-15 17:24:05 -050088 body = client.create_image(
Vadim Rovachev80ee4e12014-02-05 16:59:07 +040089 data_utils.rand_name('image'), container_format='bare',
90 disk_format='raw', visibility='private')
John Warren66207252015-07-31 15:51:02 -040091 # TODO(jswarren) Move ['image'] up to initial body value assignment
92 # once both v1 and v2 glance clients include the full response
93 # object.
94 if 'image' in body:
95 body = body['image']
Vadim Rovachev80ee4e12014-02-05 16:59:07 +040096 cls.image_ids.append(body['id'])
David Kranza5299eb2015-01-15 17:24:05 -050097 return body
Vadim Rovachev7bcea352013-12-26 15:56:17 +040098
99 @staticmethod
100 def cleanup_resources(method, list_of_ids):
101 for resource_id in list_of_ids:
vrovachev95a16cc2014-02-04 15:29:48 +0400102 try:
Vadim Rovachev7bcea352013-12-26 15:56:17 +0400103 method(resource_id)
Masayuki Igawabfa07602015-01-20 18:47:17 +0900104 except lib_exc.NotFound:
vrovachev95a16cc2014-02-04 15:29:48 +0400105 pass
Vadim Rovachev7bcea352013-12-26 15:56:17 +0400106
107 @classmethod
Andrea Frittolic76d04d2014-09-15 13:14:54 +0100108 def resource_cleanup(cls):
Vadim Rovachev7bcea352013-12-26 15:56:17 +0400109 cls.cleanup_resources(cls.telemetry_client.delete_alarm, cls.alarm_ids)
110 cls.cleanup_resources(cls.servers_client.delete_server, cls.server_ids)
Vadim Rovachev80ee4e12014-02-05 16:59:07 +0400111 cls.cleanup_resources(cls.image_client.delete_image, cls.image_ids)
Andrea Frittolic76d04d2014-09-15 13:14:54 +0100112 super(BaseTelemetryTest, cls).resource_cleanup()
Vadim Rovachev7bcea352013-12-26 15:56:17 +0400113
114 def await_samples(self, metric, query):
115 """
116 This method is to wait for sample to add it to database.
117 There are long time delays when using Postgresql (or Mysql)
118 database as ceilometer backend
119 """
120 timeout = CONF.compute.build_timeout
121 start = timeutils.utcnow()
122 while timeutils.delta_seconds(start, timeutils.utcnow()) < timeout:
David Kranz20d06f42015-02-09 14:54:15 -0500123 body = self.telemetry_client.list_samples(metric, query)
Vadim Rovachev7bcea352013-12-26 15:56:17 +0400124 if body:
David Kranz20d06f42015-02-09 14:54:15 -0500125 return body
Vadim Rovachev7bcea352013-12-26 15:56:17 +0400126 time.sleep(CONF.compute.build_interval)
127
128 raise exceptions.TimeoutException(
129 'Sample for metric:%s with query:%s has not been added to the '
130 'database within %d seconds' % (metric, query,
131 CONF.compute.build_timeout))
gordon chungee23ddb2015-02-11 20:05:07 -0500132
133
134class BaseTelemetryAdminTest(BaseTelemetryTest):
135 """Base test case class for admin Telemetry API tests."""
136
137 credentials = ['primary', 'admin']
138
139 @classmethod
140 def setup_clients(cls):
141 super(BaseTelemetryAdminTest, cls).setup_clients()
142 cls.telemetry_admin_client = cls.os_adm.telemetry_client
143
144 def await_events(self, query):
145 timeout = CONF.compute.build_timeout
146 start = timeutils.utcnow()
147 while timeutils.delta_seconds(start, timeutils.utcnow()) < timeout:
148 body = self.telemetry_admin_client.list_events(query)
149 if body:
150 return body
151 time.sleep(CONF.compute.build_interval)
152
153 raise exceptions.TimeoutException(
154 'Event with query:%s has not been added to the '
155 'database within %d seconds' % (query, CONF.compute.build_timeout))