- Notifications
You must be signed in to change notification settings - Fork1k
refactor(scaletest): add runner for creating users#19811
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
23 changes: 23 additions & 0 deletionsscaletest/createusers/config.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package createusers | ||
import ( | ||
"github.com/google/uuid" | ||
"golang.org/x/xerrors" | ||
) | ||
typeConfigstruct { | ||
// OrganizationID is the ID of the organization to add the user to. | ||
OrganizationID uuid.UUID`json:"organization_id"` | ||
// Username is the username of the new user. Generated if empty. | ||
Usernamestring`json:"username"` | ||
// Email is the email of the new user. Generated if empty. | ||
Emailstring`json:"email"` | ||
} | ||
func (cConfig)Validate()error { | ||
ifc.OrganizationID==uuid.Nil { | ||
returnxerrors.New("organization_id must not be a nil UUID") | ||
} | ||
returnnil | ||
} |
106 changes: 106 additions & 0 deletionsscaletest/createusers/run.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
package createusers | ||
import ( | ||
"context" | ||
"fmt" | ||
"io" | ||
"github.com/google/uuid" | ||
"golang.org/x/xerrors" | ||
"cdr.dev/slog" | ||
"cdr.dev/slog/sloggers/sloghuman" | ||
"github.com/coder/coder/v2/coderd/tracing" | ||
"github.com/coder/coder/v2/codersdk" | ||
"github.com/coder/coder/v2/cryptorand" | ||
"github.com/coder/coder/v2/scaletest/loadtestutil" | ||
) | ||
type Runner struct { | ||
client *codersdk.Client | ||
cfg Config | ||
user codersdk.User | ||
} | ||
type User struct { | ||
codersdk.User | ||
SessionToken string | ||
} | ||
func NewRunner(client *codersdk.Client, cfg Config) *Runner { | ||
return &Runner{ | ||
client: client, | ||
cfg: cfg, | ||
} | ||
} | ||
func (r *Runner) RunReturningUser(ctx context.Context, id string, logs io.Writer) (User, error) { | ||
ctx, span := tracing.StartSpan(ctx) | ||
defer span.End() | ||
logs = loadtestutil.NewSyncWriter(logs) | ||
logger := slog.Make(sloghuman.Sink(logs)).Leveled(slog.LevelDebug) | ||
r.client.SetLogger(logger) | ||
r.client.SetLogBodies(true) | ||
if r.cfg.Username == "" || r.cfg.Email == "" { | ||
genUsername, genEmail, err := loadtestutil.GenerateUserIdentifier(id) | ||
if err != nil { | ||
return User{}, xerrors.Errorf("generate user identifier: %w", err) | ||
} | ||
if r.cfg.Username == "" { | ||
r.cfg.Username = genUsername | ||
} | ||
if r.cfg.Email == "" { | ||
r.cfg.Email = genEmail | ||
} | ||
} | ||
_, _ = fmt.Fprintln(logs, "Generating user password...") | ||
password, err := cryptorand.String(16) | ||
if err != nil { | ||
return User{}, xerrors.Errorf("generate random password for user: %w", err) | ||
} | ||
_, _ = fmt.Fprintln(logs, "Creating user:") | ||
user, err := r.client.CreateUserWithOrgs(ctx, codersdk.CreateUserRequestWithOrgs{ | ||
OrganizationIDs: []uuid.UUID{r.cfg.OrganizationID}, | ||
Username: r.cfg.Username, | ||
Email: r.cfg.Email, | ||
Password: password, | ||
}) | ||
if err != nil { | ||
return User{}, xerrors.Errorf("create user: %w", err) | ||
} | ||
r.user = user | ||
_, _ = fmt.Fprintln(logs, "\nLogging in as new user...") | ||
client := codersdk.New(r.client.URL) | ||
loginRes, err := client.LoginWithPassword(ctx, codersdk.LoginWithPasswordRequest{ | ||
Email: r.cfg.Email, | ||
Password: password, | ||
}) | ||
if err != nil { | ||
return User{}, xerrors.Errorf("login as new user: %w", err) | ||
} | ||
_, _ = fmt.Fprintf(logs, "\tOrg ID: %s\n", r.cfg.OrganizationID.String()) | ||
_, _ = fmt.Fprintf(logs, "\tUsername: %s\n", user.Username) | ||
_, _ = fmt.Fprintf(logs, "\tEmail: %s\n", user.Email) | ||
_, _ = fmt.Fprintf(logs, "\tPassword: ****************\n") | ||
return User{User: user, SessionToken: loginRes.SessionToken}, nil | ||
} | ||
func (r *Runner) Cleanup(ctx context.Context, _ string, logs io.Writer) error { | ||
if r.user.ID != uuid.Nil { | ||
err := r.client.DeleteUser(ctx, r.user.ID) | ||
if err != nil { | ||
_, _ = fmt.Fprintf(logs, "failed to delete user %q: %v\n", r.user.ID.String(), err) | ||
return xerrors.Errorf("delete user: %w", err) | ||
} | ||
} | ||
return nil | ||
} |
51 changes: 16 additions & 35 deletionsscaletest/createworkspaces/run.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.