From 2eabe8162811fe7ab4f7f7f1541f9a97eac5b1ad Mon Sep 17 00:00:00 2001 From: sebv Date: Tue, 24 Oct 2023 15:36:56 +0800 Subject: [PATCH] fix GetCanonicalServiceName --- internal/imagerunner/utils.go | 5 ++++- internal/imagerunner/utils_test.go | 16 ++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) create mode 100644 internal/imagerunner/utils_test.go diff --git a/internal/imagerunner/utils.go b/internal/imagerunner/utils.go index cc9442076..317a2d8d4 100644 --- a/internal/imagerunner/utils.go +++ b/internal/imagerunner/utils.go @@ -6,12 +6,15 @@ import ( ) func GetCanonicalServiceName(serviceName string) string { + serviceName = strings.TrimSpace(serviceName) if serviceName == "" { return "" } // make sure the service name has only lowercase letters, numbers, and underscores - canonicalName := strings.ToLower(regexp.MustCompile("[^a-z0-9-]").ReplaceAllString(serviceName, "-")) + canonicalName := strings.ToLower(regexp.MustCompile("[^a-zA-Z0-9-]").ReplaceAllString(serviceName, "-")) // remove successives dashes canonicalName = regexp.MustCompile("-+").ReplaceAllString(canonicalName, "-") + // avoids container names starting with dash + canonicalName = regexp.MustCompile("^-").ReplaceAllString(canonicalName, "s-") return canonicalName } diff --git a/internal/imagerunner/utils_test.go b/internal/imagerunner/utils_test.go new file mode 100644 index 000000000..e13d4abb7 --- /dev/null +++ b/internal/imagerunner/utils_test.go @@ -0,0 +1,16 @@ +package imagerunner + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestUtils_GetCanonicalServiceName(t *testing.T) { + assert.Equal(t, "", GetCanonicalServiceName("")) + assert.Equal(t, "foo", GetCanonicalServiceName("foo")) + assert.Equal(t, "fo123o", GetCanonicalServiceName("FO123o")) + assert.Equal(t, "fo1-23o", GetCanonicalServiceName("FO1_23o")) + assert.Equal(t, "fo1-23o", GetCanonicalServiceName(" FO1 23o ")) + assert.Equal(t, "s-23ao", GetCanonicalServiceName("_23Ao")) +}