diff --git a/cli/scaletest.go b/cli/scaletest.go index 7e6c79ad8a..be3eb22ac6 100644 --- a/cli/scaletest.go +++ b/cli/scaletest.go @@ -471,7 +471,7 @@ func (r *RootCmd) scaletestCleanup() *clibase.Cmd { } cliui.Errorf(inv.Stderr, "Found %d scaletest users\n", len(users)) - if len(workspaces) != 0 { + if len(users) != 0 { cliui.Infof(inv.Stdout, "Deleting scaletest users..."+"\n") harness := harness.NewTestHarness(cleanupStrategy.toStrategy(), harness.ConcurrentExecutionStrategy{}) @@ -535,6 +535,8 @@ func (r *RootCmd) scaletestCreateWorkspaces() *clibase.Cmd { connectInterval time.Duration connectTimeout time.Duration + useHostUser bool + tracingFlags = &scaletestTracingFlags{} strategy = &scaletestStrategyFlags{} cleanupStrategy = &scaletestStrategyFlags{cleanup: true} @@ -693,28 +695,16 @@ func (r *RootCmd) scaletestCreateWorkspaces() *clibase.Cmd { const name = "workspacebuild" id := strconv.Itoa(i) - username, email, err := newScaleTestUser(id) - if err != nil { - return xerrors.Errorf("create scaletest username and email: %w", err) - } - workspaceName, err := newScaleTestWorkspace(id) - if err != nil { - return xerrors.Errorf("create scaletest workspace name: %w", err) - } - config := createworkspaces.Config{ User: createworkspaces.UserConfig{ // TODO: configurable org OrganizationID: me.OrganizationIDs[0], - Username: username, - Email: email, }, Workspace: workspacebuild.Config{ OrganizationID: me.OrganizationIDs[0], // UserID is set by the test automatically. Request: codersdk.CreateWorkspaceRequest{ TemplateID: tpl.ID, - Name: workspaceName, ParameterValues: params, }, NoWaitForAgents: noWaitForAgents, @@ -722,6 +712,20 @@ func (r *RootCmd) scaletestCreateWorkspaces() *clibase.Cmd { NoCleanup: noCleanup, } + if useHostUser { + config.User.SessionToken = client.SessionToken() + } else { + config.User.Username, config.User.Email, err = newScaleTestUser(id) + if err != nil { + return xerrors.Errorf("create scaletest username and email: %w", err) + } + } + + config.Workspace.Request.Name, err = newScaleTestWorkspace(id) + if err != nil { + return xerrors.Errorf("create scaletest workspace name: %w", err) + } + if runCommand != "" { config.ReconnectingPTY = &reconnectingpty.Config{ // AgentID is set by the test automatically. @@ -927,6 +931,13 @@ func (r *RootCmd) scaletestCreateWorkspaces() *clibase.Cmd { Description: "Timeout for each request to the --connect-url.", Value: clibase.DurationOf(&connectTimeout), }, + { + Flag: "use-host-login", + Env: "CODER_SCALETEST_USE_HOST_LOGIN", + Default: "false", + Description: "Use the use logged in on the host machine, instead of creating users.", + Value: clibase.BoolOf(&useHostUser), + }, } tracingFlags.attach(&cmd.Options) @@ -1009,9 +1020,6 @@ func isScaleTestUser(user codersdk.User) bool { } func isScaleTestWorkspace(workspace codersdk.Workspace) bool { - if !strings.HasPrefix(workspace.OwnerName, "scaletest-") { - return false - } - - return strings.HasPrefix(workspace.Name, "scaletest-") + return strings.HasPrefix(workspace.OwnerName, "scaletest-") || + strings.HasPrefix(workspace.Name, "scaletest-") } diff --git a/cli/testdata/coder_scaletest_create-workspaces_--help.golden b/cli/testdata/coder_scaletest_create-workspaces_--help.golden index a2756e46ba..568cf3da72 100644 --- a/cli/testdata/coder_scaletest_create-workspaces_--help.golden +++ b/cli/testdata/coder_scaletest_create-workspaces_--help.golden @@ -116,5 +116,8 @@ It is recommended that all rate limits are disabled on the server before running if the server is configured with the exact same tracing configuration as the client. + --use-host-login bool, $CODER_SCALETEST_USE_HOST_LOGIN (default: false) + Use the use logged in on the host machine, instead of creating users. + --- Run `coder --help` for a list of global options. diff --git a/coderd/provisionerdserver/provisionerdserver.go b/coderd/provisionerdserver/provisionerdserver.go index 1af62fe13d..f4f3e6b1de 100644 --- a/coderd/provisionerdserver/provisionerdserver.go +++ b/coderd/provisionerdserver/provisionerdserver.go @@ -490,14 +490,14 @@ func (server *Server) UpdateJob(ctx context.Context, request *proto.UpdateJobReq slog.F("stage", log.Stage), slog.F("output", log.Output)) } - //nolint:gocritic // Provisionerd has specific authz rules. - logs, err := server.Database.InsertProvisionerJobLogs(dbauthz.AsProvisionerd(context.Background()), insertParams) + + logs, err := server.Database.InsertProvisionerJobLogs(ctx, insertParams) if err != nil { server.Logger.Error(ctx, "failed to insert job logs", slog.F("job_id", parsedID), slog.Error(err)) return nil, xerrors.Errorf("insert job logs: %w", err) } - // Publish by the lowest log ID inserted so the - // log stream will fetch everything from that point. + // Publish by the lowest log ID inserted so the log stream will fetch + // everything from that point. lowestID := logs[0].ID server.Logger.Debug(ctx, "inserted job logs", slog.F("job_id", parsedID)) data, err := json.Marshal(ProvisionerJobLogsNotifyMessage{ diff --git a/docs/cli/scaletest_create-workspaces.md b/docs/cli/scaletest_create-workspaces.md index e221ff1c71..76ad18aed3 100644 --- a/docs/cli/scaletest_create-workspaces.md +++ b/docs/cli/scaletest_create-workspaces.md @@ -282,3 +282,13 @@ Enables trace exporting to Honeycomb.io using the provided API key. | Environment | $CODER_SCALETEST_TRACE_PROPAGATE | Enables trace propagation to the Coder backend, which will be used to correlate server-side spans with client-side spans. Only enable this if the server is configured with the exact same tracing configuration as the client. + +### --use-host-login + +| | | +| ----------- | -------------------------------------------- | +| Type | bool | +| Environment | $CODER_SCALETEST_USE_HOST_LOGIN | +| Default | false | + +Use the use logged in on the host machine, instead of creating users. diff --git a/scaletest/createworkspaces/config.go b/scaletest/createworkspaces/config.go index 0b904485e6..e8607dff86 100644 --- a/scaletest/createworkspaces/config.go +++ b/scaletest/createworkspaces/config.go @@ -17,6 +17,9 @@ type UserConfig struct { Username string `json:"username"` // Email is the email of the new user. Email string `json:"email"` + // SessionToken is the session token of an already existing user. If set, no + // user will be created. + SessionToken string `json:"session_token"` } func (c UserConfig) Validate() error { diff --git a/scaletest/createworkspaces/run.go b/scaletest/createworkspaces/run.go index 5d6f530b23..df8805b735 100644 --- a/scaletest/createworkspaces/run.go +++ b/scaletest/createworkspaces/run.go @@ -52,44 +52,59 @@ func (r *Runner) Run(ctx context.Context, id string, logs io.Writer) error { r.client.Logger = logger r.client.LogBodies = true - _, _ = fmt.Fprintln(logs, "Generating user password...") - password, err := cryptorand.String(16) - if err != nil { - return xerrors.Errorf("generate random password for user: %w", err) + var ( + client = r.client + user codersdk.User + err error + ) + if r.cfg.User.SessionToken != "" { + _, _ = fmt.Fprintln(logs, "Using existing user session token:") + user, err = client.User(ctx, "me") + if err != nil { + 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) + } + + _, _ = fmt.Fprintln(logs, "Creating user:") + + user, err := r.client.CreateUser(ctx, codersdk.CreateUserRequest{ + OrganizationID: r.cfg.User.OrganizationID, + Username: r.cfg.User.Username, + Email: r.cfg.User.Email, + Password: password, + }) + if err != nil { + return xerrors.Errorf("create user: %w", err) + } + r.userID = user.ID + + _, _ = fmt.Fprintln(logs, "\nLogging in as new 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) } - _, _ = fmt.Fprintln(logs, "Creating user:") _, _ = fmt.Fprintf(logs, "\tOrg ID: %s\n", r.cfg.User.OrganizationID.String()) - _, _ = fmt.Fprintf(logs, "\tUsername: %s\n", r.cfg.User.Username) - _, _ = fmt.Fprintf(logs, "\tEmail: %s\n", r.cfg.User.Email) + _, _ = fmt.Fprintf(logs, "\tUsername: %s\n", user.Username) + _, _ = fmt.Fprintf(logs, "\tEmail: %s\n", user.Email) _, _ = fmt.Fprintf(logs, "\tPassword: ****************\n") - user, err := r.client.CreateUser(ctx, codersdk.CreateUserRequest{ - OrganizationID: r.cfg.User.OrganizationID, - Username: r.cfg.User.Username, - Email: r.cfg.User.Email, - Password: password, - }) - if err != nil { - return xerrors.Errorf("create user: %w", err) - } - r.userID = user.ID - - _, _ = fmt.Fprintln(logs, "\nLogging in as new user...") - userClient := codersdk.New(r.client.URL) - loginRes, err := userClient.LoginWithPassword(ctx, codersdk.LoginWithPasswordRequest{ - Email: r.cfg.User.Email, - Password: password, - }) - if err != nil { - return xerrors.Errorf("login as new user: %w", err) - } - userClient.SetSessionToken(loginRes.SessionToken) _, _ = fmt.Fprintln(logs, "\nCreating workspace...") workspaceBuildConfig := r.cfg.Workspace workspaceBuildConfig.OrganizationID = r.cfg.User.OrganizationID workspaceBuildConfig.UserID = user.ID.String() - r.workspacebuildRunner = workspacebuild.NewRunner(userClient, workspaceBuildConfig) + r.workspacebuildRunner = workspacebuild.NewRunner(client, workspaceBuildConfig) err = r.workspacebuildRunner.Run(ctx, id, logs) if err != nil { return xerrors.Errorf("create workspace: %w", err) @@ -104,7 +119,7 @@ func (r *Runner) Run(ctx context.Context, id string, logs io.Writer) error { if err != nil { return xerrors.Errorf("get workspace ID: %w", err) } - workspace, err := userClient.Workspace(ctx, workspaceID) + workspace, err := client.Workspace(ctx, workspaceID) if err != nil { return xerrors.Errorf("get workspace %q: %w", workspaceID.String(), err) } @@ -128,7 +143,7 @@ resourceLoop: reconnectingPTYConfig := *r.cfg.ReconnectingPTY reconnectingPTYConfig.AgentID = agent.ID - reconnectingPTYRunner := reconnectingpty.NewRunner(userClient, reconnectingPTYConfig) + reconnectingPTYRunner := reconnectingpty.NewRunner(client, reconnectingPTYConfig) err := reconnectingPTYRunner.Run(egCtx, id, logs) if err != nil { return xerrors.Errorf("run reconnecting pty: %w", err) @@ -142,7 +157,7 @@ resourceLoop: agentConnConfig := *r.cfg.AgentConn agentConnConfig.AgentID = agent.ID - agentConnRunner := agentconn.NewRunner(userClient, agentConnConfig) + agentConnRunner := agentconn.NewRunner(client, agentConnConfig) err := agentConnRunner.Run(egCtx, id, logs) if err != nil { return xerrors.Errorf("run agent connection: %w", err)