Sfoglia il codice sorgente

Update tests to match the new default model of running with private networking.

Enis Afgan 9 anni fa
parent
commit
2c98623199

+ 43 - 2
test/helpers.py

@@ -6,6 +6,7 @@ import unittest
 from six import reraise
 
 from cloudbridge.cloud.factory import CloudProviderFactory
+from cloudbridge.cloud.interfaces import InstanceState
 from cloudbridge.cloud.interfaces import TestMockHelperMixin
 
 
@@ -70,6 +71,25 @@ def get_provider_test_data(provider, key):
     return None
 
 
+def create_test_network(provider, name):
+    """
+    Create a network with one subnet, returning the network and subnet objects.
+    """
+    net = provider.network.create(name=name)
+    cidr_block = net.cidr_block or '10.0.0.1'
+    sn = net.create_subnet(cidr_block='{0}/28'.format(cidr_block, name=name))
+    return net, sn
+
+
+def delete_test_network(network):
+    """
+    Delete the supplied network, first deleting any contained subnets.
+    """
+    for sn in network.subnets():
+        sn.delete()
+    network.delete()
+
+
 def create_test_instance(
         provider, instance_name, zone=None, launch_config=None,
         key_pair=None, security_groups=None):
@@ -83,16 +103,37 @@ def create_test_instance(
         launch_config=launch_config)
 
 
-def get_test_instance(provider, name, key_pair=None, security_groups=None):
+def get_test_instance(provider, name, key_pair=None, security_groups=None,
+                      network=None):
+    launch_config = None
+    if network:
+        launch_config = provider.compute.instances.create_launch_config()
+        launch_config.add_network_interface(network.id)
     instance = create_test_instance(
         provider,
         name,
         key_pair=key_pair,
-        security_groups=security_groups)
+        security_groups=security_groups,
+        launch_config=launch_config)
     instance.wait_till_ready()
     return instance
 
 
