From 6d5e0fd2d488329f66f31a4d6a1153a4a64ed6aa Mon Sep 17 00:00:00 2001 From: gtt116 Date: Thu, 17 Jan 2013 07:36:43 +0000 Subject: Fix incorrect use of context object As in nova/availability_zones.py, the method get_host_availability_zone() expects "context" as a module rather than a RequestContext object. But for our preference, the argument "context" is better to be a RequestContext object. So fix this by passing an context object into the method. * Fix the usage of argument "context" * Add unittest for the module "availability_zones" Fix bug lp:#1100598 Change-Id: I5886ba26131261d97be65f18e9492c40401c5a7f --- nova/api/ec2/ec2utils.py | 3 +- nova/availability_zones.py | 4 +- nova/tests/test_availability_zones.py | 114 ++++++++++++++++++++++++++++++++++ 3 files changed, 118 insertions(+), 3 deletions(-) create mode 100644 nova/tests/test_availability_zones.py diff --git a/nova/api/ec2/ec2utils.py b/nova/api/ec2/ec2utils.py index 1ce880de4..bc47b3e0d 100644 --- a/nova/api/ec2/ec2utils.py +++ b/nova/api/ec2/ec2utils.py @@ -117,7 +117,8 @@ def get_ip_info_for_instance(context, instance): def get_availability_zone_by_host(services, host): if len(services) > 0: - return availability_zones.get_host_availability_zone(context, host) + return availability_zones.get_host_availability_zone( + context.get_admin_context(), host) return 'unknown zone' diff --git a/nova/availability_zones.py b/nova/availability_zones.py index 7493ce5c5..62c83f6ed 100644 --- a/nova/availability_zones.py +++ b/nova/availability_zones.py @@ -46,7 +46,7 @@ def set_availability_zones(context, services): az = CONF.internal_service_availability_zone if service['topic'] == "compute": if metadata.get(service['host']): - az = str(metadata[service['host']])[5:-2] + az = u','.join(list(metadata[service['host']])) else: az = CONF.default_availability_zone service['availability_zone'] = az @@ -55,7 +55,7 @@ def set_availability_zones(context, services): def get_host_availability_zone(context, host): metadata = db.aggregate_metadata_get_by_host( - context.get_admin_context(), host, key='availability_zone') + context, host, key='availability_zone') if 'availability_zone' in metadata: return list(metadata['availability_zone'])[0] else: diff --git a/nova/tests/test_availability_zones.py b/nova/tests/test_availability_zones.py new file mode 100644 index 000000000..2c5c06921 --- /dev/null +++ b/nova/tests/test_availability_zones.py @@ -0,0 +1,114 @@ +# vim: tabstop=4 shiftwidth=4 softtabstop=4 + +# 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 nova import availability_zones as az +from nova import context +from nova import db +from nova.openstack.common import cfg +from nova import service +from nova import test + +CONF = cfg.CONF +CONF.import_opt('internal_service_availability_zone', + 'nova.availability_zones') +CONF.import_opt('default_availability_zone', + 'nova.availability_zones') + + +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() + + agg = {'name': 'agg1'} + self.agg = db.aggregate_create(self.context, agg) + + metadata = {'availability_zone': self.availability_zone} + db.aggregate_metadata_add(self.context, self.agg['id'], metadata) + + def tearDown(self): + db.aggregate_delete(self.context, self.agg['id']) + super(AvailabilityZoneTestCases, self).tearDown() + + def _create_service_with_topic(self, topic): + values = { + 'binary': 'bin', + 'host': self.host, + 'topic': topic, + } + 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): + return db.aggregate_host_add(self.context, + self.agg['id'], service['host']) + + def _delete_from_aggregate(self, service): + return db.aggregate_host_delete(self.context, + self.agg['id'], service['host']) + + def test_set_availability_zone_compute_service(self): + """Test for compute service get right availability zone.""" + service = self._create_service_with_topic('compute') + 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.assertEquals(new_service['availability_zone'], + self.default_az) + + # The service is added into aggregate, confirm return the aggregate + # availability zone. + self._add_to_aggregate(service) + new_service = az.set_availability_zones(self.context, services)[0] + self.assertEquals(new_service['availability_zone'], + self.availability_zone) + + 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') + services = db.service_get_all(self.context) + new_service = az.set_availability_zones(self.context, services)[0] + self.assertEquals(new_service['availability_zone'], + self.default_in_az) + self._destroy_service(service) + + def test_get_host_availability_zone(self): + """Test get right availability zone by given host.""" + self.assertEquals(self.default_az, + az.get_host_availability_zone(self.context, self.host)) + + service = self._create_service_with_topic('compute') + self._add_to_aggregate(service) + + self.assertEquals(self.availability_zone, + az.get_host_availability_zone(self.context, self.host)) -- cgit