Thanks to visit codestin.com
Credit goes to github.com

Skip to content

feat(scaletest): allow scaletests to run using the host credentials #7075

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 26 additions & 18 deletions cli/scaletest.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{})

Expand Down Expand Up @@ -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}
Expand Down Expand Up @@ -693,35 +695,37 @@ 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,
},
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.
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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-")
}
3 changes: 3 additions & 0 deletions cli/testdata/coder_scaletest_create-workspaces_--help.golden
Original file line number Diff line number Diff line change
Expand Up @@ -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.
8 changes: 4 additions & 4 deletions coderd/provisionerdserver/provisionerdserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did this get changed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was looking through some other stuff at the same time. This is essentially a no-op so I just went ahead and removed it here.

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{
Expand Down
10 changes: 10 additions & 0 deletions docs/cli/scaletest_create-workspaces.md
Original file line number Diff line number Diff line change
Expand Up @@ -282,3 +282,13 @@ Enables trace exporting to Honeycomb.io using the provided API key.
| Environment | <code>$CODER_SCALETEST_TRACE_PROPAGATE</code> |

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 | <code>bool</code> |
| Environment | <code>$CODER_SCALETEST_USE_HOST_LOGIN</code> |
| Default | <code>false</code> |

Use the use logged in on the host machine, instead of creating users.
3 changes: 3 additions & 0 deletions scaletest/createworkspaces/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
79 changes: 47 additions & 32 deletions scaletest/createworkspaces/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
}
Expand All @@ -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)
Expand All @@ -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)
Expand Down