blob: 81c53d64ca3a608bf7c3ac83981c4a8d3ed43a94 [file] [log] [blame]
Ryota MIBUf0832d52015-12-10 15:32:13 +09001# 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
13import time
14
15from oslo_utils import timeutils
Ryota MIBUf0832d52015-12-10 15:32:13 +090016from tempest.common import compute
17from tempest.common.utils import data_utils
18from tempest import config
19from tempest import exceptions
Ryota MIBU08ff8672015-12-10 15:54:38 +090020from tempest.lib import exceptions as lib_exc
Ryota MIBUf0832d52015-12-10 15:32:13 +090021import tempest.test
22
Ryota MIBU08ff8672015-12-10 15:54:38 +090023from ceilometer.tests.tempest.service import client
24
25
Ryota MIBUf0832d52015-12-10 15:32:13 +090026CONF = config.CONF
27
28
Ryota MIBU08ff8672015-12-10 15:54:38 +090029class ClientManager(client.Manager):
30
31 load_clients = [
32 'servers_client',
33 'compute_networks_client',
34 'compute_floating_ips_client',
35 'flavors_client',
36 'image_client',
37 'image_client_v2',
38 'telemetry_client',
39 ]
40
41
Ryota MIBUf0832d52015-12-10 15:32:13 +090042class BaseTelemetryTest(tempest.test.BaseTestCase):
43
44 """Base test case class for all Telemetry API tests."""
45
46 credentials = ['primary']
Ryota MIBU08ff8672015-12-10 15:54:38 +090047 client_manager = ClientManager
Ryota MIBUf0832d52015-12-10 15:32:13 +090048
49 @classmethod
50 def skip_checks(cls):
51 super(BaseTelemetryTest, cls).skip_checks()
ghanshyam905e0812016-04-14 11:45:56 +090052 if not CONF.service_available.ceilometer:
Ryota MIBUf0832d52015-12-10 15:32:13 +090053 raise cls.skipException("Ceilometer support is required")
54
55 @classmethod
56 def setup_credentials(cls):
57 cls.set_network_resources()
58 super(BaseTelemetryTest, cls).setup_credentials()
59
60 @classmethod
61 def setup_clients(cls):
62 super(BaseTelemetryTest, cls).setup_clients()
Ryota MIBU08ff8672015-12-10 15:54:38 +090063 cls.telemetry_client = cls.os_primary.telemetry_client
64 cls.servers_client = cls.os_primary.servers_client
65 cls.flavors_client = cls.os_primary.flavors_client
66 cls.image_client = cls.os_primary.image_client
67 cls.image_client_v2 = cls.os_primary.image_client_v2
Ryota MIBUf0832d52015-12-10 15:32:13 +090068
69 @classmethod
70 def resource_setup(cls):
71 super(BaseTelemetryTest, cls).resource_setup()
72 cls.nova_notifications = ['memory', 'vcpus', 'disk.root.size',
73 'disk.ephemeral.size']
74
75 cls.glance_notifications = ['image.size']
76
77 cls.glance_v2_notifications = ['image.download', 'image.serve']
78
79 cls.server_ids = []
80 cls.image_ids = []
81
82 @classmethod
83 def create_server(cls):
84 tenant_network = cls.get_tenant_network()
85 body, server = compute.create_test_server(
Ryota MIBU08ff8672015-12-10 15:54:38 +090086 cls.os_primary,
Ryota MIBUf0832d52015-12-10 15:32:13 +090087 tenant_network=tenant_network,
88 name=data_utils.rand_name('ceilometer-instance'),
89 wait_until='ACTIVE')
90 cls.server_ids.append(body['id'])
91 return body
92
93 @classmethod
Ryota MIBU08ff8672015-12-10 15:54:38 +090094 def create_image(cls, client, **kwargs):
95 body = client.create_image(name=data_utils.rand_name('image'),
96 container_format='bare',
97 disk_format='raw',
98 **kwargs)
Ryota MIBUf0832d52015-12-10 15:32:13 +090099 # TODO(jswarren) Move ['image'] up to initial body value assignment
100 # once both v1 and v2 glance clients include the full response
101 # object.
102 if 'image' in body:
103 body = body['image']
104 cls.image_ids.append(body['id'])
105 return body
106
107 @staticmethod
108 def cleanup_resources(method, list_of_ids):
109 for resource_id in list_of_ids:
110 try:
111 method(resource_id)
112 except lib_exc.NotFound:
113 pass
114
115 @classmethod
116 def resource_cleanup(cls):
117 cls.cleanup_resources(cls.servers_client.delete_server, cls.server_ids)
118 cls.cleanup_resources(cls.image_client.delete_image, cls.image_ids)
119 super(BaseTelemetryTest, cls).resource_cleanup()
120
121 def await_samples(self, metric, query):
122 """This method is to wait for sample to add it to database.
123
124 There are long time delays when using Postgresql (or Mysql)
125 database as ceilometer backend
126 """
127 timeout = CONF.compute.build_timeout
128 start = timeutils.utcnow()
129 while timeutils.delta_seconds(start, timeutils.utcnow()) < timeout:
130 body = self.telemetry_client.list_samples(metric, query)
131 if body:
132 return body
133 time.sleep(CONF.compute.build_interval)
134
135 raise exceptions.TimeoutException(
136 'Sample for metric:%s with query:%s has not been added to the '
137 'database within %d seconds' % (metric, query,
138 CONF.compute.build_timeout))
139
140
141class BaseTelemetryAdminTest(BaseTelemetryTest):
142 """Base test case class for admin Telemetry API tests."""
143
144 credentials = ['primary', 'admin']
145
146 @classmethod
147 def setup_clients(cls):
148 super(BaseTelemetryAdminTest, cls).setup_clients()
149 cls.telemetry_admin_client = cls.os_adm.telemetry_client
150
151 def await_events(self, query):
152 timeout = CONF.compute.build_timeout
153 start = timeutils.utcnow()
154 while timeutils.delta_seconds(start, timeutils.utcnow()) < timeout:
155 body = self.telemetry_admin_client.list_events(query)
156 if body:
157 return body
158 time.sleep(CONF.compute.build_interval)
159
160 raise exceptions.TimeoutException(
161 'Event with query:%s has not been added to the '
162 'database within %d seconds' % (query, CONF.compute.build_timeout))