Răsfoiți Sursa

Adjusted code and tests to validate labels and names identically

Nuwan Goonasekera 7 ani în urmă
părinte
comite
21f0ad3a1b

+ 7 - 18
cloudbridge/cloud/base/resources.py

@@ -60,27 +60,16 @@ class BaseCloudResource(CloudResource):
     """
     Base implementation of a CloudBridge Resource.
     """
-    # Regular expression for valid cloudbridge resource labels.
-    # Can be None or any alphanumeric string that does not start
-    # or end with a dash
-    # based on: https://stackoverflow.com/questions/2525327/regex-for-a-za-z0-9
+    # Regular expression for valid cloudbridge resource names/labels.
+    # Can be alphanumeric string that does not start or end with a dash
+    # Must be at least 3 characters in length.
+    # Ref: https://stackoverflow.com/questions/2525327/regex-for-a-za-z0-9
     # -with-dashes-allowed-in-between-but-not-at-the-start-or-e
-    CB_LABEL_PATTERN = re.compile(
-        r"(?=[a-z0-9\-]{1,63}$)^[a-z0-9]+(\-[a-z0-9]+)*$")
-    # Same as the above, but must be at least 3 characters in length
     CB_NAME_PATTERN = re.compile(r"^[a-z][-a-z0-9]{1,61}[a-z0-9]$")
 
     def __init__(self, provider):
         self.__provider = provider
 
-    @staticmethod
-    def is_valid_resource_label(label):
-        if not label:
-            return False
-        else:
-            return (True if BaseCloudResource.CB_LABEL_PATTERN.match(label)
-                    else False)
-
     @staticmethod
     def is_valid_resource_name(name):
         if not name:
@@ -91,7 +80,7 @@ class BaseCloudResource(CloudResource):
 
     @staticmethod
     def assert_valid_resource_label(name):
-        if not BaseCloudResource.is_valid_resource_label(name):
+        if not BaseCloudResource.is_valid_resource_name(name):
             log.debug("InvalidLabelException raised on %s", name)
             raise InvalidLabelException(
                 u"Invalid label: %s. Label must be at most 63 characters "
@@ -738,14 +727,14 @@ class BaseBucketObject(BaseCloudResource, BucketObject):
     #
     # Note: The following regex is based on: https://stackoverflow.com/question
     # s/537772/what-is-the-most-correct-regular-expression-for-a-unix-file-path
-    CB_LABEL_PATTERN = re.compile(r"[^\0]+")
+    CB_NAME_PATTERN = re.compile(r"[^\0]+")
 
     def __init__(self, provider):
         super(BaseBucketObject, self).__init__(provider)
 
     @staticmethod
     def is_valid_resource_name(name):
-        return (True if BaseBucketObject.CB_LABEL_PATTERN.match(name)
+        return (True if BaseBucketObject.CB_NAME_PATTERN.match(name)
                 else False)
 
     @staticmethod

+ 9 - 23
test/helpers/standard_interface_tests.py

@@ -216,7 +216,7 @@ def check_standard_behaviour(test, service, obj):
 
 
 def check_create(test, service, iface, name_prefix,
-                 create_func, cleanup_func, supports_labels):
+                 create_func, cleanup_func):
     # check create with invalid label
     with test.assertRaises(InvalidLabelException):
         # spaces should raise an exception
@@ -245,27 +245,17 @@ def check_create(test, service, iface, name_prefix,
     with test.assertRaises(InvalidLabelException):
         create_func("197.10.100.42")
 
-    if supports_labels:
-        # Comment out this test for now because actually creating two
-        # objects violates certain test assumptions
-        pass
-        # empty labels should be allowed
-        # obj = None
-#         with helpers.cleanup_action(lambda: cleanup_func(obj)):
-#             obj = create_func(None)
-    else:  # supports name only
-        # empty name are not allowed
-        with test.assertRaises(InvalidLabelException):
-            create_func(None)
-        # names of length less than 3 should raise an exception
-        with test.assertRaises(InvalidLabelException):
-            create_func("cb")
+    # empty name are not allowed
+    with test.assertRaises(InvalidLabelException):
+        create_func(None)
+    # names of length less than 3 should raise an exception
+    with test.assertRaises(InvalidLabelException):
+        create_func("cb")
 
 
 def check_crud(test, service, iface, label_prefix,
                create_func, cleanup_func, extra_test_func=None,
-               custom_check_delete=None, supports_labels=True,
-               skip_name_check=False):
+               custom_check_delete=None, skip_name_check=False):
     """
     Checks crud behaviour of a given cloudbridge service. The create_func will
     be used as a factory function to create a service object and the
@@ -311,10 +301,6 @@ def check_crud(test, service, iface, label_prefix,
                                 instead of the standard check_delete function
                                 to make sure that the object has been deleted.
 
-    :type  supports_labels: ``boolean``
-    :param supports_labels:  Indicates whether the resource supports labels.
-        If so, label related tests will be run.
-
     :type  skip_name_check: ``boolean``
     :param skip_name_check:  If True, the name related checking will be
                              skipped.
@@ -326,7 +312,7 @@ def check_crud(test, service, iface, label_prefix,
         label = "{0}-{1}".format(label_prefix, helpers.get_uuid())
         if not skip_name_check:
             check_create(test, service, iface, label_prefix,
-                         create_func, cleanup_func, supports_labels)
+                         create_func, cleanup_func)
         obj = create_func(label)
         if issubclass(iface, ObjectLifeCycleMixin):
             obj.wait_till_ready()

+ 1 - 1
test/test_network_service.py

@@ -120,7 +120,7 @@ class CloudNetworkServiceTestCase(ProviderTestBase):
                 lambda: helpers.delete_test_gateway(net, gw)):
             sit.check_crud(self, gw.floating_ips, FloatingIP,
                            "cb-crudfip", create_fip, cleanup_fip,
-                           skip_name_check=True, supports_labels=False)
+                           skip_name_check=True)
 
     def test_floating_ip_properties(self):
         # Check floating IP address

+ 1 - 1
test/test_object_store_service.py

@@ -42,7 +42,7 @@ class CloudObjectStoreServiceTestCase(ProviderTestBase):
 
         sit.check_crud(self, self.provider.storage.buckets, Bucket,
                        "cb-crudbucket", create_bucket, cleanup_bucket,
-                       supports_labels=False, extra_test_func=extra_tests)
+                       extra_test_func=extra_tests)
 
     @helpers.skipIfNoService(['storage.buckets'])
     def test_crud_bucket_object(self):

+ 1 - 1
test/test_security_service.py

@@ -32,7 +32,7 @@ class CloudSecurityServiceTestCase(ProviderTestBase):
 
         sit.check_crud(self, self.provider.security.key_pairs, KeyPair,
                        "cb-crudkp", create_kp, cleanup_kp,
-                       supports_labels=False, extra_test_func=extra_tests)
+                       extra_test_func=extra_tests)
 
     @helpers.skipIfNoService(['security.key_pairs'])
     def test_key_pair_properties(self):