mirror of
https://github.com/coder/coder.git
synced 2026-06-03 13:08:25 +00:00
091d31224d
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
22 lines
527 B
Go
22 lines
527 B
Go
package testutil
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/coder/coder/v2/coderd/util/namesgenerator"
|
|
)
|
|
|
|
// 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()
|
|
return namesgenerator.UniqueName()
|
|
}
|
|
|
|
// GetRandomNameHyphenated is like GetRandomName but uses hyphens instead of
|
|
// underscores.
|
|
func GetRandomNameHyphenated(t testing.TB) string {
|
|
t.Helper()
|
|
return namesgenerator.UniqueNameWith("-")
|
|
}
|