Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Forge] Small fix to sanitize_forge_resource_name. #8909

Merged
merged 1 commit into from
Jun 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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