Răsfoiți Sursa

Added tests for repr methods. Also changed instance_id to id.

nuwan_ag 10 ani în urmă
părinte
comite
0107799e1d

+ 1 - 1
cloudbridge/cloud/base.py

@@ -182,7 +182,7 @@ class BaseInstance(BaseObjectLifeCycleMixin, Instance):
 
     def __repr__(self):
         return "<CB-{0}: {1} ({2})>".format(self.__class__.__name__,
-                                            self.name, self.instance_id)
+                                            self.name, self.id)
 
 
 class BaseLaunchConfig(LaunchConfig):

+ 1 - 1
cloudbridge/cloud/interfaces/resources.py

@@ -141,7 +141,7 @@ class Instance(ObjectLifeCycleMixin):
     __metaclass__ = ABCMeta
 
     @abstractproperty
-    def instance_id(self):
+    def id(self):
         """
         Get the instance identifier.
 

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

@@ -194,7 +194,7 @@ class AWSInstance(BaseInstance):
         self._ec2_instance = ec2_instance
 
     @property
-    def instance_id(self):
+    def id(self):
         """
         Get the instance identifier.
         """
@@ -376,7 +376,7 @@ class AWSVolume(BaseVolume):
         """
         Attach this volume to an instance.
         """
-        instance_id = instance.instance_id if isinstance(
+        instance_id = instance.id if isinstance(
             instance,
             AWSInstance) else instance
         self._volume.attach(instance_id, device)

+ 3 - 3
cloudbridge/cloud/providers/openstack/resources.py

@@ -200,7 +200,7 @@ class OpenStackInstance(BaseInstance):
         self._os_instance = os_instance
 
     @property
-    def instance_id(self):
+    def id(self):
         """
         Get the instance identifier.
         """
@@ -338,7 +338,7 @@ class OpenStackInstance(BaseInstance):
         for its latest state.
         """
         instance = self._provider.compute.instances.get(
-            self.instance_id)
+            self.id)
         if instance:
             self._os_instance = instance._os_instance
         else:
@@ -412,7 +412,7 @@ class OpenStackVolume(BaseVolume):
         """
         Attach this volume to an instance.
         """
