Skip to content

Commit 41f7bb8

Browse files
committed
validate and fail-fast
1 parent 6aa0a52 commit 41f7bb8

File tree

4 files changed

+85
-38
lines changed

4 files changed

+85
-38
lines changed

cli/exp_scaletest.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1065,6 +1065,12 @@ func (r *RootCmd) scaletestDashboard() *clibase.Cmd {
10651065
r.InitClient(client),
10661066
),
10671067
Handler: func(inv *clibase.Invocation) error {
1068+
if !(minWait > 0) {
1069+
return xerrors.Errorf("--min-wait must be greater than zero")
1070+
}
1071+
if !(maxWait > minWait) {
1072+
return xerrors.Errorf("--max-wait must be greater than --min-wait")
1073+
}
10681074
ctx := inv.Context()
10691075
logger := slog.Make(sloghuman.Sink(inv.Stdout)).Leveled(slog.LevelInfo)
10701076
tracerProvider, closeTracing, tracingEnabled, err := tracingFlags.provider(ctx)

cli/exp_scaletest_test.go

Lines changed: 70 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -92,28 +92,77 @@ func TestScaleTestWorkspaceTraffic(t *testing.T) {
9292
// This test just validates that the CLI command accepts its known arguments.
9393
func TestScaleTestDashboard(t *testing.T) {
9494
t.Parallel()
95-
ctx, cancelFunc := context.WithTimeout(context.Background(), testutil.WaitMedium)
96-
defer cancelFunc()
97-
98-
log := slogtest.Make(t, &slogtest.Options{IgnoreErrors: true})
99-
client := coderdtest.New(t, &coderdtest.Options{
100-
Logger: &log,
95+
t.Run("MinWait", func(t *testing.T) {
96+
t.Parallel()
97+
ctx, cancelFunc := context.WithTimeout(context.Background(), testutil.WaitShort)
98+
defer cancelFunc()
99+
100+
log := slogtest.Make(t, &slogtest.Options{IgnoreErrors: true})
101+
client := coderdtest.New(t, &coderdtest.Options{
102+
Logger: &log,
103+
})
104+
_ = coderdtest.CreateFirstUser(t, client)
105+
106+
inv, root := clitest.New(t, "exp", "scaletest", "dashboard",
107+
"--min-wait", "0s",
108+
)
109+
clitest.SetupConfig(t, client, root)
110+
pty := ptytest.New(t)
111+
inv.Stdout = pty.Output()
112+
inv.Stderr = pty.Output()
113+
114+
err := inv.WithContext(ctx).Run()
115+
require.ErrorContains(t, err, "--min-wait must be greater than zero")
101116
})
102-
_ = coderdtest.CreateFirstUser(t, client)
103117

104-
inv, root := clitest.New(t, "exp", "scaletest", "dashboard",
105-
"--count", "1",
106-
"--min-wait", "100ms",
107-
"--max-wait", "1s",
108-
"--timeout", "5s",
109-
"--scaletest-prometheus-address", "127.0.0.1:0",
110-
"--scaletest-prometheus-wait", "0s",
111-
)
112-
clitest.SetupConfig(t, client, root)
113-
pty := ptytest.New(t)
114-
inv.Stdout = pty.Output()
115-
inv.Stderr = pty.Output()
118+
t.Run("MaxWait", func(t *testing.T) {
119+
t.Parallel()
120+
ctx, cancelFunc := context.WithTimeout(context.Background(), testutil.WaitShort)
121+
defer cancelFunc()
122+
123+
log := slogtest.Make(t, &slogtest.Options{IgnoreErrors: true})
124+
client := coderdtest.New(t, &coderdtest.Options{
125+
Logger: &log,
126+
})
127+
_ = coderdtest.CreateFirstUser(t, client)
128+
129+
inv, root := clitest.New(t, "exp", "scaletest", "dashboard",
130+
"--min-wait", "0s",
131+
)
132+
clitest.SetupConfig(t, client, root)
133+
pty := ptytest.New(t)
134+
inv.Stdout = pty.Output()
135+
inv.Stderr = pty.Output()
136+
137+
err := inv.WithContext(ctx).Run()
138+
require.ErrorContains(t, err, "--max-wait must be greater than --min-wait")
139+
})
116140

117-
err := inv.WithContext(ctx).Run()
118-
require.NoError(t, err, "")
141+
t.Run("OK", func(t *testing.T) {
142+
t.Parallel()
143+
ctx, cancelFunc := context.WithTimeout(context.Background(), testutil.WaitMedium)
144+
defer cancelFunc()
145+
146+
log := slogtest.Make(t, &slogtest.Options{IgnoreErrors: true})
147+
client := coderdtest.New(t, &coderdtest.Options{
148+
Logger: &log,
149+
})
150+
_ = coderdtest.CreateFirstUser(t, client)
151+
152+
inv, root := clitest.New(t, "exp", "scaletest", "dashboard",
153+
"--count", "1",
154+
"--min-wait", "100ms",
155+
"--max-wait", "1s",
156+
"--timeout", "5s",
157+
"--scaletest-prometheus-address", "127.0.0.1:0",
158+
"--scaletest-prometheus-wait", "0s",
159+
)
160+
clitest.SetupConfig(t, client, root)
161+
pty := ptytest.New(t)
162+
inv.Stdout = pty.Output()
163+
inv.Stderr = pty.Output()
164+
165+
err := inv.WithContext(ctx).Run()
166+
require.NoError(t, err, "")
167+
})
119168
}

scaletest/dashboard/config.go

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,30 +11,26 @@ import (
1111

1212
type Config struct {
1313
// MinWait is the minimum interval between fetches.
14-
MinWait time.Duration `json:"duration_min"`
14+
MinWait time.Duration `json:"min_wait"`
1515
// MaxWait is the maximum interval between fetches.
16-
MaxWait time.Duration `json:"duration_max"`
16+
MaxWait time.Duration `json:"max_wait"`
1717
// Trace is whether to trace the requests.
1818
Trace bool `json:"trace"`
1919
// Logger is the logger to use.
2020
Logger slog.Logger `json:"-"`
2121
// Headless controls headless mode for chromedp.
22-
Headless bool `json:"no_headless"`
22+
Headless bool `json:"headless"`
2323
// ActionFunc is a function that returns an action to run.
2424
ActionFunc func(ctx context.Context) (Label, Action, error) `json:"-"`
2525
}
2626

2727
func (c Config) Validate() error {
28-
if c.MinWait <= 0 {
29-
return xerrors.Errorf("validate duration_min: must be greater than zero")
28+
if !(c.MinWait > 0) {
29+
return xerrors.Errorf("validate min_wait: must be greater than zero")
3030
}
3131

32-
if c.MaxWait <= 0 {
33-
return xerrors.Errorf("validate duration_max: must be greater than zero")
34-
}
35-
36-
if c.MinWait > c.MaxWait {
37-
return xerrors.Errorf("validate duration_min: must be less than duration_max")
32+
if !(c.MaxWait > c.MinWait) {
33+
return xerrors.Errorf("validate max_wait: must be greater than min_wait")
3834
}
3935

4036
if c.ActionFunc == nil {

scaletest/dashboard/run.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,7 @@ func (*Runner) Cleanup(_ context.Context, _ string) error {
8686
}
8787

8888
func (r *Runner) randWait() time.Duration {
89-
var wait time.Duration
90-
if r.cfg.MaxWait > r.cfg.MinWait {
91-
//nolint:gosec // This is not for cryptographic purposes. Chill, gosec. Chill.
92-
wait = time.Duration(rand.Intn(int(r.cfg.MaxWait) - int(r.cfg.MinWait)))
93-
}
94-
89+
//nolint:gosec // This is not for cryptographic purposes. Chill, gosec. Chill.
90+
wait := time.Duration(rand.Intn(int(r.cfg.MaxWait) - int(r.cfg.MinWait)))
9591
return r.cfg.MinWait + wait
9692
}

0 commit comments

Comments
 (0)