Repository: cloudtools/troposphere
Pull request #2125 (2023-02-22 15:34:35)

Fix valid values used by validators
Also fixed a broken `.join()` that I found while writing tests.


Patch:
diff --git a/troposphere/validators/appconfig.py b/troposphere/validators/appconfig.py
index 2ead80d4..055eb9a0 100644
--- a/troposphere/validators/appconfig.py
+++ b/troposphere/validators/appconfig.py
@@ -9,12 +9,12 @@ def validate_growth_type(growth_type):
     Property: DeploymentStrategy.GrowthType
     """
 
-    VALID_GROWTH_TYPE = "LINEAR"
+    VALID_GROWTH_TYPES = ("LINEAR",)
 
-    if growth_type not in VALID_GROWTH_TYPE:
+    if growth_type not in VALID_GROWTH_TYPES:
         raise ValueError(
             "DeploymentStrategy GrowthType must be one of: %s"
-            % ", ".join(VALID_GROWTH_TYPE)
+            % ", ".join(VALID_GROWTH_TYPES)
         )
     return growth_type
 
diff --git a/troposphere/validators/codebuild.py b/troposphere/validators/codebuild.py
index f6428701..5390fda2 100644
--- a/troposphere/validators/codebuild.py
+++ b/troposphere/validators/codebuild.py
@@ -51,7 +51,7 @@ def validate_credentials_provider(credential_provider):
     Property: RegistryCredential.CredentialProvider
     """
 
-    VALID_CREDENTIAL_PROVIDERS = "SECRETS_MANAGER"
+    VALID_CREDENTIAL_PROVIDERS = ("SECRETS_MANAGER",)
 
     if credential_provider not in VALID_CREDENTIAL_PROVIDERS:
         raise ValueError(
@@ -89,12 +89,12 @@ def validate_projectfilesystemlocation_type(projectfilesystemlocation_type):
     Property: ProjectFileSystemLocation.Type
     """
 
-    VALID_PROJECTFILESYSTEMLOCATION_TYPE = "EFS"
+    VALID_PROJECTFILESYSTEMLOCATION_TYPES = ("EFS",)
 
-    if projectfilesystemlocation_type not in VALID_PROJECTFILESYSTEMLOCATION_TYPE:
+    if projectfilesystemlocation_type not in VALID_PROJECTFILESYSTEMLOCATION_TYPES:
         raise ValueError(
             "ProjectFileSystemLocation Type must be one of: %s"
-            % ", ".join(VALID_PROJECTFILESYSTEMLOCATION_TYPE)
+            % ", ".join(VALID_PROJECTFILESYSTEMLOCATION_TYPES)
         )
     return projectfilesystemlocation_type
 
diff --git a/troposphere/validators/dlm.py b/troposphere/validators/dlm.py
index a9d13c2c..8607402d 100644
--- a/troposphere/validators/dlm.py
+++ b/troposphere/validators/dlm.py
@@ -25,7 +25,10 @@ def validate_interval(interval):
     VALID_INTERVALS = (2, 3, 4, 6, 8, 12, 24)
 
     if interval not in VALID_INTERVALS:
-        raise ValueError("Interval must be one of : %s" % ", ".join(VALID_INTERVALS))
+        raise ValueError(
+            "Interval must be one of : %s"
+            % ", ".join([str(i) for i in VALID_INTERVALS])
+        )
     return interval
 
 
@@ -35,7 +38,7 @@ def validate_interval_unit(interval_unit):
     Property: CreateRule.IntervalUnit
     """
 
-    VALID_INTERVAL_UNITS = "HOURS"
+    VALID_INTERVAL_UNITS = ("HOURS",)
 
     if interval_unit not in VALID_INTERVAL_UNITS:
         raise ValueError(


Test patch:
diff --git a/tests/test_appconfig.py b/tests/test_appconfig.py
new file mode 100644
index 00000000..fe42431c
--- /dev/null
+++ b/tests/test_appconfig.py
@@ -0,0 +1,47 @@
+import unittest
+
+from troposphere.appconfig import DeploymentStrategy, Validators
+
+
+class TestAppconfigDeploymentStrategy(unittest.TestCase):
+    def test_deploymentstrategy_growthtype_bad_value(self):
+        with self.assertRaisesRegex(ValueError, "GrowthType must be one of"):
+            DeploymentStrategy(
+                "DeploymentStrategy",
+                DeploymentDurationInMinutes=1,
+                GrowthFactor=1,
+                GrowthType="LINEA",
+                Name="DeploymentStrategy",
+                ReplicateTo="NONE",
+            )
+
+    def test_deploymentstrategy_replicateto_bad_value(self):
+        with self.assertRaisesRegex(ValueError, "ReplicateTo must be one of"):
+            DeploymentStrategy(
+                "DeploymentStrategy",
+                DeploymentDurationInMinutes=1,
+                GrowthFactor=1,
+                Name="DeploymentStrategy",
+                ReplicateTo="none",
+            )
+
+    def test_deploymentstrategy(self):
+        for replicate_to in ("NONE", "SSM_DOCUMENT"):
+            DeploymentStrategy(
+                "DeploymentStrategy",
+                DeploymentDurationInMinutes=1,
+                GrowthFactor=1,
+                GrowthType="LINEAR",
+                Name="DeploymentStrategy",
+                ReplicateTo=replicate_to,
+            )
+
+
+class TestAppconfigValidators(unittest.TestCase):
+    def test_validators_type_bad_value(self):
+        with self.assertRaisesRegex(ValueError, "Validator Type must be one of"):
+            Validators(Type="JSON_SCHEM")
+
+    def test_validators_type(self):
+        for validator_type in ("JSON_SCHEMA", "LAMBDA"):
+            Validators(Type=validator_type)
diff --git a/tests/test_codebuild.py b/tests/test_codebuild.py
index 818e072b..3e70bd86 100644
--- a/tests/test_codebuild.py
+++ b/tests/test_codebuild.py
@@ -84,3 +84,37 @@ class TestCodeBuildFilters(unittest.TestCase):
                     [None],
                 ]
             ).validate()
