mirror of
https://github.com/coder/coder.git
synced 2026-06-04 05:28:20 +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
104 lines
1.9 KiB
Go
104 lines
1.9 KiB
Go
package namesgenerator
|
|
|
|
import (
|
|
"strings"
|
|
"testing"
|
|
"unicode"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestTruncate(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
tests := []struct {
|
|
name string
|
|
input string
|
|
maxLen int
|
|
want string
|
|
}{
|
|
{
|
|
name: "no truncation needed",
|
|
input: "foo1",
|
|
maxLen: 10,
|
|
want: "foo1",
|
|
},
|
|
{
|
|
name: "exact fit",
|
|
input: "foo1",
|
|
maxLen: 4,
|
|
want: "foo1",
|
|
},
|
|
{
|
|
name: "truncate base",
|
|
input: "foobar42",
|
|
maxLen: 5,
|
|
want: "foo42",
|
|
},
|
|
{
|
|
name: "truncate more",
|
|
input: "foobar3",
|
|
maxLen: 3,
|
|
want: "fo3",
|
|
},
|
|
{
|
|
name: "long suffix",
|
|
input: "foo123456",
|
|
maxLen: 8,
|
|
want: "fo123456",
|
|
},
|
|
{
|
|
name: "realistic name",
|
|
input: "condescending_proskuriakova999999",
|
|
maxLen: 32,
|
|
want: "condescending_proskuriakov999999",
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
t.Parallel()
|
|
got := truncate(tt.input, tt.maxLen)
|
|
assert.Equal(t, tt.want, got)
|
|
assert.LessOrEqual(t, len(got), tt.maxLen)
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestUniqueNameLength(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
// Generate many names to exercise the truncation logic.
|
|
const iter = 10000
|
|
for range iter {
|
|
name := UniqueName()
|
|
assert.LessOrEqual(t, len(name), maxNameLen)
|
|
assert.Contains(t, name, "_")
|
|
assert.Equal(t, name, strings.ToLower(name))
|
|
verifyNoWhitespace(t, name)
|
|
}
|
|
}
|
|
|
|
func TestUniqueNameWithLength(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
// Generate many names with hyphen delimiter.
|
|
const iter = 10000
|
|
for range iter {
|
|
name := UniqueNameWith("-")
|
|
assert.LessOrEqual(t, len(name), maxNameLen)
|
|
assert.Contains(t, name, "-")
|
|
assert.Equal(t, name, strings.ToLower(name))
|
|
verifyNoWhitespace(t, name)
|
|
}
|
|
}
|
|
|
|
func verifyNoWhitespace(t *testing.T, s string) {
|
|
t.Helper()
|
|
for _, r := range s {
|
|
if unicode.IsSpace(r) {
|
|
t.Fatalf("found whitespace in string %q: %v", s, r)
|
|
}
|
|
}
|
|
}
|