Skip to content

Commit

Permalink
[Forge] Rename forge namespace.
Browse files Browse the repository at this point in the history
  • Loading branch information
JoshLind committed Jun 30, 2023
1 parent 13239f4 commit ebd0ee5
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
18 changes: 14 additions & 4 deletions testsuite/forge.py
Original file line number Diff line number Diff line change
Expand Up @@ -988,18 +988,28 @@ def image_exists(

def sanitize_forge_resource_name(forge_resource: str, max_length: int = 63) -> str:
"""
Sanitize the intended forge resource name to be a valid k8s resource name
Sanitize the intended forge resource name to be a valid k8s resource name.
Resource names must be: (i) 63 characters or less; (ii) contain characters
that are alphanumeric, '-', or '.'; (iii) start and end with an alphanumeric
character; and (iv) start with "forge-".
"""
if not forge_resource.startswith("forge-"):
raise Exception("Forge resource name must start with 'forge-'")

sanitized_namespace = ""
for i, c in enumerate(forge_resource):
if i >= max_length:
break
if c.isalnum():
sanitized_namespace += c
else:
sanitized_namespace += "-"
if not forge_resource.startswith("forge-"):
raise Exception("Forge resource name must start with 'forge-'")
sanitized_namespace += "-" # Replace the invalid character with a '-'

if sanitized_namespace.endswith("-"):
sanitized_namespace = (
sanitized_namespace[:-1] + "0"
) # Set the last character to '0'

return sanitized_namespace


Expand Down
11 changes: 11 additions & 0 deletions testsuite/forge_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -623,11 +623,22 @@ def testFormatReport(self) -> None:
"testFormatReport.fixture",
)

def testSanitizeForgeNamespaceLastCharacter(self) -> None:
namespace_with_invalid_last_char = "forge-$$$"
namespace = sanitize_forge_resource_name(namespace_with_invalid_last_char)
self.assertEqual(namespace, "forge---0")

def testSanitizeForgeNamespaceSlashes(self) -> None:
namespace_with_slash = "forge-banana/apple"
namespace = sanitize_forge_resource_name(namespace_with_slash)
self.assertEqual(namespace, "forge-banana-apple")

def testSanitizeForgeNamespaceStartsWith(self) -> None:
namespace_with_invalid_start = "frog-"
self.assertRaises(
Exception, sanitize_forge_resource_name, namespace_with_invalid_start
)

def testSanitizeForgeNamespaceTooLong(self) -> None:
namespace_too_long = "forge-" + "a" * 10000
namespace = sanitize_forge_resource_name(namespace_too_long)
Expand Down

0 comments on commit ebd0ee5

Please sign in to comment.