Mauro S. M. Rodrigues | 92ed9b8 | 2013-12-17 07:21:48 -0500 | [diff] [blame] | 1 | # Copyright 2013 IBM Corp. |
| 2 | # All Rights Reserved. |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 5 | # not use this file except in compliance with the License. You may obtain |
| 6 | # a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | # License for the specific language governing permissions and limitations |
| 14 | # under the License. |
| 15 | |
| 16 | from tempest.common import tempest_fixtures as fixtures |
| 17 | from tempest.common.utils.data_utils import rand_name |
| 18 | from tempest.openstack.common import log as logging |
| 19 | from tempest.scenario import manager |
| 20 | from tempest import test |
| 21 | |
| 22 | |
| 23 | LOG = logging.getLogger(__name__) |
| 24 | |
| 25 | |
| 26 | class TestAggregatesBasicOps(manager.OfficialClientTest): |
| 27 | """ |
| 28 | Creates an aggregate within an availability zone |
| 29 | Adds a host to the aggregate |
| 30 | Checks aggregate details |
| 31 | Updates aggregate's name |
| 32 | Removes host from aggregate |
| 33 | Deletes aggregate |
| 34 | """ |
| 35 | @classmethod |
| 36 | def credentials(cls): |
| 37 | return cls.admin_credentials() |
| 38 | |
| 39 | def _create_aggregate(self, aggregate_name, availability_zone=None): |
| 40 | aggregate = self.compute_client.aggregates.create(aggregate_name, |
| 41 | availability_zone) |
| 42 | self.assertEqual(aggregate.name, aggregate_name) |
| 43 | self.assertEqual(aggregate.availability_zone, availability_zone) |
| 44 | self.set_resource(aggregate.id, aggregate) |
| 45 | LOG.debug("Aggregate %s created." % (aggregate.name)) |
| 46 | return aggregate |
| 47 | |
| 48 | def _delete_aggregate(self, aggregate): |
| 49 | self.compute_client.aggregates.delete(aggregate.id) |
| 50 | self.remove_resource(aggregate.id) |
| 51 | LOG.debug("Aggregate %s deleted. " % (aggregate.name)) |
| 52 | |
| 53 | def _get_host_name(self): |
| 54 | hosts = self.compute_client.hosts.list() |
| 55 | self.assertTrue(len(hosts) >= 1) |
| 56 | hostname = hosts[0].host_name |
| 57 | return hostname |
| 58 | |
| 59 | def _add_host(self, aggregate_name, host): |
| 60 | aggregate = self.compute_client.aggregates.add_host(aggregate_name, |
| 61 | host) |
| 62 | self.assertIn(host, aggregate.hosts) |
| 63 | LOG.debug("Host %s added to Aggregate %s." % (host, aggregate.name)) |
| 64 | |
| 65 | def _remove_host(self, aggregate_name, host): |
| 66 | aggregate = self.compute_client.aggregates.remove_host(aggregate_name, |
| 67 | host) |
| 68 | self.assertNotIn(host, aggregate.hosts) |
| 69 | LOG.debug("Host %s removed to Aggregate %s." % (host, aggregate.name)) |
| 70 | |
| 71 | def _check_aggregate_details(self, aggregate, aggregate_name, azone, |
| 72 | hosts, metadata): |
| 73 | aggregate = self.compute_client.aggregates.get(aggregate.id) |
| 74 | self.assertEqual(aggregate_name, aggregate.name) |
| 75 | self.assertEqual(azone, aggregate.availability_zone) |
| 76 | self.assertEqual(aggregate.hosts, hosts) |
| 77 | for meta_key in metadata.keys(): |
| 78 | self.assertIn(meta_key, aggregate.metadata) |
| 79 | self.assertEqual(metadata[meta_key], aggregate.metadata[meta_key]) |
| 80 | LOG.debug("Aggregate %s details match." % aggregate.name) |
| 81 | |
| 82 | def _set_aggregate_metadata(self, aggregate, meta): |
| 83 | aggregate = self.compute_client.aggregates.set_metadata(aggregate.id, |
| 84 | meta) |
| 85 | |
| 86 | for key, value in meta.items(): |
| 87 | self.assertEqual(meta[key], aggregate.metadata[key]) |
| 88 | LOG.debug("Aggregate %s metadata updated successfully." % |
| 89 | aggregate.name) |
| 90 | |
| 91 | def _update_aggregate(self, aggregate, aggregate_name, |
| 92 | availability_zone): |
| 93 | values = {} |
| 94 | if aggregate_name: |
| 95 | values.update({'name': aggregate_name}) |
| 96 | if availability_zone: |
| 97 | values.update({'availability_zone': availability_zone}) |
| 98 | if values.keys(): |
| 99 | aggregate = self.compute_client.aggregates.update(aggregate.id, |
| 100 | values) |
| 101 | for key, values in values.items(): |
| 102 | self.assertEqual(getattr(aggregate, key), values) |
| 103 | return aggregate |
| 104 | |
| 105 | @test.services('compute') |
| 106 | def test_aggregate_basic_ops(self): |
| 107 | self.useFixture(fixtures.LockFixture('availability_zone')) |
| 108 | az = 'foo_zone' |
| 109 | aggregate_name = rand_name('aggregate-scenario') |
| 110 | aggregate = self._create_aggregate(aggregate_name, az) |
| 111 | |
| 112 | metadata = {'meta_key': 'meta_value'} |
| 113 | self._set_aggregate_metadata(aggregate, metadata) |
| 114 | |
| 115 | host = self._get_host_name() |
| 116 | self._add_host(aggregate, host) |
| 117 | self._check_aggregate_details(aggregate, aggregate_name, az, [host], |
| 118 | metadata) |
| 119 | |
| 120 | aggregate_name = rand_name('renamed-aggregate-scenario') |
| 121 | aggregate = self._update_aggregate(aggregate, aggregate_name, None) |
| 122 | |
| 123 | additional_metadata = {'foo': 'bar'} |
| 124 | self._set_aggregate_metadata(aggregate, additional_metadata) |
| 125 | |
| 126 | metadata.update(additional_metadata) |
| 127 | self._check_aggregate_details(aggregate, aggregate.name, az, [host], |
| 128 | metadata) |
| 129 | |
| 130 | self._remove_host(aggregate, host) |
| 131 | self._delete_aggregate(aggregate) |