From eac230f93efeb40bd1ff5e3d59c79fd7de641602 Mon Sep 17 00:00:00 2001 From: Tim Ramlot <42113979+inteon@users.noreply.github.com> Date: Fri, 22 Sep 2023 12:44:52 +0200 Subject: [PATCH] add more test cases and fix typo Signed-off-by: Tim Ramlot <42113979+inteon@users.noreply.github.com> --- pkg/api/util/names.go | 2 +- pkg/api/util/names_test.go | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/pkg/api/util/names.go b/pkg/api/util/names.go index 5aec2dfea..a61c1a1be 100644 --- a/pkg/api/util/names.go +++ b/pkg/api/util/names.go @@ -82,7 +82,7 @@ func ComputeSecureUniqueDeterministicNameFromData(fullName string, maxNameLength return fmt.Sprintf("%s-%08x", prefix, hashResult), nil } -// DNSSafeShortenToNCharacters shortens the input string to 52 chars and ensures the last char is an alpha-numeric character. +// DNSSafeShortenToNCharacters shortens the input string to N chars and ensures the last char is an alpha-numeric character. func DNSSafeShortenToNCharacters(in string, maxLength int) string { var alphaNumeric = regexp.MustCompile(`[a-zA-Z\d]`) diff --git a/pkg/api/util/names_test.go b/pkg/api/util/names_test.go index 5a5742c1c..477e67989 100644 --- a/pkg/api/util/names_test.go +++ b/pkg/api/util/names_test.go @@ -122,6 +122,26 @@ func TestDNSSafeShortenToNCharacters(t *testing.T) { } tests := []testcase{ + { + in: "aaaaaaaaaaaaaaa", + maxLength: 0, + expOut: "", + }, + { + in: "aa-----aaaa", + maxLength: 5, + expOut: "aa", + }, + { + in: "aa11111aaaa", + maxLength: 5, + expOut: "aa111", + }, + { + in: "aaAAAAAaaaa", + maxLength: 5, + expOut: "aaAAA", + }, { in: "aaaaaaaaaaaaaaa", maxLength: 3,