Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitd02ff5f

Browse files
refactor(scaletest): add runner for creating users (#19811)
Closescoder/internal#985Simple 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.
1 parent6d9e29b commitd02ff5f

File tree

3 files changed

+145
-35
lines changed

3 files changed

+145
-35
lines changed

‎scaletest/createusers/config.go‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package createusers
2+
3+
import (
4+
"github.com/google/uuid"
5+
"golang.org/x/xerrors"
6+
)
7+
8+
typeConfigstruct {
9+
// OrganizationID is the ID of the organization to add the user to.
10+
OrganizationID uuid.UUID`json:"organization_id"`
11+
// Username is the username of the new user. Generated if empty.
12+
Usernamestring`json:"username"`
13+
// Email is the email of the new user. Generated if empty.
14+
Emailstring`json:"email"`
15+
}
16+
17+
func (cConfig)Validate()error {
18+
ifc.OrganizationID==uuid.Nil {
19+
returnxerrors.New("organization_id must not be a nil UUID")
20+
}
21+
22+
returnnil
23+
}

‎scaletest/createusers/run.go‎

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
package createusers
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"io"
7+
8+
"github.com/google/uuid"
9+
"golang.org/x/xerrors"
10+
11+
"cdr.dev/slog"
12+
"cdr.dev/slog/sloggers/sloghuman"
13+
14+
"github.com/coder/coder/v2/coderd/tracing"
15+
"github.com/coder/coder/v2/codersdk"
16+
"github.com/coder/coder/v2/cryptorand"
17+
"github.com/coder/coder/v2/scaletest/loadtestutil"
18+
)
19+
20+
typeRunnerstruct {
21+
client*codersdk.Client
22+
cfgConfig
23+
24+
user codersdk.User
25+
}
26+
27+
typeUserstruct {
28+
codersdk.User
29+
SessionTokenstring
30+
}
31+
32+
funcNewRunner(client*codersdk.Client,cfgConfig)*Runner {
33+
return&Runner{
34+
client:client,
35+
cfg:cfg,
36+
}
37+
}
38+
39+
func (r*Runner)RunReturningUser(ctx context.Context,idstring,logs io.Writer) (User,error) {
40+
ctx,span:=tracing.StartSpan(ctx)
41+
deferspan.End()
42+
43+
logs=loadtestutil.NewSyncWriter(logs)
44+
logger:=slog.Make(sloghuman.Sink(logs)).Leveled(slog.LevelDebug)
45+
r.client.SetLogger(logger)
46+
r.client.SetLogBodies(true)
47+
48+
ifr.cfg.Username==""||r.cfg.Email=="" {
49+
genUsername,genEmail,err:=loadtestutil.GenerateUserIdentifier(id)
50+
iferr!=nil {
51+
returnUser{},xerrors.Errorf("generate user identifier: %w",err)
52+
}
53+
ifr.cfg.Username=="" {
54+
r.cfg.Username=genUsername
55+
}
56+
ifr.cfg.Email=="" {
57+
r.cfg.Email=genEmail
58+
}
59+
}
60+
61+
_,_=fmt.Fprintln(logs,"Generating user password...")
62+
password,err:=cryptorand.String(16)
63+
iferr!=nil {
64+
returnUser{},xerrors.Errorf("generate random password for user: %w",err)
65+
}
66+
67+
_,_=fmt.Fprintln(logs,"Creating user:")
68+
user,err:=r.client.CreateUserWithOrgs(ctx, codersdk.CreateUserRequestWithOrgs{
69+
OrganizationIDs: []uuid.UUID{r.cfg.OrganizationID},
70+
Username:r.cfg.Username,
71+
Email:r.cfg.Email,
72+
Password:password,
73+
})
74+
iferr!=nil {
75+
returnUser{},xerrors.Errorf("create user: %w",err)
76+
}
77+
r.user=user
78+
79+
_,_=fmt.Fprintln(logs,"\nLogging in as new user...")
80+
client:=codersdk.New(r.client.URL)
81+
loginRes,err:=client.LoginWithPassword(ctx, codersdk.LoginWithPasswordRequest{
82+
Email:r.cfg.Email,
83+
Password:password,
84+
})
85+
iferr!=nil {
86+
returnUser{},xerrors.Errorf("login as new user: %w",err)
87+
}
88+
89+
_,_=fmt.Fprintf(logs,"\tOrg ID: %s\n",r.cfg.OrganizationID.String())
90+
_,_=fmt.Fprintf(logs,"\tUsername: %s\n",user.Username)
91+
_,_=fmt.Fprintf(logs,"\tEmail: %s\n",user.Email)
92+
_,_=fmt.Fprintf(logs,"\tPassword: ****************\n")
93+
94+
returnUser{User:user,SessionToken:loginRes.SessionToken},nil
95+
}
96+
97+
func (r*Runner)Cleanup(ctx context.Context,_string,logs io.Writer)error {
98+
ifr.user.ID!=uuid.Nil {
99+
err:=r.client.DeleteUser(ctx,r.user.ID)
100+
iferr!=nil {
101+
_,_=fmt.Fprintf(logs,"failed to delete user %q: %v\n",r.user.ID.String(),err)
102+
returnxerrors.Errorf("delete user: %w",err)
103+
}
104+
}
105+
returnnil
106+
}

