blob: 03e9acdd6ff0d076ac20a33cd9c501f742e84641 [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
Ryota MIBU08ff8672015-12-10 15:54:38 +090019from tempest.lib import exceptions as lib_exc
Ryota MIBUf0832d52015-12-10 15:32:13 +090020import tempest.test
21
Ryota MIBU08ff8672015-12-10 15:54:38 +090022from ceilometer.tests.tempest.service import client
23
24
Ryota MIBUf0832d52015-12-10 15:32:13 +090025CONF = config.CONF
26
27
Ryota MIBU08ff8672015-12-10 15:54:38 +090028class ClientManager(client.Manager):
29
30 load_clients = [
31 'servers_client',
32 'compute_networks_client',
33 'compute_floating_ips_client',
34 'flavors_client',
Ryota MIBU08ff8672015-12-10 15:54:38 +090035 'image_client_v2',
36 'telemetry_client',
37 ]
38
39
Ryota MIBUf0832d52015-12-10 15:32:13 +090040class BaseTelemetryTest(tempest.test.BaseTestCase):
41
42 """Base test case class for all Telemetry API tests."""
43
44 credentials = ['primary']
Ryota MIBU08ff8672015-12-10 15:54:38 +090045 client_manager = ClientManager
Ryota MIBUf0832d52015-12-10 15:32:13 +090046
47 @classmethod
48 def skip_checks(cls):
49 super(BaseTelemetryTest, cls).skip_checks()
ghanshyam905e0812016-04-14 11:45:56 +090050 if not CONF.service_available.ceilometer:
Ryota MIBUf0832d52015-12-10 15:32:13 +090051 raise cls.skipException("Ceilometer support is required")
52
53 @classmethod
54 def setup_credentials(cls):
55 cls.set_network_resources()
56 super(BaseTelemetryTest, cls).setup_credentials()
57
58 @classmethod
59 def setup_clients(cls):
60 super(BaseTelemetryTest, cls).setup_clients()
Ryota MIBU08ff8672015-12-10 15:54:38 +090061 cls.telemetry_client = cls.os_primary.telemetry_client
62 cls.servers_client = cls.os_primary.servers_client
63 cls.flavors_client = cls.os_primary.flavors_client
Ryota MIBU08ff8672015-12-10 15:54:38 +090064 cls.image_client_v2 = cls.os_primary.image_client_v2
Ryota MIBUf0832d52015-12-10 15:32:13 +090065
66 @classmethod
67 def resource_setup(cls):
68 super(BaseTelemetryTest, cls).resource_setup()
69 cls.nova_notifications = ['memory', 'vcpus', 'disk.root.size',
70 'disk.ephemeral.size']
71
Ryota MIBUf0832d52015-12-10 15:32:13 +090072 cls.glance_v2_notifications = ['image.download', 'image.serve']
73
74 cls.server_ids = []
75 cls.image_ids = []
76
77 @classmethod
78 def create_server(cls):
79 tenant_network = cls.get_tenant_network()
80 body, server = compute.create_test_server(
Ryota MIBU08ff8672015-12-10 15:54:38 +090081 cls.os_primary,
Ryota MIBUf0832d52015-12-10 15:32:13 +090082 tenant_network=tenant_network,
83 name=data_utils.rand_name('ceilometer-instance'),
84 wait_until='ACTIVE')
85 cls.server_ids.append(body['id'])
86 return body
87
88 @classmethod
Ryota MIBU08ff8672015-12-10 15:54:38 +090089 def create_image(cls, client, **kwargs):
90 body = client.create_image(name=data_utils.rand_name('image'),
91 container_format='bare',
92 disk_format='raw',
93 **kwargs)
Ryota MIBUf0832d52015-12-10 15:32:13 +090094 # TODO(jswarren) Move ['image'] up to initial body value assignment
95 # once both v1 and v2 glance clients include the full response
96 # object.
97 if 'image' in body:
98 body = body['image']
99 cls.image_ids.append(body['id'])
100 return body
101
102 @staticmethod
103 def cleanup_resources(method, list_of_ids):
104 for resource_id in list_of_ids:
105 try:
106 method(resource_id)
107 except lib_exc.NotFound:
108 pass
109
110 @classmethod
111 def resource_cleanup(cls):
112 cls.cleanup_resources(cls.servers_client.delete_server, cls.server_ids)
liushenge65b3912016-11-17 15:22:39 +0800113 cls.cleanup_resources(cls.image_client_v2.delete_image, cls.image_ids)
Ryota MIBUf0832d52015-12-10 15:32:13 +0900114 super(BaseTelemetryTest, cls).resource_cleanup()
115
116 def await_samples(self, metric, query):
117 """This method is to wait for sample to add it to database.
118
119 There are long time delays when using Postgresql (or Mysql)
120 database as ceilometer backend
121 """
122 timeout = CONF.compute.build_timeout
123 start = timeutils.utcnow()
124 while timeutils.delta_seconds(start, timeutils.utcnow()) < timeout:
125 body = self.telemetry_client.list_samples(metric, query)
126 if body:
127 return body
128 time.sleep(CONF.compute.build_interval)
129
Hanxi Liu3146cd92016-12-22 17:08:48 +0800130 raise lib_exc.TimeoutException(
Ryota MIBUf0832d52015-12-10 15:32:13 +0900131 'Sample for metric:%s with query:%s has not been added to the '
132 'database within %d seconds' % (metric, query,
133 CONF.compute.build_timeout))
134
135
136class BaseTelemetryAdminTest(BaseTelemetryTest):
137 """Base test case class for admin Telemetry API tests."""
138
139 credentials = ['primary', 'admin']
140
141 @classmethod
142 def setup_clients(cls):
143 super(BaseTelemetryAdminTest, cls).setup_clients()
144 cls.telemetry_admin_client = cls.os_adm.telemetry_client