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

Commitfe4ed6b

Browse files
committed
refactor(scaletest): add scaletest runner for creating users
1 parent57b628d commitfe4ed6b

File tree

3 files changed

+158
-34
lines changed

3 files changed

+158
-34
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: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
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/harness"
18+
"github.com/coder/coder/v2/scaletest/loadtestutil"
19+
)
20+
21+
typeRunnerstruct {
22+
client*codersdk.Client
23+
cfgConfig
24+
25+
userID uuid.UUID
26+
sessionTokenstring
27+
user codersdk.User
28+
}
29+
30+
var (
31+
_ harness.Runnable=&Runner{}
32+
_ harness.Cleanable=&Runner{}
33+
)
34+
35+
funcNewRunner(client*codersdk.Client,cfgConfig)*Runner {
36+
return&Runner{
37+
client:client,
38+
cfg:cfg,
39+
}
40+
}
41+
42+
func (r*Runner)Run(ctx context.Context,idstring,logs io.Writer)error {
43+
ctx,span:=tracing.StartSpan(ctx)
44+
deferspan.End()
45+
46+
logs=loadtestutil.NewSyncWriter(logs)
47+
logger:=slog.Make(sloghuman.Sink(logs)).Leveled(slog.LevelDebug)
48+
r.client.SetLogger(logger)
49+
r.client.SetLogBodies(true)
50+
51+
ifr.cfg.Username==""||r.cfg.Email=="" {
52+
genUsername,genEmail,err:=loadtestutil.GenerateUserIdentifier(id)
53+
iferr!=nil {
54+
returnxerrors.Errorf("generate user identifier: %w",err)
55+
}
56+
ifr.cfg.Username=="" {
57+
r.cfg.Username=genUsername
58+
}
59+
ifr.cfg.Email=="" {
60+
r.cfg.Email=genEmail
61+
}
62+
}
63+
64+
_,_=fmt.Fprintln(logs,"Generating user password...")
65+
password,err:=cryptorand.String(16)
66+
iferr!=nil {
67+
returnxerrors.Errorf("generate random password for user: %w",err)
68+
}
69+
70+
_,_=fmt.Fprintln(logs,"Creating user:")
71+
user,err:=r.client.CreateUserWithOrgs(ctx, codersdk.CreateUserRequestWithOrgs{
72+
OrganizationIDs: []uuid.UUID{r.cfg.OrganizationID},
73+
Username:r.cfg.Username,
74+
Email:r.cfg.Email,
75+
Password:password,
76+
})
77+
iferr!=nil {
78+
returnxerrors.Errorf("create user: %w",err)
79+
}
80+
r.userID=user.ID
81+
r.user=user
82+
83+
_,_=fmt.Fprintln(logs,"\nLogging in as new user...")
84+
client:=codersdk.New(r.client.URL)
85+
loginRes,err:=client.LoginWithPassword(ctx, codersdk.LoginWithPasswordRequest{
86+
Email:r.cfg.Email,
87+
Password:password,
88+
})
89+
iferr!=nil {
90+
returnxerrors.Errorf("login as new user: %w",err)
91+
}
92+
r.sessionToken=loginRes.SessionToken
93+
94+
_,_=fmt.Fprintf(logs,"\tOrg ID: %s\n",r.cfg.OrganizationID.String())
95+
_,_=fmt.Fprintf(logs,"\tUsername: %s\n",user.Username)
96+
_,_=fmt.Fprintf(logs,"\tEmail: %s\n",user.Email)
97+
_,_=fmt.Fprintf(logs,"\tPassword: ****************\n")
98+
99+
returnnil
100+
}
101+
102+
func (r*Runner)Cleanup(ctx context.Context,_string,logs io.Writer)error {
103+
ifr.userID!=uuid.Nil {
104+
err:=r.client.DeleteUser(ctx,r.userID)
105+
iferr!=nil {
106+
_,_=fmt.Fprintf(logs,"failed to delete user %q: %v\n",r.userID.String(),err)
107+
returnxerrors.Errorf("delete user: %w",err)
108+
}
109+
}
110+
returnnil
111+
}
112+
113+
func (r*Runner)SessionToken()string {
114+
returnr.sessionToken
115+
}
116+
117+
func (r*Runner)User() codersdk.User {
118+
returnr.user
119+
}

‎scaletest/createworkspaces/run.go‎

Lines changed: 16 additions & 34 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,25 @@ 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+
err=r.createUserRunner.Run(ctx,id,logs)
8177
iferr!=nil {
8278
returnxerrors.Errorf("create user: %w",err)
8379
}
84-
r.userID=user.ID
8580

86-
_,_=fmt.Fprintln(logs,"\nLogging in as new user...")
81+
user=r.createUserRunner.User()
8782
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)
83+
client.SetSessionToken(r.createUserRunner.SessionToken())
9684
}
9785

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-
10386
_,_=fmt.Fprintln(logs,"\nCreating workspace...")
10487
workspaceBuildConfig:=r.cfg.Workspace
10588
workspaceBuildConfig.OrganizationID=r.cfg.User.OrganizationID
@@ -189,11 +172,10 @@ func (r *Runner) Cleanup(ctx context.Context, id string, logs io.Writer) error {
189172
}
190173
}
191174

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

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp