From c4f24bb1057cd5ded287b10f21aca2e74ffecb1a Mon Sep 17 00:00:00 2001 From: Balazs Gibizer Date: Mon, 27 Mar 2017 16:17:51 +0200 Subject: [PATCH] do not log error for missing _save_tags The instance.save always logs the below error message if instance.tags are lazy loaded before the save. AttributeError: 'Instance' object has no attribute '_save_tags' The tags should never be saved through the instance object so adding an empty _save_tags function is better than logging an error every time. Change-Id: Id41b06e7a294d803bd580d38ef04cc0ffb32a3bf --- nova/objects/instance.py | 4 ++++ nova/tests/unit/objects/test_instance.py | 19 +++++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/nova/objects/instance.py b/nova/objects/instance.py index ca805b7d6ecb..deae03adb780 100644 --- a/nova/objects/instance.py +++ b/nova/objects/instance.py @@ -616,6 +616,10 @@ class Instance(base.NovaPersistentObject, base.NovaObject, # be dropped. pass + def _save_tags(self, context): + # NOTE(gibi): tags are not saved through the instance + pass + def _save_flavor(self, context): if not any([x in self.obj_what_changed() for x in ('flavor', 'old_flavor', 'new_flavor')]): diff --git a/nova/tests/unit/objects/test_instance.py b/nova/tests/unit/objects/test_instance.py index 13f59e9eac5b..9b5eed0b7ee9 100644 --- a/nova/tests/unit/objects/test_instance.py +++ b/nova/tests/unit/objects/test_instance.py @@ -234,6 +234,25 @@ class _TestInstanceObject(object): self.assertEqual(1, len(instance.tags)) self.assertEqual('foo', instance.tags[0].tag) + @mock.patch('nova.objects.instance.LOG.exception') + def test_save_does_not_log_exception_after_tags_loaded(self, mock_log): + instance = objects.Instance(self.context, uuid=uuids.instance, + user_id=self.context.user_id, + project_id=self.context.project_id) + instance.create() + tag = objects.Tag(self.context, resource_id=instance.uuid, tag='foo') + tag.create() + + # this will lazy load tags so instance.tags will be set + self.assertEqual(1, len(instance.tags)) + + # instance.save will try to find a way to save tags but is should not + # spam the log with errors + instance.display_name = 'foobar' + instance.save() + + self.assertFalse(mock_log.called) + @mock.patch.object(db, 'instance_get') def test_get_by_id(self, mock_get): mock_get.return_value = self.fake_instance