mirror of
https://github.com/coder/coder.git
synced 2026-06-02 20:48:20 +00:00
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:
@@ -30,17 +30,17 @@ import (
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
"time"
|
||||
"unicode"
|
||||
|
||||
"cloud.google.com/go/compute/metadata"
|
||||
"github.com/fullsailor/pkcs7"
|
||||
"github.com/go-chi/chi/v5"
|
||||
"github.com/golang-jwt/jwt/v4"
|
||||
"github.com/google/uuid"
|
||||
"github.com/moby/moby/pkg/namesgenerator"
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
"golang.org/x/text/cases"
|
||||
"golang.org/x/text/language"
|
||||
"golang.org/x/xerrors"
|
||||
"google.golang.org/api/idtoken"
|
||||
"google.golang.org/api/option"
|
||||
@@ -80,6 +80,7 @@ import (
|
||||
"github.com/coder/coder/v2/coderd/schedule"
|
||||
"github.com/coder/coder/v2/coderd/telemetry"
|
||||
"github.com/coder/coder/v2/coderd/updatecheck"
|
||||
"github.com/coder/coder/v2/coderd/util/namesgenerator"
|
||||
"github.com/coder/coder/v2/coderd/util/ptr"
|
||||
"github.com/coder/coder/v2/coderd/webpush"
|
||||
"github.com/coder/coder/v2/coderd/workspaceapps"
|
||||
@@ -792,7 +793,7 @@ func AuthzUserSubject(user codersdk.User, orgID uuid.UUID) rbac.Subject {
|
||||
|
||||
func createAnotherUserRetry(t testing.TB, client *codersdk.Client, organizationIDs []uuid.UUID, retries int, roles []rbac.RoleIdentifier, mutators ...func(r *codersdk.CreateUserRequestWithOrgs)) (*codersdk.Client, codersdk.User) {
|
||||
req := codersdk.CreateUserRequestWithOrgs{
|
||||
Email: namesgenerator.GetRandomName(10) + "@coder.com",
|
||||
Email: namesgenerator.UniqueName() + "@coder.com",
|
||||
Username: RandomUsername(t),
|
||||
Name: RandomName(t),
|
||||
Password: "SomeSecurePassword!",
|
||||
@@ -1556,37 +1557,15 @@ func NewAzureInstanceIdentity(t testing.TB, instanceID string) (x509.VerifyOptio
|
||||
}
|
||||
}
|
||||
|
||||
func RandomUsername(t testing.TB) string {
|
||||
suffix, err := cryptorand.String(3)
|
||||
require.NoError(t, err)
|
||||
suffix = "-" + suffix
|
||||
n := strings.ReplaceAll(namesgenerator.GetRandomName(10), "_", "-") + suffix
|
||||
if len(n) > 32 {
|
||||
n = n[:32-len(suffix)] + suffix
|
||||
}
|
||||
return n
|
||||
func RandomUsername(_ testing.TB) string {
|
||||
return namesgenerator.UniqueNameWith("-")
|
||||
}
|
||||
|
||||
func RandomName(t testing.TB) string {
|
||||
var sb strings.Builder
|
||||
var err error
|
||||
ss := strings.Split(namesgenerator.GetRandomName(10), "_")
|
||||
for si, s := range ss {
|
||||
for ri, r := range s {
|
||||
if ri == 0 {
|
||||
_, err = sb.WriteRune(unicode.ToTitle(r))
|
||||
require.NoError(t, err)
|
||||
} else {
|
||||
_, err = sb.WriteRune(r)
|
||||
require.NoError(t, err)
|
||||
}
|
||||
}
|
||||
if si < len(ss)-1 {
|
||||
_, err = sb.WriteRune(' ')
|
||||
require.NoError(t, err)
|
||||
}
|
||||
}
|
||||
return sb.String()
|
||||
// RandomName returns a random name in title case (e.g. "Happy Einstein").
|
||||
func RandomName(_ testing.TB) string {
|
||||
return cases.Title(language.English).String(
|
||||
namesgenerator.NameWith(" "),
|
||||
)
|
||||
}
|
||||
|
||||
// Used to easily create an HTTP transport!
|
||||
|
||||
Reference in New Issue
Block a user