-        instance_id = instance.instance_id if isinstance(
+        instance_id = instance.id if isinstance(
             instance,
             OpenStackInstance) else instance
         self._volume.attach(instance_id, device)

+ 4 - 0
test/test_instance_types_service.py

@@ -11,6 +11,10 @@ class ProviderInstanceTypesServiceTestCase(ProviderTestBase):
     def test_instance_types(self):
         instance_types = self.provider.compute.instance_types.list()
         for inst_type in instance_types:
+            self.assertTrue(
+                inst_type.id in repr(inst_type),
+                "repr(obj) should contain the object id so that the object"
+                " can be reconstructed, but does not. eval(repr(obj)) == obj")
             self.assertIsNotNone(
                 inst_type.id,
                 "InstanceType id must have a value")

+ 9 - 0
test/test_provider_block_store_service.py

@@ -32,6 +32,10 @@ class ProviderBlockStoreServiceTestCase(ProviderTestBase):
 
         with helpers.cleanup_action(lambda: cleanup_vol(test_vol)):
             test_vol.wait_till_ready(interval=self.get_test_wait_interval())
+            self.assertTrue(
+                test_vol.id in repr(test_vol),
+                "repr(obj) should contain the object id so that the object"
+                " can be reconstructed, but does not. eval(repr(obj)) == obj")
             volumes = self.provider.block_store.volumes.list()
             found_volumes = [vol for vol in volumes if vol.name == name]
             self.assertTrue(
@@ -115,6 +119,11 @@ class ProviderBlockStoreServiceTestCase(ProviderTestBase):
             with helpers.cleanup_action(lambda: cleanup_snap(test_snap)):
                 test_snap.wait_till_ready(
                     interval=self.get_test_wait_interval())
+                self.assertTrue(
+                    test_snap.id in repr(test_snap),
+                    "repr(obj) should contain the object id so that the object"
+                    " can be reconstructed, but does not.")
+
                 snaps = self.provider.block_store.snapshots.list()
                 found_snaps = [snap for snap in snaps
                                if snap.name == snap_name]

+ 12 - 7
test/test_provider_compute_service.py

@@ -31,6 +31,7 @@ class ProviderComputeServiceTestCase(ProviderTestBase):
 
         with helpers.cleanup_action(lambda: cleanup_inst(inst)):
             inst.wait_till_ready(interval=self.get_test_wait_interval())
+
             all_instances = self.provider.compute.instances.list()
             found_instances = [i for i in all_instances if i.name == name]
             self.assertTrue(
@@ -39,14 +40,14 @@ class ProviderComputeServiceTestCase(ProviderTestBase):
                 name)
 
             get_inst = self.provider.compute.instances.get(
-                inst.instance_id)
+                inst.id)
             self.assertTrue(
-                found_instances[0].instance_id ==
-                get_inst.instance_id == inst.instance_id,
+                found_instances[0].id ==
+                get_inst.id == inst.id,
                 "Ids returned by list: {0} and get: {1} are not as "
-                " expected: {2}" .format(found_instances[0].instance_id,
-                                         get_inst.instance_id,
-                                         inst.instance_id))
+                " expected: {2}" .format(found_instances[0].id,
+                                         get_inst.id,
+                                         inst.id))
             self.assertTrue(
                 found_instances[0].name ==
                 get_inst.name == inst.name,
@@ -55,7 +56,7 @@ class ProviderComputeServiceTestCase(ProviderTestBase):
                                          get_inst.name,
                                          inst.name))
         deleted_inst = self.provider.compute.instances.get(
-            inst.instance_id)
+            inst.id)
         self.assertTrue(
             deleted_inst is None or deleted_inst.state in (
                 InstanceState.TERMINATED,
@@ -77,6 +78,10 @@ class ProviderComputeServiceTestCase(ProviderTestBase):
         test_instance = helpers.get_test_instance(self.provider,
                                                   instance_name)
         with helpers.cleanup_action(lambda: test_instance.terminate()):
+            self.assertTrue(
+                test_instance.id in repr(test_instance),
+                "repr(obj) should contain the object id so that the object"
+                " can be reconstructed, but does not. eval(repr(obj)) == obj")
             self.assertEqual(
                 test_instance.name, instance_name,
                 "Instance name {0} is not equal to the expected name"

+ 5 - 0
test/test_provider_image_service.py

@@ -36,6 +36,11 @@ class ProviderImageServiceTestCase(ProviderTestBase):
                 test_image.wait_till_ready(
                     interval=self.get_test_wait_interval())
 
+                self.assertTrue(
+                    test_instance.id in repr(test_instance),
+                    "repr(obj) should contain the object id so that the object"
+                    " can be reconstructed, but does not.")
+
                 self.assertTrue(
                     test_image.description is None or isinstance(
                         test_image.description, six.string_types),

+ 5 - 0
test/test_provider_region_service.py

@@ -25,3 +25,8 @@ class ProviderRegionServiceTestCase(ProviderTestBase):
             region,
             regions[0],
             "List and get methods should return the same regions")
+
+        self.assertTrue(
+            region.id in repr(region),
+            "repr(obj) should contain the object id so that the object"
+            " can be reconstructed, but does not.")

+ 12 - 9
test/test_provider_security_service.py

@@ -44,8 +44,9 @@ class ProviderSecurityServiceTestCase(ProviderTestBase):
                 "List key pairs did not return the expected key {0}."
                 .format(name))
             self.assertTrue(
-                repr(kp) == "<CBKeyPair: {0}>".format(name),
-                "KeyPair repr {0} not matching expected format.".format(kp))
+                kp.name in repr(kp),
+                "repr(obj) should contain the object id so that the object"
+                " can be reconstructed, but does not. eval(repr(obj)) == obj")
             self.assertIsNotNone(
                 kp.material,
                 "KeyPair material is empty but it should not be.")
@@ -98,14 +99,16 @@ class ProviderSecurityServiceTestCase(ProviderTestBase):
             self.assertTrue(
                 len(found_rules) == 1,
                 "Expected rule not found in security group: {0}".format(name))
+
+            object_keys = (
+                sg.rules[0].ip_protocol,
+                sg.rules[0].from_port,
+                sg.rules[0].to_port)
             self.assertTrue(
-                repr(sg.rules[0]) == ("<CBSecurityGroupRule: IP: {0}; from: "
-                                      "{1}; to: {2}>"
-                                      .format(sg.rules[0].ip_protocol,
-                                              sg.rules[0].from_port,
-                                              sg.rules[0].to_port)),
-                ("Security group rule repr {0} not matching expected format."
-                 .format(sg.rules[0])))
+                all(str(key) in repr(sg.rules[0]) for key in object_keys),
+                "repr(obj) should contain ip_protocol, form_port and to_port"
+                " so that the object can be reconstructed, but does not."
+                " eval(repr(obj)) == obj")
             self.assertTrue(
                 sg == sg,
                 "The same security groups should be equal?")