diff --git a/pkg/slug/slug.go b/pkg/slug/slug.go index 28aeaa958e..a5fc3ec293 100644 --- a/pkg/slug/slug.go +++ b/pkg/slug/slug.go @@ -30,7 +30,7 @@ func Slug(data string) string { } func LimitedSlug(data string, slugMaxSize int) string { - if len(data) == 0 || slugify(data) == data && len(data) < slugMaxSize { + if len(data) == 0 || slugify(data) == data && len(data) <= slugMaxSize { return data } diff --git a/pkg/slug/slug_test.go b/pkg/slug/slug_test.go index 6e660280e9..f916a63fcd 100644 --- a/pkg/slug/slug_test.go +++ b/pkg/slug/slug_test.go @@ -47,6 +47,10 @@ func TestSlug(t *testing.T) { if len(result) > DefaultSlugMaxSize { t.Errorf("Max size exceeded: [EXPECTED]: %d [GOT]: %d", DefaultSlugMaxSize, len(result)) } + + tRunIdempotence(t, test.name, test.data, func(s string) string { + return LimitedSlug(s, DefaultSlugMaxSize) + }) }) } } @@ -90,6 +94,8 @@ func TestDockerTag(t *testing.T) { t.Errorf("Max size exceeded: [EXPECTED]: %d [GOT]: %d", dockerTagMaxSize, len(result)) } }) + + tRunIdempotence(t, test.name, test.data, DockerTag) } } @@ -137,6 +143,8 @@ func TestHelmRelease(t *testing.T) { t.Errorf("Max size exceeded: [EXPECTED]: %d [GOT]: %d", helmReleaseMaxSize, len(result)) } }) + + tRunIdempotence(t, test.name, test.data, HelmRelease) } } @@ -179,5 +187,17 @@ func TestKubernetesNamespace(t *testing.T) { t.Errorf("Max size exceeded: [EXPECTED]: %d [GOT]: %d", kubernetesNamespaceMaxSize, len(result)) } }) + + tRunIdempotence(t, test.name, test.data, KubernetesNamespace) } } + +func tRunIdempotence(t *testing.T, testName, testData string, slugger func(string) string) { + t.Run(testName+"-idempotence", func(t *testing.T) { + firstResult := slugger(testData) + secondResult := slugger(firstResult) + if firstResult != secondResult { + t.Errorf("\n[EXPECTED]: %s (%d)\n[GOT]: %s (%d)", firstResult, len(firstResult), secondResult, len(secondResult)) + } + }) +}