blob: 3ad5c6982dcd4faa17be82b33ac089076dd091ef [file] [log] [blame]
Mauro S. M. Rodrigues92ed9b82013-12-17 07:21:48 -05001# 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
16from tempest.common import tempest_fixtures as fixtures
Masayuki Igawa4ded9f02014-02-17 15:05:59 +090017from tempest.common.utils import data_utils
Mauro S. M. Rodrigues92ed9b82013-12-17 07:21:48 -050018from tempest.openstack.common import log as logging
19from tempest.scenario import manager
20from tempest import test
21
22
23LOG = logging.getLogger(__name__)
24
25
Masayuki Igawaccd66592014-07-17 00:42:42 +090026class TestAggregatesBasicOps(manager.ScenarioTest):
Mauro S. M. Rodrigues92ed9b82013-12-17 07:21:48 -050027 """
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
Masayuki Igawaccd66592014-07-17 00:42:42 +090036 def setUpClass(cls):
37 super(TestAggregatesBasicOps, cls).setUpClass()
38 cls.aggregates_client = cls.manager.aggregates_client
39 cls.hosts_client = cls.manager.hosts_client
40
41 @classmethod
Mauro S. M. Rodrigues92ed9b82013-12-17 07:21:48 -050042 def credentials(cls):
43 return cls.admin_credentials()
44
Yuiko Takadaf93d2482014-01-30 16:25:08 +000045 def _create_aggregate(self, **kwargs):
Masayuki Igawaccd66592014-07-17 00:42:42 +090046 _, aggregate = self.aggregates_client.create_aggregate(**kwargs)
47 self.addCleanup(self._delete_aggregate, aggregate)
Yuiko Takadaf93d2482014-01-30 16:25:08 +000048 aggregate_name = kwargs['name']
49 availability_zone = kwargs['availability_zone']
Masayuki Igawaccd66592014-07-17 00:42:42 +090050 self.assertEqual(aggregate['name'], aggregate_name)
51 self.assertEqual(aggregate['availability_zone'], availability_zone)
Mauro S. M. Rodrigues92ed9b82013-12-17 07:21:48 -050052 return aggregate
53
54 def _delete_aggregate(self, aggregate):
Masayuki Igawaccd66592014-07-17 00:42:42 +090055 self.aggregates_client.delete_aggregate(aggregate['id'])
Mauro S. M. Rodrigues92ed9b82013-12-17 07:21:48 -050056
57 def _get_host_name(self):
Masayuki Igawaccd66592014-07-17 00:42:42 +090058 _, hosts = self.hosts_client.list_hosts()
Mauro S. M. Rodrigues92ed9b82013-12-17 07:21:48 -050059 self.assertTrue(len(hosts) >= 1)
Masayuki Igawaccd66592014-07-17 00:42:42 +090060 computes = [x for x in hosts if x['service'] == 'compute']
61 return computes[0]['host_name']
Mauro S. M. Rodrigues92ed9b82013-12-17 07:21:48 -050062
Masayuki Igawaccd66592014-07-17 00:42:42 +090063 def _add_host(self, aggregate_id, host):
64 _, aggregate = self.aggregates_client.add_host(aggregate_id, host)
65 self.addCleanup(self._remove_host, aggregate['id'], host)
66 self.assertIn(host, aggregate['hosts'])
Mauro S. M. Rodrigues92ed9b82013-12-17 07:21:48 -050067
Masayuki Igawaccd66592014-07-17 00:42:42 +090068 def _remove_host(self, aggregate_id, host):
69 _, aggregate = self.aggregates_client.remove_host(aggregate_id, host)
70 self.assertNotIn(host, aggregate['hosts'])
Mauro S. M. Rodrigues92ed9b82013-12-17 07:21:48 -050071
72 def _check_aggregate_details(self, aggregate, aggregate_name, azone,
73 hosts, metadata):
Masayuki Igawaccd66592014-07-17 00:42:42 +090074 _, aggregate = self.aggregates_client.get_aggregate(aggregate['id'])
75 self.assertEqual(aggregate_name, aggregate['name'])
76 self.assertEqual(azone, aggregate['availability_zone'])
77 self.assertEqual(hosts, aggregate['hosts'])
Mauro S. M. Rodrigues92ed9b82013-12-17 07:21:48 -050078 for meta_key in metadata.keys():
Masayuki Igawaccd66592014-07-17 00:42:42 +090079 self.assertIn(meta_key, aggregate['metadata'])
80 self.assertEqual(metadata[meta_key],
81 aggregate['metadata'][meta_key])
Mauro S. M. Rodrigues92ed9b82013-12-17 07:21:48 -050082
83 def _set_aggregate_metadata(self, aggregate, meta):
Masayuki Igawaccd66592014-07-17 00:42:42 +090084 _, aggregate = self.aggregates_client.set_metadata(aggregate['id'],
85 meta)
Mauro S. M. Rodrigues92ed9b82013-12-17 07:21:48 -050086
87 for key, value in meta.items():
Masayuki Igawaccd66592014-07-17 00:42:42 +090088 self.assertEqual(meta[key], aggregate['metadata'][key])
Mauro S. M. Rodrigues92ed9b82013-12-17 07:21:48 -050089
90 def _update_aggregate(self, aggregate, aggregate_name,
91 availability_zone):
Masayuki Igawaccd66592014-07-17 00:42:42 +090092 _, aggregate = self.aggregates_client.update_aggregate(
93 aggregate['id'], name=aggregate_name,
94 availability_zone=availability_zone)
95 self.assertEqual(aggregate['name'], aggregate_name)
96 self.assertEqual(aggregate['availability_zone'], availability_zone)
Mauro S. M. Rodrigues92ed9b82013-12-17 07:21:48 -050097 return aggregate
98
99 @test.services('compute')
100 def test_aggregate_basic_ops(self):
101 self.useFixture(fixtures.LockFixture('availability_zone'))
102 az = 'foo_zone'
Masayuki Igawa4ded9f02014-02-17 15:05:59 +0900103 aggregate_name = data_utils.rand_name('aggregate-scenario')
Yuiko Takadaf93d2482014-01-30 16:25:08 +0000104 aggregate = self._create_aggregate(name=aggregate_name,
105 availability_zone=az)
Mauro S. M. Rodrigues92ed9b82013-12-17 07:21:48 -0500106
107 metadata = {'meta_key': 'meta_value'}
108 self._set_aggregate_metadata(aggregate, metadata)
109
110 host = self._get_host_name()
Masayuki Igawaccd66592014-07-17 00:42:42 +0900111 self._add_host(aggregate['id'], host)
Mauro S. M. Rodrigues92ed9b82013-12-17 07:21:48 -0500112 self._check_aggregate_details(aggregate, aggregate_name, az, [host],
113 metadata)
114
Masayuki Igawa4ded9f02014-02-17 15:05:59 +0900115 aggregate_name = data_utils.rand_name('renamed-aggregate-scenario')
Masayuki Igawaccd66592014-07-17 00:42:42 +0900116 # Updating the name alone. The az must be specified again otherwise
117 # the tempest client would send None in the put body
118 aggregate = self._update_aggregate(aggregate, aggregate_name, az)
Mauro S. M. Rodrigues92ed9b82013-12-17 07:21:48 -0500119
Masayuki Igawaccd66592014-07-17 00:42:42 +0900120 new_metadata = {'foo': 'bar'}
121 self._set_aggregate_metadata(aggregate, new_metadata)
Mauro S. M. Rodrigues92ed9b82013-12-17 07:21:48 -0500122
Masayuki Igawaccd66592014-07-17 00:42:42 +0900123 self._check_aggregate_details(aggregate, aggregate['name'], az,
124 [host], new_metadata)