Yassine Lamgarchal | 0563876 | 2013-12-27 20:10:32 +0100 | [diff] [blame] | 1 | # 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 Rovachev | 7bcea35 | 2013-12-26 15:56:17 +0400 | [diff] [blame] | 13 | import time |
| 14 | |
vrovachev | 95a16cc | 2014-02-04 15:29:48 +0400 | [diff] [blame] | 15 | from tempest.common.utils import data_utils |
Yassine Lamgarchal | 0563876 | 2013-12-27 20:10:32 +0100 | [diff] [blame] | 16 | from tempest import config |
vrovachev | 95a16cc | 2014-02-04 15:29:48 +0400 | [diff] [blame] | 17 | from tempest import exceptions |
Vadim Rovachev | 7bcea35 | 2013-12-26 15:56:17 +0400 | [diff] [blame] | 18 | from tempest.openstack.common import timeutils |
Yassine Lamgarchal | 0563876 | 2013-12-27 20:10:32 +0100 | [diff] [blame] | 19 | import tempest.test |
| 20 | |
| 21 | CONF = config.CONF |
| 22 | |
| 23 | |
| 24 | class BaseTelemetryTest(tempest.test.BaseTestCase): |
| 25 | |
| 26 | """Base test case class for all Telemetry API tests.""" |
| 27 | |
| 28 | @classmethod |
| 29 | def setUpClass(cls): |
Yassine Lamgarchal | 0563876 | 2013-12-27 20:10:32 +0100 | [diff] [blame] | 30 | if not CONF.service_available.ceilometer: |
| 31 | raise cls.skipException("Ceilometer support is required") |
vrovachev | 95a16cc | 2014-02-04 15:29:48 +0400 | [diff] [blame] | 32 | super(BaseTelemetryTest, cls).setUpClass() |
| 33 | os = cls.get_client_manager() |
| 34 | cls.telemetry_client = os.telemetry_client |
Vadim Rovachev | 7bcea35 | 2013-12-26 15:56:17 +0400 | [diff] [blame] | 35 | cls.servers_client = os.servers_client |
| 36 | cls.flavors_client = os.flavors_client |
| 37 | |
| 38 | cls.nova_notifications = ['memory', 'vcpus', 'disk.root.size', |
| 39 | 'disk.ephemeral.size'] |
| 40 | cls.server_ids = [] |
vrovachev | 95a16cc | 2014-02-04 15:29:48 +0400 | [diff] [blame] | 41 | cls.alarm_ids = [] |
| 42 | |
| 43 | @classmethod |
| 44 | def create_alarm(cls, **kwargs): |
| 45 | resp, body = cls.telemetry_client.create_alarm( |
| 46 | name=data_utils.rand_name('telemetry_alarm'), |
| 47 | type='threshold', **kwargs) |
| 48 | if resp['status'] == '201': |
| 49 | cls.alarm_ids.append(body['alarm_id']) |
| 50 | return resp, body |
| 51 | |
| 52 | @classmethod |
Vadim Rovachev | 7bcea35 | 2013-12-26 15:56:17 +0400 | [diff] [blame] | 53 | def create_server(cls): |
| 54 | resp, body = cls.servers_client.create_server( |
| 55 | data_utils.rand_name('ceilometer-instance'), |
| 56 | CONF.compute.image_ref, CONF.compute.flavor_ref, |
| 57 | wait_until='ACTIVE') |
| 58 | if resp['status'] == '202': |
| 59 | cls.server_ids.append(body['id']) |
| 60 | return resp, body |
| 61 | |
| 62 | @staticmethod |
| 63 | def cleanup_resources(method, list_of_ids): |
| 64 | for resource_id in list_of_ids: |
vrovachev | 95a16cc | 2014-02-04 15:29:48 +0400 | [diff] [blame] | 65 | try: |
Vadim Rovachev | 7bcea35 | 2013-12-26 15:56:17 +0400 | [diff] [blame] | 66 | method(resource_id) |
vrovachev | 95a16cc | 2014-02-04 15:29:48 +0400 | [diff] [blame] | 67 | except exceptions.NotFound: |
| 68 | pass |
Vadim Rovachev | 7bcea35 | 2013-12-26 15:56:17 +0400 | [diff] [blame] | 69 | |
| 70 | @classmethod |
| 71 | def tearDownClass(cls): |
| 72 | cls.cleanup_resources(cls.telemetry_client.delete_alarm, cls.alarm_ids) |
| 73 | cls.cleanup_resources(cls.servers_client.delete_server, cls.server_ids) |
vrovachev | 95a16cc | 2014-02-04 15:29:48 +0400 | [diff] [blame] | 74 | cls.clear_isolated_creds() |
| 75 | super(BaseTelemetryTest, cls).tearDownClass() |
Vadim Rovachev | 7bcea35 | 2013-12-26 15:56:17 +0400 | [diff] [blame] | 76 | |
| 77 | def await_samples(self, metric, query): |
| 78 | """ |
| 79 | This method is to wait for sample to add it to database. |
| 80 | There are long time delays when using Postgresql (or Mysql) |
| 81 | database as ceilometer backend |
| 82 | """ |
| 83 | timeout = CONF.compute.build_timeout |
| 84 | start = timeutils.utcnow() |
| 85 | while timeutils.delta_seconds(start, timeutils.utcnow()) < timeout: |
| 86 | resp, body = self.telemetry_client.list_samples(metric, query) |
| 87 | self.assertEqual(resp.status, 200) |
| 88 | if body: |
| 89 | return resp, body |
| 90 | time.sleep(CONF.compute.build_interval) |
| 91 | |
| 92 | raise exceptions.TimeoutException( |
| 93 | 'Sample for metric:%s with query:%s has not been added to the ' |
| 94 | 'database within %d seconds' % (metric, query, |
| 95 | CONF.compute.build_timeout)) |