@@ -164,6 +164,148 @@ func TestWorkspace(t *testing.T) {
164
164
assert .Equal (t , templateDisplayName , ws .TemplateDisplayName )
165
165
assert .Equal (t , templateAllowUserCancelWorkspaceJobs , ws .TemplateAllowUserCancelWorkspaceJobs )
166
166
})
167
+
168
+ t .Run ("Health" , func (t * testing.T ) {
169
+ t .Parallel ()
170
+
171
+ t .Run ("Healthy" , func (t * testing.T ) {
172
+ t .Parallel ()
173
+ client := coderdtest .New (t , & coderdtest.Options {IncludeProvisionerDaemon : true })
174
+ user := coderdtest .CreateFirstUser (t , client )
175
+ version := coderdtest .CreateTemplateVersion (t , client , user .OrganizationID , & echo.Responses {
176
+ Parse : echo .ParseComplete ,
177
+ ProvisionApply : []* proto.Provision_Response {{
178
+ Type : & proto.Provision_Response_Complete {
179
+ Complete : & proto.Provision_Complete {
180
+ Resources : []* proto.Resource {{
181
+ Name : "some" ,
182
+ Type : "example" ,
183
+ Agents : []* proto.Agent {{
184
+ Id : uuid .NewString (),
185
+ Auth : & proto.Agent_Token {},
186
+ }},
187
+ }},
188
+ },
189
+ },
190
+ }},
191
+ })
192
+ coderdtest .AwaitTemplateVersionJob (t , client , version .ID )
193
+ template := coderdtest .CreateTemplate (t , client , user .OrganizationID , version .ID )
194
+ workspace := coderdtest .CreateWorkspace (t , client , user .OrganizationID , template .ID )
195
+ coderdtest .AwaitWorkspaceBuildJob (t , client , workspace .LatestBuild .ID )
196
+
197
+ ctx , cancel := context .WithTimeout (context .Background (), testutil .WaitLong )
198
+ defer cancel ()
199
+
200
+ workspace , err := client .Workspace (ctx , workspace .ID )
201
+ require .NoError (t , err )
202
+
203
+ agent := workspace .LatestBuild .Resources [0 ].Agents [0 ]
204
+
205
+ assert .True (t , workspace .Health .Healthy )
206
+ assert .Equal (t , []uuid.UUID {}, workspace .Health .FailingAgents )
207
+ assert .True (t , agent .Health .Healthy )
208
+ assert .Empty (t , agent .Health .Reason )
209
+ })
210
+
211
+ t .Run ("Unhealthy" , func (t * testing.T ) {
212
+ t .Parallel ()
213
+ client := coderdtest .New (t , & coderdtest.Options {IncludeProvisionerDaemon : true })
214
+ user := coderdtest .CreateFirstUser (t , client )
215
+ version := coderdtest .CreateTemplateVersion (t , client , user .OrganizationID , & echo.Responses {
216
+ Parse : echo .ParseComplete ,
217
+ ProvisionApply : []* proto.Provision_Response {{
218
+ Type : & proto.Provision_Response_Complete {
219
+ Complete : & proto.Provision_Complete {
220
+ Resources : []* proto.Resource {{
221
+ Name : "some" ,
222
+ Type : "example" ,
223
+ Agents : []* proto.Agent {{
224
+ Id : uuid .NewString (),
225
+ Auth : & proto.Agent_Token {},
226
+ ConnectionTimeoutSeconds : 1 ,
227
+ }},
228
+ }},
229
+ },
230
+ },
231
+ }},
232
+ })
233
+ coderdtest .AwaitTemplateVersionJob (t , client , version .ID )
234
+ template := coderdtest .CreateTemplate (t , client , user .OrganizationID , version .ID )
235
+ workspace := coderdtest .CreateWorkspace (t , client , user .OrganizationID , template .ID )
236
+ coderdtest .AwaitWorkspaceBuildJob (t , client , workspace .LatestBuild .ID )
237
+
238
+ ctx , cancel := context .WithTimeout (context .Background (), testutil .WaitLong )
239
+ defer cancel ()
240
+
241
+ var err error
242
+ testutil .Eventually (ctx , t , func (ctx context.Context ) bool {
243
+ workspace , err = client .Workspace (ctx , workspace .ID )
244
+ return assert .NoError (t , err ) && ! workspace .Health .Healthy
245
+ }, testutil .IntervalMedium )
246
+
247
+ agent := workspace .LatestBuild .Resources [0 ].Agents [0 ]
248
+
249
+ assert .False (t , workspace .Health .Healthy )
250
+ assert .Equal (t , []uuid.UUID {agent .ID }, workspace .Health .FailingAgents )
251
+ assert .False (t , agent .Health .Healthy )
252
+ assert .NotEmpty (t , agent .Health .Reason )
253
+ })
254
+
255
+ t .Run ("Mixed health" , func (t * testing.T ) {
256
+ t .Parallel ()
257
+ client := coderdtest .New (t , & coderdtest.Options {IncludeProvisionerDaemon : true })
258
+ user := coderdtest .CreateFirstUser (t , client )
259
+ version := coderdtest .CreateTemplateVersion (t , client , user .OrganizationID , & echo.Responses {
260
+ Parse : echo .ParseComplete ,
261
+ ProvisionApply : []* proto.Provision_Response {{
262
+ Type : & proto.Provision_Response_Complete {
263
+ Complete : & proto.Provision_Complete {
264
+ Resources : []* proto.Resource {{
265
+ Name : "some" ,
266
+ Type : "example" ,
267
+ Agents : []* proto.Agent {{
268
+ Id : uuid .NewString (),
269
+ Name : "a1" ,
270
+ Auth : & proto.Agent_Token {},
271
+ }, {
272
+ Id : uuid .NewString (),
273
+ Name : "a2" ,
274
+ Auth : & proto.Agent_Token {},
275
+ ConnectionTimeoutSeconds : 1 ,
276
+ }},
277
+ }},
278
+ },
279
+ },
280
+ }},
281
+ })
282
+ coderdtest .AwaitTemplateVersionJob (t , client , version .ID )
283
+ template := coderdtest .CreateTemplate (t , client , user .OrganizationID , version .ID )
284
+ workspace := coderdtest .CreateWorkspace (t , client , user .OrganizationID , template .ID )
285
+ coderdtest .AwaitWorkspaceBuildJob (t , client , workspace .LatestBuild .ID )
286
+
287
+ ctx , cancel := context .WithTimeout (context .Background (), testutil .WaitLong )
288
+ defer cancel ()
289
+
290
+ var err error
291
+ testutil .Eventually (ctx , t , func (ctx context.Context ) bool {
292
+ workspace , err = client .Workspace (ctx , workspace .ID )
293
+ return assert .NoError (t , err ) && ! workspace .Health .Healthy
294
+ }, testutil .IntervalMedium )
295
+
296
+ assert .False (t , workspace .Health .Healthy )
297
+ assert .Len (t , workspace .Health .FailingAgents , 1 )
298
+
299
+ agent1 := workspace .LatestBuild .Resources [0 ].Agents [0 ]
300
+ agent2 := workspace .LatestBuild .Resources [0 ].Agents [1 ]
301
+
302
+ assert .Equal (t , []uuid.UUID {agent2 .ID }, workspace .Health .FailingAgents )
303
+ assert .True (t , agent1 .Health .Healthy )
304
+ assert .Empty (t , agent1 .Health .Reason )
305
+ assert .False (t , agent2 .Health .Healthy )
306
+ assert .NotEmpty (t , agent2 .Health .Reason )
307
+ })
308
+ })
167
309
}
168
310
169
311
func TestAdminViewAllWorkspaces (t * testing.T ) {
0 commit comments