Skip to content

Commit 4a56a40

Browse files
authored
chore: apptest: update test helpers to context-aware testutil.Eventually (#19918)
Relates to coder/internal#960
1 parent b71d671 commit 4a56a40

1 file changed

Lines changed: 39 additions & 39 deletions

File tree

‎coderd/workspaceapps/apptest/apptest.go‎

Lines changed: 39 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ func Run(t *testing.T, appHostIsPrimary bool, factory DeploymentFactory) {
6767
// reconnecting-pty proxy server we want to test is mounted.
6868
client := appDetails.AppClient(t)
6969
testReconnectingPTY(ctx, t, client, appDetails.Agent.ID, "")
70-
assertWorkspaceLastUsedAtUpdated(t, appDetails)
70+
assertWorkspaceLastUsedAtUpdated(ctx, t, appDetails)
7171
})
7272

7373
t.Run("SignedTokenQueryParameter", func(t *testing.T) {
@@ -97,7 +97,7 @@ func Run(t *testing.T, appHostIsPrimary bool, factory DeploymentFactory) {
9797
// Make an unauthenticated client.
9898
unauthedAppClient := codersdk.New(appDetails.AppClient(t).URL)
9999
testReconnectingPTY(ctx, t, unauthedAppClient, appDetails.Agent.ID, issueRes.SignedToken)
100-
assertWorkspaceLastUsedAtUpdated(t, appDetails)
100+
assertWorkspaceLastUsedAtUpdated(ctx, t, appDetails)
101101
})
102102
})
103103

@@ -123,7 +123,7 @@ func Run(t *testing.T, appHostIsPrimary bool, factory DeploymentFactory) {
123123
require.Contains(t, string(body), "Path-based applications are disabled")
124124
// Even though path-based apps are disabled, the request should indicate
125125
// that the workspace was used.
126-
assertWorkspaceLastUsedAtNotUpdated(t, appDetails)
126+
assertWorkspaceLastUsedAtNotUpdated(ctx, t, appDetails)
127127
})
128128

