Преглед изворни кода

Update security tests to use helper methods

Enis Afgan пре 9 година
родитељ
комит
c9e7a82042
2 измењених фајлова са 22 додато и 16 уклоњено
  1. 6 4
      test/helpers.py
  2. 16 12
      test/test_security_service.py

+ 6 - 4
test/helpers.py

@@ -145,15 +145,17 @@ def get_test_instance(provider, name, key_pair=None, security_groups=None,
 
 def cleanup_test_resources(instance=None, network=None, security_group=None,
                            key_pair=None):
+    """Clean up any combination of supplied resources."""
     with cleanup_action(lambda: delete_test_network(network)
                         if network else None):
         with cleanup_action(lambda: key_pair.delete() if key_pair else None):
             with cleanup_action(lambda: security_group.delete()
                                 if security_group else None):
-                instance.terminate()
-                instance.wait_for(
-                    [InstanceState.TERMINATED, InstanceState.UNKNOWN],
-                    terminal_states=[InstanceState.ERROR])
+                if instance:
+                    instance.terminate()
+                    instance.wait_for(
+                        [InstanceState.TERMINATED, InstanceState.UNKNOWN],
+                        terminal_states=[InstanceState.ERROR])
 
 
 class ProviderTestBase(unittest.TestCase):

+ 16 - 12
test/test_security_service.py

@@ -105,11 +105,12 @@ class CloudSecurityServiceTestCase(ProviderTestBase):
 
     @helpers.skipIfNoService(['security.security_groups'])
     def test_crud_security_group_service(self):
-        name = 'cbtestsecuritygroupA-{0}'.format(uuid.uuid4())
-        net = self.provider.network.create(name=name)
+        name = 'CBTestSecurityGroupA-{0}'.format(uuid.uuid4())
+        net, _ = helpers.create_test_network(self.provider, name)
         sg = self.provider.security.security_groups.create(
             name=name, description=name, network_id=net.id)
-        with helpers.cleanup_action(lambda: self.cleanup_sg(sg, net)):
+        with helpers.cleanup_action(lambda: helpers.cleanup_test_resources(
+                network=net, security_group=sg)):
             self.assertEqual(name, sg.description)
 
             # test list method
@@ -160,11 +161,12 @@ class CloudSecurityServiceTestCase(ProviderTestBase):
     @helpers.skipIfNoService(['security.security_groups'])
     def test_security_group(self):
         """Test for proper creation of a security group."""
-        name = 'cbtestsecuritygroupB-{0}'.format(uuid.uuid4())
-        net = self.provider.network.create(name=name)
+        name = 'CBTestSecurityGroupB-{0}'.format(uuid.uuid4())
+        net, _ = helpers.create_test_network(self.provider, name)
         sg = self.provider.security.security_groups.create(
             name=name, description=name, network_id=net.id)
-        with helpers.cleanup_action(lambda: self.cleanup_sg(sg, net)):
+        with helpers.cleanup_action(lambda: helpers.cleanup_test_resources(
+                network=net, security_group=sg)):
             rule = sg.add_rule(ip_protocol='tcp', from_port=1111, to_port=1111,
                                cidr_ip='0.0.0.0/0')
             found_rule = sg.get_rule(ip_protocol='tcp', from_port=1111,
@@ -216,11 +218,12 @@ class CloudSecurityServiceTestCase(ProviderTestBase):
                 "Mock provider returns InvalidParameterValue: "
                 "Value security_group is invalid for parameter.")
 
-        name = 'cbtestsecuritygroupB-{0}'.format(uuid.uuid4())
-        net = self.provider.network.create(name=name)
+        name = 'CBTestSecurityGroupC-{0}'.format(uuid.uuid4())
+        net, _ = helpers.create_test_network(self.provider, name)
         sg = self.provider.security.security_groups.create(
             name=name, description=name, network_id=net.id)
-        with helpers.cleanup_action(lambda: self.cleanup_sg(sg, net)):
+        with helpers.cleanup_action(lambda: helpers.cleanup_test_resources(
+                network=net, security_group=sg)):
             rule = sg.add_rule(ip_protocol='tcp', from_port=1111, to_port=1111,
                                cidr_ip='0.0.0.0/0')
             # attempting to add the same rule twice should succeed
@@ -234,11 +237,12 @@ class CloudSecurityServiceTestCase(ProviderTestBase):
     @helpers.skipIfNoService(['security.security_groups'])
     def test_security_group_group_rule(self):
         """Test for proper creation of a security group rule."""
-        name = 'cbtestsecuritygroupC-{0}'.format(uuid.uuid4())
-        net = self.provider.network.create(name=name)
+        name = 'CBTestSecurityGroupD-{0}'.format(uuid.uuid4())
+        net, _ = helpers.create_test_network(self.provider, name)
         sg = self.provider.security.security_groups.create(
             name=name, description=name, network_id=net.id)
-        with helpers.cleanup_action(lambda: self.cleanup_sg(sg, net)):
+        with helpers.cleanup_action(lambda: helpers.cleanup_test_resources(
+                network=net, security_group=sg)):
             self.assertTrue(
                 len(sg.rules) == 0,
                 "Expected no security group group rule. Got {0}."