blob: d19e025b4cea0e5f802f595b0c33883523ef510e [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',
Ryota MIBU08ff8672015-12-10 15:54:38 +090036 'image_client_v2',
37 'telemetry_client',
38 ]
39
40
Ryota MIBUf0832d52015-12-10 15:32:13 +090041class BaseTelemetryTest(tempest.test.BaseTestCase):
42
43 """Base test case class for all Telemetry API tests."""
44
45 credentials = ['primary']
Ryota MIBU08ff8672015-12-10 15:54:38 +090046 client_manager = ClientManager
Ryota MIBUf0832d52015-12-10 15:32:13 +090047
48 @classmethod
49 def skip_checks(cls):
50 super(BaseTelemetryTest, cls).skip_checks()
ghanshyam905e0812016-04-14 11:45:56 +090051 if not CONF.service_available.ceilometer:
Ryota MIBUf0832d52015-12-10 15:32:13 +090052 raise cls.skipException("Ceilometer support is required")
53
54 @classmethod
55 def setup_credentials(cls):
56 cls.set_network_resources()
57 super(BaseTelemetryTest, cls).setup_credentials()
58
59 @classmethod
60 def setup_clients(cls):
61 super(BaseTelemetryTest, cls).setup_clients()
Ryota MIBU08ff8672015-12-10 15:54:38 +090062 cls.telemetry_client = cls.os_primary.telemetry_client
63 cls.servers_client = cls.os_primary.servers_client
64 cls.flavors_client = cls.os_primary.flavors_client
Ryota MIBU08ff8672015-12-10 15:54:38 +090065 cls.image_client_v2 = cls.os_primary.image_client_v2
Ryota MIBUf0832d52015-12-10 15:32:13 +090066
67 @classmethod
68 def resource_setup(cls):
69 super(BaseTelemetryTest, cls).resource_setup()
70 cls.nova_notifications = ['memory', 'vcpus', 'disk.root.size',
71 'disk.ephemeral.size']
72
Ryota MIBUf0832d52015-12-10 15:32:13 +090073 cls.glance_v2_notifications = ['image.download', 'image.serve']
74
75 cls.server_ids = []
76 cls.image_ids = []
77
78 @classmethod
79 def create_server(cls):
80 tenant_network = cls.get_tenant_network()
81 body, server = compute.create_test_server(
Ryota MIBU08ff8672015-12-10 15:54:38 +090082 cls.os_primary,
Ryota MIBUf0832d52015-12-10 15:32:13 +090083 tenant_network=tenant_network,
84 name=data_utils.rand_name('ceilometer-instance'),
85 wait_until='ACTIVE')
86 cls.server_ids.append(body['id'])
87 return body
88
89 @classmethod
Ryota MIBU08ff8672015-12-10 15:54:38 +090090 def create_image(cls, client, **kwargs):
91 body = client.create_image(name=data_utils.rand_name('image'),
92 container_format='bare',
93 disk_format='raw',
94 **kwargs)
Ryota MIBUf0832d52015-12-10 15:32:13 +090095 # TODO(jswarren) Move ['image'] up to initial body value assignment
96 # once both v1 and v2 glance clients include the full response
97 # object.
98 if 'image' in body:
99 body = body['image']
100 cls.image_ids.append(body['id'])
101 return body
102
103 @staticmethod
104 def cleanup_resources(method, list_of_ids):
105 for resource_id in list_of_ids:
106 try:
107 method(resource_id)
108 except lib_exc.NotFound:
109 pass
110
111 @classmethod
112 def resource_cleanup(cls):
113 cls.cleanup_resources(cls.servers_client.delete_server, cls.server_ids)
liushenge65b3912016-11-17 15:22:39 +0800114 cls.cleanup_resources(cls.image_client_v2.delete_image, cls.image_ids)
Ryota MIBUf0832d52015-12-10 15:32:13 +0900115 super(BaseTelemetryTest, cls).resource_cleanup()
116
117 def await_samples(self, metric, query):
118 """This method is to wait for sample to add it to database.
119
120 There are long time delays when using Postgresql (or Mysql)
121 database as ceilometer backend
122 """
123 timeout = CONF.compute.build_timeout
124 start = timeutils.utcnow()
125 while timeutils.delta_seconds(start, timeutils.utcnow()) < timeout:
126 body = self.telemetry_client.list_samples(metric, query)
127 if body:
128 return body
129 time.sleep(CONF.compute.build_interval)
130
131 raise exceptions.TimeoutException(
132 'Sample for metric:%s with query:%s has not been added to the '
133 'database within %d seconds' % (metric, query,
134 CONF.compute.build_timeout))
135
136
137class BaseTelemetryAdminTest(BaseTelemetryTest):
138 """Base test case class for admin Telemetry API tests."""
139
140 credentials = ['primary', 'admin']
141
142 @classmethod
143 def setup_clients(cls):
144 super(BaseTelemetryAdminTest, cls).setup_clients()
145 cls.telemetry_admin_client = cls.os_adm.telemetry_client