MCPcopy Index your code
hub / github.com/coder/coder / RunReturningUser

Method RunReturningUser

scaletest/createusers/run.go:38–100  ·  view source on GitHub ↗
(ctx context.Context, id string, logs io.Writer)

Source from the content-addressed store, hash-verified

36}
37
38func (r *Runner) RunReturningUser(ctx context.Context, id string, logs io.Writer) (User, error) {
39 ctx, span := tracing.StartSpan(ctx)
40 defer span.End()
41
42 logs = loadtestutil.NewSyncWriter(logs)
43 logger := slog.Make(sloghuman.Sink(logs)).Leveled(slog.LevelDebug)
44 r.client.SetLogger(logger)
45 r.client.SetLogBodies(true)
46
47 if r.cfg.Username == "" || r.cfg.Email == "" {
48 genUsername, genEmail, err := loadtestutil.GenerateUserIdentifier(id)
49 if err != nil {
50 return User{}, xerrors.Errorf("generate user identifier: %w", err)
51 }
52 if r.cfg.Username == "" {
53 r.cfg.Username = genUsername
54 }
55 if r.cfg.Email == "" {
56 r.cfg.Email = genEmail
57 }
58 }
59
60 _, _ = fmt.Fprintln(logs, "Generating user password...")
61 password, err := cryptorand.String(16)
62 if err != nil {
63 return User{}, xerrors.Errorf("generate random password for user: %w", err)
64 }
65
66 _, _ = fmt.Fprintln(logs, "Creating user:")
67 user, err := r.client.CreateUserWithOrgs(ctx, codersdk.CreateUserRequestWithOrgs{
68 OrganizationIDs: []uuid.UUID{r.cfg.OrganizationID},
69 Username: r.cfg.Username,
70 Email: r.cfg.Email,
71 Password: password,
72 })
73 if err != nil {
74 return User{}, xerrors.Errorf("create user: %w", err)
75 }
76 r.user = user
77
78 _, _ = fmt.Fprintln(logs, "\nLogging in as new user...")
79 // Duplicate the client with an independent transport to ensure each user
80 // login gets its own HTTP connection pool, preventing connection sharing
81 // during load testing.
82 client, err := loadtestutil.DupClientCopyingHeaders(r.client, nil)
83 if err != nil {
84 return User{}, xerrors.Errorf("duplicate client: %w", err)
85 }
86 loginRes, err := client.LoginWithPassword(ctx, codersdk.LoginWithPasswordRequest{
87 Email: r.cfg.Email,
88 Password: password,
89 })
90 if err != nil {
91 return User{}, xerrors.Errorf("login as new user: %w", err)
92 }
93
94 _, _ = fmt.Fprintf(logs, "\tOrg ID: %s\n", r.cfg.OrganizationID.String())
95 _, _ = fmt.Fprintf(logs, "\tUsername: %s\n", user.Username)

Callers 5

RunMethod · 0.80
SetupMethod · 0.80
RunMethod · 0.80
RunReturningResultMethod · 0.80
RunMethod · 0.80

Calls 11

StartSpanFunction · 0.92
NewSyncWriterFunction · 0.92
GenerateUserIdentifierFunction · 0.92
StringFunction · 0.92
DupClientCopyingHeadersFunction · 0.92
CreateUserWithOrgsMethod · 0.80
LoginWithPasswordMethod · 0.80
SetLoggerMethod · 0.65
SetLogBodiesMethod · 0.65
ErrorfMethod · 0.45
StringMethod · 0.45

Tested by

no test coverage detected