Przeglądaj źródła

Make sure network is deleted even if instance creation fails.

Nuwan Goonasekera 9 lat temu
rodzic
commit
dbfeb760a1
2 zmienionych plików z 29 dodań i 28 usunięć
  1. 8 5
      test/helpers.py
  2. 21 23
      test/test_compute_service.py

+ 8 - 5
test/helpers.py

@@ -143,6 +143,13 @@ def get_test_instance(provider, name, key_pair=None, security_groups=None,
     return instance
 
 
+def delete_test_instance(instance):
+    if instance:
+        instance.terminate()
+        instance.wait_for([InstanceState.TERMINATED, InstanceState.UNKNOWN],
+                          terminal_states=[InstanceState.ERROR])
+
+
 def cleanup_test_resources(instance=None, network=None, security_group=None,
                            key_pair=None):
     """Clean up any combination of supplied resources."""
@@ -151,11 +158,7 @@ def cleanup_test_resources(instance=None, network=None, security_group=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):
-                if instance:
-                    instance.terminate()
-                    instance.wait_for(
-                        [InstanceState.TERMINATED, InstanceState.UNKNOWN],
-                        terminal_states=[InstanceState.ERROR])
+                delete_test_instance(instance)
 
 
 class ProviderTestBase(unittest.TestCase):

+ 21 - 23
test/test_compute_service.py

@@ -293,26 +293,24 @@ class CloudComputeServiceTestCase(ProviderTestBase):
 
                 net, subnet = helpers.create_test_network(self.provider, name)
 
-                inst = helpers.create_test_instance(
-                    self.provider,
-                    name,
-                    subnet=subnet,
-                    zone=helpers.get_provider_test_data(self.provider,
-                                                        'placement'),
-                    launch_config=lc)
-
-                def cleanup(instance, net):
-                    instance.terminate()
-                    instance.wait_for(
-                        [InstanceState.TERMINATED, InstanceState.UNKNOWN],
-                        terminal_states=[InstanceState.ERROR])
-                    helpers.delete_test_network(net)
-
-                with helpers.cleanup_action(lambda: cleanup(inst, net)):
-                    try:
-                        inst.wait_till_ready()
-                    except WaitStateException as e:
-                        self.fail("The block device mapped launch did not "
-                                  " complete successfully: %s" % e)
-                    # TODO: Check instance attachments and make sure they
-                    # correspond to requested mappings
+                with helpers.cleanup_action(lambda:
+                                            helpers.delete_test_network(net)):
+
+                    inst = helpers.create_test_instance(
+                        self.provider,
+                        name,
+                        subnet=subnet,
+                        zone=helpers.get_provider_test_data(self.provider,
+                                                            'placement'),
+                        launch_config=lc)
+
+                    with helpers.cleanup_action(lambda:
+                                                helpers.delete_test_instance(
+                                                    inst, net)):
+                        try:
+                            inst.wait_till_ready()
+                        except WaitStateException as e:
+                            self.fail("The block device mapped launch did not "
+                                      " complete successfully: %s" % e)
+                        # TODO: Check instance attachments and make sure they
+                        # correspond to requested mappings