‎scaletest/createworkspaces/run.go‎

Lines changed: 16 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ import (
1414

1515
"github.com/coder/coder/v2/coderd/tracing"
1616
"github.com/coder/coder/v2/codersdk"
17-
"github.com/coder/coder/v2/cryptorand"
1817
"github.com/coder/coder/v2/scaletest/agentconn"
18+
"github.com/coder/coder/v2/scaletest/createusers"
1919
"github.com/coder/coder/v2/scaletest/harness"
2020
"github.com/coder/coder/v2/scaletest/loadtestutil"
2121
"github.com/coder/coder/v2/scaletest/reconnectingpty"
@@ -26,7 +26,7 @@ type Runner struct {
2626
client*codersdk.Client
2727
cfgConfig
2828

29-
userID uuid.UUID
29+
createUserRunner*createusers.Runner
3030
workspacebuildRunner*workspacebuild.Runner
3131
}
3232

@@ -64,42 +64,24 @@ func (r *Runner) Run(ctx context.Context, id string, logs io.Writer) error {
6464
returnxerrors.Errorf("generate random password for user: %w",err)
6565
}
6666
}else {
67-
_,_=fmt.Fprintln(logs,"Generating user password...")
68-
password,err:=cryptorand.String(16)
69-
iferr!=nil {
70-
returnxerrors.Errorf("generate random password for user: %w",err)
67+
createUserConfig:= createusers.Config{
68+
OrganizationID:r.cfg.User.OrganizationID,
69+
Username:r.cfg.User.Username,
70+
Email:r.cfg.User.Email,
7171
}
72-
73-
_,_=fmt.Fprintln(logs,"Creating user:")
74-
75-
user,err=r.client.CreateUserWithOrgs(ctx, codersdk.CreateUserRequestWithOrgs{
76-
OrganizationIDs: []uuid.UUID{r.cfg.User.OrganizationID},
77-
Username:r.cfg.User.Username,
78-
Email:r.cfg.User.Email,
79-
Password:password,
80-
})
72+
iferr:=createUserConfig.Validate();err!=nil {
73+
returnxerrors.Errorf("validate create user config: %w",err)
74+
}
75+
r.createUserRunner=createusers.NewRunner(r.client,createUserConfig)
76+
newUser,err:=r.createUserRunner.RunReturningUser(ctx,id,logs)
8177
iferr!=nil {
8278
returnxerrors.Errorf("create user: %w",err)
8379
}
84-
r.userID=user.ID
85-
86-
_,_=fmt.Fprintln(logs,"\nLogging in as new user...")
80+
user=newUser.User
8781
client=codersdk.New(r.client.URL)
88-
loginRes,err:=client.LoginWithPassword(ctx, codersdk.LoginWithPasswordRequest{
89-
Email:r.cfg.User.Email,
90-
Password:password,
91-
})
92-
iferr!=nil {
93-
returnxerrors.Errorf("login as new user: %w",err)
94-
}
95-
client.SetSessionToken(loginRes.SessionToken)
82+
client.SetSessionToken(newUser.SessionToken)
9683
}
9784

98-
_,_=fmt.Fprintf(logs,"\tOrg ID: %s\n",r.cfg.User.OrganizationID.String())
99-
_,_=fmt.Fprintf(logs,"\tUsername: %s\n",user.Username)
100-
_,_=fmt.Fprintf(logs,"\tEmail: %s\n",user.Email)
101-
_,_=fmt.Fprintf(logs,"\tPassword: ****************\n")
102-
10385
_,_=fmt.Fprintln(logs,"\nCreating workspace...")
10486
workspaceBuildConfig:=r.cfg.Workspace
10587
workspaceBuildConfig.OrganizationID=r.cfg.User.OrganizationID
@@ -189,11 +171,10 @@ func (r *Runner) Cleanup(ctx context.Context, id string, logs io.Writer) error {
189171
}
190172
}
191173

192-
ifr.userID!=uuid.Nil {
193-
err:=r.client.DeleteUser(ctx,r.userID)
174+
ifr.createUserRunner!=nil {
175+
err:=r.createUserRunner.Cleanup(ctx,id,logs)
194176
iferr!=nil {
195-
_,_=fmt.Fprintf(logs,"failed to delete user %q: %v\n",r.userID.String(),err)
196-
returnxerrors.Errorf("delete user: %w",err)
177+
returnxerrors.Errorf("cleanup user: %w",err)
197178
}
198179
}
199180

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp