2
0
Эх сурвалжийг харах

Minimised instance creation during tests to save time and more test
fixes.

nuwan_ag 10 жил өмнө
parent
commit
47ae0f8538

+ 2 - 4
cloudbridge/providers/aws/resources.py

@@ -211,6 +211,7 @@ class AWSInstance(BaseInstance):
         """
         self._ec2_instance.terminate()
 
+    @property
     def image_id(self):
         """
         Get the image ID for this insance.
@@ -240,10 +241,7 @@ class AWSInstance(BaseInstance):
         # boto instance.groups field returns a ``Group`` object so need to
         # convert that into a ``SecurityGroup`` object before creating a
         # cloudbridge SecurityGroup object
-        security_groups = []
-        names = []
-        for group in self._ec2_instance.groups:
-            names.append(group.name)
+        names = [group.name for group in self._ec2_instance.groups]
         security_groups = self.provider.security.security_groups.get(names)
         return [AWSSecurityGroup(self.provider, group)
                 for group in security_groups]

+ 1 - 1
cloudbridge/providers/openstack/resources.py

@@ -233,7 +233,7 @@ class OpenStackInstance(BaseInstance):
         """
         Get the image ID for this instance.
         """
-        return self._os_instance.image_id
+        return self._os_instance.image.get("id")
 
     @property
     def placement_zone(self):

+ 24 - 25
test/test_provider_block_store_service.py

@@ -12,12 +12,6 @@ class ProviderBlockStoreServiceTestCase(ProviderTestBase):
         super(ProviderBlockStoreServiceTestCase, self).__init__(
             methodName=methodName, provider=provider)
 
-    def setUp(self):
-        self.instance = helpers.get_test_instance(self.provider)
-
-    def tearDown(self):
-        self.instance.terminate()
-
     def test_crud_volume(self):
         """
         Create a new volume, check whether the expected values are set,
@@ -27,8 +21,8 @@ class ProviderBlockStoreServiceTestCase(ProviderTestBase):
         test_vol = self.provider.block_store.volumes.create_volume(
             name,
             1,
-            self.instance.placement_zone)
-        with helpers.exception_action(lambda x: test_vol.delete()):
+            helpers.get_provider_test_data(self.provider, "placement"))
+        with helpers.exception_action(lambda: test_vol.delete()):
             test_vol.wait_till_ready()
             volumes = self.provider.block_store.volumes.list_volumes()
             found_volumes = [vol for vol in volumes if vol.name == name]
@@ -51,20 +45,25 @@ class ProviderBlockStoreServiceTestCase(ProviderTestBase):
         """
         Create a new volume, and attempt to attach it to an instance
         """
-        name = "CBUnitTestAttachVol-{0}".format(uuid.uuid4())
-        test_vol = self.provider.block_store.volumes.create_volume(
-            name, 1, self.instance.placement_zone)
-        with helpers.exception_action(lambda x: test_vol.delete()):
-            test_vol.wait_till_ready()
-            test_vol.attach(self.instance, '/dev/sda2')
-            test_vol.wait_for(
-                [VolumeState.IN_USE], terminal_states=[VolumeState.ERROR,
-                                                       VolumeState.DELETED])
-            test_vol.detach()
-            test_vol.wait_for(
-                [VolumeState.AVAILABLE], terminal_states=[VolumeState.ERROR,
-                                                          VolumeState.DELETED])
-            test_vol.delete()
+        instance_name = "CBVolOps-{0}-{1}".format(
+            self.provider.name,
+            uuid.uuid4())
+        test_instance = helpers.get_test_instance(self.provider, instance_name)
+        with helpers.exception_action(lambda: test_instance.terminate()):
+            name = "CBUnitTestAttachVol-{0}".format(uuid.uuid4())
+            test_vol = self.provider.block_store.volumes.create_volume(
+                name, 1, test_instance.placement_zone)
+            with helpers.exception_action(lambda: test_vol.delete()):
+                test_vol.wait_till_ready()
+                test_vol.attach(test_instance, '/dev/sda2')
+                test_vol.wait_for(
+                    [VolumeState.IN_USE],
+                    terminal_states=[VolumeState.ERROR, VolumeState.DELETED])
+                test_vol.detach()
+                test_vol.wait_for(
+                    [VolumeState.AVAILABLE],
+                    terminal_states=[VolumeState.ERROR, VolumeState.DELETED])
+                test_vol.delete()
 
     def test_crud_snapshot(self):
         """
@@ -76,8 +75,8 @@ class ProviderBlockStoreServiceTestCase(ProviderTestBase):
         test_vol = self.provider.block_store.volumes.create_volume(
             name,
             1,
-            self.instance.placement_zone)
-        with helpers.exception_action(lambda x: test_vol.delete()):
+            helpers.get_provider_test_data(self.provider, "placement"))
+        with helpers.exception_action(lambda: test_vol.delete()):
             test_vol.wait_till_ready()
             snap_name = "CBSnapshot-{0}".format(name)
             test_snap = test_vol.create_snapshot(name=snap_name,
@@ -89,7 +88,7 @@ class ProviderBlockStoreServiceTestCase(ProviderTestBase):
                     [SnapshotState.UNKNOWN],
                     terminal_states=[SnapshotState.ERROR])
 
