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
13"""
14test_aggregate
15----------------------------------
16
17Functional tests for `shade` aggregate resource.
18"""
19
20from openstack.tests.functional import base
21
22
23class TestAggregate(base.BaseFunctionalTest):
24
25    def test_aggregates(self):
26        aggregate_name = self.getUniqueString()
27        availability_zone = self.getUniqueString()
28        self.addCleanup(self.cleanup, aggregate_name)
29        aggregate = self.operator_cloud.create_aggregate(aggregate_name)
30
31        aggregate_ids = [v['id']
32                         for v in self.operator_cloud.list_aggregates()]
33        self.assertIn(aggregate['id'], aggregate_ids)
34
35        aggregate = self.operator_cloud.update_aggregate(
36            aggregate_name,
37            availability_zone=availability_zone
38        )
39        self.assertEqual(availability_zone, aggregate['availability_zone'])
40
41        aggregate = self.operator_cloud.set_aggregate_metadata(
42            aggregate_name,
43            {'key': 'value'}
44        )
45        self.assertIn('key', aggregate['metadata'])
46
47        aggregate = self.operator_cloud.set_aggregate_metadata(
48            aggregate_name,
49            {'key': None}
50        )
51        self.assertNotIn('key', aggregate['metadata'])
52
53        # Validate that we can delete by name
54        self.assertTrue(
55            self.operator_cloud.delete_aggregate(aggregate_name))
56
57    def cleanup(self, aggregate_name):
58        aggregate = self.operator_cloud.get_aggregate(aggregate_name)
59        if aggregate:
60            self.operator_cloud.delete_aggregate(aggregate['id'])
61