summaryrefslogtreecommitdiff
path: root/nova/tests/unit/test_availability_zones.py
blob: f2e02e39c71806b2c9c3379f5bfa57c6dca2325d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
# Copyright 2013 Netease Corporation
# All Rights Reserved.
#
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.

"""
Tests for availability zones
"""

from unittest import mock

from oslo_utils.fixture import uuidsentinel

from nova import availability_zones as az
from nova.compute import api as compute_api
import nova.conf
from nova import context
from nova.db.main import api as db
from nova import objects
from nova import test

CONF = nova.conf.CONF


class AvailabilityZoneTestCases(test.TestCase):
    """Test case for aggregate based availability zone."""

    def setUp(self):
        super(AvailabilityZoneTestCases, self).setUp()
        self.host = 'me'
        self.availability_zone = 'nova-test'
        self.default_az = CONF.default_availability_zone
        self.default_in_az = CONF.internal_service_availability_zone
        self.context = context.get_admin_context()
        self.agg = self._create_az('az_agg', self.availability_zone)

    def tearDown(self):
        self.agg.destroy()
        super(AvailabilityZoneTestCases, self).tearDown()

    def _create_az(self, agg_name, az_name):
        agg_meta = {'name': agg_name, 'uuid': uuidsentinel.agg_uuid,
                    'metadata': {'availability_zone': az_name}}
        agg = objects.Aggregate(self.context, **agg_meta)
        agg.create()
        agg = objects.Aggregate.get_by_id(self.context, agg.id)
        return agg

    def _update_az(self, aggregate, az_name):
        aggregate.update_metadata({'availability_zone': az_name})

    def _create_service_with_topic(self, topic, host, disabled=False):
        values = {
            'binary': 'nova-bin',
            'host': host,
            'topic': topic,
            'disabled': disabled,
        }
        return db.service_create(self.context, values)

    def _destroy_service(self, service):
        return db.service_destroy(self.context, service['id'])

    def _add_to_aggregate(self, service, aggregate):
        aggregate.add_host(service['host'])

    def _delete_from_aggregate(self, service, aggregate):
        aggregate.delete_host(service['host'])

    def test_rest_availability_zone_reset_cache(self):
        az._get_cache().region.get_or_create('cache', lambda: 'fake_value')
        az.reset_cache()
        self.assertIsNone(az._get_cache().get('cache'))

    def test_update_host_availability_zone_cache(self):
        """Test availability zone cache could be update."""
        service = self._create_service_with_topic('compute', self.host)

        # Create a new aggregate with an AZ and add the host to the AZ
        az_name = 'az1'
        cache_key = az._make_cache_key(self.host)
        agg_az1 = self._create_az('agg-az1', az_name)
        self._add_to_aggregate(service, agg_az1)
        az.update_host_availability_zone_cache(self.context, self.host)
        self.assertEqual('az1', az._get_cache().get(cache_key))
        az.update_host_availability_zone_cache(self.context, self.host, 'az2')
        self.assertEqual('az2', az._get_cache().get(cache_key))

    def test_set_availability_zone_compute_service(self):
        """Test for compute service get right availability zone."""
        service = self._create_service_with_topic('compute', self.host)
        services = db.service_get_all(self.context)

        # The service is not add into aggregate, so confirm it is default
        # availability zone.
        new_service = az.set_availability_zones(self.context, services)[0]
        self.assertEqual(self.default_az, new_service['availability_zone'])

        # The service is added into aggregate, confirm return the aggregate
        # availability zone.
        self._add_to_aggregate(service, self.agg)
        new_service = az.set_availability_zones(self.context, services)[0]
        self.assertEqual(self.availability_zone,
                         new_service['availability_zone'])

        self._destroy_service(service)

    def test_set_availability_zone_unicode_key(self):
        """Test set availability zone cache key is unicode."""
        service = self._create_service_with_topic('network', self.host)
        services = db.service_get_all(self.context)
        az.set_availability_zones(self.context, services)
        self.assertIsInstance(services[0]['host'], str)
        cached_key = az._make_cache_key(services[0]['host'])
        self.assertIsInstance(cached_key, str)
        self._destroy_service(service)

    def test_set_availability_zone_not_compute_service(self):
        """Test not compute service get right availability zone."""
        service = self._create_service_with_topic('network', self.host)
        services = db.service_get_all(self.context)
        new_service = az.set_availability_zones(self.context, services)[0]
        self.assertEqual(self.default_in_az, new_service['availability_zone'])
        self._destroy_service(service)

    def test_get_host_availability_zone(self):
        """Test get right availability zone by given host."""
        self.assertEqual(self.default_az,
                        az.get_host_availability_zone(self.context, self.host))

        service = self._create_service_with_topic('compute', self.host)
        self._add_to_aggregate(service, self.agg)

        self.assertEqual(self.availability_zone,
                        az.get_host_availability_zone(self.context, self.host))

    def test_update_host_availability_zone(self):
        """Test availability zone could be update by given host."""
        service = self._create_service_with_topic('compute', self.host)

        # Create a new aggregate with an AZ and add the host to the AZ
        az_name = 'az1'
        agg_az1 = self._create_az('agg-az1', az_name)
        self._add_to_aggregate(service, agg_az1)
        self.assertEqual(az_name,
                    az.get_host_availability_zone(self.context, self.host))
        # Update AZ
        new_az_name = 'az2'
        self._update_az(agg_az1, new_az_name)
        self.assertEqual(new_az_name,
                    az.get_host_availability_zone(self.context, self.host))

    def test_delete_host_availability_zone(self):
        """Test availability zone could be deleted successfully."""
        service = self._create_service_with_topic('compute', self.host)

        # Create a new aggregate with an AZ and add the host to the AZ
        az_name = 'az1'
        agg_az1 = self._create_az('agg-az1', az_name)
        self._add_to_aggregate(service, agg_az1)
        self.assertEqual(az_name,
                    az.get_host_availability_zone(self.context, self.host))
        # Delete the AZ via deleting the aggregate
        self._delete_from_aggregate(service, agg_az1)
        self.assertEqual(self.default_az,
                    az.get_host_availability_zone(self.context, self.host))

    def test_get_availability_zones(self):
        """Test get_availability_zones."""

        # When the param get_only_available of get_availability_zones is set
        # to default False, it returns two lists, zones with at least one
        # enabled services, and zones with no enabled services,
        # when get_only_available is set to True, only return a list of zones
        # with at least one enabled services.
        # Use the following test data:
        #
        # zone         host        enabled
        # nova-test    host1       Yes
        # nova-test    host2       No
        # nova-test2   host3       Yes
        # nova-test3   host4       No
        # <default>    host5       No

        agg2 = self._create_az('agg-az2', 'nova-test2')
        agg3 = self._create_az('agg-az3', 'nova-test3')

        service1 = self._create_service_with_topic('compute', 'host1',
                                                   disabled=False)
        service2 = self._create_service_with_topic('compute', 'host2',
                                                   disabled=True)
        service3 = self._create_service_with_topic('compute', 'host3',
                                                   disabled=False)
        service4 = self._create_service_with_topic('compute', 'host4',
                                                   disabled=True)
        self._create_service_with_topic('compute', 'host5',
                                        disabled=True)

        self._add_to_aggregate(service1, self.agg)
        self._add_to_aggregate(service2, self.agg)
        self._add_to_aggregate(service3, agg2)
        self._add_to_aggregate(service4, agg3)

        host_api = compute_api.HostAPI()
        zones, not_zones = az.get_availability_zones(self.context, host_api)

        self.assertEqual(['nova-test', 'nova-test2'], zones)
        self.assertEqual(['nova', 'nova-test3'], not_zones)

        zones = az.get_availability_zones(self.context, host_api,
                                          get_only_available=True)

        self.assertEqual(['nova-test', 'nova-test2'], zones)

        zones, not_zones = az.get_availability_zones(self.context, host_api,
                                                     with_hosts=True)

        self.assertJsonEqual(zones,
                         [(u'nova-test2', set([u'host3'])),
                                 (u'nova-test', set([u'host1']))])
        self.assertJsonEqual(not_zones,
                         [(u'nova-test3', set([u'host4'])),
                                 (u'nova', set([u'host5']))])

    def test_get_instance_availability_zone_default_value(self):
        """Test get right availability zone by given an instance."""
        fake_inst = objects.Instance(host=self.host,
                                     availability_zone=None)

        self.assertEqual(self.default_az,
                az.get_instance_availability_zone(self.context, fake_inst))

    def test_get_instance_availability_zone_from_aggregate(self):
        """Test get availability zone from aggregate by given an instance."""
        host = 'host170'
        service = self._create_service_with_topic('compute', host)
        self._add_to_aggregate(service, self.agg)

        fake_inst = objects.Instance(host=host,
                                     availability_zone=self.availability_zone)

        self.assertEqual(self.availability_zone,
                az.get_instance_availability_zone(self.context, fake_inst))

    @mock.patch.object(az._get_cache(), 'get')
    def test_get_instance_availability_zone_cache_differs(self, cache_get):
        host = 'host170'
        service = self._create_service_with_topic('compute', host)
        self._add_to_aggregate(service, self.agg)
        cache_get.return_value = self.default_az

        fake_inst = objects.Instance(host=host,
                                     availability_zone=self.availability_zone)
        self.assertEqual(
            self.availability_zone,
            az.get_instance_availability_zone(self.context, fake_inst))

    def test_get_instance_availability_zone_no_host(self):
        """Test get availability zone from instance if host is None."""
        fake_inst = objects.Instance(host=None, availability_zone='inst-az')

        result = az.get_instance_availability_zone(self.context, fake_inst)
        self.assertEqual('inst-az', result)

    def test_get_instance_availability_zone_no_host_set(self):
        """Test get availability zone from instance if host not set.

        This is testing the case in the compute API where the Instance object
        does not have the host attribute set because it's just the object that
        goes into the BuildRequest, it wasn't actually pulled from the DB. The
        instance in this case doesn't actually get inserted into the DB until
        it reaches conductor. So the host attribute may not be set but we
        expect availability_zone always will in the API because of
        _validate_and_build_base_options setting a default value which goes
        into the object.
        """
        fake_inst = objects.Instance(availability_zone='inst-az')
        result = az.get_instance_availability_zone(self.context, fake_inst)
        self.assertEqual('inst-az', result)

    def test_get_instance_availability_zone_no_host_no_az(self):
        """Test get availability zone if neither host nor az is set."""
        fake_inst = objects.Instance(host=None, availability_zone=None)

        result = az.get_instance_availability_zone(self.context, fake_inst)
        self.assertIsNone(result)