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

Commit57b628d

Browse files
committed
refactor(scaletest): generate user and workspace names if omitted
1 parent4d8dc22 commit57b628d

File tree

5 files changed

+63
-114
lines changed

5 files changed

+63
-114
lines changed

‎cli/exp_scaletest.go‎

Lines changed: 3 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,11 @@ import (
3232
"github.com/coder/coder/v2/coderd/tracing"
3333
"github.com/coder/coder/v2/codersdk"
3434
"github.com/coder/coder/v2/codersdk/workspacesdk"
35-
"github.com/coder/coder/v2/cryptorand"
3635
"github.com/coder/coder/v2/scaletest/agentconn"
3736
"github.com/coder/coder/v2/scaletest/createworkspaces"
3837
"github.com/coder/coder/v2/scaletest/dashboard"
3938
"github.com/coder/coder/v2/scaletest/harness"
39+
"github.com/coder/coder/v2/scaletest/loadtestutil"
4040
"github.com/coder/coder/v2/scaletest/reconnectingpty"
4141
"github.com/coder/coder/v2/scaletest/workspacebuild"
4242
"github.com/coder/coder/v2/scaletest/workspacetraffic"
@@ -647,16 +647,6 @@ func (r *RootCmd) scaletestCreateWorkspaces() *serpent.Command {
647647

648648
ifuseHostUser {
649649
config.User.SessionToken=client.SessionToken()
650-
}else {
651-
config.User.Username,config.User.Email,err=newScaleTestUser(id)
652-
iferr!=nil {
653-
returnxerrors.Errorf("create scaletest username and email: %w",err)
654-
}
655-
}
656-
657-
config.Workspace.Request.Name,err=newScaleTestWorkspace(id)
658-
iferr!=nil {
659-
returnxerrors.Errorf("create scaletest workspace name: %w",err)
660650
}
661651

662652
ifrunCommand!="" {
@@ -1408,31 +1398,6 @@ func (r *runnableTraceWrapper) Cleanup(ctx context.Context, id string, logs io.W
14081398
returnc.Cleanup(ctx,id,logs)
14091399
}
14101400

1411-
// newScaleTestUser returns a random username and email address that can be used
1412-
// for scale testing. The returned username is prefixed with "scaletest-" and
1413-
// the returned email address is suffixed with "@scaletest.local".
1414-
funcnewScaleTestUser(idstring) (usernamestring,emailstring,errerror) {
1415-
randStr,err:=cryptorand.String(8)
1416-
returnfmt.Sprintf("scaletest-%s-%s",randStr,id),fmt.Sprintf("%s-%s@scaletest.local",randStr,id),err
1417-
}
1418-
1419-
// newScaleTestWorkspace returns a random workspace name that can be used for
1420-
// scale testing. The returned workspace name is prefixed with "scaletest-" and
1421-
// suffixed with the given id.
1422-
funcnewScaleTestWorkspace(idstring) (namestring,errerror) {
1423-
randStr,err:=cryptorand.String(8)
1424-
returnfmt.Sprintf("scaletest-%s-%s",randStr,id),err
1425-
}
1426-
1427-
funcisScaleTestUser(user codersdk.User)bool {
1428-
returnstrings.HasSuffix(user.Email,"@scaletest.local")
1429-
}
1430-
1431-
funcisScaleTestWorkspace(workspace codersdk.Workspace)bool {
1432-
returnstrings.HasPrefix(workspace.OwnerName,"scaletest-")||
1433-
strings.HasPrefix(workspace.Name,"scaletest-")
1434-
}
1435-
14361401
funcgetScaletestWorkspaces(ctx context.Context,client*codersdk.Client,owner,templatestring) ([]codersdk.Workspace,int,error) {
14371402
var (
14381403
pageNumber=0
@@ -1471,7 +1436,7 @@ func getScaletestWorkspaces(ctx context.Context, client *codersdk.Client, owner,
14711436

14721437
pageWorkspaces:=make([]codersdk.Workspace,0,len(page.Workspaces))
14731438
for_,w:=rangepage.Workspaces {
1474-
if!isScaleTestWorkspace(w) {
1439+
if!loadtestutil.IsScaleTestWorkspace(w.Name,w.OwnerName) {
14751440
continue
14761441
}
14771442
ifnoOwnerAccess&&w.OwnerID!=me.ID {
@@ -1511,7 +1476,7 @@ func getScaletestUsers(ctx context.Context, client *codersdk.Client) ([]codersdk
15111476

15121477
pageUsers:=make([]codersdk.User,0,len(page.Users))
15131478
for_,u:=rangepage.Users {
1514-
ifisScaleTestUser(u) {
1479+
ifloadtestutil.IsScaleTestUser(u.Username,u.Email) {
15151480
pageUsers=append(pageUsers,u)
15161481
}
15171482
}

‎scaletest/createworkspaces/config.go‎

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ import (
1313
typeUserConfigstruct {
1414
// OrganizationID is the ID of the organization to add the user to.
1515
OrganizationID uuid.UUID`json:"organization_id"`
16-
// Username is the username of the new user.
16+
// Username is the username of the new user. Generated if empty.
1717
Usernamestring`json:"username"`
18-
// Email is the email of the new user.
18+
// Email is the email of the new user. Generated if empty.
1919
Emailstring`json:"email"`
2020
// SessionToken is the session token of an already existing user. If set, no
2121
// user will be created.
@@ -26,14 +26,6 @@ func (c UserConfig) Validate() error {
2626
ifc.OrganizationID==uuid.Nil {
2727
returnxerrors.New("organization_id must not be a nil UUID")
2828
}
29-
ifc.SessionToken=="" {
30-
ifc.Username=="" {
31-
returnxerrors.New("username must be set")
32-
}
33-
ifc.Email=="" {
34-
returnxerrors.New("email must be set")
35-
}
36-
}
3729

3830
returnnil
3931
}

‎scaletest/createworkspaces/config_test.go‎

Lines changed: 0 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -15,68 +15,6 @@ import (
1515
"github.com/coder/coder/v2/scaletest/workspacebuild"
1616
)
1717

18-
funcTest_UserConfig(t*testing.T) {
19-
t.Parallel()
20-
21-
id:=uuid.New()
22-
23-
cases:= []struct {
24-
namestring
25-
config createworkspaces.UserConfig
26-
errContainsstring
27-
}{
28-
{
29-
name:"OK",
30-
config: createworkspaces.UserConfig{
31-
OrganizationID:id,
32-
Username:"test",
33-
Email:"test@test.coder.com",
34-
},
35-
},
36-
{
37-
name:"NoOrganizationID",
38-
config: createworkspaces.UserConfig{
39-
OrganizationID:uuid.Nil,
40-
Username:"test",
41-
Email:"test@test.coder.com",
42-
},
43-
errContains:"organization_id must not be a nil UUID",
44-
},
45-
{
46-
name:"NoUsername",
47-
config: createworkspaces.UserConfig{
48-
OrganizationID:id,
49-
Username:"",
50-
Email:"test@test.coder.com",
51-
},
52-
errContains:"username must be set",
53-
},
54-
{
55-
name:"NoEmail",
56-
config: createworkspaces.UserConfig{
57-
OrganizationID:id,
58-
Username:"test",
59-
Email:"",
60-
},
61-
errContains:"email must be set",
62-
},
63-
}
64-
65-
for_,c:=rangecases {
66-
t.Run(c.name,func(t*testing.T) {
67-
t.Parallel()
68-
69-
err:=c.config.Validate()
70-
ifc.errContains!="" {
71-
require.Error(t,err)
72-
require.Contains(t,err.Error(),c.errContains)
73-
}else {
74-
require.NoError(t,err)
75-
}
76-
})
77-
}
78-
}
79-
8018
funcTest_Config(t*testing.T) {
8119
t.Parallel()
8220

‎scaletest/loadtestutil/names.go‎

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package loadtestutil
2+
3+
import (
4+
"fmt"
5+
"strings"
6+
7+
"github.com/coder/coder/v2/cryptorand"
8+
)
9+
10+
const (
11+
// Prefix for all scaletest resources (users and workspaces)
12+
ScaleTestPrefix="scaletest"
13+
14+
// Email domain for scaletest users
15+
EmailDomain="@scaletest.local"
16+
17+
DefaultRandLength=8
18+
)
19+
20+
// GenerateUserIdentifier generates a username and email for scale testing.
21+
// The username follows the pattern: scaletest-<random>-<id>
22+
// The email follows the pattern: <random>-<id>@scaletest.local
23+
funcGenerateUserIdentifier(idstring) (username,emailstring,errerror) {
24+
randStr,err:=cryptorand.String(DefaultRandLength)
25+
iferr!=nil {
26+
return"","",err
27+
}
28+
29+
username=fmt.Sprintf("%s-%s-%s",ScaleTestPrefix,randStr,id)
30+
email=fmt.Sprintf("%s-%s%s",randStr,id,EmailDomain)
31+
returnusername,email,nil
32+
}
33+
34+
// GenerateWorkspaceName generates a workspace name for scale testing.
35+
// The workspace name follows the pattern: scaletest-<random>-<id>
36+
funcGenerateWorkspaceName(idstring) (namestring,errerror) {
37+
randStr,err:=cryptorand.String(DefaultRandLength)
38+
iferr!=nil {
39+
return"",err
40+
}
41+
42+
returnfmt.Sprintf("%s-%s-%s",ScaleTestPrefix,randStr,id),nil
43+
}
44+
45+
// IsScaleTestUser checks if a username indicates it was created for scale testing.
46+
funcIsScaleTestUser(username,emailstring)bool {
47+
returnstrings.HasPrefix(username,ScaleTestPrefix+"-")||
48+
strings.HasSuffix(email,EmailDomain)
49+
}
50+
51+
// IsScaleTestWorkspace checks if a workspace name indicates it was created for scale testing.
52+
funcIsScaleTestWorkspace(workspaceName,ownerNamestring)bool {
53+
returnstrings.HasPrefix(workspaceName,ScaleTestPrefix+"-")||
54+
strings.HasPrefix(ownerName,ScaleTestPrefix+"-")
55+
}

‎scaletest/workspacebuild/run.go‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import (
1515

1616
"github.com/coder/coder/v2/coderd/tracing"
1717
"github.com/coder/coder/v2/codersdk"
18-
"github.com/coder/coder/v2/cryptorand"
1918
"github.com/coder/coder/v2/scaletest/harness"
2019
"github.com/coder/coder/v2/scaletest/loadtestutil"
2120
)
@@ -40,7 +39,7 @@ func NewRunner(client *codersdk.Client, cfg Config) *Runner {
4039
}
4140

4241
// Run implements Runnable.
43-
func (r*Runner)Run(ctx context.Context,_string,logs io.Writer)error {
42+
func (r*Runner)Run(ctx context.Context,idstring,logs io.Writer)error {
4443
ctx,span:=tracing.StartSpan(ctx)
4544
deferspan.End()
4645

@@ -51,11 +50,11 @@ func (r *Runner) Run(ctx context.Context, _ string, logs io.Writer) error {
5150

5251
req:=r.cfg.Request
5352
ifreq.Name=="" {
54-
randName,err:=cryptorand.HexString(8)
53+
randName,err:=loadtestutil.GenerateWorkspaceName(id)
5554
iferr!=nil {
5655
returnxerrors.Errorf("generate random name for workspace: %w",err)
5756
}
58-
req.Name="test-"+randName
57+
req.Name=randName
5958
}
6059

6160
workspace,err:=r.client.CreateWorkspace(ctx,r.cfg.OrganizationID,r.cfg.UserID,req)

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp