Skip to content

Commit 2a1290d

Browse files
committed
chore: use dbfake for ssh tests rather than provisionerd
1 parent 7060069 commit 2a1290d

File tree

5 files changed

+141
-99
lines changed

5 files changed

+141
-99
lines changed

cli/ping_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ func TestPing(t *testing.T) {
1919
t.Run("OK", func(t *testing.T) {
2020
t.Parallel()
2121

22-
client, workspace, agentToken := setupWorkspaceForAgent(t, nil)
22+
client, workspace, agentToken := setupWorkspaceForAgent(t)
2323
inv, root := clitest.New(t, "ping", workspace.Name)
2424
clitest.SetupConfig(t, client, root)
2525
pty := ptytest.New(t)

cli/speedtest_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ func TestSpeedtest(t *testing.T) {
2323
if testing.Short() {
2424
t.Skip("This test takes a minimum of 5ms per a hardcoded value in Tailscale!")
2525
}
26-
client, workspace, agentToken := setupWorkspaceForAgent(t, nil)
26+
client, workspace, agentToken := setupWorkspaceForAgent(t)
2727
_ = agenttest.New(t, client.URL, agentToken)
2828
coderdtest.AwaitWorkspaceAgents(t, client, workspace.ID)
2929

cli/ssh_test.go

+53-62
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,11 @@ import (
2121
"testing"
2222
"time"
2323

24-
"golang.org/x/xerrors"
25-
26-
"github.com/google/uuid"
2724
"github.com/stretchr/testify/assert"
2825
"github.com/stretchr/testify/require"
2926
"golang.org/x/crypto/ssh"
3027
gosshagent "golang.org/x/crypto/ssh/agent"
28+
"golang.org/x/xerrors"
3129

3230
"cdr.dev/slog"
3331
"cdr.dev/slog/sloggers/slogtest"
@@ -38,61 +36,25 @@ import (
3836
"github.com/coder/coder/v2/cli/cliui"
3937
"github.com/coder/coder/v2/coderd/coderdtest"
4038
"github.com/coder/coder/v2/coderd/database"
39+
"github.com/coder/coder/v2/coderd/database/dbfake"
40+
"github.com/coder/coder/v2/coderd/database/dbtestutil"
4141
"github.com/coder/coder/v2/codersdk"
42-
"github.com/coder/coder/v2/provisioner/echo"
4342
"github.com/coder/coder/v2/provisionersdk/proto"
4443
"github.com/coder/coder/v2/pty"
4544
"github.com/coder/coder/v2/pty/ptytest"
4645
"github.com/coder/coder/v2/testutil"
4746
)
4847

49-
const (
50-
startupScriptPattern = "i-am-ready"
51-
)
52-
53-
func setupWorkspaceForAgent(t *testing.T, mutate func([]*proto.Agent) []*proto.Agent) (*codersdk.Client, codersdk.Workspace, string) {
48+
func setupWorkspaceForAgent(t *testing.T, mutations ...func([]*proto.Agent) []*proto.Agent) (*codersdk.Client, database.Workspace, string) {
5449
t.Helper()
55-
if mutate == nil {
56-
mutate = func(a []*proto.Agent) []*proto.Agent {
57-
return a
58-
}
59-
}
60-
client := coderdtest.New(t, &coderdtest.Options{IncludeProvisionerDaemon: true})
50+
51+
client, store := coderdtest.NewWithDatabase(t, nil)
6152
client.SetLogger(slogtest.Make(t, nil).Named("client").Leveled(slog.LevelDebug))
6253
user := coderdtest.CreateFirstUser(t, client)
63-
agentToken := uuid.NewString()
64-
version := coderdtest.CreateTemplateVersion(t, client, user.OrganizationID, &echo.Responses{
65-
Parse: echo.ParseComplete,
66-
ProvisionPlan: echo.PlanComplete,
67-
ProvisionApply: []*proto.Response{{
68-
Type: &proto.Response_Apply{
69-
Apply: &proto.ApplyComplete{
70-
Resources: []*proto.Resource{{
71-
Name: "dev",
72-
Type: "google_compute_instance",
73-
Agents: mutate([]*proto.Agent{{
74-
Id: uuid.NewString(),
75-
Auth: &proto.Agent_Token{
76-
Token: agentToken,
77-
},
78-
Scripts: []*proto.Script{
79-
{
80-
Script: fmt.Sprintf("echo '%s'", startupScriptPattern),
81-
RunOnStart: true,
82-
},
83-
},
84-
}}),
85-
}},
86-
},
87-
},
88-
}},
89-
})
90-
coderdtest.AwaitTemplateVersionJobCompleted(t, client, version.ID)
91-
template := coderdtest.CreateTemplate(t, client, user.OrganizationID, version.ID)
92-
workspace := coderdtest.CreateWorkspace(t, client, user.OrganizationID, template.ID)
93-
coderdtest.AwaitWorkspaceBuildJobCompleted(t, client, workspace.LatestBuild.ID)
94-
workspace, err := client.Workspace(context.Background(), workspace.ID)
95-
require.NoError(t, err)
54+
workspace, agentToken := dbfake.WorkspaceWithAgent(t, store, database.Workspace{
55+
OrganizationID: user.OrganizationID,
56+
OwnerID: user.UserID,
57+
}, mutations...)
9658

9759
return client, workspace, agentToken
9860
}
@@ -102,7 +64,7 @@ func TestSSH(t *testing.T) {
10264
t.Run("ImmediateExit", func(t *testing.T) {
10365
t.Parallel()
10466

105-
client, workspace, agentToken := setupWorkspaceForAgent(t, nil)
67+
client, workspace, agentToken := setupWorkspaceForAgent(t)
10668
inv, root := clitest.New(t, "ssh", workspace.Name)
10769
clitest.SetupConfig(t, client, root)
10870
pty := ptytest.New(t).Attach(inv)
@@ -159,7 +121,14 @@ func TestSSH(t *testing.T) {
159121
t.Skip("Windows doesn't seem to clean up the process, maybe #7100 will fix it")
160122
}
161123

162-
client, workspace, agentToken := setupWorkspaceForAgent(t, nil)
124+
store, ps := dbtestutil.NewDB(t)
125+
client := coderdtest.New(t, &coderdtest.Options{Pubsub: ps, Database: store})
126+
client.SetLogger(slogtest.Make(t, nil).Named("client").Leveled(slog.LevelDebug))
127+
user := coderdtest.CreateFirstUser(t, client)
128+
workspace, agentToken := dbfake.WorkspaceWithAgent(t, store, database.Workspace{
129+
OrganizationID: user.OrganizationID,
130+
OwnerID: user.UserID,
131+
})
163132
inv, root := clitest.New(t, "ssh", workspace.Name)
164133
clitest.SetupConfig(t, client, root)
165134
pty := ptytest.New(t).Attach(inv)
@@ -180,7 +149,13 @@ func TestSSH(t *testing.T) {
180149
pty.WriteLine("echo hell'o'")
181150
pty.ExpectMatchContext(ctx, "hello")
182151

183-
workspace = coderdtest.MustTransitionWorkspace(t, client, workspace.ID, database.WorkspaceTransitionStart, database.WorkspaceTransitionStop)
152+
_ = dbfake.WorkspaceBuildBuilder(t, store, workspace).
153+
Seed(database.WorkspaceBuild{
154+
Transition: database.WorkspaceTransitionStop,
155+
BuildNumber: 2,
156+
}).
157+
Pubsub(ps).Do()
158+
t.Log("stopped workspace")
184159

185160
select {
186161
case <-cmdDone:
@@ -191,7 +166,7 @@ func TestSSH(t *testing.T) {
191166

192167
t.Run("Stdio", func(t *testing.T) {
193168
t.Parallel()
194-
client, workspace, agentToken := setupWorkspaceForAgent(t, nil)
169+
client, workspace, agentToken := setupWorkspaceForAgent(t)
195170
_, _ = tGoContext(t, func(ctx context.Context) {
196171
// Run this async so the SSH command has to wait for
197172
// the build and agent to connect!
@@ -251,7 +226,7 @@ func TestSSH(t *testing.T) {
251226

252227
t.Run("Stdio_RemoteForward_Signal", func(t *testing.T) {
253228
t.Parallel()
254-
client, workspace, agentToken := setupWorkspaceForAgent(t, nil)
229+
client, workspace, agentToken := setupWorkspaceForAgent(t)
255230
_, _ = tGoContext(t, func(ctx context.Context) {
256231
// Run this async so the SSH command has to wait for
257232
// the build and agent to connect!
@@ -312,7 +287,7 @@ func TestSSH(t *testing.T) {
312287

313288
t.Run("Stdio_BrokenConn", func(t *testing.T) {
314289
t.Parallel()
315-
client, workspace, agentToken := setupWorkspaceForAgent(t, nil)
290+
client, workspace, agentToken := setupWorkspaceForAgent(t)
316291
_, _ = tGoContext(t, func(ctx context.Context) {
317292
// Run this async so the SSH command has to wait for
318293
// the build and agent to connect!
@@ -373,7 +348,7 @@ func TestSSH(t *testing.T) {
373348
}
374349
t.Parallel()
375350
ctx := testutil.Context(t, testutil.WaitSuperLong)
376-
client, workspace, agentToken := setupWorkspaceForAgent(t, nil)
351+
client, workspace, agentToken := setupWorkspaceForAgent(t)
377352
_, _ = tGoContext(t, func(ctx context.Context) {
378353
// Run this async so the SSH command has to wait for
379354
// the build and agent to connect!
@@ -483,7 +458,16 @@ func TestSSH(t *testing.T) {
483458
if runtime.GOOS == "windows" {
484459
t.Skip("Windows doesn't seem to clean up the process, maybe #7100 will fix it")
485460
}
486-
client, workspace, agentToken := setupWorkspaceForAgent(t, nil)
461+
462+
store, ps := dbtestutil.NewDB(t)
463+
client := coderdtest.New(t, &coderdtest.Options{Pubsub: ps, Database: store})
464+
client.SetLogger(slogtest.Make(t, nil).Named("client").Leveled(slog.LevelDebug))
465+
user := coderdtest.CreateFirstUser(t, client)
466+
workspace, agentToken := dbfake.WorkspaceWithAgent(t, store, database.Workspace{
467+
OrganizationID: user.OrganizationID,
468+
OwnerID: user.UserID,
469+
})
470+
487471
_, _ = tGoContext(t, func(ctx context.Context) {
488472
// Run this async so the SSH command has to wait for
489473
// the build and agent to connect.
@@ -533,7 +517,14 @@ func TestSSH(t *testing.T) {
533517
err = session.Shell()
534518
require.NoError(t, err)
535519

536-
workspace = coderdtest.MustTransitionWorkspace(t, client, workspace.ID, database.WorkspaceTransitionStart, database.WorkspaceTransitionStop)
520+
_ = dbfake.WorkspaceBuildBuilder(t, store, workspace).
521+
Seed(database.WorkspaceBuild{
522+
Transition: database.WorkspaceTransitionStop,
523+
BuildNumber: 2,
524+
}).
525+
Pubsub(ps).
526+
Do()
527+
t.Log("stopped workspace")
537528

538529
select {
539530
case <-cmdDone:
@@ -549,7 +540,7 @@ func TestSSH(t *testing.T) {
549540

550541
t.Parallel()
551542

552-
client, workspace, agentToken := setupWorkspaceForAgent(t, nil)
543+
client, workspace, agentToken := setupWorkspaceForAgent(t)
553544

554545
_ = agenttest.New(t, client.URL, agentToken)
555546
coderdtest.AwaitWorkspaceAgents(t, client, workspace.ID)
@@ -635,7 +626,7 @@ func TestSSH(t *testing.T) {
635626
}))
636627
defer httpServer.Close()
637628

638-
client, workspace, agentToken := setupWorkspaceForAgent(t, nil)
629+
client, workspace, agentToken := setupWorkspaceForAgent(t)
639630
_ = agenttest.New(t, client.URL, agentToken)
640631
coderdtest.AwaitWorkspaceAgents(t, client, workspace.ID)
641632

@@ -686,7 +677,7 @@ func TestSSH(t *testing.T) {
686677

687678
t.Parallel()
688679

689-
client, workspace, agentToken := setupWorkspaceForAgent(t, nil)
680+
client, workspace, agentToken := setupWorkspaceForAgent(t)
690681

691682
_ = agenttest.New(t, client.URL, agentToken)
692683
coderdtest.AwaitWorkspaceAgents(t, client, workspace.ID)
@@ -733,7 +724,7 @@ func TestSSH(t *testing.T) {
733724

734725
logDir := t.TempDir()
735726

736-
client, workspace, agentToken := setupWorkspaceForAgent(t, nil)
727+
client, workspace, agentToken := setupWorkspaceForAgent(t)
737728
inv, root := clitest.New(t, "ssh", "-l", logDir, workspace.Name)
738729
clitest.SetupConfig(t, client, root)
739730
pty := ptytest.New(t).Attach(inv)
@@ -908,7 +899,7 @@ Expire-Date: 0
908899
workspaceAgentSocketPath := strings.TrimSpace(stdout.String())
909900
require.NotEqual(t, extraSocketPath, workspaceAgentSocketPath, "socket path should be different")
910901

911-
client, workspace, agentToken := setupWorkspaceForAgent(t, nil)
902+
client, workspace, agentToken := setupWorkspaceForAgent(t)
912903

913904
_ = agenttest.New(t, client.URL, agentToken, func(o *agent.Options) {
914905
o.EnvironmentVariables = map[string]string{

cli/vscodessh_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import (
2222
func TestVSCodeSSH(t *testing.T) {
2323
t.Parallel()
2424
ctx := testutil.Context(t, testutil.WaitLong)
25-
client, workspace, agentToken := setupWorkspaceForAgent(t, nil)
25+
client, workspace, agentToken := setupWorkspaceForAgent(t)
2626
user, err := client.User(ctx, codersdk.Me)
2727
require.NoError(t, err)
2828

0 commit comments

Comments
 (0)