@@ -21,13 +21,11 @@ import (
21
21
"testing"
22
22
"time"
23
23
24
- "golang.org/x/xerrors"
25
-
26
- "github.com/google/uuid"
27
24
"github.com/stretchr/testify/assert"
28
25
"github.com/stretchr/testify/require"
29
26
"golang.org/x/crypto/ssh"
30
27
gosshagent "golang.org/x/crypto/ssh/agent"
28
+ "golang.org/x/xerrors"
31
29
32
30
"cdr.dev/slog"
33
31
"cdr.dev/slog/sloggers/slogtest"
@@ -38,61 +36,25 @@ import (
38
36
"github.com/coder/coder/v2/cli/cliui"
39
37
"github.com/coder/coder/v2/coderd/coderdtest"
40
38
"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"
41
41
"github.com/coder/coder/v2/codersdk"
42
- "github.com/coder/coder/v2/provisioner/echo"
43
42
"github.com/coder/coder/v2/provisionersdk/proto"
44
43
"github.com/coder/coder/v2/pty"
45
44
"github.com/coder/coder/v2/pty/ptytest"
46
45
"github.com/coder/coder/v2/testutil"
47
46
)
48
47
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 ) {
54
49
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 )
61
52
client .SetLogger (slogtest .Make (t , nil ).Named ("client" ).Leveled (slog .LevelDebug ))
62
53
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 ... )
96
58
97
59
return client , workspace , agentToken
98
60
}
@@ -102,7 +64,7 @@ func TestSSH(t *testing.T) {
102
64
t .Run ("ImmediateExit" , func (t * testing.T ) {
103
65
t .Parallel ()
104
66
105
- client , workspace , agentToken := setupWorkspaceForAgent (t , nil )
67
+ client , workspace , agentToken := setupWorkspaceForAgent (t )
106
68
inv , root := clitest .New (t , "ssh" , workspace .Name )
107
69
clitest .SetupConfig (t , client , root )
108
70
pty := ptytest .New (t ).Attach (inv )
@@ -159,7 +121,14 @@ func TestSSH(t *testing.T) {
159
121
t .Skip ("Windows doesn't seem to clean up the process, maybe #7100 will fix it" )
160
122
}
161
123
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
+ })
163
132
inv , root := clitest .New (t , "ssh" , workspace .Name )
164
133
clitest .SetupConfig (t , client , root )
165
134
pty := ptytest .New (t ).Attach (inv )
@@ -180,7 +149,13 @@ func TestSSH(t *testing.T) {
180
149
pty .WriteLine ("echo hell'o'" )
181
150
pty .ExpectMatchContext (ctx , "hello" )
182
151
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" )
184
159
185
160
select {
186
161
case <- cmdDone :
@@ -191,7 +166,7 @@ func TestSSH(t *testing.T) {
191
166
192
167
t .Run ("Stdio" , func (t * testing.T ) {
193
168
t .Parallel ()
194
- client , workspace , agentToken := setupWorkspaceForAgent (t , nil )
169
+ client , workspace , agentToken := setupWorkspaceForAgent (t )
195
170
_ , _ = tGoContext (t , func (ctx context.Context ) {
196
171
// Run this async so the SSH command has to wait for
197
172
// the build and agent to connect!
@@ -251,7 +226,7 @@ func TestSSH(t *testing.T) {
251
226
252
227
t .Run ("Stdio_RemoteForward_Signal" , func (t * testing.T ) {
253
228
t .Parallel ()
254
- client , workspace , agentToken := setupWorkspaceForAgent (t , nil )
229
+ client , workspace , agentToken := setupWorkspaceForAgent (t )
255
230
_ , _ = tGoContext (t , func (ctx context.Context ) {
256
231
// Run this async so the SSH command has to wait for
257
232
// the build and agent to connect!
@@ -312,7 +287,7 @@ func TestSSH(t *testing.T) {
312
287
313
288
t .Run ("Stdio_BrokenConn" , func (t * testing.T ) {
314
289
t .Parallel ()
315
- client , workspace , agentToken := setupWorkspaceForAgent (t , nil )
290
+ client , workspace , agentToken := setupWorkspaceForAgent (t )
316
291
_ , _ = tGoContext (t , func (ctx context.Context ) {
317
292
// Run this async so the SSH command has to wait for
318
293
// the build and agent to connect!
@@ -373,7 +348,7 @@ func TestSSH(t *testing.T) {
373
348
}
374
349
t .Parallel ()
375
350
ctx := testutil .Context (t , testutil .WaitSuperLong )
376
- client , workspace , agentToken := setupWorkspaceForAgent (t , nil )
351
+ client , workspace , agentToken := setupWorkspaceForAgent (t )
377
352
_ , _ = tGoContext (t , func (ctx context.Context ) {
378
353
// Run this async so the SSH command has to wait for
379
354
// the build and agent to connect!
@@ -483,7 +458,16 @@ func TestSSH(t *testing.T) {
483
458
if runtime .GOOS == "windows" {
484
459
t .Skip ("Windows doesn't seem to clean up the process, maybe #7100 will fix it" )
485
460
}
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
+
487
471
_ , _ = tGoContext (t , func (ctx context.Context ) {
488
472
// Run this async so the SSH command has to wait for
489
473
// the build and agent to connect.
@@ -533,7 +517,14 @@ func TestSSH(t *testing.T) {
533
517
err = session .Shell ()
534
518
require .NoError (t , err )
535
519
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" )
537
528
538
529
select {
539
530
case <- cmdDone :
@@ -549,7 +540,7 @@ func TestSSH(t *testing.T) {
549
540
550
541
t .Parallel ()
551
542
552
- client , workspace , agentToken := setupWorkspaceForAgent (t , nil )
543
+ client , workspace , agentToken := setupWorkspaceForAgent (t )
553
544
554
545
_ = agenttest .New (t , client .URL , agentToken )
555
546
coderdtest .AwaitWorkspaceAgents (t , client , workspace .ID )
@@ -635,7 +626,7 @@ func TestSSH(t *testing.T) {
635
626
}))
636
627
defer httpServer .Close ()
637
628
638
- client , workspace , agentToken := setupWorkspaceForAgent (t , nil )
629
+ client , workspace , agentToken := setupWorkspaceForAgent (t )
639
630
_ = agenttest .New (t , client .URL , agentToken )
640
631
coderdtest .AwaitWorkspaceAgents (t , client , workspace .ID )
641
632
@@ -686,7 +677,7 @@ func TestSSH(t *testing.T) {
686
677
687
678
t .Parallel ()
688
679
689
- client , workspace , agentToken := setupWorkspaceForAgent (t , nil )
680
+ client , workspace , agentToken := setupWorkspaceForAgent (t )
690
681
691
682
_ = agenttest .New (t , client .URL , agentToken )
692
683
coderdtest .AwaitWorkspaceAgents (t , client , workspace .ID )
@@ -733,7 +724,7 @@ func TestSSH(t *testing.T) {
733
724
734
725
logDir := t .TempDir ()
735
726
736
- client , workspace , agentToken := setupWorkspaceForAgent (t , nil )
727
+ client , workspace , agentToken := setupWorkspaceForAgent (t )
737
728
inv , root := clitest .New (t , "ssh" , "-l" , logDir , workspace .Name )
738
729
clitest .SetupConfig (t , client , root )
739
730
pty := ptytest .New (t ).Attach (inv )
@@ -908,7 +899,7 @@ Expire-Date: 0
908
899
workspaceAgentSocketPath := strings .TrimSpace (stdout .String ())
909
900
require .NotEqual (t , extraSocketPath , workspaceAgentSocketPath , "socket path should be different" )
910
901
911
- client , workspace , agentToken := setupWorkspaceForAgent (t , nil )
902
+ client , workspace , agentToken := setupWorkspaceForAgent (t )
912
903
913
904
_ = agenttest .New (t , client .URL , agentToken , func (o * agent.Options ) {
914
905
o .EnvironmentVariables = map [string ]string {
0 commit comments