Files
coder/scaletest/loadtestutil/names.go
T
Ethan 6d9e29beb1 refactor(scaletest): generate user and workspace names if omitted (#19885)
Relates to https://github.com/coder/internal/issues/985.

Some scaletest runners would autogenerate names if they weren't supplied on the config, while others required a name be supplied, and a name was autogenerated in the CLI command handler. This PR unifies the runners to make names and emails optional on each config, and generate them in the scaletest runner if omitted. 

The create user runner in the PR above in the stack will do this too.
2025-09-22 17:25:47 +10:00

56 lines
1.7 KiB
Go

package loadtestutil
import (
"fmt"
"strings"
"github.com/coder/coder/v2/cryptorand"
)
const (
// Prefix for all scaletest resources (users and workspaces)
ScaleTestPrefix = "scaletest"
// Email domain for scaletest users
EmailDomain = "@scaletest.local"
DefaultRandLength = 8
)
// GenerateUserIdentifier generates a username and email for scale testing.
// The username follows the pattern: scaletest-<random>-<id>
// The email follows the pattern: <random>-<id>@scaletest.local
func GenerateUserIdentifier(id string) (username, email string, err error) {
randStr, err := cryptorand.String(DefaultRandLength)
if err != nil {
return "", "", err
}
username = fmt.Sprintf("%s-%s-%s", ScaleTestPrefix, randStr, id)
email = fmt.Sprintf("%s-%s%s", randStr, id, EmailDomain)
return username, email, nil
}
// GenerateWorkspaceName generates a workspace name for scale testing.
// The workspace name follows the pattern: scaletest-<random>-<id>
func GenerateWorkspaceName(id string) (name string, err error) {
randStr, err := cryptorand.String(DefaultRandLength)
if err != nil {
return "", err
}
return fmt.Sprintf("%s-%s-%s", ScaleTestPrefix, randStr, id), nil
}
// IsScaleTestUser checks if a username indicates it was created for scale testing.
func IsScaleTestUser(username, email string) bool {
return strings.HasPrefix(username, ScaleTestPrefix+"-") ||
strings.HasSuffix(email, EmailDomain)
}
// IsScaleTestWorkspace checks if a workspace name indicates it was created for scale testing.
func IsScaleTestWorkspace(workspaceName, ownerName string) bool {
return strings.HasPrefix(workspaceName, ScaleTestPrefix+"-") ||
strings.HasPrefix(ownerName, ScaleTestPrefix+"-")
}