From 39dd4f8c5fbd2aec2a1818a1168ed43e45aa3c27 Mon Sep 17 00:00:00 2001 From: Dan Smith Date: Fri, 2 Aug 2013 13:07:34 -0700 Subject: [PATCH] Fix Instance objects with empty security groups This makes sure that a DB instance with an empty list of security groups does not result in an object with an unset attribute, thus triggering a lazy load on access (and ultimately an exception). Related to blueprint compute-api-objects Change-Id: Ib33b05853e9d067cbac5d3e87824a68e9935b822 --- nova/objects/instance.py | 2 +- nova/tests/objects/test_instance.py | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/nova/objects/instance.py b/nova/objects/instance.py index 323cec2b9404..42de00101b4c 100644 --- a/nova/objects/instance.py +++ b/nova/objects/instance.py @@ -235,7 +235,7 @@ class Instance(base.NovaObject): instance_info_cache.InstanceInfoCache._from_db_object( context, instance['info_cache'], db_inst['info_cache']) if ('security_groups' in expected_attrs and - db_inst.get('security_groups')): + db_inst.get('security_groups') is not None): instance['security_groups'] = security_group.SecurityGroupList() security_group._make_secgroup_list(context, instance['security_groups'], diff --git a/nova/tests/objects/test_instance.py b/nova/tests/objects/test_instance.py index 460085a1b812..8b7455974501 100644 --- a/nova/tests/objects/test_instance.py +++ b/nova/tests/objects/test_instance.py @@ -348,6 +348,17 @@ class _TestInstanceObject(object): inst.save() self.assertEqual(inst.security_groups.obj_what_changed(), set()) + def test_with_empty_security_groups(self): + ctxt = context.get_admin_context() + fake_inst = dict(self.fake_instance, security_groups=[]) + fake_uuid = fake_inst['uuid'] + self.mox.StubOutWithMock(db, 'instance_get_by_uuid') + db.instance_get_by_uuid(ctxt, fake_uuid, columns_to_join=[] + ).AndReturn(fake_inst) + self.mox.ReplayAll() + inst = instance.Instance.get_by_uuid(ctxt, fake_uuid) + self.assertEqual(0, len(inst.security_groups)) + def test_with_fault(self): ctxt = context.get_admin_context() fake_inst = dict(self.fake_instance)