mirror of
https://github.com/coder/coder.git
synced 2025-07-13 21:36:50 +00:00
fix(testutil): ensure GetRandomName never returns strings greater tha… (#14153)
This commit is contained in:
@ -10,6 +10,8 @@ import (
|
||||
|
||||
var n atomic.Int64
|
||||
|
||||
const maxNameLen = 32
|
||||
|
||||
// GetRandomName returns a random name using moby/pkg/namesgenerator.
|
||||
// namesgenerator.GetRandomName exposes a retry parameter that appends
|
||||
// a pseudo-random number between 1 and 10 to its return value.
|
||||
@ -19,5 +21,20 @@ var n atomic.Int64
|
||||
// to the return value.
|
||||
func GetRandomName(t testing.TB) string {
|
||||
t.Helper()
|
||||
return namesgenerator.GetRandomName(0) + strconv.FormatInt(n.Add(1), 10)
|
||||
name := namesgenerator.GetRandomName(0)
|
||||
return incSuffix(name, n.Add(1), maxNameLen)
|
||||
}
|
||||
|
||||
func incSuffix(s string, num int64, maxLen int) string {
|
||||
suffix := strconv.FormatInt(num, 10)
|
||||
if len(s)+len(suffix) <= maxLen {
|
||||
return s + suffix
|
||||
}
|
||||
stripLen := (len(s) + len(suffix)) - maxLen
|
||||
stripIdx := len(s) - stripLen
|
||||
if stripIdx < 0 {
|
||||
return ""
|
||||
}
|
||||
s = s[:stripIdx]
|
||||
return s + suffix
|
||||
}
|
||||
|
Reference in New Issue
Block a user