Skip to content

Commit a6a36f7

Browse files
committed
chore: add createworkspaces tests
1 parent 2ff62ec commit a6a36f7

File tree

4 files changed

+446
-2
lines changed

4 files changed

+446
-2
lines changed
Lines changed: 194 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,194 @@
1+
package createworkspaces_test
2+
3+
import (
4+
"testing"
5+
"time"
6+
7+
"github.com/google/uuid"
8+
"github.com/stretchr/testify/require"
9+
10+
"github.com/coder/coder/coderd/httpapi"
11+
"github.com/coder/coder/codersdk"
12+
"github.com/coder/coder/scaletest/agentconn"
13+
"github.com/coder/coder/scaletest/createworkspaces"
14+
"github.com/coder/coder/scaletest/reconnectingpty"
15+
"github.com/coder/coder/scaletest/workspacebuild"
16+
)
17+
18+
func Test_UserConfig(t *testing.T) {
19+
t.Parallel()
20+
21+
id := uuid.New()
22+
23+
cases := []struct {
24+
name string
25+
config createworkspaces.UserConfig
26+
errContains string
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 := range cases {
66+
c := c
67+
68+
t.Run(c.name, func(t *testing.T) {
69+
t.Parallel()
70+
71+
err := c.config.Validate()
72+
if c.errContains != "" {
73+
require.Error(t, err)
74+
require.Contains(t, err.Error(), c.errContains)
75+
} else {
76+
require.NoError(t, err)
77+
}
78+
})
79+
}
80+
}
81+
82+
func Test_Config(t *testing.T) {
83+
t.Parallel()
84+
85+
id := uuid.New()
86+
87+
userConfig := createworkspaces.UserConfig{
88+
OrganizationID: id,
89+
Username: id.String(),
90+
Email: id.String() + "@example.com",
91+
}
92+
93+
workspaceConfig := workspacebuild.Config{
94+
OrganizationID: id,
95+
UserID: id.String(),
96+
Request: codersdk.CreateWorkspaceRequest{
97+
TemplateID: id,
98+
},
99+
}
100+
101+
reconnectingPTYConfig := reconnectingpty.Config{
102+
AgentID: id,
103+
}
104+
105+
agentConnConfig := agentconn.Config{
106+
AgentID: id,
107+
ConnectionMode: agentconn.ConnectionModeDirect,
108+
HoldDuration: httpapi.Duration(time.Minute),
109+
}
110+
111+
cases := []struct {
112+
name string
113+
config createworkspaces.Config
114+
errContains string
115+
}{
116+
{
117+
name: "OK",
118+
config: createworkspaces.Config{
119+
User: userConfig,
120+
Workspace: workspaceConfig,
121+
ReconnectingPTY: &reconnectingPTYConfig,
122+
AgentConn: &agentConnConfig,
123+
},
124+
},
125+
{
126+
name: "OKOptional",
127+
config: createworkspaces.Config{
128+
User: userConfig,
129+
Workspace: workspaceConfig,
130+
ReconnectingPTY: nil,
131+
AgentConn: nil,
132+
},
133+
},
134+
{
135+
name: "BadUserConfig",
136+
config: createworkspaces.Config{
137+
User: createworkspaces.UserConfig{
138+
OrganizationID: uuid.Nil,
139+
},
140+
},
141+
errContains: "validate user",
142+
},
143+
{
144+
name: "BadWorkspaceConfig",
145+
config: createworkspaces.Config{
146+
User: userConfig,
147+
Workspace: workspacebuild.Config{
148+
Request: codersdk.CreateWorkspaceRequest{
149+
TemplateID: uuid.Nil,
150+
},
151+
},
152+
},
153+
errContains: "validate workspace",
154+
},
155+
{
156+
name: "BadReconnectingPTYConfig",
157+
config: createworkspaces.Config{
158+
User: userConfig,
159+
Workspace: workspaceConfig,
160+
ReconnectingPTY: &reconnectingpty.Config{
161+
Timeout: httpapi.Duration(-1 * time.Second),
162+
},
163+
},
164+
errContains: "validate reconnecting pty",
165+
},
166+
{
167+
name: "BadAgentConnConfig",
168+
config: createworkspaces.Config{
169+
User: userConfig,
170+
Workspace: workspaceConfig,
171+
AgentConn: &agentconn.Config{
172+
ConnectionMode: "bad",
173+
},
174+
},
175+
errContains: "validate agent conn",
176+
},
177+
}
178+
179+
for _, c := range cases {
180+
c := c
181+
182+
t.Run(c.name, func(t *testing.T) {
183+
t.Parallel()
184+
185+
err := c.config.Validate()
186+
if c.errContains != "" {
187+
require.Error(t, err)
188+
require.Contains(t, err.Error(), c.errContains)
189+
} else {
190+
require.NoError(t, err)
191+
}
192+
})
193+
}
194+
}

scaletest/createworkspaces/run.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,15 @@ import (
99
"golang.org/x/sync/errgroup"
1010
"golang.org/x/xerrors"
1111

12+
"cdr.dev/slog"
13+
"cdr.dev/slog/sloggers/sloghuman"
14+
15+
"github.com/coder/coder/coderd/tracing"
1216
"github.com/coder/coder/codersdk"
1317
"github.com/coder/coder/cryptorand"
1418
"github.com/coder/coder/scaletest/agentconn"
1519
"github.com/coder/coder/scaletest/harness"
20+
"github.com/coder/coder/scaletest/loadtestutil"
1621
"github.com/coder/coder/scaletest/reconnectingpty"
1722
"github.com/coder/coder/scaletest/workspacebuild"
1823
)
@@ -37,13 +42,22 @@ func NewRunner(client *codersdk.Client, cfg Config) *Runner {
3742

3843
// Run implements Runnable.
3944
func (r *Runner) Run(ctx context.Context, id string, logs io.Writer) error {
45+
ctx, span := tracing.StartSpan(ctx)
46+
defer span.End()
47+
48+
logs = loadtestutil.NewSyncWriter(logs)
49+
logger := slog.Make(sloghuman.Sink(logs)).Leveled(slog.LevelDebug)
50+
r.client.Logger = logger
51+
r.client.LogBodies = true
52+
4053
_, _ = fmt.Fprintln(logs, "Generating user password...")
4154
password, err := cryptorand.HexString(16)
4255
if err != nil {
4356
return xerrors.Errorf("generate random password for user: %w", err)
4457
}
4558

4659
_, _ = fmt.Fprintln(logs, "Creating user:")
60+
_, _ = fmt.Fprintf(logs, "\tOrg ID: %s\n", r.cfg.User.OrganizationID.String())
4761
_, _ = fmt.Fprintf(logs, "\tUsername: %s\n", r.cfg.User.Username)
4862
_, _ = fmt.Fprintf(logs, "\tEmail: %s\n", r.cfg.User.Email)
4963
_, _ = fmt.Fprintf(logs, "\tPassword: ****************\n")

0 commit comments

Comments
 (0)