@@ -22,8 +22,9 @@ import (
22
22
"github.com/coder/coder/v2/agent/agenttest"
23
23
"github.com/coder/coder/v2/cli/clitest"
24
24
"github.com/coder/coder/v2/coderd/coderdtest"
25
+ "github.com/coder/coder/v2/coderd/database"
26
+ "github.com/coder/coder/v2/coderd/database/dbfake"
25
27
"github.com/coder/coder/v2/codersdk"
26
- "github.com/coder/coder/v2/provisioner/echo"
27
28
"github.com/coder/coder/v2/provisionersdk/proto"
28
29
"github.com/coder/coder/v2/pty/ptytest"
29
30
"github.com/coder/coder/v2/testutil"
@@ -64,8 +65,7 @@ func TestConfigSSH(t *testing.T) {
64
65
const hostname = "test-coder."
65
66
const expectedKey = "ConnectionAttempts"
66
67
const removeKey = "ConnectionTimeout"
67
- client := coderdtest .New (t , & coderdtest.Options {
68
- IncludeProvisionerDaemon : true ,
68
+ client , db := coderdtest .NewWithDatabase (t , & coderdtest.Options {
69
69
ConfigSSH : codersdk.SSHConfigResponse {
70
70
HostnamePrefix : hostname ,
71
71
SSHConfigOptions : map [string ]string {
@@ -76,32 +76,13 @@ func TestConfigSSH(t *testing.T) {
76
76
},
77
77
})
78
78
owner := coderdtest .CreateFirstUser (t , client )
79
- member , _ := coderdtest .CreateAnotherUser (t , client , owner .OrganizationID )
80
- authToken := uuid .NewString ()
81
- version := coderdtest .CreateTemplateVersion (t , client , owner .OrganizationID , & echo.Responses {
82
- Parse : echo .ParseComplete ,
83
- ProvisionPlan : []* proto.Response {{
84
- Type : & proto.Response_Plan {
85
- Plan : & proto.PlanComplete {
86
- Resources : []* proto.Resource {{
87
- Name : "example" ,
88
- Type : "aws_instance" ,
89
- Agents : []* proto.Agent {{
90
- Id : uuid .NewString (),
91
- Name : "example" ,
92
- }},
93
- }},
94
- },
95
- },
96
- }},
97
- ProvisionApply : echo .ProvisionApplyWithAgent (authToken ),
79
+ member , memberUser := coderdtest .CreateAnotherUser (t , client , owner .OrganizationID )
80
+ ws , authToken := dbfake .WorkspaceWithAgent (t , db , database.Workspace {
81
+ OrganizationID : owner .OrganizationID ,
82
+ OwnerID : memberUser .ID ,
98
83
})
99
- coderdtest .AwaitTemplateVersionJobCompleted (t , client , version .ID )
100
- template := coderdtest .CreateTemplate (t , client , owner .OrganizationID , version .ID )
101
- workspace := coderdtest .CreateWorkspace (t , member , owner .OrganizationID , template .ID )
102
- coderdtest .AwaitWorkspaceBuildJobCompleted (t , client , workspace .LatestBuild .ID )
103
84
_ = agenttest .New (t , client .URL , authToken )
104
- resources := coderdtest .AwaitWorkspaceAgents (t , client , workspace .ID )
85
+ resources := coderdtest .AwaitWorkspaceAgents (t , client , ws .ID )
105
86
agentConn , err := client .DialWorkspaceAgent (context .Background (), resources [0 ].Agents [0 ].ID , nil )
106
87
require .NoError (t , err )
107
88
defer agentConn .Close ()
@@ -172,7 +153,7 @@ func TestConfigSSH(t *testing.T) {
172
153
173
154
home := filepath .Dir (filepath .Dir (sshConfigFile ))
174
155
// #nosec
175
- sshCmd := exec .Command ("ssh" , "-F" , sshConfigFile , hostname + workspace .Name , "echo" , "test" )
156
+ sshCmd := exec .Command ("ssh" , "-F" , sshConfigFile , hostname + ws .Name , "echo" , "test" )
176
157
pty = ptytest .New (t )
177
158
// Set HOME because coder config is included from ~/.ssh/coder.
178
159
sshCmd .Env = append (sshCmd .Env , fmt .Sprintf ("HOME=%s" , home ))
@@ -213,13 +194,13 @@ func TestConfigSSH_FileWriteAndOptionsFlow(t *testing.T) {
213
194
match , write string
214
195
}
215
196
tests := []struct {
216
- name string
217
- args []string
218
- matches []match
219
- writeConfig writeConfig
220
- wantConfig wantConfig
221
- wantErr bool
222
- echoResponse * echo. Responses
197
+ name string
198
+ args []string
199
+ matches []match
200
+ writeConfig writeConfig
201
+ wantConfig wantConfig
202
+ wantErr bool
203
+ hasAgent bool
223
204
}{
224
205
{
225
206
name : "Config file is created" ,
@@ -576,11 +557,8 @@ func TestConfigSSH_FileWriteAndOptionsFlow(t *testing.T) {
576
557
args : []string {
577
558
"-y" , "--coder-binary-path" , "/foo/bar/coder" ,
578
559
},
579
- wantErr : false ,
580
- echoResponse : & echo.Responses {
581
- Parse : echo .ParseComplete ,
582
- ProvisionApply : echo .ProvisionApplyWithAgent ("" ),
583
- },
560
+ wantErr : false ,
561
+ hasAgent : true ,
584
562
wantConfig : wantConfig {
585
563
regexMatch : "ProxyCommand /foo/bar/coder" ,
586
564
},
@@ -591,15 +569,14 @@ func TestConfigSSH_FileWriteAndOptionsFlow(t *testing.T) {
591
569
t .Run (tt .name , func (t * testing.T ) {
592
570
t .Parallel ()
593
571
594
- var (
595
- client = coderdtest .New (t , & coderdtest.Options {IncludeProvisionerDaemon : true })
596
- user = coderdtest .CreateFirstUser (t , client )
597
- version = coderdtest .CreateTemplateVersion (t , client , user .OrganizationID , tt .echoResponse )
598
- _ = coderdtest .AwaitTemplateVersionJobCompleted (t , client , version .ID )
599
- project = coderdtest .CreateTemplate (t , client , user .OrganizationID , version .ID )
600
- workspace = coderdtest .CreateWorkspace (t , client , user .OrganizationID , project .ID )
601
- _ = coderdtest .AwaitWorkspaceBuildJobCompleted (t , client , workspace .LatestBuild .ID )
602
- )
572
+ client , db := coderdtest .NewWithDatabase (t , nil )
573
+ user := coderdtest .CreateFirstUser (t , client )
574
+ if tt .hasAgent {
575
+ _ , _ = dbfake .WorkspaceWithAgent (t , db , database.Workspace {
576
+ OrganizationID : user .OrganizationID ,
577
+ OwnerID : user .UserID ,
578
+ })
579
+ }
603
580
604
581
// Prepare ssh config files.
605
582
sshConfigName := sshConfigFileName (t )
@@ -710,17 +687,15 @@ func TestConfigSSH_Hostnames(t *testing.T) {
710
687
resources = append (resources , resource )
711
688
}
712
689
713
- client := coderdtest .New (t , & coderdtest. Options { IncludeProvisionerDaemon : true } )
690
+ client , db := coderdtest .NewWithDatabase (t , nil )
714
691
owner := coderdtest .CreateFirstUser (t , client )
715
- member , _ := coderdtest .CreateAnotherUser (t , client , owner .OrganizationID )
716
- // authToken := uuid.NewString()
717
- version := coderdtest .CreateTemplateVersion (t , client , owner .OrganizationID ,
718
- echo .WithResources (resources ))
719
- coderdtest .AwaitTemplateVersionJobCompleted (t , client , version .ID )
720
- template := coderdtest .CreateTemplate (t , client , owner .OrganizationID , version .ID )
721
- workspace := coderdtest .CreateWorkspace (t , member , owner .OrganizationID , template .ID )
722
- coderdtest .AwaitWorkspaceBuildJobCompleted (t , client , workspace .LatestBuild .ID )
692
+ member , memberUser := coderdtest .CreateAnotherUser (t , client , owner .OrganizationID )
723
693
694
+ ws := dbfake .Workspace (t , db , database.Workspace {
695
+ OrganizationID : owner .OrganizationID ,
696
+ OwnerID : memberUser .ID ,
697
+ })
698
+ dbfake .WorkspaceBuild (t , db , ws , database.WorkspaceBuild {}, resources ... )
724
699
sshConfigFile := sshConfigFileName (t )
725
700
726
701
inv , root := clitest .New (t , "config-ssh" , "--ssh-config-file" , sshConfigFile )
@@ -745,7 +720,7 @@ func TestConfigSSH_Hostnames(t *testing.T) {
745
720
746
721
var expectedHosts []string
747
722
for _ , hostnamePattern := range tt .expected {
748
- hostname := strings .ReplaceAll (hostnamePattern , "@" , workspace .Name )
723
+ hostname := strings .ReplaceAll (hostnamePattern , "@" , ws .Name )
749
724
expectedHosts = append (expectedHosts , hostname )
750
725
}
751
726
0 commit comments