]> xenbits.xensource.com Git - osstest/openstack-nova.git/commitdiff
Refactor two nearly useless secgroup tests
authorDan Smith <dansmith@redhat.com>
Thu, 3 Nov 2016 17:50:26 +0000 (10:50 -0700)
committerDan Smith <dansmith@redhat.com>
Wed, 23 Nov 2016 00:07:28 +0000 (16:07 -0800)
There are two tests that masquerade as "compute api tests" to
validate some instance security group associations. However, those
are just there because it's a convenient place to create instances
with a full complement of things. They depend on behaviors we're
about to move, and are also slated to be irrelevant when we remove
nova-network. In the meantime, move them closer to the other tests
for the bits they are exercising to get them out of the way of
the following patch.

Change-Id: I58f2d9d166446cd762a2fd4ad9d6b4bf5fff0c1d

nova/tests/unit/compute/test_compute.py
nova/tests/unit/db/test_db_api.py
nova/tests/unit/objects/test_instance.py

index 74685bbc30eac0a94594ccc725516228614c402e..1ba0bacba1ce352878d7ef4c25bbd55d01363d6b 100644 (file)
@@ -7957,38 +7957,6 @@ class ComputeAPITestCase(BaseTestCase):
                 scheduler_hints={'group':
                                      '5b674f73-c8cf-40ef-9965-3b6fe4b304b1'})
 
-    def test_destroy_instance_disassociates_security_groups(self):
-        # Make sure destroying disassociates security groups.
-        group = self._create_group()
-
-        (ref, resv_id) = self.compute_api.create(
-                self.context,
-                instance_type=flavors.get_default_flavor(),
-                image_href=uuids.image_href_id,
-                security_groups=['testgroup'])
-
-        db.instance_destroy(self.context, ref[0]['uuid'])
-        group = db.security_group_get(self.context, group['id'],
-                                      columns_to_join=['instances'])
-        self.assertEqual(0, len(group['instances']))
-
-    def test_destroy_security_group_disassociates_instances(self):
-        # Make sure destroying security groups disassociates instances.
-        group = self._create_group()
-
-        (ref, resv_id) = self.compute_api.create(
-                self.context,
-                instance_type=flavors.get_default_flavor(),
-                image_href=uuids.image_href_id,
-                security_groups=['testgroup'])
-
-        db.security_group_destroy(self.context, group['id'])
-        admin_deleted_context = context.get_admin_context(
-                read_deleted="only")
-        group = db.security_group_get(admin_deleted_context, group['id'],
-                                      columns_to_join=['instances'])
-        self.assertEqual(0, len(group['instances']))
-
     def _test_rebuild(self, vm_state):
         instance = self._create_fake_instance_obj()
         instance_uuid = instance['uuid']
index 0da5e29cb2abc4ac9c9d891015fb5289f73f8d9d..8f472123ccc1541750f005526df324cb80912f7e 100644 (file)
@@ -2008,6 +2008,23 @@ class SecurityGroupTestCase(test.TestCase, ModelsObjectComparatorMixin):
                 columns_to_join=['instances',
                                  'rules']), security_group2)
 
+    def test_security_group_destroy_with_instance(self):
+        security_group1 = self._create_security_group({})
+
+        instance = db.instance_create(self.ctxt, {})
+        db.instance_add_security_group(self.ctxt, instance.uuid,
+                                       security_group1.id)
+
+        self.assertEqual(
+            1,
+            len(db.security_group_get_by_instance(self.ctxt, instance.uuid)))
+
+        db.security_group_destroy(self.ctxt, security_group1['id'])
+
+        self.assertEqual(
+            0,
+            len(db.security_group_get_by_instance(self.ctxt, instance.uuid)))
+
     def test_security_group_get(self):
         security_group1 = self._create_security_group({})
         self._create_security_group({'name': 'fake_sec_group2'})
index 65eba294b17dfd3da2d1c1d1fafa64ad914f7e4e..d2f98af89d77bd172f1a6f609283c752c913e260 100644 (file)
@@ -1837,6 +1837,28 @@ class _TestInstanceListObject(object):
         self.assertTrue(instances[0].obj_attr_is_set('system_metadata'))
         self.assertEqual({'foo': 'bar'}, instances[0].system_metadata)
 
+    def test_get_by_security_group_after_destroy(self):
+        db_sg = db.security_group_create(
+            self.context,
+            {'name': 'foo',
+             'description': 'test group',
+             'user_id': self.context.user_id,
+             'project_id': self.context.project_id})
+        self.assertFalse(db.security_group_in_use(self.context, db_sg.id))
+        inst = objects.Instance(
+            context=self.context,
+            user_id=self.context.user_id,
+            project_id=self.context.project_id)
+        inst.create()
+
+        db.instance_add_security_group(self.context,
+                                       inst.uuid,
+                                       db_sg.id)
+
+        self.assertTrue(db.security_group_in_use(self.context, db_sg.id))
+        inst.destroy()
+        self.assertFalse(db.security_group_in_use(self.context, db_sg.id))
+
     def test_get_by_grantee_security_group_ids(self):
         fake_instances = [
             fake_instance.fake_db_instance(id=1),