@@ -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,71 +36,36 @@ 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
- 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 )
96
-
97
- return client , workspace , agentToken
53
+ first := coderdtest .CreateFirstUser (t , client )
54
+ userClient , user := coderdtest .CreateAnotherUser (t , client , first .OrganizationID )
55
+ workspace , agentToken := dbfake .WorkspaceWithAgent (t , store , database.Workspace {
56
+ OrganizationID : first .OrganizationID ,
57
+ OwnerID : user .ID ,
58
+ }, mutations ... )
59
+
60
+ return userClient , workspace , agentToken
98
61
}
99
62
100
63
func TestSSH (t * testing.T ) {
101
64
t .Parallel ()
102
65
t .Run ("ImmediateExit" , func (t * testing.T ) {
103
66
t .Parallel ()
104
67
105
- client , workspace , agentToken := setupWorkspaceForAgent (t , nil )
68
+ client , workspace , agentToken := setupWorkspaceForAgent (t )
106
69
inv , root := clitest .New (t , "ssh" , workspace .Name )
107
70
clitest .SetupConfig (t , client , root )
108
71
pty := ptytest .New (t ).Attach (inv )
@@ -159,9 +122,17 @@ func TestSSH(t *testing.T) {
159
122
t .Skip ("Windows doesn't seem to clean up the process, maybe #7100 will fix it" )
160
123
}
161
124
162
- client , workspace , agentToken := setupWorkspaceForAgent (t , nil )
125
+ store , ps := dbtestutil .NewDB (t )
126
+ client := coderdtest .New (t , & coderdtest.Options {Pubsub : ps , Database : store })
127
+ client .SetLogger (slogtest .Make (t , nil ).Named ("client" ).Leveled (slog .LevelDebug ))
128
+ first := coderdtest .CreateFirstUser (t , client )
129
+ userClient , user := coderdtest .CreateAnotherUser (t , client , first .OrganizationID )
130
+ workspace , agentToken := dbfake .WorkspaceWithAgent (t , store , database.Workspace {
131
+ OrganizationID : first .OrganizationID ,
132
+ OwnerID : user .ID ,
133
+ })
163
134
inv , root := clitest .New (t , "ssh" , workspace .Name )
164
- clitest .SetupConfig (t , client , root )
135
+ clitest .SetupConfig (t , userClient , root )
165
136
pty := ptytest .New (t ).Attach (inv )
166
137
167
138
ctx , cancel := context .WithTimeout (context .Background (), testutil .WaitLong )
@@ -180,7 +151,13 @@ func TestSSH(t *testing.T) {
180
151
pty .WriteLine ("echo hell'o'" )
181
152
pty .ExpectMatchContext (ctx , "hello" )
182
153
183
- workspace = coderdtest .MustTransitionWorkspace (t , client , workspace .ID , database .WorkspaceTransitionStart , database .WorkspaceTransitionStop )
154
+ _ = dbfake .WorkspaceBuildBuilder (t , store , workspace ).
155
+ Seed (database.WorkspaceBuild {
156
+ Transition : database .WorkspaceTransitionStop ,
157
+ BuildNumber : 2 ,
158
+ }).
159
+ Pubsub (ps ).Do ()
160
+ t .Log ("stopped workspace" )
184
161
185
162
select {
186
163
case <- cmdDone :
@@ -191,7 +168,7 @@ func TestSSH(t *testing.T) {
191
168
192
169
t .Run ("Stdio" , func (t * testing.T ) {
193
170
t .Parallel ()
194
- client , workspace , agentToken := setupWorkspaceForAgent (t , nil )
171
+ client , workspace , agentToken := setupWorkspaceForAgent (t )
195
172
_ , _ = tGoContext (t , func (ctx context.Context ) {
196
173
// Run this async so the SSH command has to wait for
197
174
// the build and agent to connect!
@@ -251,7 +228,7 @@ func TestSSH(t *testing.T) {
251
228
252
229
t .Run ("Stdio_RemoteForward_Signal" , func (t * testing.T ) {
253
230
t .Parallel ()
254
- client , workspace , agentToken := setupWorkspaceForAgent (t , nil )
231
+ client , workspace , agentToken := setupWorkspaceForAgent (t )
255
232
_ , _ = tGoContext (t , func (ctx context.Context ) {
256
233
// Run this async so the SSH command has to wait for
257
234
// the build and agent to connect!
@@ -312,7 +289,7 @@ func TestSSH(t *testing.T) {
312
289
313
290
t .Run ("Stdio_BrokenConn" , func (t * testing.T ) {
314
291
t .Parallel ()
315
- client , workspace , agentToken := setupWorkspaceForAgent (t , nil )
292
+ client , workspace , agentToken := setupWorkspaceForAgent (t )
316
293
_ , _ = tGoContext (t , func (ctx context.Context ) {
317
294
// Run this async so the SSH command has to wait for
318
295
// the build and agent to connect!
@@ -373,7 +350,7 @@ func TestSSH(t *testing.T) {
373
350
}
374
351
t .Parallel ()
375
352
ctx := testutil .Context (t , testutil .WaitSuperLong )
376
- client , workspace , agentToken := setupWorkspaceForAgent (t , nil )
353
+ client , workspace , agentToken := setupWorkspaceForAgent (t )
377
354
_ , _ = tGoContext (t , func (ctx context.Context ) {
378
355
// Run this async so the SSH command has to wait for
379
356
// the build and agent to connect!
@@ -483,7 +460,17 @@ func TestSSH(t *testing.T) {
483
460
if runtime .GOOS == "windows" {
484
461
t .Skip ("Windows doesn't seem to clean up the process, maybe #7100 will fix it" )
485
462
}
486
- client , workspace , agentToken := setupWorkspaceForAgent (t , nil )
463
+
464
+ store , ps := dbtestutil .NewDB (t )
465
+ client := coderdtest .New (t , & coderdtest.Options {Pubsub : ps , Database : store })
466
+ client .SetLogger (slogtest .Make (t , nil ).Named ("client" ).Leveled (slog .LevelDebug ))
467
+ first := coderdtest .CreateFirstUser (t , client )
468
+ userClient , user := coderdtest .CreateAnotherUser (t , client , first .OrganizationID )
469
+ workspace , agentToken := dbfake .WorkspaceWithAgent (t , store , database.Workspace {
470
+ OrganizationID : first .OrganizationID ,
471
+ OwnerID : user .ID ,
472
+ })
473
+
487
474
_ , _ = tGoContext (t , func (ctx context.Context ) {
488
475
// Run this async so the SSH command has to wait for
489
476
// the build and agent to connect.
@@ -503,7 +490,7 @@ func TestSSH(t *testing.T) {
503
490
defer cancel ()
504
491
505
492
inv , root := clitest .New (t , "ssh" , "--stdio" , workspace .Name )
506
- clitest .SetupConfig (t , client , root )
493
+ clitest .SetupConfig (t , userClient , root )
507
494
inv .Stdin = clientOutput
508
495
inv .Stdout = serverInput
509
496
inv .Stderr = io .Discard
@@ -533,7 +520,14 @@ func TestSSH(t *testing.T) {
533
520
err = session .Shell ()
534
521
require .NoError (t , err )
535
522
536
- workspace = coderdtest .MustTransitionWorkspace (t , client , workspace .ID , database .WorkspaceTransitionStart , database .WorkspaceTransitionStop )
523
+ _ = dbfake .WorkspaceBuildBuilder (t , store , workspace ).
524
+ Seed (database.WorkspaceBuild {
525
+ Transition : database .WorkspaceTransitionStop ,
526
+ BuildNumber : 2 ,
527
+ }).
528
+ Pubsub (ps ).
529
+ Do ()
530
+ t .Log ("stopped workspace" )
537
531
538
532
select {
539
533
case <- cmdDone :
@@ -549,7 +543,7 @@ func TestSSH(t *testing.T) {
549
543
550
544
t .Parallel ()
551
545
552
- client , workspace , agentToken := setupWorkspaceForAgent (t , nil )
546
+ client , workspace , agentToken := setupWorkspaceForAgent (t )
553
547
554
548
_ = agenttest .New (t , client .URL , agentToken )
555
549
coderdtest .AwaitWorkspaceAgents (t , client , workspace .ID )
@@ -635,7 +629,7 @@ func TestSSH(t *testing.T) {
635
629
}))
636
630
defer httpServer .Close ()
637
631
638
- client , workspace , agentToken := setupWorkspaceForAgent (t , nil )
632
+ client , workspace , agentToken := setupWorkspaceForAgent (t )
639
633
_ = agenttest .New (t , client .URL , agentToken )
640
634
coderdtest .AwaitWorkspaceAgents (t , client , workspace .ID )
641
635
@@ -686,7 +680,7 @@ func TestSSH(t *testing.T) {
686
680
687
681
t .Parallel ()
688
682
689
- client , workspace , agentToken := setupWorkspaceForAgent (t , nil )
683
+ client , workspace , agentToken := setupWorkspaceForAgent (t )
690
684
691
685
_ = agenttest .New (t , client .URL , agentToken )
692
686
coderdtest .AwaitWorkspaceAgents (t , client , workspace .ID )
@@ -733,7 +727,7 @@ func TestSSH(t *testing.T) {
733
727
734
728
logDir := t .TempDir ()
735
729
736
- client , workspace , agentToken := setupWorkspaceForAgent (t , nil )
730
+ client , workspace , agentToken := setupWorkspaceForAgent (t )
737
731
inv , root := clitest .New (t , "ssh" , "-l" , logDir , workspace .Name )
738
732
clitest .SetupConfig (t , client , root )
739
733
pty := ptytest .New (t ).Attach (inv )
@@ -908,7 +902,7 @@ Expire-Date: 0
908
902
workspaceAgentSocketPath := strings .TrimSpace (stdout .String ())
909
903
require .NotEqual (t , extraSocketPath , workspaceAgentSocketPath , "socket path should be different" )
910
904
911
- client , workspace , agentToken := setupWorkspaceForAgent (t , nil )
905
+ client , workspace , agentToken := setupWorkspaceForAgent (t )
912
906
913
907
_ = agenttest .New (t , client .URL , agentToken , func (o * agent.Options ) {
914
908
o .EnvironmentVariables = map [string ]string {
0 commit comments