From 2868d52e42cccf539938afbb252650fb4ea4e3c5 Mon Sep 17 00:00:00 2001 From: Matt Riedemann Date: Thu, 25 May 2017 15:46:22 -0400 Subject: [PATCH] Avoid lazy-load error when getting instance AZ When [cinder]cross_az_attach=False (not the default) and doing boot from volume, the API code validates the BDM by seeing if the instance and the volume are in the same availability zone. To get the AZ for the instance, the code is first trying to get the instance.host value. In Ocata we stopped creating the instance in the API and moved that to conductor for cells v2. So the Instance object in this case now is created in the _provision_instances method and stored in the BuildRequest object. Since there is no host to set on the instance yet and the Instance object wasn't populated from DB values, which before would set the host field on the instance object to None by default, trying to get instance.host will lazy-load the field and it blows up with ObjectActionError. The correct thing to do here is check if the host attribute is set on the Instance object. There is clear intent to assume host is not set in the instance since it was using instance.get('host'), probably from way back in the days when the instance in this case was a dict. So it's expecting to handle None, but we need to modernize how that is checked. Change-Id: I0dccb6a416dfe0eae4f7c52dfc28786a449b17bd Closes-Bug: #1693600 (cherry picked from commit 91bd79058abf79978335010a325952f17729d7a5) --- nova/availability_zones.py | 2 +- nova/tests/unit/test_availability_zones.py | 18 +++++++++++++++++- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/nova/availability_zones.py b/nova/availability_zones.py index 190882d90d4a..40ba8a8d0aaa 100644 --- a/nova/availability_zones.py +++ b/nova/availability_zones.py @@ -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 diff --git a/nova/tests/unit/test_availability_zones.py b/nova/tests/unit/test_availability_zones.py index 63831a4c2ee3..1dc4ade306df 100644 --- a/nova/tests/unit/test_availability_zones.py +++ b/nova/tests/unit/test_availability_zones.py @@ -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)