@@ -16,7 +16,6 @@ import (
16
16
"github.com/coder/coder/coderd/coderdtest"
17
17
"github.com/coder/coder/provisioner/echo"
18
18
"github.com/coder/coder/provisionersdk/proto"
19
- "github.com/coder/coder/testutil"
20
19
)
21
20
22
21
func TestWorkspaceAgent (t * testing.T ) {
@@ -56,24 +55,16 @@ func TestWorkspaceAgent(t *testing.T) {
56
55
coderdtest .AwaitWorkspaceBuildJob (t , client , workspace .LatestBuild .ID )
57
56
58
57
logDir := t .TempDir ()
59
- cmd , _ := clitest .New (t ,
58
+ inv , _ := clitest .New (t ,
60
59
"agent" ,
61
60
"--auth" , "token" ,
62
61
"--agent-token" , authToken ,
63
62
"--agent-url" , client .URL .String (),
64
63
"--log-dir" , logDir ,
65
64
)
66
- ctx , cancel := context .WithTimeout (context .Background (), testutil .WaitMedium )
67
- defer cancel ()
68
- errC := make (chan error , 1 )
69
- go func () {
70
- errC <- cmd .ExecuteContext (ctx )
71
- }()
72
- coderdtest .AwaitWorkspaceAgents (t , client , workspace .ID )
73
65
74
- cancel ()
75
- err := <- errC
76
- require .NoError (t , err )
66
+ clitest .Start (t , inv )
67
+ coderdtest .AwaitWorkspaceAgents (t , client , workspace .ID )
77
68
78
69
info , err := os .Stat (filepath .Join (logDir , "coder-agent.log" ))
79
70
require .NoError (t , err )
@@ -83,7 +74,7 @@ func TestWorkspaceAgent(t *testing.T) {
83
74
t .Run ("Azure" , func (t * testing.T ) {
84
75
t .Parallel ()
85
76
instanceID := "instanceidentifier"
86
- certificates , metadataClient := coderdtest .NewAzureInstanceIdentity (t , instanceID )
77
+ certificates , _ := coderdtest .NewAzureInstanceIdentity (t , instanceID )
87
78
client := coderdtest .New (t , & coderdtest.Options {
88
79
AzureCertificates : certificates ,
89
80
IncludeProvisionerDaemon : true ,
@@ -112,16 +103,10 @@ func TestWorkspaceAgent(t *testing.T) {
112
103
workspace := coderdtest .CreateWorkspace (t , client , user .OrganizationID , template .ID )
113
104
coderdtest .AwaitWorkspaceBuildJob (t , client , workspace .LatestBuild .ID )
114
105
115
- cmd , _ := clitest .New (t , "agent" , "--auth" , "azure-instance-identity" , "--agent-url" , client .URL .String ())
106
+ inv , _ := clitest .New (t , "agent" , "--auth" , "azure-instance-identity" , "--agent-url" , client .URL .String ())
116
107
ctx , cancelFunc := context .WithCancel (context .Background ())
117
108
defer cancelFunc ()
118
- errC := make (chan error )
119
- go func () {
120
- // A linting error occurs for weakly typing the context value here.
121
- //nolint // The above seems reasonable for a one-off test.
122
- ctx := context .WithValue (ctx , "azure-client" , metadataClient )
123
- errC <- cmd .ExecuteContext (ctx )
124
- }()
109
+ clitest .Start (t , inv )
125
110
coderdtest .AwaitWorkspaceAgents (t , client , workspace .ID )
126
111
workspace , err := client .Workspace (ctx , workspace .ID )
127
112
require .NoError (t , err )
@@ -133,15 +118,12 @@ func TestWorkspaceAgent(t *testing.T) {
133
118
require .NoError (t , err )
134
119
defer dialer .Close ()
135
120
require .True (t , dialer .AwaitReachable (context .Background ()))
136
- cancelFunc ()
137
- err = <- errC
138
- require .NoError (t , err )
139
121
})
140
122
141
123
t .Run ("AWS" , func (t * testing.T ) {
142
124
t .Parallel ()
143
125
instanceID := "instanceidentifier"
144
- certificates , metadataClient := coderdtest .NewAWSInstanceIdentity (t , instanceID )
126
+ certificates , _ := coderdtest .NewAWSInstanceIdentity (t , instanceID )
145
127
client := coderdtest .New (t , & coderdtest.Options {
146
128
AWSCertificates : certificates ,
147
129
IncludeProvisionerDaemon : true ,
@@ -170,36 +152,25 @@ func TestWorkspaceAgent(t *testing.T) {
170
152
workspace := coderdtest .CreateWorkspace (t , client , user .OrganizationID , template .ID )
171
153
coderdtest .AwaitWorkspaceBuildJob (t , client , workspace .LatestBuild .ID )
172
154
173
- cmd , _ := clitest .New (t , "agent" , "--auth" , "aws-instance-identity" , "--agent-url" , client .URL .String ())
174
- ctx , cancelFunc := context .WithCancel (context .Background ())
175
- defer cancelFunc ()
176
- errC := make (chan error )
177
- go func () {
178
- // A linting error occurs for weakly typing the context value here.
179
- //nolint // The above seems reasonable for a one-off test.
180
- ctx := context .WithValue (ctx , "aws-client" , metadataClient )
181
- errC <- cmd .ExecuteContext (ctx )
182
- }()
155
+ inv , _ := clitest .New (t , "agent" , "--auth" , "aws-instance-identity" , "--agent-url" , client .URL .String ())
156
+ clitest .Start (t , inv )
183
157
coderdtest .AwaitWorkspaceAgents (t , client , workspace .ID )
184
- workspace , err := client .Workspace (ctx , workspace .ID )
158
+ workspace , err := client .Workspace (inv . Context () , workspace .ID )
185
159
require .NoError (t , err )
186
160
resources := workspace .LatestBuild .Resources
187
161
if assert .NotEmpty (t , resources ) && assert .NotEmpty (t , resources [0 ].Agents ) {
188
162
assert .NotEmpty (t , resources [0 ].Agents [0 ].Version )
189
163
}
190
- dialer , err := client .DialWorkspaceAgent (ctx , resources [0 ].Agents [0 ].ID , nil )
164
+ dialer , err := client .DialWorkspaceAgent (inv . Context () , resources [0 ].Agents [0 ].ID , nil )
191
165
require .NoError (t , err )
192
166
defer dialer .Close ()
193
167
require .True (t , dialer .AwaitReachable (context .Background ()))
194
- cancelFunc ()
195
- err = <- errC
196
- require .NoError (t , err )
197
168
})
198
169
199
170
t .Run ("GoogleCloud" , func (t * testing.T ) {
200
171
t .Parallel ()
201
172
instanceID := "instanceidentifier"
202
- validator , metadata := coderdtest .NewGoogleInstanceIdentity (t , instanceID , false )
173
+ validator , _ := coderdtest .NewGoogleInstanceIdentity (t , instanceID , false )
203
174
client := coderdtest .New (t , & coderdtest.Options {
204
175
GoogleTokenValidator : validator ,
205
176
IncludeProvisionerDaemon : true ,
@@ -228,16 +199,11 @@ func TestWorkspaceAgent(t *testing.T) {
228
199
workspace := coderdtest .CreateWorkspace (t , client , user .OrganizationID , template .ID )
229
200
coderdtest .AwaitWorkspaceBuildJob (t , client , workspace .LatestBuild .ID )
230
201
231
- cmd , _ := clitest .New (t , "agent" , "--auth" , "google-instance-identity" , "--agent-url" , client .URL .String ())
232
- ctx , cancelFunc := context .WithCancel (context .Background ())
233
- defer cancelFunc ()
234
- errC := make (chan error )
235
- go func () {
236
- // A linting error occurs for weakly typing the context value here.
237
- //nolint // The above seems reasonable for a one-off test.
238
- ctx := context .WithValue (ctx , "gcp-client" , metadata )
239
- errC <- cmd .ExecuteContext (ctx )
240
- }()
202
+ inv , _ := clitest .New (t , "agent" , "--auth" , "google-instance-identity" , "--agent-url" , client .URL .String ())
203
+ clitest .Start (t , inv )
204
+
205
+ ctx := inv .Context ()
206
+
241
207
coderdtest .AwaitWorkspaceAgents (t , client , workspace .ID )
242
208
workspace , err := client .Workspace (ctx , workspace .ID )
243
209
require .NoError (t , err )
@@ -264,9 +230,5 @@ func TestWorkspaceAgent(t *testing.T) {
264
230
require .NoError (t , err )
265
231
_ , err = uuid .Parse (strings .TrimSpace (string (token )))
266
232
require .NoError (t , err )
267
-
268
- cancelFunc ()
269
- err = <- errC
270
- require .NoError (t , err )
271
233
})
272
234
}
0 commit comments