From d02ff5f9b2fb70bfeeaeb43b5d3b43f35a78db5b Mon Sep 17 00:00:00 2001 From: Ethan <39577870+ethanndickson@users.noreply.github.com> Date: Mon, 22 Sep 2025 17:35:36 +1000 Subject: [PATCH] refactor(scaletest): add runner for creating users (#19811) Closes https://github.com/coder/internal/issues/985 Simple refactor of the user creation logic into it's own test runner. This lets us create users independently of workspaces, for use in a bunch of load generators, including the Coder Connect load generator. This PR creates the new runner, and has the existing `createworkspaces` runner use it. --- scaletest/createusers/config.go | 23 +++++++ scaletest/createusers/run.go | 106 ++++++++++++++++++++++++++++++ scaletest/createworkspaces/run.go | 51 +++++--------- 3 files changed, 145 insertions(+), 35 deletions(-) create mode 100644 scaletest/createusers/config.go create mode 100644 scaletest/createusers/run.go diff --git a/scaletest/createusers/config.go b/scaletest/createusers/config.go new file mode 100644 index 0000000000..e5bb1f3409 --- /dev/null +++ b/scaletest/createusers/config.go @@ -0,0 +1,23 @@ +package createusers + +import ( + "github.com/google/uuid" + "golang.org/x/xerrors" +) + +type Config struct { + // OrganizationID is the ID of the organization to add the user to. + OrganizationID uuid.UUID `json:"organization_id"` + // Username is the username of the new user. Generated if empty. + Username string `json:"username"` + // Email is the email of the new user. Generated if empty. + Email string `json:"email"` +} + +func (c Config) Validate() error { + if c.OrganizationID == uuid.Nil { + return xerrors.New("organization_id must not be a nil UUID") + } + + return nil +} diff --git a/scaletest/createusers/run.go b/scaletest/createusers/run.go new file mode 100644 index 0000000000..956ef7d361 --- /dev/null +++ b/scaletest/createusers/run.go @@ -0,0 +1,106 @@ +package createusers + +import ( + "context" + "fmt" + "io" + + "github.com/google/uuid" + "golang.org/x/xerrors" + + "cdr.dev/slog" + "cdr.dev/slog/sloggers/sloghuman" + + "github.com/coder/coder/v2/coderd/tracing" + "github.com/coder/coder/v2/codersdk" + "github.com/coder/coder/v2/cryptorand" + "github.com/coder/coder/v2/scaletest/loadtestutil" +) + +type Runner struct { + client *codersdk.Client + cfg Config + + user codersdk.User +} + +type User struct { + codersdk.User + SessionToken string +} + +func NewRunner(client *codersdk.Client, cfg Config) *Runner { + return &Runner{ + client: client, + cfg: cfg, + } +} + +func (r *Runner) RunReturningUser(ctx context.Context, id string, logs io.Writer) (User, error) { + ctx, span := tracing.StartSpan(ctx) + defer span.End() + + logs = loadtestutil.NewSyncWriter(logs) + logger := slog.Make(sloghuman.Sink(logs)).Leveled(slog.LevelDebug) + r.client.SetLogger(logger) + r.client.SetLogBodies(true) + + if r.cfg.Username == "" || r.cfg.Email == "" { + genUsername, genEmail, err := loadtestutil.GenerateUserIdentifier(id) + if err != nil { + return User{}, xerrors.Errorf("generate user identifier: %w", err) + } + if r.cfg.Username == "" { + r.cfg.Username = genUsername + } + if r.cfg.Email == "" { + r.cfg.Email = genEmail + } + } + + _, _ = fmt.Fprintln(logs, "Generating user password...") + password, err := cryptorand.String(16) + if err != nil { + return User{}, xerrors.Errorf("generate random password for user: %w", err) + } + + _, _ = fmt.Fprintln(logs, "Creating user:") + user, err := r.client.CreateUserWithOrgs(ctx, codersdk.CreateUserRequestWithOrgs{ + OrganizationIDs: []uuid.UUID{r.cfg.OrganizationID}, + Username: r.cfg.Username, + Email: r.cfg.Email, + Password: password, + }) + if err != nil { + return User{}, xerrors.Errorf("create user: %w", err) + } + r.user = user + + _, _ = fmt.Fprintln(logs, "\nLogging in as new user...") + client := codersdk.New(r.client.URL) + loginRes, err := client.LoginWithPassword(ctx, codersdk.LoginWithPasswordRequest{ + Email: r.cfg.Email, + Password: password, + }) + if err != nil { + return User{}, xerrors.Errorf("login as new user: %w", err) + } + + _, _ = fmt.Fprintf(logs, "\tOrg ID: %s\n", r.cfg.OrganizationID.String()) + _, _ = fmt.Fprintf(logs, "\tUsername: %s\n", user.Username) + _, _ = fmt.Fprintf(logs, "\tEmail: %s\n", user.Email) + _, _ = fmt.Fprintf(logs, "\tPassword: ****************\n") + + return User{User: user, SessionToken: loginRes.SessionToken}, nil +} + +func (r *Runner) Cleanup(ctx context.Context, _ string, logs io.Writer) error { + if r.user.ID != uuid.Nil { + err := r.client.DeleteUser(ctx, r.user.ID) + if err != nil { + _, _ = fmt.Fprintf(logs, "failed to delete user %q: %v\n", r.user.ID.String(), err) + return xerrors.Errorf("delete user: %w", err) + } + } + return nil +} diff --git a/scaletest/createworkspaces/run.go b/scaletest/createworkspaces/run.go index b31091f498..85597079d8 100644 --- a/scaletest/createworkspaces/run.go +++ b/scaletest/createworkspaces/run.go @@ -14,8 +14,8 @@ import ( "github.com/coder/coder/v2/coderd/tracing" "github.com/coder/coder/v2/codersdk" - "github.com/coder/coder/v2/cryptorand" "github.com/coder/coder/v2/scaletest/agentconn" + "github.com/coder/coder/v2/scaletest/createusers" "github.com/coder/coder/v2/scaletest/harness" "github.com/coder/coder/v2/scaletest/loadtestutil" "github.com/coder/coder/v2/scaletest/reconnectingpty" @@ -26,7 +26,7 @@ type Runner struct { client *codersdk.Client cfg Config - userID uuid.UUID + createUserRunner *createusers.Runner workspacebuildRunner *workspacebuild.Runner } @@ -64,42 +64,24 @@ func (r *Runner) Run(ctx context.Context, id string, logs io.Writer) error { return xerrors.Errorf("generate random password for user: %w", err) } } else { - _, _ = fmt.Fprintln(logs, "Generating user password...") - password, err := cryptorand.String(16) - if err != nil { - return xerrors.Errorf("generate random password for user: %w", err) + createUserConfig := createusers.Config{ + OrganizationID: r.cfg.User.OrganizationID, + Username: r.cfg.User.Username, + Email: r.cfg.User.Email, } - - _, _ = fmt.Fprintln(logs, "Creating user:") - - user, err = r.client.CreateUserWithOrgs(ctx, codersdk.CreateUserRequestWithOrgs{ - OrganizationIDs: []uuid.UUID{r.cfg.User.OrganizationID}, - Username: r.cfg.User.Username, - Email: r.cfg.User.Email, - Password: password, - }) + if err := createUserConfig.Validate(); err != nil { + return xerrors.Errorf("validate create user config: %w", err) + } + r.createUserRunner = createusers.NewRunner(r.client, createUserConfig) + newUser, err := r.createUserRunner.RunReturningUser(ctx, id, logs) if err != nil { return xerrors.Errorf("create user: %w", err) } - r.userID = user.ID - - _, _ = fmt.Fprintln(logs, "\nLogging in as new user...") + user = newUser.User client = codersdk.New(r.client.URL) - loginRes, err := client.LoginWithPassword(ctx, codersdk.LoginWithPasswordRequest{ - Email: r.cfg.User.Email, - Password: password, - }) - if err != nil { - return xerrors.Errorf("login as new user: %w", err) - } - client.SetSessionToken(loginRes.SessionToken) + client.SetSessionToken(newUser.SessionToken) } - _, _ = fmt.Fprintf(logs, "\tOrg ID: %s\n", r.cfg.User.OrganizationID.String()) - _, _ = fmt.Fprintf(logs, "\tUsername: %s\n", user.Username) - _, _ = fmt.Fprintf(logs, "\tEmail: %s\n", user.Email) - _, _ = fmt.Fprintf(logs, "\tPassword: ****************\n") - _, _ = fmt.Fprintln(logs, "\nCreating workspace...") workspaceBuildConfig := r.cfg.Workspace workspaceBuildConfig.OrganizationID = r.cfg.User.OrganizationID @@ -189,11 +171,10 @@ func (r *Runner) Cleanup(ctx context.Context, id string, logs io.Writer) error { } } - if r.userID != uuid.Nil { - err := r.client.DeleteUser(ctx, r.userID) + if r.createUserRunner != nil { + err := r.createUserRunner.Cleanup(ctx, id, logs) if err != nil { - _, _ = fmt.Fprintf(logs, "failed to delete user %q: %v\n", r.userID.String(), err) - return xerrors.Errorf("delete user: %w", err) + return xerrors.Errorf("cleanup user: %w", err) } }