Skip to content

Commit

Permalink
Merge pull request kubernetes-sigs#327 from pmalek/fix-random-name-dash
Browse files Browse the repository at this point in the history
fix: fix RandomName's dash when no prefix provided
  • Loading branch information
k8s-ci-robot authored Oct 7, 2023
2 parents f03df76 + ba5be0f commit 74e7870
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
3 changes: 3 additions & 0 deletions pkg/envconf/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]
}
17 changes: 17 additions & 0 deletions pkg/envconf/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package envconf
import (
"flag"
"os"
"strings"
"testing"
)

Expand Down Expand Up @@ -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)
}
})
}

0 comments on commit 74e7870

Please sign in to comment.