fix: replace moby/moby namesgenerator with internal implementation (#21377)
Replace the external moby/moby/pkg/namesgenerator dependency with an
internal implementation using gofakeit/v7. The moby package has ~25k
unique name combinations, and with its retry parameter only adds a
random digit 0-9, giving ~250k possibilities. In parallel tests, this
has led to collisions (flakes).
The new internal API at coderd/util/namesgenerator eliminates the
external dependnecy and offers functions with explicit uniqueness
guarantees. This PR also consolidates fragmented name generation in a
few places to use the new package.
| Old (moby/moby) | New |
|-------------------------------------|------------------------|
| namesgenerator.GetRandomName(0) | NameWith("_") |
| namesgenerator.GetRandomName(>0) | NameDigitWith("_") |
| testutil.GetRandomName(t) | UniqueName() |
| testutil.GetRandomNameHyphenated(t) | UniqueNameWith("-") |
namesgenerator package API:
- NameWith(delim): random name, not unique
- NameDigitWith(delim): random name with 1-9 suffix, not unique
- UniqueName(): guaranteed unique via atomic counter
- UniqueNameWith(delim): unique with custom delimiter
Names continue to be docker style `[adjective][delim][surname]`. Unique
names are truncated to 32 characters (preserving the numeric suffix) to
fit common name length limits in Coder.
Related test flakes:
https://github.com/coder/internal/issues/1212
https://github.com/coder/internal/issues/118
https://github.com/coder/internal/issues/1068
This commit is contained in:
+7
-35
@@ -1,49 +1,21 @@
|
||||
package testutil
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
|
||||
"github.com/moby/moby/pkg/namesgenerator"
|
||||
"github.com/coder/coder/v2/coderd/util/namesgenerator"
|
||||
)
|
||||
|
||||
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.
|
||||
// While this reduces the probability of collisions, it does not negate them.
|
||||
// This function calls namesgenerator.GetRandomName without the retry
|
||||
// parameter and instead increments a monotonically increasing integer
|
||||
// to the return value.
|
||||
// GetRandomName returns a random name with a unique suffix, truncated to 32
|
||||
// characters to fit common name length limits in tests.
|
||||
func GetRandomName(t testing.TB) string {
|
||||
t.Helper()
|
||||
name := namesgenerator.GetRandomName(0)
|
||||
return incSuffix(name, n.Add(1), maxNameLen)
|
||||
return namesgenerator.UniqueName()
|
||||
}
|
||||
|
||||
// GetRandomNameHyphenated is as GetRandomName but uses a hyphen "-" instead of
|
||||
// an underscore.
|
||||
// GetRandomNameHyphenated is like GetRandomName but uses hyphens instead of
|
||||
// underscores.
|
||||
func GetRandomNameHyphenated(t testing.TB) string {
|
||||
t.Helper()
|
||||
name := GetRandomName(t)
|
||||
return strings.ReplaceAll(name, "_", "-")
|
||||
}
|
||||
|
||||
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
|
||||
return namesgenerator.UniqueNameWith("-")
|
||||
}
|
||||
|
||||
@@ -1,52 +0,0 @@
|
||||
package testutil
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestIncSuffix(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
for _, tt := range []struct {
|
||||
s string
|
||||
num int64
|
||||
maxLen int
|
||||
want string
|
||||
}{
|
||||
{
|
||||
s: "foo",
|
||||
num: 1,
|
||||
maxLen: 4,
|
||||
want: "foo1",
|
||||
},
|
||||
{
|
||||
s: "foo",
|
||||
num: 42,
|
||||
maxLen: 3,
|
||||
want: "f42",
|
||||
},
|
||||
{
|
||||
s: "foo",
|
||||
num: 3,
|
||||
maxLen: 2,
|
||||
want: "f3",
|
||||
},
|
||||
{
|
||||
s: "foo",
|
||||
num: 4,
|
||||
maxLen: 1,
|
||||
want: "4",
|
||||
},
|
||||
{
|
||||
s: "foo",
|
||||
num: 0,
|
||||
maxLen: 0,
|
||||
want: "",
|
||||
},
|
||||
} {
|
||||
actual := incSuffix(tt.s, tt.num, tt.maxLen)
|
||||
assert.Equal(t, tt.want, actual)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user