Merge "Avoid lazy-load error when getting instance AZ" into stable/ocata

This commit is contained in:
Jenkins 2017-06-06 14:50:52 +00:00 committed by Gerrit Code Review
commit 3e089065ad
2 changed files with 18 additions and 2 deletions

View File

@ -160,7 +160,7 @@ def get_availability_zones(context, get_only_available=False,
def get_instance_availability_zone(context, instance):
"""Return availability zone of specified instance."""
host = instance.get('host')
host = instance.host if 'host' in instance else None
if not host:
# Likely hasn't reached a viable compute node yet so give back the
# desired availability_zone in the instance record if the boot request

View File

@ -266,12 +266,28 @@ class AvailabilityZoneTestCases(test.TestCase):
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 not set."""
"""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)