+
+
+class TestCodeBuildRegistryCredential(unittest.TestCase):
+    def test_registrycredential_provider_bad_value(self):
+        with self.assertRaisesRegex(ValueError, "CredentialProvider must be one of"):
+            codebuild.RegistryCredential(
+                Credential="Foo",
+                CredentialProvider="SECRETS",
+            )
+
+    def test_registrycredential(self):
+        codebuild.RegistryCredential(
+            Credential="Foo",
+            CredentialProvider="SECRETS_MANAGER",
+        )
+
+
+class TestCodeBuildProjectFileSystemLocation(unittest.TestCase):
+    def test_projectfilesystemlocation_type_bad_value(self):
+        with self.assertRaisesRegex(ValueError, "Type must be one of"):
+            codebuild.ProjectFileSystemLocation(
+                Identifier="foo",
+                Location="bar",
+                MountPoint="baz",
+                Type="EF",
+            )
+
+    def test_projectfilesystemlocation(self):
+        codebuild.ProjectFileSystemLocation(
+            Identifier="foo",
+            Location="bar",
+            MountPoint="baz",
+            Type="EFS",
+        )
diff --git a/tests/test_dlm.py b/tests/test_dlm.py
new file mode 100644
index 00000000..044deb8f
--- /dev/null
+++ b/tests/test_dlm.py
@@ -0,0 +1,16 @@
+import unittest
+
+from troposphere.dlm import CreateRule
+
+
+class TestDlmCreateRule(unittest.TestCase):
+    def test_createrule_interval_bad_value(self):
+        with self.assertRaisesRegex(ValueError, "Interval must be one of"):
+            CreateRule("CreateRule", Interval=25)
+
+    def test_createrule_intervalunit_bad_value(self):
+        with self.assertRaisesRegex(ValueError, "Interval unit must be one of"):
+            CreateRule("CreateRule", Interval=24, IntervalUnit="HOUR")
+
+    def test_createrule(self):
+        CreateRule("CreateRule", Interval=24, IntervalUnit="HOURS")