diff --git a/pkg/envconf/config.go b/pkg/envconf/config.go index 1534c564..5e6da96b 100644 --- a/pkg/envconf/config.go +++ b/pkg/envconf/config.go @@ -303,5 +303,8 @@ func RandomName(prefix string, n int) string { rand.Seed(time.Now().UnixNano()) p := make([]byte, n) rand.Read(p) + if prefix == "" { + return hex.EncodeToString(p)[:n] + } return fmt.Sprintf("%s-%s", prefix, hex.EncodeToString(p))[:n] } diff --git a/pkg/envconf/config_test.go b/pkg/envconf/config_test.go index 5da18c64..5be48838 100644 --- a/pkg/envconf/config_test.go +++ b/pkg/envconf/config_test.go @@ -19,6 +19,7 @@ package envconf import ( "flag" "os" + "strings" "testing" ) @@ -85,3 +86,19 @@ func TestConfig_New_WithIgnorePanicRecovery(t *testing.T) { t.Error("expected ignore-panic-recovery mode to be enabled when -disable-graceful-teardown argument is passed") } } + +func TestRandomName(t *testing.T) { + t.Run("no prefix yields random name without dash", func(t *testing.T) { + out := RandomName("", 16) + if strings.Contains(out, "-") { + t.Errorf("random name %q shouldn't contain a dash when no prefix provided", out) + } + }) + + t.Run("non empty prefix yields random name with dash", func(t *testing.T) { + out := RandomName("abc", 16) + if !strings.Contains(out, "-") { + t.Errorf("random name %q should contain a dash when prefix provided", out) + } + }) +}