]> xenbits.xensource.com Git - osstest/openstack-nova.git/commitdiff
Check deleted flag in Instance.create()
authorDan Smith <dansmith@redhat.com>
Mon, 28 Nov 2016 15:24:21 +0000 (07:24 -0800)
committerDan Smith <dansmith@redhat.com>
Mon, 28 Nov 2016 19:07:32 +0000 (11:07 -0800)
An attempt to create an instance with the deleted flag set will
not work as intended, especially from instance.create(), which
can't properly pass the yet-to-be-determined deleted=id protocol.

This makes Instance.create() throw an error like the one for
being already created, and fixes the one test where we depend
on this working.

Change-Id: I60d18ca694a2eb7f333c0cf2a898cf8f17eb83dd
Closes-Bug: #1644513

nova/objects/instance.py
nova/tests/unit/compute/test_compute.py
nova/tests/unit/network/test_manager.py
nova/tests/unit/objects/test_instance.py

index 7fd0087071cc5e5d4cc162f05f54dc8b4bd17951..b9068bccbd91e429cf7aa32407ec8376f0e4b382 100644 (file)
@@ -479,6 +479,9 @@ class Instance(base.NovaPersistentObject, base.NovaObject,
         if self.obj_attr_is_set('id'):
             raise exception.ObjectActionError(action='create',
                                               reason='already created')
+        if self.obj_attr_is_set('deleted') and self.deleted:
+            raise exception.ObjectActionError(action='create',
+                                              reason='already deleted')
         updates = self.obj_get_changes()
         expected_attrs = [attr for attr in INSTANCE_DEFAULT_FIELDS
                           if attr in updates]
index 782376df0fc6f5f3bf1c92d7e90270960d2f62d0..8a392ea0fe1833e041bd8c0f18b3e6f6045dca94 100644 (file)
@@ -6279,10 +6279,14 @@ class ComputeTestCase(BaseTestCase):
         admin_context = context.get_admin_context()
         deleted_at = (timeutils.utcnow() -
                       datetime.timedelta(hours=1, minutes=5))
-        instance1 = self._create_fake_instance_obj({"deleted_at": deleted_at,
-                                                    "deleted": True})
-        instance2 = self._create_fake_instance_obj({"deleted_at": deleted_at,
-                                                    "deleted": True})
+        instance1 = self._create_fake_instance_obj()
+        instance2 = self._create_fake_instance_obj()
+
+        timeutils.set_time_override(deleted_at)
+        instance1.destroy()
+        instance2.destroy()
+        timeutils.clear_time_override()
+
         self.flags(running_deleted_instance_timeout=3600,
                    running_deleted_instance_action=action)
 
index fd20a25e60d8fe0d38b3b1bd6b41498a0f9e62f5..721400e1078c5580a140b1197a774ecd2781aa87 100644 (file)
@@ -3006,8 +3006,8 @@ class FloatingIPTestCase(test.TestCase):
                        lambda *args, **kwargs: None)
         instance = objects.Instance(context=self.context)
         instance.project_id = self.project_id
-        instance.deleted = True
         instance.create()
+        instance.destroy()
         network = db.network_create_safe(self.context.elevated(), {
                 'project_id': self.project_id,
                 'host': CONF.host,
index d2f98af89d77bd172f1a6f609283c752c913e260..e166008f52f6de9a3a7e7948850d1a0de62fa583 100644 (file)
@@ -1058,6 +1058,13 @@ class _TestInstanceObject(object):
         inst2 = objects.Instance.get_by_uuid(self.context, inst1.uuid)
         self.assertEqual('foo-host', inst2.host)
 
+    def test_create_deleted(self):
+        inst1 = objects.Instance(context=self.context,
+                                 user_id=self.context.user_id,
+                                 project_id=self.context.project_id,
+                                 deleted=True)
+        self.assertRaises(exception.ObjectActionError, inst1.create)
+
     def test_create_with_extras(self):
         inst = objects.Instance(context=self.context,
             uuid=self.fake_instance['uuid'],