-            with helpers.exception_action(lambda x: cleanup_snap(test_snap)):
+            with helpers.exception_action(lambda: cleanup_snap(test_snap)):
                 test_snap.wait_till_ready()
                 snaps = self.provider.block_store.snapshots.list_snapshots()
                 found_snaps = [snap for snap in snaps

+ 27 - 26
test/test_provider_image_service.py

@@ -10,37 +10,38 @@ class ProviderImageServiceTestCase(ProviderTestBase):
         super(ProviderImageServiceTestCase, self).__init__(
             methodName=methodName, provider=provider)
 
-    def setUp(self):
-        self.instance = helpers.get_test_instance(self.provider)
-
-    def tearDown(self):
-        self.instance.terminate()
-
     def test_create_and_list_image(self):
         """
         Create a new image and check whether that image can be listed.
         This covers waiting till the image is ready, checking that the image
         name is the expected one and whether list_images is functional.
         """
-        name = "CBUnitTestListImg-{0}".format(uuid.uuid4())
-        test_image = self.instance.create_image(name)
-        with helpers.exception_action(lambda x: test_image.delete()):
-            test_image.wait_till_ready()
-            images = self.provider.images.list_images()
-            found_images = [image for image in images if image.name == name]
-            self.assertTrue(
-                len(found_images) == 1,
-                "List images does not return the expected image %s" %
-                name)
-            test_image.delete()
-            test_image.wait_for(
-                [MachineImageState.UNKNOWN],
-                terminal_states=[MachineImageState.ERROR])
+        instance_name = "CBImageTest-{0}-{1}".format(
+            self.provider.name,
+            uuid.uuid4())
+        test_instance = helpers.get_test_instance(self.provider, instance_name)
+        with helpers.exception_action(lambda: test_instance.terminate()):
+            name = "CBUnitTestListImg-{0}".format(uuid.uuid4())
+            test_image = test_instance.create_image(name)
+            with helpers.exception_action(lambda: test_image.delete()):
+                test_image.wait_till_ready()
+                images = self.provider.images.list_images()
+                found_images = [image for image in images
+                                if image.name == name]
+                self.assertTrue(
+                    len(found_images) == 1,
+                    "List images does not return the expected image %s" %
+                    name)
+                test_image.delete()
+                test_image.wait_for(
+                    [MachineImageState.UNKNOWN],
+                    terminal_states=[MachineImageState.ERROR])
             # TODO: Images take a long time to deregister on EC2. Needs
             # investigation
-#             images = self.provider.images.list_images()
-#             found_images = [image for image in images if image.name == name]
-#             self.assertTrue(
-#                 len(found_images) == 0,
-#                 "Image %s should have been deleted but still exists." %
-#                 name)
+#                 images = self.provider.images.list_images()
+#                 found_images = [image for image in images
+#                                 if image.name == name]
+#                 self.assertTrue(
+#                     len(found_images) == 0,
+#                     "Image %s should have been deleted but still exists." %
+#                     name)

+ 5 - 5
test/test_provider_object_store_service.py

@@ -23,7 +23,7 @@ class ProviderObjectStoreServiceTestCase(ProviderTestBase):
         """
         name = "cbtestcreatecontainer-{0}".format(uuid.uuid4())
         test_container = self.provider.object_store.create_container(name)
-        with helpers.exception_action(lambda x: test_container.delete()):
+        with helpers.exception_action(lambda: test_container.delete()):
             containers = self.provider.object_store.list_containers()
             found_containers = [c for c in containers if c.name == name]
             self.assertTrue(
@@ -51,11 +51,11 @@ class ProviderObjectStoreServiceTestCase(ProviderTestBase):
         objects = test_container.list()
         self.assertEqual([], objects)
 
-        with helpers.exception_action(lambda x: test_container.delete()):
+        with helpers.exception_action(lambda: test_container.delete()):
             obj_name = "hello_world.txt"
             obj = test_container.create_object(obj_name)
 
-            with helpers.exception_action(lambda x: obj.delete()):
+            with helpers.exception_action(lambda: obj.delete()):
                 # TODO: This is wrong. We shouldn't have to have a separate
                 # call to upload some content before being able to delete
                 # the content. Maybe the create_object method should accept
@@ -81,11 +81,11 @@ class ProviderObjectStoreServiceTestCase(ProviderTestBase):
         name = "cbtestcontainerobjs-{0}".format(uuid.uuid4())
         test_container = self.provider.object_store.create_container(name)
 
-        with helpers.exception_action(lambda x: test_container.delete()):
+        with helpers.exception_action(lambda: test_container.delete()):
             obj_name = "hello_upload_download.txt"
             obj = test_container.create_object(obj_name)
 
-            with helpers.exception_action(lambda x: obj.delete()):
+            with helpers.exception_action(lambda: obj.delete()):
                 content = "Hello World. Here's some content"
                 # TODO: Upload and download methods accept different parameter
                 # types. Need to make this consistent - possibly provider