@@ -36,13 +36,15 @@ import (
36
36
"github.com/coder/coder/v2/agent"
37
37
"github.com/coder/coder/v2/agent/agentssh"
38
38
"github.com/coder/coder/v2/agent/agenttest"
39
+ agentproto"github.com/coder/coder/v2/agent/proto"
39
40
"github.com/coder/coder/v2/cli/clitest"
40
41
"github.com/coder/coder/v2/cli/cliui"
41
42
"github.com/coder/coder/v2/coderd/coderdtest"
42
43
"github.com/coder/coder/v2/coderd/database"
43
44
"github.com/coder/coder/v2/coderd/database/dbfake"
44
45
"github.com/coder/coder/v2/coderd/database/dbtestutil"
45
46
"github.com/coder/coder/v2/coderd/rbac"
47
+ "github.com/coder/coder/v2/coderd/workspacestats/workspacestatstest"
46
48
"github.com/coder/coder/v2/codersdk"
47
49
"github.com/coder/coder/v2/provisioner/echo"
48
50
"github.com/coder/coder/v2/provisionersdk/proto"
@@ -1292,6 +1294,115 @@ func TestSSH(t *testing.T) {
1292
1294
require .NoError (t ,err )
1293
1295
require .Len (t ,ents ,1 ,"expected one file in logdir %s" ,logDir )
1294
1296
})
1297
+ t .Run ("UpdateUsage" ,func (t * testing.T ) {
1298
+ t .Parallel ()
1299
+
1300
+ type testCase struct {
1301
+ name string
1302
+ experiment bool
1303
+ usageAppName string
1304
+ expectedCalls int
1305
+ expectedCountSSH int
1306
+ expectedCountJetbrains int
1307
+ expectedCountVscode int
1308
+ }
1309
+ tcs := []testCase {
1310
+ {
1311
+ name :"NoExperiment" ,
1312
+ },
1313
+ {
1314
+ name :"Empty" ,
1315
+ experiment :true ,
1316
+ expectedCalls :1 ,
1317
+ expectedCountSSH :1 ,
1318
+ },
1319
+ {
1320
+ name :"SSH" ,
1321
+ experiment :true ,
1322
+ usageAppName :"ssh" ,
1323
+ expectedCalls :1 ,
1324
+ expectedCountSSH :1 ,
1325
+ },
1326
+ {
1327
+ name :"Jetbrains" ,
1328
+ experiment :true ,
1329
+ usageAppName :"jetbrains" ,
1330
+ expectedCalls :1 ,
1331
+ expectedCountJetbrains :1 ,
1332
+ },
1333
+ {
1334
+ name :"Vscode" ,
1335
+ experiment :true ,
1336
+ usageAppName :"vscode" ,
1337
+ expectedCalls :1 ,
1338
+ expectedCountVscode :1 ,
1339
+ },
1340
+ {
1341
+ name :"InvalidDefaultsToSSH" ,
1342
+ experiment :true ,
1343
+ usageAppName :"invalid" ,
1344
+ expectedCalls :1 ,
1345
+ expectedCountSSH :1 ,
1346
+ },
1347
+ {
1348
+ name :"Disable" ,
1349
+ experiment :true ,
1350
+ usageAppName :"disable" ,
1351
+ },
1352
+ }
1353
+
1354
+ for _ ,tc := range tcs {
1355
+ tc := tc
1356
+ t .Run (tc .name ,func (t * testing.T ) {
1357
+ t .Parallel ()
1358
+
1359
+ dv := coderdtest .DeploymentValues (t )
1360
+ if tc .experiment {
1361
+ dv .Experiments = []string {string (codersdk .ExperimentWorkspaceUsage )}
1362
+ }
1363
+ batcher := & workspacestatstest.StatsBatcher {
1364
+ LastStats :& agentproto.Stats {},
1365
+ }
1366
+ admin ,store := coderdtest .NewWithDatabase (t ,& coderdtest.Options {
1367
+ DeploymentValues :dv ,
1368
+ StatsBatcher :batcher ,
1369
+ })
1370
+ admin .SetLogger (slogtest .Make (t ,nil ).Named ("client" ).Leveled (slog .LevelDebug ))
1371
+ first := coderdtest .CreateFirstUser (t ,admin )
1372
+ client ,user := coderdtest .CreateAnotherUser (t ,admin ,first .OrganizationID )
1373
+ r := dbfake .WorkspaceBuild (t ,store , database.Workspace {
1374
+ OrganizationID :first .OrganizationID ,
1375
+ OwnerID :user .ID ,
1376
+ }).WithAgent ().Do ()
1377
+ workspace := r .Workspace
1378
+ agentToken := r .AgentToken
1379
+ inv ,root := clitest .New (t ,"ssh" ,workspace .Name ,fmt .Sprintf ("--usage-app=%s" ,tc .usageAppName ))
1380
+ clitest .SetupConfig (t ,client ,root )
1381
+ pty := ptytest .New (t ).Attach (inv )
1382
+
1383
+ ctx ,cancel := context .WithTimeout (context .Background (),testutil .WaitLong )
1384
+ defer cancel ()
1385
+
1386
+ cmdDone := tGo (t ,func () {
1387
+ err := inv .WithContext (ctx ).Run ()
1388
+ assert .NoError (t ,err )
1389
+ })
1390
+ pty .ExpectMatch ("Waiting" )
1391
+
1392
+ _ = agenttest .New (t ,client .URL ,agentToken )
1393
+ coderdtest .AwaitWorkspaceAgents (t ,client ,workspace .ID )
1394
+
1395
+ // Shells on Mac, Windows, and Linux all exit shells with the "exit" command.
1396
+ pty .WriteLine ("exit" )
1397
+ <- cmdDone
1398
+
1399
+ require .EqualValues (t ,tc .expectedCalls ,batcher .Called )
1400
+ require .EqualValues (t ,tc .expectedCountSSH ,batcher .LastStats .SessionCountSsh )
1401
+ require .EqualValues (t ,tc .expectedCountJetbrains ,batcher .LastStats .SessionCountJetbrains )
1402
+ require .EqualValues (t ,tc .expectedCountVscode ,batcher .LastStats .SessionCountVscode )
1403
+ })
1404
+ }
1405
+ })
1295
1406
}
1296
1407
1297
1408
//nolint:paralleltest // This test uses t.Setenv, parent test MUST NOT be parallel.