@@ -43,6 +43,7 @@ import (
43
43
"cdr.dev/slog/sloggers/slogtest"
44
44
"github.com/coder/coder/agent"
45
45
"github.com/coder/coder/agent/agentssh"
46
+ "github.com/coder/coder/agent/agenttest"
46
47
"github.com/coder/coder/coderd/httpapi"
47
48
"github.com/coder/coder/codersdk"
48
49
"github.com/coder/coder/codersdk/agentsdk"
@@ -969,14 +970,14 @@ func TestAgent_Metadata(t *testing.T) {
969
970
970
971
var gotMd map [string ]agentsdk.PostMetadataRequest
971
972
require .Eventually (t , func () bool {
972
- gotMd = client .getMetadata ()
973
+ gotMd = client .GetMetadata ()
973
974
return len (gotMd ) == 1
974
975
}, testutil .WaitShort , testutil .IntervalMedium )
975
976
976
977
collectedAt := gotMd ["greeting" ].CollectedAt
977
978
978
979
require .Never (t , func () bool {
979
- gotMd = client .getMetadata ()
980
+ gotMd = client .GetMetadata ()
980
981
if len (gotMd ) != 1 {
981
982
panic ("unexpected number of metadata" )
982
983
}
@@ -1000,7 +1001,7 @@ func TestAgent_Metadata(t *testing.T) {
1000
1001
1001
1002
var gotMd map [string ]agentsdk.PostMetadataRequest
1002
1003
require .Eventually (t , func () bool {
1003
- gotMd = client .getMetadata ()
1004
+ gotMd = client .GetMetadata ()
1004
1005
return len (gotMd ) == 1
1005
1006
}, testutil .WaitShort , testutil .IntervalMedium )
1006
1007
@@ -1010,7 +1011,7 @@ func TestAgent_Metadata(t *testing.T) {
1010
1011
}
1011
1012
1012
1013
if ! assert .Eventually (t , func () bool {
1013
- gotMd = client .getMetadata ()
1014
+ gotMd = client .GetMetadata ()
1014
1015
return gotMd ["greeting" ].CollectedAt .After (collectedAt1 )
1015
1016
}, testutil .WaitShort , testutil .IntervalMedium ) {
1016
1017
t .Fatalf ("expected metadata to be collected again" )
@@ -1052,11 +1053,11 @@ func TestAgentMetadata_Timing(t *testing.T) {
1052
1053
}, 0 )
1053
1054
1054
1055
require .Eventually (t , func () bool {
1055
- return len (client .getMetadata ()) == 2
1056
+ return len (client .GetMetadata ()) == 2
1056
1057
}, testutil .WaitShort , testutil .IntervalMedium )
1057
1058
1058
1059
for start := time .Now (); time .Since (start ) < testutil .WaitMedium ; time .Sleep (testutil .IntervalMedium ) {
1059
- md := client .getMetadata ()
1060
+ md := client .GetMetadata ()
1060
1061
require .Len (t , md , 2 , "got: %+v" , md )
1061
1062
1062
1063
require .Equal (t , "hello\n " , md ["greeting" ].Value )
@@ -1110,7 +1111,7 @@ func TestAgent_Lifecycle(t *testing.T) {
1110
1111
1111
1112
var got []codersdk.WorkspaceAgentLifecycle
1112
1113
assert .Eventually (t , func () bool {
1113
- got = client .getLifecycleStates ()
1114
+ got = client .GetLifecycleStates ()
1114
1115
return len (got ) > 0 && got [len (got )- 1 ] == want [len (want )- 1 ]
1115
1116
}, testutil .WaitShort , testutil .IntervalMedium )
1116
1117
@@ -1132,7 +1133,7 @@ func TestAgent_Lifecycle(t *testing.T) {
1132
1133
1133
1134
var got []codersdk.WorkspaceAgentLifecycle
1134
1135
assert .Eventually (t , func () bool {
1135
- got = client .getLifecycleStates ()
1136
+ got = client .GetLifecycleStates ()
1136
1137
return len (got ) > 0 && got [len (got )- 1 ] == want [len (want )- 1 ]
1137
1138
}, testutil .WaitShort , testutil .IntervalMedium )
1138
1139
@@ -1154,7 +1155,7 @@ func TestAgent_Lifecycle(t *testing.T) {
1154
1155
1155
1156
var got []codersdk.WorkspaceAgentLifecycle
1156
1157
assert .Eventually (t , func () bool {
1157
- got = client .getLifecycleStates ()
1158
+ got = client .GetLifecycleStates ()
1158
1159
return len (got ) > 0 && got [len (got )- 1 ] == want [len (want )- 1 ]
1159
1160
}, testutil .WaitShort , testutil .IntervalMedium )
1160
1161
@@ -1171,7 +1172,7 @@ func TestAgent_Lifecycle(t *testing.T) {
1171
1172
1172
1173
var ready []codersdk.WorkspaceAgentLifecycle
1173
1174
assert .Eventually (t , func () bool {
1174
- ready = client .getLifecycleStates ()
1175
+ ready = client .GetLifecycleStates ()
1175
1176
return len (ready ) > 0 && ready [len (ready )- 1 ] == codersdk .WorkspaceAgentLifecycleReady
1176
1177
}, testutil .WaitShort , testutil .IntervalMedium )
1177
1178
@@ -1192,7 +1193,7 @@ func TestAgent_Lifecycle(t *testing.T) {
1192
1193
1193
1194
var got []codersdk.WorkspaceAgentLifecycle
1194
1195
assert .Eventually (t , func () bool {
1195
- got = client .getLifecycleStates ()[len (ready ):]
1196
+ got = client .GetLifecycleStates ()[len (ready ):]
1196
1197
return len (got ) > 0 && got [len (got )- 1 ] == want [len (want )- 1 ]
1197
1198
}, testutil .WaitShort , testutil .IntervalMedium )
1198
1199
@@ -1209,7 +1210,7 @@ func TestAgent_Lifecycle(t *testing.T) {
1209
1210
1210
1211
var ready []codersdk.WorkspaceAgentLifecycle
1211
1212
assert .Eventually (t , func () bool {
1212
- ready = client .getLifecycleStates ()
1213
+ ready = client .GetLifecycleStates ()
1213
1214
return len (ready ) > 0 && ready [len (ready )- 1 ] == codersdk .WorkspaceAgentLifecycleReady
1214
1215
}, testutil .WaitShort , testutil .IntervalMedium )
1215
1216
@@ -1231,7 +1232,7 @@ func TestAgent_Lifecycle(t *testing.T) {
1231
1232
1232
1233
var got []codersdk.WorkspaceAgentLifecycle
1233
1234
assert .Eventually (t , func () bool {
1234
- got = client .getLifecycleStates ()[len (ready ):]
1235
+ got = client .GetLifecycleStates ()[len (ready ):]
1235
1236
return len (got ) > 0 && got [len (got )- 1 ] == want [len (want )- 1 ]
1236
1237
}, testutil .WaitShort , testutil .IntervalMedium )
1237
1238
@@ -1248,7 +1249,7 @@ func TestAgent_Lifecycle(t *testing.T) {
1248
1249
1249
1250
var ready []codersdk.WorkspaceAgentLifecycle
1250
1251
assert .Eventually (t , func () bool {
1251
- ready = client .getLifecycleStates ()
1252
+ ready = client .GetLifecycleStates ()
1252
1253
return len (ready ) > 0 && ready [len (ready )- 1 ] == codersdk .WorkspaceAgentLifecycleReady
1253
1254
}, testutil .WaitShort , testutil .IntervalMedium )
1254
1255
@@ -1270,7 +1271,7 @@ func TestAgent_Lifecycle(t *testing.T) {
1270
1271
1271
1272
var got []codersdk.WorkspaceAgentLifecycle
1272
1273
assert .Eventually (t , func () bool {
1273
- got = client .getLifecycleStates ()[len (ready ):]
1274
+ got = client .GetLifecycleStates ()[len (ready ):]
1274
1275
return len (got ) > 0 && got [len (got )- 1 ] == want [len (want )- 1 ]
1275
1276
}, testutil .WaitShort , testutil .IntervalMedium )
1276
1277
@@ -1281,11 +1282,12 @@ func TestAgent_Lifecycle(t *testing.T) {
1281
1282
t .Parallel ()
1282
1283
logger := slogtest .Make (t , nil ).Leveled (slog .LevelDebug )
1283
1284
expected := "this-is-shutdown"
1285
+ derpMap , _ := tailnettest .RunDERPAndSTUN (t )
1284
1286
client := & client {
1285
1287
t : t ,
1286
1288
agentID : uuid .New (),
1287
1289
manifest : agentsdk.Manifest {
1288
- DERPMap : tailnettest . RunDERPAndSTUN ( t ) ,
1290
+ DERPMap : derpMap ,
1289
1291
StartupScript : "echo 1" ,
1290
1292
ShutdownScript : "echo " + expected ,
1291
1293
},
@@ -1343,9 +1345,9 @@ func TestAgent_Startup(t *testing.T) {
1343
1345
Directory : "" ,
1344
1346
}, 0 )
1345
1347
assert .Eventually (t , func () bool {
1346
- return client .getStartup ().Version != ""
1348
+ return client .GetStartup ().Version != ""
1347
1349
}, testutil .WaitShort , testutil .IntervalFast )
1348
- require .Equal (t , "" , client .getStartup ().ExpandedDirectory )
1350
+ require .Equal (t , "" , client .GetStartup ().ExpandedDirectory )
1349
1351
})
1350
1352
1351
1353
t .Run ("HomeDirectory" , func (t * testing.T ) {
@@ -1357,11 +1359,11 @@ func TestAgent_Startup(t *testing.T) {
1357
1359
Directory : "~" ,
1358
1360
}, 0 )
1359
1361
assert .Eventually (t , func () bool {
1360
- return client .getStartup ().Version != ""
1362
+ return client .GetStartup ().Version != ""
1361
1363
}, testutil .WaitShort , testutil .IntervalFast )
1362
1364
homeDir , err := os .UserHomeDir ()
1363
1365
require .NoError (t , err )
1364
- require .Equal (t , homeDir , client .getStartup ().ExpandedDirectory )
1366
+ require .Equal (t , homeDir , client .GetStartup ().ExpandedDirectory )
1365
1367
})
1366
1368
1367
1369
t .Run ("NotAbsoluteDirectory" , func (t * testing.T ) {
@@ -1373,11 +1375,11 @@ func TestAgent_Startup(t *testing.T) {
1373
1375
Directory : "coder/coder" ,
1374
1376
}, 0 )
1375
1377
assert .Eventually (t , func () bool {
1376
- return client .getStartup ().Version != ""
1378
+ return client .GetStartup ().Version != ""
1377
1379
}, testutil .WaitShort , testutil .IntervalFast )
1378
1380
homeDir , err := os .UserHomeDir ()
1379
1381
require .NoError (t , err )
1380
- require .Equal (t , filepath .Join (homeDir , "coder/coder" ), client .getStartup ().ExpandedDirectory )
1382
+ require .Equal (t , filepath .Join (homeDir , "coder/coder" ), client .GetStartup ().ExpandedDirectory )
1381
1383
})
1382
1384
1383
1385
t .Run ("HomeEnvironmentVariable" , func (t * testing.T ) {
@@ -1389,11 +1391,11 @@ func TestAgent_Startup(t *testing.T) {
1389
1391
Directory : "$HOME" ,
1390
1392
}, 0 )
1391
1393
assert .Eventually (t , func () bool {
1392
- return client .getStartup ().Version != ""
1394
+ return client .GetStartup ().Version != ""
1393
1395
}, testutil .WaitShort , testutil .IntervalFast )
1394
1396
homeDir , err := os .UserHomeDir ()
1395
1397
require .NoError (t , err )
1396
- require .Equal (t , homeDir , client .getStartup ().ExpandedDirectory )
1398
+ require .Equal (t , homeDir , client .GetStartup ().ExpandedDirectory )
1397
1399
})
1398
1400
}
1399
1401
@@ -1532,7 +1534,7 @@ func TestAgent_Speedtest(t *testing.T) {
1532
1534
t .Skip ("This test is relatively flakey because of Tailscale's speedtest code..." )
1533
1535
ctx , cancel := context .WithTimeout (context .Background (), testutil .WaitLong )
1534
1536
defer cancel ()
1535
- derpMap := tailnettest .RunDERPAndSTUN (t )
1537
+ derpMap , _ := tailnettest .RunDERPAndSTUN (t )
1536
1538
//nolint:dogsled
1537
1539
conn , _ , _ , _ , _ := setupAgent (t , agentsdk.Manifest {
1538
1540
DERPMap : derpMap ,
@@ -1553,7 +1555,7 @@ func TestAgent_Reconnect(t *testing.T) {
1553
1555
1554
1556
agentID := uuid .New ()
1555
1557
statsCh := make (chan * agentsdk.Stats )
1556
- derpMap := tailnettest .RunDERPAndSTUN (t )
1558
+ derpMap , _ := tailnettest .RunDERPAndSTUN (t )
1557
1559
client := & client {
1558
1560
t : t ,
1559
1561
agentID : agentID ,
@@ -1691,14 +1693,14 @@ func (c closeFunc) Close() error {
1691
1693
1692
1694
func setupAgent (t * testing.T , metadata agentsdk.Manifest , ptyTimeout time.Duration , opts ... func (agent.Options ) agent.Options ) (
1693
1695
* codersdk.WorkspaceAgentConn ,
1694
- * client ,
1696
+ * agenttest. Client ,
1695
1697
<- chan * agentsdk.Stats ,
1696
1698
afero.Fs ,
1697
1699
io.Closer ,
1698
1700
) {
1699
1701
logger := slogtest .Make (t , nil ).Leveled (slog .LevelDebug )
1700
1702
if metadata .DERPMap == nil {
1701
- metadata .DERPMap = tailnettest .RunDERPAndSTUN (t )
1703
+ metadata .DERPMap , _ = tailnettest .RunDERPAndSTUN (t )
1702
1704
}
1703
1705
coordinator := tailnet .NewCoordinator (logger )
1704
1706
t .Cleanup (func () {
@@ -1707,13 +1709,7 @@ func setupAgent(t *testing.T, metadata agentsdk.Manifest, ptyTimeout time.Durati
1707
1709
agentID := uuid .New ()
1708
1710
statsCh := make (chan * agentsdk.Stats , 50 )
1709
1711
fs := afero .NewMemMapFs ()
1710
- c := & client {
1711
- t : t ,
1712
- agentID : agentID ,
1713
- manifest : metadata ,
1714
- statsChan : statsCh ,
1715
- coordinator : coordinator ,
1716
- }
1712
+ c := agenttest .NewClient (t , agentID , metadata , statsCh , coordinator )
1717
1713
1718
1714
options := agent.Options {
1719
1715
Client : c ,
0 commit comments