129129
t.Run("LoginWithoutAuthOnPrimary", func(t *testing.T) {
@@ -150,7 +150,7 @@ func Run(t *testing.T, appHostIsPrimary bool, factory DeploymentFactory) {
150150
require.NoError(t, err)
151151
require.True(t, loc.Query().Has("message"))
152152
require.True(t, loc.Query().Has("redirect"))
153-
assertWorkspaceLastUsedAtNotUpdated(t, appDetails)
153+
assertWorkspaceLastUsedAtNotUpdated(ctx, t, appDetails)
154154
})
155155

156156
t.Run("LoginWithoutAuthOnProxy", func(t *testing.T) {
@@ -189,7 +189,7 @@ func Run(t *testing.T, appHostIsPrimary bool, factory DeploymentFactory) {
189189
// request is getting stripped.
190190
require.Equal(t, u.Path, redirectURI.Path+"/")
191191
require.Equal(t, u.RawQuery, redirectURI.RawQuery)
192-
assertWorkspaceLastUsedAtNotUpdated(t, appDetails)
192+
assertWorkspaceLastUsedAtNotUpdated(ctx, t, appDetails)
193193
})
194194

195195
t.Run("NoAccessShould404", func(t *testing.T) {
@@ -288,7 +288,7 @@ func Run(t *testing.T, appHostIsPrimary bool, factory DeploymentFactory) {
288288
require.NoError(t, err)
289289
require.Equal(t, proxyTestAppBody, string(body))
290290
require.Equal(t, http.StatusOK, resp.StatusCode)
291-
assertWorkspaceLastUsedAtUpdated(t, appDetails)
291+
assertWorkspaceLastUsedAtUpdated(ctx, t, appDetails)
292292
})
293293

294294
t.Run("ProxiesHTTPS", func(t *testing.T) {
@@ -334,7 +334,7 @@ func Run(t *testing.T, appHostIsPrimary bool, factory DeploymentFactory) {
334334
require.NoError(t, err)
335335
require.Equal(t, proxyTestAppBody, string(body))
336336
require.Equal(t, http.StatusOK, resp.StatusCode)
337-
assertWorkspaceLastUsedAtUpdated(t, appDetails)
337+
assertWorkspaceLastUsedAtUpdated(ctx, t, appDetails)
338338
})
339339

340340
t.Run("BlocksMe", func(t *testing.T) {
@@ -355,7 +355,7 @@ func Run(t *testing.T, appHostIsPrimary bool, factory DeploymentFactory) {
355355
body, err := io.ReadAll(resp.Body)
356356
require.NoError(t, err)
357357
require.Contains(t, string(body), "must be accessed with the full username, not @me")
358-
assertWorkspaceLastUsedAtNotUpdated(t, appDetails)
358+
assertWorkspaceLastUsedAtNotUpdated(ctx, t, appDetails)
359359
})
360360

361361
t.Run("ForwardsIP", func(t *testing.T) {
@@ -375,7 +375,7 @@ func Run(t *testing.T, appHostIsPrimary bool, factory DeploymentFactory) {
375375
require.Equal(t, proxyTestAppBody, string(body))
376376
require.Equal(t, http.StatusOK, resp.StatusCode)
377377
require.Equal(t, "1.1.1.1,127.0.0.1", resp.Header.Get("X-Forwarded-For"))
378-
assertWorkspaceLastUsedAtUpdated(t, appDetails)
378+
assertWorkspaceLastUsedAtUpdated(ctx, t, appDetails)
379379
})
380380

381381
t.Run("ProxyError", func(t *testing.T) {
@@ -391,7 +391,7 @@ func Run(t *testing.T, appHostIsPrimary bool, factory DeploymentFactory) {
391391
require.Equal(t, http.StatusBadGateway, resp.StatusCode)
392392
// An valid authenticated attempt to access a workspace app
393393
// should count as usage regardless of success.
394-
assertWorkspaceLastUsedAtUpdated(t, appDetails)
394+
assertWorkspaceLastUsedAtUpdated(ctx, t, appDetails)
395395
})
396396

397397
t.Run("NoProxyPort", func(t *testing.T) {
@@ -407,7 +407,7 @@ func Run(t *testing.T, appHostIsPrimary bool, factory DeploymentFactory) {
407407
// TODO(@deansheather): This should be 400. There's a todo in the
408408
// resolve request code to fix this.
409409
require.Equal(t, http.StatusInternalServerError, resp.StatusCode)
410-
assertWorkspaceLastUsedAtNotUpdated(t, appDetails)
410+
assertWorkspaceLastUsedAtNotUpdated(ctx, t, appDetails)
411411
})
412412

413413
t.Run("BadJWT", func(t *testing.T) {
@@ -464,7 +464,7 @@ func Run(t *testing.T, appHostIsPrimary bool, factory DeploymentFactory) {
464464
require.NoError(t, err)
465465
require.Equal(t, proxyTestAppBody, string(body))
466466
require.Equal(t, http.StatusOK, resp.StatusCode)
467-
assertWorkspaceLastUsedAtUpdated(t, appDetails)
467+
assertWorkspaceLastUsedAtUpdated(ctx, t, appDetails)
468468

469469
// Since the old token is invalid, the signed app token cookie should have a new value.
470470
newTokenCookie := findCookie(resp.Cookies(), codersdk.SignedAppTokenCookie)
@@ -1139,7 +1139,7 @@ func Run(t *testing.T, appHostIsPrimary bool, factory DeploymentFactory) {
11391139
_ = resp.Body.Close()
11401140
require.Equal(t, http.StatusOK, resp.StatusCode)
11411141
require.Equal(t, resp.Header.Get("X-Got-Host"), u.Host)
1142-
assertWorkspaceLastUsedAtUpdated(t, appDetails)
1142+
assertWorkspaceLastUsedAtUpdated(ctx, t, appDetails)
11431143
})
11441144

11451145
t.Run("WorkspaceAppsProxySubdomainHostnamePrefix/Different", func(t *testing.T) {
@@ -1190,7 +1190,7 @@ func Run(t *testing.T, appHostIsPrimary bool, factory DeploymentFactory) {
11901190
require.NoError(t, err)
11911191
_ = resp.Body.Close()
11921192
require.NotEqual(t, http.StatusOK, resp.StatusCode)
1193-
assertWorkspaceLastUsedAtUpdated(t, appDetails)
1193+
assertWorkspaceLastUsedAtUpdated(ctx, t, appDetails)
11941194
})
11951195

11961196
// This test ensures that the subdomain handler does nothing if
@@ -1274,7 +1274,7 @@ func Run(t *testing.T, appHostIsPrimary bool, factory DeploymentFactory) {
12741274
require.NoError(t, err)
12751275
defer resp.Body.Close()
12761276
require.Equal(t, http.StatusNotFound, resp.StatusCode)
1277-
assertWorkspaceLastUsedAtNotUpdated(t, appDetails)
1277+
assertWorkspaceLastUsedAtNotUpdated(ctx, t, appDetails)
12781278
})
12791279

12801280
t.Run("RedirectsWithSlash", func(t *testing.T) {
@@ -1295,7 +1295,7 @@ func Run(t *testing.T, appHostIsPrimary bool, factory DeploymentFactory) {
12951295
loc, err := resp.Location()
12961296
require.NoError(t, err)
12971297
require.Equal(t, appDetails.SubdomainAppURL(appDetails.Apps.Owner).Path, loc.Path)
1298-
assertWorkspaceLastUsedAtNotUpdated(t, appDetails)
1298+
assertWorkspaceLastUsedAtNotUpdated(ctx, t, appDetails)
12991299
})
13001300

13011301
t.Run("RedirectsWithQuery", func(t *testing.T) {
@@ -1315,7 +1315,7 @@ func Run(t *testing.T, appHostIsPrimary bool, factory DeploymentFactory) {
13151315
loc, err := resp.Location()
13161316
require.NoError(t, err)
13171317
require.Equal(t, appDetails.SubdomainAppURL(appDetails.Apps.Owner).RawQuery, loc.RawQuery)
1318-
assertWorkspaceLastUsedAtNotUpdated(t, appDetails)
1318+
assertWorkspaceLastUsedAtNotUpdated(ctx, t, appDetails)
13191319
})
13201320

13211321
t.Run("Proxies", func(t *testing.T) {
@@ -1358,7 +1358,7 @@ func Run(t *testing.T, appHostIsPrimary bool, factory DeploymentFactory) {
13581358
require.NoError(t, err)
13591359
require.Equal(t, proxyTestAppBody, string(body))
13601360
require.Equal(t, http.StatusOK, resp.StatusCode)
1361-
assertWorkspaceLastUsedAtUpdated(t, appDetails)
1361+
assertWorkspaceLastUsedAtUpdated(ctx, t, appDetails)
13621362
})
13631363

13641364
t.Run("ProxiesHTTPS", func(t *testing.T) {
@@ -1403,7 +1403,7 @@ func Run(t *testing.T, appHostIsPrimary bool, factory DeploymentFactory) {
14031403
require.NoError(t, err)
14041404
require.Equal(t, proxyTestAppBody, string(body))
14051405
require.Equal(t, http.StatusOK, resp.StatusCode)
1406-
assertWorkspaceLastUsedAtUpdated(t, appDetails)
1406+
assertWorkspaceLastUsedAtUpdated(ctx, t, appDetails)
14071407
})
14081408

14091409
t.Run("ProxiesPort", func(t *testing.T) {
@@ -1420,7 +1420,7 @@ func Run(t *testing.T, appHostIsPrimary bool, factory DeploymentFactory) {
14201420
require.NoError(t, err)
14211421
require.Equal(t, proxyTestAppBody, string(body))
14221422
require.Equal(t, http.StatusOK, resp.StatusCode)
1423-
assertWorkspaceLastUsedAtUpdated(t, appDetails)
1423+
assertWorkspaceLastUsedAtUpdated(ctx, t, appDetails)
14241424
})
14251425

14261426
t.Run("ProxyError", func(t *testing.T) {
@@ -1434,7 +1434,7 @@ func Run(t *testing.T, appHostIsPrimary bool, factory DeploymentFactory) {
14341434
require.NoError(t, err)
14351435
defer resp.Body.Close()
14361436
require.Equal(t, http.StatusBadGateway, resp.StatusCode)
1437-
assertWorkspaceLastUsedAtUpdated(t, appDetails)
1437+
assertWorkspaceLastUsedAtUpdated(ctx, t, appDetails)
14381438
})
14391439

14401440
t.Run("ProxyPortMinimumError", func(t *testing.T) {
@@ -1456,7 +1456,7 @@ func Run(t *testing.T, appHostIsPrimary bool, factory DeploymentFactory) {
14561456
err = json.NewDecoder(resp.Body).Decode(&resBody)
14571457
require.NoError(t, err)
14581458
require.Contains(t, resBody.Message, "Coder reserves ports less than")
1459-
assertWorkspaceLastUsedAtNotUpdated(t, appDetails)
1459+
assertWorkspaceLastUsedAtNotUpdated(ctx, t, appDetails)
14601460
})
14611461

14621462
t.Run("SuffixWildcardOK", func(t *testing.T) {
@@ -1479,7 +1479,7 @@ func Run(t *testing.T, appHostIsPrimary bool, factory DeploymentFactory) {
14791479
require.NoError(t, err)
14801480
require.Equal(t, proxyTestAppBody, string(body))
14811481
require.Equal(t, http.StatusOK, resp.StatusCode)
1482-
assertWorkspaceLastUsedAtUpdated(t, appDetails)
1482+
assertWorkspaceLastUsedAtUpdated(ctx, t, appDetails)
14831483
})
14841484

14851485
t.Run("WildcardPortOK", func(t *testing.T) {
@@ -1512,7 +1512,7 @@ func Run(t *testing.T, appHostIsPrimary bool, factory DeploymentFactory) {
15121512
require.NoError(t, err)
15131513
require.Equal(t, proxyTestAppBody, string(body))
15141514
require.Equal(t, http.StatusOK, resp.StatusCode)
1515-
assertWorkspaceLastUsedAtUpdated(t, appDetails)
1515+
assertWorkspaceLastUsedAtUpdated(ctx, t, appDetails)
15161516
})
15171517

15181518
t.Run("SuffixWildcardNotMatch", func(t *testing.T) {
@@ -1542,7 +1542,7 @@ func Run(t *testing.T, appHostIsPrimary bool, factory DeploymentFactory) {
15421542
// It's probably rendering the dashboard or a 404 page, so only
15431543
// ensure that the body doesn't match.
15441544
require.NotContains(t, string(body), proxyTestAppBody)
1545-
assertWorkspaceLastUsedAtNotUpdated(t, appDetails)
1545+
assertWorkspaceLastUsedAtNotUpdated(ctx, t, appDetails)
15461546
})
15471547

15481548
t.Run("DifferentSuffix", func(t *testing.T) {
@@ -1569,7 +1569,7 @@ func Run(t *testing.T, appHostIsPrimary bool, factory DeploymentFactory) {
15691569
// It's probably rendering the dashboard, so only ensure that the body
15701570
// doesn't match.
15711571
require.NotContains(t, string(body), proxyTestAppBody)
1572-
assertWorkspaceLastUsedAtNotUpdated(t, appDetails)
1572+
assertWorkspaceLastUsedAtNotUpdated(ctx, t, appDetails)
15731573
})
15741574
})
15751575

@@ -1628,7 +1628,7 @@ func Run(t *testing.T, appHostIsPrimary bool, factory DeploymentFactory) {
16281628
require.NoError(t, err)
16291629
require.Equal(t, proxyTestAppBody, string(body))
16301630
require.Equal(t, http.StatusOK, resp.StatusCode)
1631-
assertWorkspaceLastUsedAtUpdated(t, appDetails)
1631+
assertWorkspaceLastUsedAtUpdated(ctx, t, appDetails)
16321632

16331633
// Since the old token is invalid, the signed app token cookie should have a new value.
16341634
newTokenCookie := findCookie(resp.Cookies(), codersdk.SignedAppTokenCookie)
@@ -1652,7 +1652,7 @@ func Run(t *testing.T, appHostIsPrimary bool, factory DeploymentFactory) {
16521652
require.NoError(t, err)
16531653
defer resp.Body.Close()
16541654
require.Equal(t, http.StatusNotFound, resp.StatusCode)
1655-
assertWorkspaceLastUsedAtNotUpdated(t, appDetails)
1655+
assertWorkspaceLastUsedAtNotUpdated(ctx, t, appDetails)
16561656
})
16571657

16581658
t.Run("AuthenticatedOK", func(t *testing.T) {
@@ -1681,7 +1681,7 @@ func Run(t *testing.T, appHostIsPrimary bool, factory DeploymentFactory) {
16811681
require.NoError(t, err)
16821682
defer resp.Body.Close()
16831683
require.Equal(t, http.StatusOK, resp.StatusCode)
1684-
assertWorkspaceLastUsedAtUpdated(t, appDetails)
1684+
assertWorkspaceLastUsedAtUpdated(ctx, t, appDetails)
16851685
})
16861686

16871687
t.Run("PublicOK", func(t *testing.T) {
@@ -1709,7 +1709,7 @@ func Run(t *testing.T, appHostIsPrimary bool, factory DeploymentFactory) {
17091709
require.NoError(t, err)
17101710
defer resp.Body.Close()
17111711
require.Equal(t, http.StatusOK, resp.StatusCode)
1712-
assertWorkspaceLastUsedAtUpdated(t, appDetails)
1712+
assertWorkspaceLastUsedAtUpdated(ctx, t, appDetails)
17131713
})
17141714

17151715
t.Run("HTTPS", func(t *testing.T) {
@@ -1739,7 +1739,7 @@ func Run(t *testing.T, appHostIsPrimary bool, factory DeploymentFactory) {
17391739
require.NoError(t, err)
17401740
defer resp.Body.Close()
17411741
require.Equal(t, http.StatusOK, resp.StatusCode)
1742-
assertWorkspaceLastUsedAtUpdated(t, appDetails)
1742+
assertWorkspaceLastUsedAtUpdated(ctx, t, appDetails)
17431743
})
17441744
})
17451745

@@ -2466,33 +2466,33 @@ func testReconnectingPTY(ctx context.Context, t *testing.T, client *codersdk.Cli
24662466
// Accessing an app should update the workspace's LastUsedAt.
24672467
// NOTE: Despite our efforts with the flush channel, this is inherently racy when used with
24682468
// parallel tests on the same workspace/app.
2469-
func assertWorkspaceLastUsedAtUpdated(t testing.TB, details *Details) {
2469+
func assertWorkspaceLastUsedAtUpdated(ctx context.Context, t testing.TB, details *Details) {
24702470
t.Helper()
24712471

24722472
require.NotNil(t, details.Workspace, "can't assert LastUsedAt on a nil workspace!")
2473-
before, err := details.SDKClient.Workspace(context.Background(), details.Workspace.ID)
2473+
before, err := details.SDKClient.Workspace(ctx, details.Workspace.ID)
24742474
require.NoError(t, err)
2475-
require.Eventually(t, func() bool {
2475+
testutil.Eventually(ctx, t, func(ctx context.Context) bool {
24762476
// We may need to flush multiple times, since the stats from the app we are testing might be
24772477
// collected asynchronously from when we see the connection close, and thus, could race
24782478
// against being flushed.
24792479
details.FlushStats()
2480-
after, err := details.SDKClient.Workspace(context.Background(), details.Workspace.ID)
2480+
after, err := details.SDKClient.Workspace(ctx, details.Workspace.ID)
24812481
return assert.NoError(t, err) && after.LastUsedAt.After(before.LastUsedAt)
2482-
}, testutil.WaitShort, testutil.IntervalMedium)
2482+
}, testutil.IntervalMedium)
24832483
}
24842484

24852485
// Except when it sometimes shouldn't (e.g. no access)
24862486
// NOTE: Despite our efforts with the flush channel, this is inherently racy when used with
24872487
// parallel tests on the same workspace/app.
2488-
func assertWorkspaceLastUsedAtNotUpdated(t testing.TB, details *Details) {
2488+
func assertWorkspaceLastUsedAtNotUpdated(ctx context.Context, t testing.TB, details *Details) {
24892489
t.Helper()
24902490

24912491
require.NotNil(t, details.Workspace, "can't assert LastUsedAt on a nil workspace!")
2492-
before, err := details.SDKClient.Workspace(context.Background(), details.Workspace.ID)
2492+
before, err := details.SDKClient.Workspace(ctx, details.Workspace.ID)
24932493
require.NoError(t, err)
24942494
details.FlushStats()
2495-
after, err := details.SDKClient.Workspace(context.Background(), details.Workspace.ID)
2495+
after, err := details.SDKClient.Workspace(ctx, details.Workspace.ID)
24962496
require.NoError(t, err)
24972497
require.Equal(t, before.LastUsedAt, after.LastUsedAt, "workspace LastUsedAt updated when it should not have been")
24982498
}

0 commit comments

Comments
 (0)