+def cleanup_test_resources(instance=None, network=None, security_group=None,
+                           key_pair=None):
+    if instance:
+        instance.terminate()
+        instance.wait_for(
+            [InstanceState.TERMINATED, InstanceState.UNKNOWN],
+            terminal_states=[InstanceState.ERROR])
+    if security_group:
+        security_group.delete()
+    if key_pair:
+        key_pair.delete()
+    if network:
+        delete_test_network(network)
+
+
 class ProviderTestBase(object):
 
     """

+ 10 - 4
test/test_block_store_service.py

@@ -98,8 +98,11 @@ class CloudBlockStoreServiceTestCase(ProviderTestBase):
         instance_name = "CBVolOps-{0}-{1}".format(
             self.provider.name,
             uuid.uuid4())
-        test_instance = helpers.get_test_instance(self.provider, instance_name)
-        with helpers.cleanup_action(lambda: test_instance.terminate()):
+        net, _ = helpers.create_test_network(self.provider, instance_name)
+        test_instance = helpers.get_test_instance(self.provider, instance_name,
+                                                  network=net)
+        with helpers.cleanup_action(lambda: helpers.cleanup_test_resources(
+                test_instance, net)):
             name = "CBUnitTestAttachVol-{0}".format(uuid.uuid4())
             test_vol = self.provider.block_store.volumes.create(
                 name, 1, test_instance.zone_id)
@@ -122,8 +125,11 @@ class CloudBlockStoreServiceTestCase(ProviderTestBase):
             self.provider.name,
             uuid.uuid4())
         vol_desc = 'newvoldesc1'
-        test_instance = helpers.get_test_instance(self.provider, instance_name)
-        with helpers.cleanup_action(lambda: test_instance.terminate()):
+        net, _ = helpers.create_test_network(self.provider, instance_name)
+        test_instance = helpers.get_test_instance(self.provider, instance_name,
+                                                  network=net)
+        with helpers.cleanup_action(lambda: helpers.cleanup_test_resources(
+                test_instance, net)):
             name = "CBUnitTestVolProps-{0}".format(uuid.uuid4())
             test_vol = self.provider.block_store.volumes.create(
                 name, 1, test_instance.zone_id, description=vol_desc)

+ 27 - 33
test/test_compute_service.py

@@ -21,17 +21,11 @@ class CloudComputeServiceTestCase(ProviderTestBase):
         name = "CBInstCrud-{0}-{1}".format(
             self.provider.name,
             uuid.uuid4())
-        inst = helpers.create_test_instance(self.provider, name)
-
-        def cleanup_inst(instance):
-            instance.terminate()
-            instance.wait_for(
-                [InstanceState.TERMINATED, InstanceState.UNKNOWN],
-                terminal_states=[InstanceState.ERROR])
-
-        with helpers.cleanup_action(lambda: cleanup_inst(inst)):
-            inst.wait_till_ready()
+        net, _ = helpers.create_test_network(self.provider, name)
+        inst = helpers.get_test_instance(self.provider, name, network=net)
 
+        with helpers.cleanup_action(lambda: helpers.cleanup_test_resources(
+                inst, net)):
             all_instances = self.provider.compute.instances.list()
 
             list_instances = [i for i in all_instances if i.name == name]
@@ -98,23 +92,17 @@ class CloudComputeServiceTestCase(ProviderTestBase):
         name = "CBInstProps-{0}-{1}".format(
             self.provider.name,
             uuid.uuid4())
+        net, _ = helpers.create_test_network(self.provider, name)
         kp = self.provider.security.key_pairs.create(name=name)
-        sg = None
-        # sg = self.provider.security.security_groups.create(
-        #     name=name, description=name)
+        sg = self.provider.security.security_groups.create(
+            name=name, description=name, network_id=net.id)
 
         test_instance = helpers.get_test_instance(self.provider,
-                                                  name, key_pair=kp)
-                                                  # security_groups=[sg])
+                                                  name, key_pair=kp,
+                                                  security_groups=[sg])
 
-        def cleanup(inst, kp, sg):
-            inst.terminate()
-            inst.wait_for([InstanceState.TERMINATED, InstanceState.UNKNOWN],
-                          terminal_states=[InstanceState.ERROR])
-            kp.delete()
-            # sg.delete()
-
-        with helpers.cleanup_action(lambda: cleanup(test_instance, kp, sg)):
+        with helpers.cleanup_action(lambda: helpers.cleanup_test_resources(
+                test_instance, net, sg, kp)):
             self.assertTrue(
                 test_instance.id in repr(test_instance),
                 "repr(obj) should contain the object id so that the object"
@@ -145,18 +133,19 @@ class CloudComputeServiceTestCase(ProviderTestBase):
                 test_instance.key_pair_name,
                 kp.name)
             self.assertIsInstance(test_instance.security_groups, list)
-            # self.assertEqual(
-            #     test_instance.security_groups[0],
-            #     sg)
-            # self.assertIsInstance(test_instance.security_group_ids, list)
-            # self.assertEqual(
-            #     test_instance.security_group_ids[0],
-            #     sg.id)
+            self.assertEqual(
+                test_instance.security_groups[0],
+                sg)
+            self.assertIsInstance(test_instance.security_group_ids, list)
+            self.assertEqual(
+                test_instance.security_group_ids[0],
+                sg.id)
             # Must have either a public or a private ip
             ip_private = test_instance.private_ips[0] \
                 if test_instance.private_ips else None
             ip_address = test_instance.public_ips[0] \
-                if test_instance.public_ips else ip_private
+                if test_instance.public_ips and test_instance.public_ips[0] \
+                else ip_private
             self.assertIsNotNone(
                 ip_address,
                 "Instance must have either a public IP or a private IP")
@@ -297,6 +286,9 @@ class CloudComputeServiceTestCase(ProviderTestBase):
         for _ in range(inst_type.num_ephemeral_disks):
             lc.add_ephemeral_device()
 
+        net, _ = helpers.create_test_network(self.provider, name)
+        lc.add_network_interface(net.id)
+
         inst = helpers.create_test_instance(
             self.provider,
             name,
@@ -305,12 +297,14 @@ class CloudComputeServiceTestCase(ProviderTestBase):
                 'placement'),
             launch_config=lc)
 
-        def cleanup(instance):
+        def cleanup(instance, net):
             instance.terminate()
             instance.wait_for(
                 [InstanceState.TERMINATED, InstanceState.UNKNOWN],
                 terminal_states=[InstanceState.ERROR])
-        with helpers.cleanup_action(lambda: cleanup(inst)):
+            helpers.delete_test_network(net)
+
+        with helpers.cleanup_action(lambda: cleanup(inst, net)):
             try:
                 inst.wait_till_ready()
             except WaitStateException as e:

+ 5 - 2
test/test_image_service.py

@@ -22,8 +22,11 @@ class CloudImageServiceTestCase(ProviderTestBase):
         instance_name = "CBImageTest-{0}-{1}".format(
             self.provider.name,
             uuid.uuid4())
-        test_instance = helpers.get_test_instance(self.provider, instance_name)
-        with helpers.cleanup_action(lambda: test_instance.terminate()):
+        net, _ = helpers.create_test_network(self.provider, instance_name)
+        test_instance = helpers.get_test_instance(self.provider, instance_name,
+                                                  network=net)
+        with helpers.cleanup_action(lambda: helpers.cleanup_test_resources(
+                test_instance, net)):
             name = "CBUnitTestListImg-{0}".format(uuid.uuid4())
             test_image = test_instance.create_image(name)