Skip to content
Prev Previous commit
Next Next commit
use the correct client
  • Loading branch information
johnstcn committed Jan 16, 2024
commit ef8d3ab149e7cbd83c2680a2fe5cbcf4197d142d
28 changes: 14 additions & 14 deletions coderd/workspaceapps/apptest/apptest.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func Run(t *testing.T, appHostIsPrimary bool, factory DeploymentFactory) {
// reconnecting-pty proxy server we want to test is mounted.
client := appDetails.AppClient(t)
testReconnectingPTY(ctx, t, client, appDetails.Agent.ID, "")
assertWorkspaceLastUsedAtUpdated(t, client, appDetails)
assertWorkspaceLastUsedAtUpdated(t, appDetails)
})

t.Run("SignedTokenQueryParameter", func(t *testing.T) {
Expand Down Expand Up @@ -93,7 +93,7 @@ func Run(t *testing.T, appHostIsPrimary bool, factory DeploymentFactory) {
// Make an unauthenticated client.
unauthedAppClient := codersdk.New(appDetails.AppClient(t).URL)
testReconnectingPTY(ctx, t, unauthedAppClient, appDetails.Agent.ID, issueRes.SignedToken)
assertWorkspaceLastUsedAtUpdated(t, appDetails.AppClient(t), appDetails)
assertWorkspaceLastUsedAtUpdated(t, appDetails)
})
})

Expand Down Expand Up @@ -121,7 +121,7 @@ func Run(t *testing.T, appHostIsPrimary bool, factory DeploymentFactory) {
require.Contains(t, string(body), "Path-based applications are disabled")
// Even though path-based apps are disabled, the request should indicate
// that the workspace was used.
assertWorkspaceLastUsedAtNotUpdated(t, appDetails.AppClient(t), appDetails)
assertWorkspaceLastUsedAtNotUpdated(t, appDetails)
})

t.Run("LoginWithoutAuthOnPrimary", func(t *testing.T) {
Expand All @@ -147,7 +147,7 @@ func Run(t *testing.T, appHostIsPrimary bool, factory DeploymentFactory) {
require.NoError(t, err)
require.True(t, loc.Query().Has("message"))
require.True(t, loc.Query().Has("redirect"))
assertWorkspaceLastUsedAtUpdated(t, appDetails.AppClient(t), appDetails)
assertWorkspaceLastUsedAtUpdated(t, appDetails)
})

t.Run("LoginWithoutAuthOnProxy", func(t *testing.T) {
Expand Down Expand Up @@ -185,7 +185,7 @@ func Run(t *testing.T, appHostIsPrimary bool, factory DeploymentFactory) {
// request is getting stripped.
require.Equal(t, u.Path, redirectURI.Path+"/")
require.Equal(t, u.RawQuery, redirectURI.RawQuery)
assertWorkspaceLastUsedAtUpdated(t, appDetails.AppClient(t), appDetails)
assertWorkspaceLastUsedAtUpdated(t, appDetails)
})

t.Run("NoAccessShould404", func(t *testing.T) {
Expand Down Expand Up @@ -280,7 +280,7 @@ func Run(t *testing.T, appHostIsPrimary bool, factory DeploymentFactory) {
require.NoError(t, err)
require.Equal(t, proxyTestAppBody, string(body))
require.Equal(t, http.StatusOK, resp.StatusCode)
assertWorkspaceLastUsedAtUpdated(t, appDetails.AppClient(t), appDetails)
assertWorkspaceLastUsedAtUpdated(t, appDetails)
})

t.Run("ProxiesHTTPS", func(t *testing.T) {
Expand Down Expand Up @@ -326,7 +326,7 @@ func Run(t *testing.T, appHostIsPrimary bool, factory DeploymentFactory) {
require.NoError(t, err)
require.Equal(t, proxyTestAppBody, string(body))
require.Equal(t, http.StatusOK, resp.StatusCode)
assertWorkspaceLastUsedAtUpdated(t, appDetails.AppClient(t), appDetails)
assertWorkspaceLastUsedAtUpdated(t, appDetails)
})

t.Run("BlocksMe", func(t *testing.T) {
Expand Down Expand Up @@ -366,7 +366,7 @@ func Run(t *testing.T, appHostIsPrimary bool, factory DeploymentFactory) {
require.Equal(t, proxyTestAppBody, string(body))
require.Equal(t, http.StatusOK, resp.StatusCode)
require.Equal(t, "1.1.1.1,127.0.0.1", resp.Header.Get("X-Forwarded-For"))
assertWorkspaceLastUsedAtUpdated(t, appDetails.AppClient(t), appDetails)
assertWorkspaceLastUsedAtUpdated(t, appDetails)
})

t.Run("ProxyError", func(t *testing.T) {
Expand All @@ -381,7 +381,7 @@ func Run(t *testing.T, appHostIsPrimary bool, factory DeploymentFactory) {
require.Equal(t, http.StatusBadGateway, resp.StatusCode)
// An valid authenticated attempt to access a workspace app
// should count as usage regardless of success.
assertWorkspaceLastUsedAtUpdated(t, appDetails.AppClient(t), appDetails)
assertWorkspaceLastUsedAtUpdated(t, appDetails)
})

t.Run("NoProxyPort", func(t *testing.T) {
Expand All @@ -396,7 +396,7 @@ func Run(t *testing.T, appHostIsPrimary bool, factory DeploymentFactory) {
// TODO(@deansheather): This should be 400. There's a todo in the
// resolve request code to fix this.
require.Equal(t, http.StatusInternalServerError, resp.StatusCode)
assertWorkspaceLastUsedAtNotUpdated(t, appDetails.AppClient(t), appDetails)
assertWorkspaceLastUsedAtNotUpdated(t, appDetails)
})
})

Expand Down Expand Up @@ -1566,21 +1566,21 @@ func testReconnectingPTY(ctx context.Context, t *testing.T, client *codersdk.Cli
}

// Accessing an app should update the workspace's LastUsedAt.
func assertWorkspaceLastUsedAtUpdated(t testing.TB, client *codersdk.Client, details *Details) {
func assertWorkspaceLastUsedAtUpdated(t testing.TB, details *Details) {
t.Helper()

details.FlushStats()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here, looping flush seems better than waiting an arbitrary time?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A loop-flush here would probably just execute after the first iteration.

ws, err := client.Workspace(context.Background(), details.Workspace.ID)
ws, err := details.SDKClient.Workspace(context.Background(), details.Workspace.ID)
require.NoError(t, err)
require.Greater(t, ws.LastUsedAt, details.Workspace.LastUsedAt, "workspace LastUsedAt not updated when it should have been")
}

// Except when it sometimes shouldn't (e.g. no access)
func assertWorkspaceLastUsedAtNotUpdated(t testing.TB, client *codersdk.Client, details *Details) {
func assertWorkspaceLastUsedAtNotUpdated(t testing.TB, details *Details) {
t.Helper()

details.FlushStats()
ws, err := client.Workspace(context.Background(), details.Workspace.ID)
ws, err := details.SDKClient.Workspace(context.Background(), details.Workspace.ID)
require.NoError(t, err)
require.Equal(t, ws.LastUsedAt, details.Workspace.LastUsedAt, "workspace LastUsedAt updated when it should not have been")
}