diff --git a/nova/availability_zones.py b/nova/availability_zones.py index 625ee1ff4f..7c8d94890c 100644 --- a/nova/availability_zones.py +++ b/nova/availability_zones.py @@ -164,7 +164,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 diff --git a/nova/tests/unit/test_availability_zones.py b/nova/tests/unit/test_availability_zones.py index f93d3d3283..eb2614b7d8 100644 --- a/nova/tests/unit/test_availability_zones.py +++ b/nova/tests/unit/test_availability_zones.py @@ -267,12 +267,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)