blob: 938bc880dc5054e2f4a063f5970c1a73b3dc70c7 [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
16from tempest_lib import exceptions as lib_exc
17
18from tempest.common import compute
19from tempest.common.utils import data_utils
20from tempest import config
21from tempest import exceptions
22import 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
31 credentials = ['primary']
32
33 @classmethod
34 def skip_checks(cls):
35 super(BaseTelemetryTest, cls).skip_checks()
36 if not CONF.service_available.ceilometer:
37 raise cls.skipException("Ceilometer support is required")
38
39 @classmethod
40 def setup_credentials(cls):
41 cls.set_network_resources()
42 super(BaseTelemetryTest, cls).setup_credentials()
43
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()
56 cls.nova_notifications = ['memory', 'vcpus', 'disk.root.size',
57 'disk.ephemeral.size']
58
59 cls.glance_notifications = ['image.size']
60
61 cls.glance_v2_notifications = ['image.download', 'image.serve']
62
63 cls.server_ids = []
64 cls.image_ids = []
65
66 @classmethod
67 def create_server(cls):
68 tenant_network = cls.get_tenant_network()
69 body, server = compute.create_test_server(
70 cls.os,
71 tenant_network=tenant_network,
72 name=data_utils.rand_name('ceilometer-instance'),
73 wait_until='ACTIVE')
74 cls.server_ids.append(body['id'])
75 return body
76
77 @classmethod
78 def create_image(cls, client):
79 body = client.create_image(
80 data_utils.rand_name('image'), container_format='bare',
81 disk_format='raw', visibility='private')
82 # TODO(jswarren) Move ['image'] up to initial body value assignment
83 # once both v1 and v2 glance clients include the full response
84 # object.
85 if 'image' in body:
86 body = body['image']
87 cls.image_ids.append(body['id'])
88 return body
89
90 @staticmethod
91 def cleanup_resources(method, list_of_ids):
92 for resource_id in list_of_ids:
93 try:
94 method(resource_id)
95 except lib_exc.NotFound:
96 pass
97
98 @classmethod
99 def resource_cleanup(cls):
100 cls.cleanup_resources(cls.servers_client.delete_server, cls.server_ids)
101 cls.cleanup_resources(cls.image_client.delete_image, cls.image_ids)
102 super(BaseTelemetryTest, cls).resource_cleanup()
103
104 def await_samples(self, metric, query):
105 """This method is to wait for sample to add it to database.
106
107 There are long time delays when using Postgresql (or Mysql)
108 database as ceilometer backend
109 """
110 timeout = CONF.compute.build_timeout
111 start = timeutils.utcnow()
112 while timeutils.delta_seconds(start, timeutils.utcnow()) < timeout:
113 body = self.telemetry_client.list_samples(metric, query)
114 if body:
115 return body
116 time.sleep(CONF.compute.build_interval)
117
118 raise exceptions.TimeoutException(
119 'Sample for metric:%s with query:%s has not been added to the '
120 'database within %d seconds' % (metric, query,
121 CONF.compute.build_timeout))
122
123
124class BaseTelemetryAdminTest(BaseTelemetryTest):
125 """Base test case class for admin Telemetry API tests."""
126
127 credentials = ['primary', 'admin']
128
129 @classmethod
130 def setup_clients(cls):
131 super(BaseTelemetryAdminTest, cls).setup_clients()
132 cls.telemetry_admin_client = cls.os_adm.telemetry_client
133
134 def await_events(self, query):
135 timeout = CONF.compute.build_timeout
136 start = timeutils.utcnow()
137 while timeutils.delta_seconds(start, timeutils.utcnow()) < timeout:
138 body = self.telemetry_admin_client.list_events(query)
139 if body:
140 return body
141 time.sleep(CONF.compute.build_interval)
142
143 raise exceptions.TimeoutException(
144 'Event with query:%s has not been added to the '
145 'database within %d seconds' % (query, CONF.compute.build_timeout))