test: copy test databases by file and start test deadlines after setup (#32426)
## Summary Mitigate Windows nightly timeout bursts caused by slow test database creation. Multiple tests were hitting `context deadline exceeded` because their 10-second deadlines started before database setup completed. Refs https://linear.app/codercom/issue/CORE-615 ## Changes - Use `STRATEGY FILE_COPY` for test database creation on PostgreSQL 15+, avoiding the more expensive default `WAL_LOG` strategy. PostgreSQL 13/14 remain unchanged. - Cache the PostgreSQL version lookup, preserving existing behavior if detection fails. - Log database copies exceeding 2 seconds, including duration and strategy. - Move context creation after database/server setup in affected WebPush, OIDC, notification, and prebuild tests, without changing timeout values. - Add unit tests for PostgreSQL version-based strategy selection. Test-only changes across 6 files; no production code changes. ## Validation - Local PostgreSQL 17.10 tests pass, including WebPush, prebuild reconciliation, OIDC, and notifications. - `go vet` and `golangci-lint` report no issues. - Local OIDC comparison: - Default strategy: 3–5 failures/run. - `FILE_COPY` with partial context fixes: 1–2 failures/run. - **Final implementation: 0 failures across 5 runs**, with maximum database creation time reduced from 13.3s to 8.9s. ## Remaining Work - Run `nightly-gauntlet` repeatedly to verify improvements on Windows and macOS. - Compare WAL volume, checkpoint overhead, and test durations. `FILE_COPY` increases checkpoints, though local database creation was faster. - Confirm PostgreSQL 13 and 17 Linux jobs pass. **Status:** Draft pending nightly validation.
This commit is contained in:
1 parent
54fc9e3e14
commit
c5af20be31
6 files changed
+129
-43
No files matched your search
@@ -184,7 +184,7 @@ func createDatabaseFromTemplate(t TBSubset, connParams ConnectionParams, db *sql
|
||||
if emptyTemplateDBName {
|
||||
templateDBName = fmt.Sprintf("tpl_%s", migrations.GetMigrationsHash()[:32])
|
||||
}
|
||||
_, err := db.Exec("CREATE DATABASE " + newDBName + " WITH TEMPLATE " + templateDBName)
|
||||
err := copyDatabaseFromTemplate(t, db, newDBName, templateDBName)
|
||||
if err == nil {
|
||||
// Template database already exists and we successfully created the new database.
|
||||
return nil
|
||||
@@ -212,12 +212,79 @@ func createDatabaseFromTemplate(t TBSubset, connParams ConnectionParams, db *sql
|
||||
}
|
||||
|
||||
// Try to create the database again now that a template exists.
|
||||
if _, err = db.Exec("CREATE DATABASE " + newDBName + " WITH TEMPLATE " + templateDBName); err != nil {
|
||||
if err = copyDatabaseFromTemplate(t, db, newDBName, templateDBName); err != nil {
|
||||
return xerrors.Errorf("create db with template after migrations: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// slowCopyThreshold is how long copying the template into a test database
|
||||
// can take before it is logged. The time counts against any deadline the test
|
||||
// started before asking for a database, and nothing else in the test output
|
||||
// shows it.
|
||||
const slowCopyThreshold = 2 * time.Second
|
||||
|
||||
// copyDatabaseFromTemplate creates newDBName as a copy of templateDBName. It
|
||||
// times only the copy, so that building a missing template is not reported as
|
||||
// a slow copy.
|
||||
func copyDatabaseFromTemplate(t TBSubset, db *sql.DB, newDBName string, templateDBName string) error {
|
||||
t.Helper()
|
||||
|
||||
clause := copyStrategyClause(t, db)
|
||||
start := time.Now()
|
||||
if _, err := db.Exec("CREATE DATABASE " + newDBName + " WITH TEMPLATE " + templateDBName + clause); err != nil {
|
||||
return err
|
||||
}
|
||||
if elapsed := time.Since(start); elapsed > slowCopyThreshold {
|
||||
strategy := "server default"
|
||||
if clause != "" {
|
||||
strategy = strings.TrimSpace(clause)
|
||||
}
|
||||
t.Logf("%s copying template into test database %q took %s (%s)",
|
||||
time.Now().Format(time.StampMilli), newDBName, elapsed.Round(time.Millisecond), strategy)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
var (
|
||||
copyStrategyClauseOnce sync.Once
|
||||
copyStrategyClauseValue string
|
||||
)
|
||||
|
||||
// copyStrategyClause returns the STRATEGY clause to append to CREATE
|
||||
// DATABASE ... WITH TEMPLATE. The server version is read through db on the
|
||||
// first call and the result is kept for the rest of the process. If that
|
||||
// read fails, the clause stays empty and the server default strategy is used.
|
||||
func copyStrategyClause(t TBSubset, db *sql.DB) string {
|
||||
t.Helper()
|
||||
|
||||
copyStrategyClauseOnce.Do(func() {
|
||||
var versionNum int
|
||||
if err := db.QueryRow("SELECT current_setting('server_version_num')::int").Scan(&versionNum); err != nil {
|
||||
t.Logf("%s failed to read the server version, test databases will use the server default copy strategy: %s",
|
||||
time.Now().Format(time.StampMilli), err.Error())
|
||||
return
|
||||
}
|
||||
copyStrategyClauseValue = copyStrategyClauseForVersion(versionNum)
|
||||
})
|
||||
return copyStrategyClauseValue
|
||||
}
|
||||
|
||||
// copyStrategyClauseForVersion returns the STRATEGY clause for a
|
||||
// server_version_num, or an empty string if that version has no STRATEGY
|
||||
// option.
|
||||
func copyStrategyClauseForVersion(versionNum int) string {
|
||||
// PostgreSQL 15 added the STRATEGY option and made WAL_LOG the default.
|
||||
// WAL_LOG writes the whole template to the write-ahead log, so many tests
|
||||
// creating databases at once wait on each other. FILE_COPY is what older
|
||||
// versions always did. It forces a checkpoint before and after each copy,
|
||||
// which is the cost to weigh if this is revisited.
|
||||
if versionNum >= 150000 {
|
||||
return " STRATEGY FILE_COPY"
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func createAndInitDatabase(t TBSubset, connParams ConnectionParams, db *sql.DB, name string, initialize func(*sql.DB) error) error {
|
||||
// We will use a tx to obtain a lock, so another test or process doesn't race with us.
|
||||
tx, err := db.BeginTx(context.Background(), nil)
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
package dbtestutil
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestCopyStrategyClauseForVersion(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
versionNum int
|
||||
want string
|
||||
}{
|
||||
{name: "PostgreSQL13HasNoStrategyOption", versionNum: 130022, want: ""},
|
||||
{name: "PostgreSQL14HasNoStrategyOption", versionNum: 140019, want: ""},
|
||||
{name: "PostgreSQL15UsesFileCopy", versionNum: 150000, want: " STRATEGY FILE_COPY"},
|
||||
{name: "PostgreSQL17UsesFileCopy", versionNum: 170006, want: " STRATEGY FILE_COPY"},
|
||||
}
|
||||
for _, tc := range tests {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
require.Equal(t, tc.want, copyStrategyClauseForVersion(tc.versionNum))
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -328,8 +328,6 @@ func TestNotificationTest(t *testing.T) {
|
||||
t.Run("OwnerCanSendTestNotification", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
ctx := testutil.Context(t, testutil.WaitShort)
|
||||
|
||||
notifyEnq := ¬ificationstest.FakeEnqueuer{}
|
||||
ownerClient := coderdtest.New(t, &coderdtest.Options{
|
||||
DeploymentValues: coderdtest.DeploymentValues(t),
|
||||
@@ -340,6 +338,7 @@ func TestNotificationTest(t *testing.T) {
|
||||
_ = coderdtest.CreateFirstUser(t, ownerClient)
|
||||
|
||||
// When: They attempt to send a test notification.
|
||||
ctx := testutil.Context(t, testutil.WaitShort)
|
||||
err := ownerClient.PostTestNotification(ctx)
|
||||
require.NoError(t, err)
|
||||
|
||||
@@ -351,8 +350,6 @@ func TestNotificationTest(t *testing.T) {
|
||||
t.Run("MemberCannotSendTestNotification", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
ctx := testutil.Context(t, testutil.WaitShort)
|
||||
|
||||
notifyEnq := ¬ificationstest.FakeEnqueuer{}
|
||||
ownerClient := coderdtest.New(t, &coderdtest.Options{
|
||||
DeploymentValues: coderdtest.DeploymentValues(t),
|
||||
@@ -364,6 +361,7 @@ func TestNotificationTest(t *testing.T) {
|
||||
memberClient, _ := coderdtest.CreateAnotherUser(t, ownerClient, ownerUser.OrganizationID)
|
||||
|
||||
// When: They attempt to send a test notification.
|
||||
ctx := testutil.Context(t, testutil.WaitShort)
|
||||
err := memberClient.PostTestNotification(ctx)
|
||||
|
||||
// Then: We expect a forbidden error with no notifications sent
|
||||
|
||||
@@ -1837,7 +1837,6 @@ func TestUserOIDC(t *testing.T) {
|
||||
|
||||
t.Run("OIDCDormancy", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
ctx := testutil.Context(t, testutil.WaitShort)
|
||||
|
||||
auditor := audit.NewMock()
|
||||
fake := oidctest.NewFakeIDP(t,
|
||||
@@ -1874,6 +1873,7 @@ func TestUserOIDC(t *testing.T) {
|
||||
ResourceType: database.ResourceTypeUser,
|
||||
AdditionalFields: json.RawMessage(`{"automatic_actor":"coder","automatic_subsystem":"dormancy"}`),
|
||||
}))
|
||||
ctx := testutil.Context(t, testutil.WaitShort)
|
||||
me, err := client.User(ctx, "me")
|
||||
require.NoError(t, err)
|
||||
|
||||
@@ -1952,7 +1952,6 @@ func TestUserOIDC(t *testing.T) {
|
||||
// user_link exists (e.g. a dormant OIDC user created via SCIM or API).
|
||||
t.Run("OIDCFirstTimeLinkByEmailAllowed", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
ctx := testutil.Context(t, testutil.WaitShort)
|
||||
|
||||
fake := oidctest.NewFakeIDP(t,
|
||||
oidctest.WithRefresh(func(_ string) error {
|
||||
@@ -1985,6 +1984,7 @@ func TestUserOIDC(t *testing.T) {
|
||||
})
|
||||
require.Equal(t, http.StatusOK, resp.StatusCode)
|
||||
|
||||
ctx := testutil.Context(t, testutil.WaitShort)
|
||||
me, err := client.User(ctx, "me")
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, user.ID, me.ID,
|
||||
@@ -2007,7 +2007,6 @@ func TestUserOIDC(t *testing.T) {
|
||||
// and that their linked_id is backfilled with the correct value.
|
||||
t.Run("OIDCLegacyLinkBackfill", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
ctx := testutil.Context(t, testutil.WaitShort)
|
||||
|
||||
fake := oidctest.NewFakeIDP(t,
|
||||
oidctest.WithRefresh(func(_ string) error {
|
||||
@@ -2042,6 +2041,7 @@ func TestUserOIDC(t *testing.T) {
|
||||
})
|
||||
require.Equal(t, http.StatusOK, resp.StatusCode)
|
||||
|
||||
ctx := testutil.Context(t, testutil.WaitShort)
|
||||
me, err := client.User(ctx, "me")
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, user.ID, me.ID,
|
||||
@@ -2067,7 +2067,6 @@ func TestUserOIDC(t *testing.T) {
|
||||
// linked_id (old issuer) differs from the newly computed one (new issuer).
|
||||
t.Run("OIDCEmailFallbackBlockedByIssuerChange", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
ctx := testutil.Context(t, testutil.WaitShort)
|
||||
|
||||
fake := oidctest.NewFakeIDP(t,
|
||||
oidctest.WithRefresh(func(_ string) error {
|
||||
@@ -2108,6 +2107,7 @@ func TestUserOIDC(t *testing.T) {
|
||||
"issuer change must block the email fallback for an existing link")
|
||||
|
||||
// The stored link must remain unchanged.
|
||||
ctx := testutil.Context(t, testutil.WaitShort)
|
||||
link, err := db.GetUserLinkByUserIDLoginType(dbauthz.AsSystemRestricted(ctx), database.GetUserLinkByUserIDLoginTypeParams{
|
||||
UserID: user.ID,
|
||||
LoginType: database.LoginTypeOIDC,
|
||||
@@ -2124,7 +2124,6 @@ func TestUserOIDC(t *testing.T) {
|
||||
// so the user can keep logging in with either subject.
|
||||
t.Run("OIDCInsecureEmailFallbackAllowed", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
ctx := testutil.Context(t, testutil.WaitShort)
|
||||
|
||||
fake := oidctest.NewFakeIDP(t,
|
||||
oidctest.WithRefresh(func(_ string) error {
|
||||
@@ -2165,6 +2164,7 @@ func TestUserOIDC(t *testing.T) {
|
||||
require.Equal(t, http.StatusOK, resp.StatusCode,
|
||||
"insecure email fallback must allow login with a mismatched subject")
|
||||
|
||||
ctx := testutil.Context(t, testutil.WaitShort)
|
||||
me, err := client.User(ctx, "me")
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, user.ID, me.ID,
|
||||
@@ -2189,7 +2189,6 @@ func TestUserOIDC(t *testing.T) {
|
||||
// matching directly.
|
||||
t.Run("OIDCInsecureEmailFallbackPreservesOriginalLogin", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
ctx := testutil.Context(t, testutil.WaitShort)
|
||||
|
||||
fake := oidctest.NewFakeIDP(t,
|
||||
oidctest.WithRefresh(func(_ string) error {
|
||||
@@ -2234,6 +2233,7 @@ func TestUserOIDC(t *testing.T) {
|
||||
})
|
||||
require.Equal(t, http.StatusOK, resp.StatusCode)
|
||||
|
||||
ctx := testutil.Context(t, testutil.WaitShort)
|
||||
me, err := client.User(ctx, "me")
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, user.ID, me.ID)
|
||||
@@ -2289,7 +2289,6 @@ func TestUserOIDC(t *testing.T) {
|
||||
|
||||
t.Run("OIDCSuspended", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
ctx := testutil.Context(t, testutil.WaitShort)
|
||||
|
||||
fake := oidctest.NewFakeIDP(t,
|
||||
oidctest.WithRefresh(func(_ string) error {
|
||||
@@ -2328,6 +2327,7 @@ func TestUserOIDC(t *testing.T) {
|
||||
|
||||
// The user's status must remain suspended; nothing in the OAuth
|
||||
// transaction should have been committed.
|
||||
ctx := testutil.Context(t, testutil.WaitShort)
|
||||
//nolint:gocritic // System read for verification.
|
||||
dbUser, err := db.GetUserByID(dbauthz.AsSystemRestricted(ctx), user.ID)
|
||||
require.NoError(t, err)
|
||||
@@ -2596,7 +2596,6 @@ func TestUserOIDC(t *testing.T) {
|
||||
|
||||
t.Run("StripRedirectHost", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
ctx := testutil.Context(t, testutil.WaitShort)
|
||||
|
||||
expectedRedirect := "/foo/bar?hello=world&bar=baz"
|
||||
redirectURL := "https://malicious" + expectedRedirect
|
||||
@@ -2643,6 +2642,7 @@ func TestUserOIDC(t *testing.T) {
|
||||
require.NotContains(t, location.String(), "malicious")
|
||||
|
||||
// Verify the user was created
|
||||
ctx := testutil.Context(t, testutil.WaitShort)
|
||||
user, err := loginClient.User(ctx, "me")
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, "user@example.com", user.Email)
|
||||
|
||||
@@ -53,9 +53,8 @@ func TestPush(t *testing.T) {
|
||||
|
||||
t.Run("SuccessfulDelivery", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
ctx := testutil.Context(t, testutil.WaitShort)
|
||||
msg := randomWebpushMessage(t)
|
||||
manager, store, serverURL := setupPushTest(ctx, t, func(w http.ResponseWriter, r *http.Request) {
|
||||
ctx, manager, store, serverURL := setupPushTest(t, func(w http.ResponseWriter, r *http.Request) {
|
||||
assertWebpushPayload(t, r)
|
||||
w.WriteHeader(http.StatusOK)
|
||||
})
|
||||
@@ -80,8 +79,7 @@ func TestPush(t *testing.T) {
|
||||
|
||||
t.Run("ExpiredSubscription", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
ctx := testutil.Context(t, testutil.WaitShort)
|
||||
manager, store, serverURL := setupPushTest(ctx, t, func(w http.ResponseWriter, r *http.Request) {
|
||||
ctx, manager, store, serverURL := setupPushTest(t, func(w http.ResponseWriter, r *http.Request) {
|
||||
assertWebpushPayload(t, r)
|
||||
w.WriteHeader(http.StatusGone)
|
||||
})
|
||||
@@ -108,8 +106,7 @@ func TestPush(t *testing.T) {
|
||||
// 5xx responses are transient failures. The subscription should
|
||||
// remain after a failed delivery so it can be retried later.
|
||||
t.Parallel()
|
||||
ctx := testutil.Context(t, testutil.WaitShort)
|
||||
manager, store, serverURL := setupPushTest(ctx, t, func(w http.ResponseWriter, r *http.Request) {
|
||||
ctx, manager, store, serverURL := setupPushTest(t, func(w http.ResponseWriter, r *http.Request) {
|
||||
assertWebpushPayload(t, r)
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
w.Write([]byte("Internal server error"))
|
||||
@@ -175,8 +172,7 @@ func TestPush(t *testing.T) {
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
ctx := testutil.Context(t, testutil.WaitShort)
|
||||
manager, store, serverURL := setupPushTest(ctx, t, func(w http.ResponseWriter, r *http.Request) {
|
||||
ctx, manager, store, serverURL := setupPushTest(t, func(w http.ResponseWriter, r *http.Request) {
|
||||
assertWebpushPayload(t, r)
|
||||
w.WriteHeader(tc.statusCode)
|
||||
w.Write([]byte(tc.body))
|
||||
@@ -215,9 +211,8 @@ func TestPush(t *testing.T) {
|
||||
// the database.
|
||||
t.Run("StaleAndFailedSubscriptions", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
ctx := testutil.Context(t, testutil.WaitShort)
|
||||
|
||||
manager, store, server500URL := setupPushTest(ctx, t, func(w http.ResponseWriter, r *http.Request) {
|
||||
ctx, manager, store, server500URL := setupPushTest(t, func(w http.ResponseWriter, r *http.Request) {
|
||||
assertWebpushPayload(t, r)
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
w.Write([]byte("transient error"))
|
||||
@@ -270,10 +265,9 @@ func TestPush(t *testing.T) {
|
||||
|
||||
t.Run("MultipleSubscriptions", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
ctx := testutil.Context(t, testutil.WaitShort)
|
||||
var okEndpointCalled bool
|
||||
var goneEndpointCalled bool
|
||||
manager, store, serverOKURL := setupPushTest(ctx, t, func(w http.ResponseWriter, r *http.Request) {
|
||||
ctx, manager, store, serverOKURL := setupPushTest(t, func(w http.ResponseWriter, r *http.Request) {
|
||||
okEndpointCalled = true
|
||||
assertWebpushPayload(t, r)
|
||||
w.WriteHeader(http.StatusOK)
|
||||
@@ -325,9 +319,8 @@ func TestPush(t *testing.T) {
|
||||
t.Run("NotificationPayload", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
ctx := testutil.Context(t, testutil.WaitShort)
|
||||
var requestReceived bool
|
||||
manager, store, serverURL := setupPushTest(ctx, t, func(w http.ResponseWriter, r *http.Request) {
|
||||
ctx, manager, store, serverURL := setupPushTest(t, func(w http.ResponseWriter, r *http.Request) {
|
||||
requestReceived = true
|
||||
assertWebpushPayload(t, r)
|
||||
w.WriteHeader(http.StatusOK)
|
||||
@@ -352,8 +345,7 @@ func TestPush(t *testing.T) {
|
||||
|
||||
t.Run("NoSubscriptions", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
ctx := testutil.Context(t, testutil.WaitShort)
|
||||
manager, store, _ := setupPushTest(ctx, t, func(w http.ResponseWriter, _ *http.Request) {
|
||||
ctx, manager, store, _ := setupPushTest(t, func(w http.ResponseWriter, _ *http.Request) {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
})
|
||||
|
||||
@@ -374,9 +366,9 @@ func TestPush(t *testing.T) {
|
||||
t.Run("CachesSubscriptionsWithinTTL", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
ctx := testutil.Context(t, testutil.WaitShort)
|
||||
clock := quartz.NewMock(t)
|
||||
rawStore, _ := dbtestutil.NewDB(t)
|
||||
ctx := testutil.Context(t, testutil.WaitShort)
|
||||
store := &countingWebpushStore{Store: rawStore}
|
||||
var delivered atomic.Int32
|
||||
manager, _, serverURL := setupPushTestWithOptions(ctx, t, store, func(w http.ResponseWriter, r *http.Request) {
|
||||
@@ -408,9 +400,9 @@ func TestPush(t *testing.T) {
|
||||
t.Run("RefreshesSubscriptionsAfterTTLExpires", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
ctx := testutil.Context(t, testutil.WaitShort)
|
||||
clock := quartz.NewMock(t)
|
||||
rawStore, _ := dbtestutil.NewDB(t)
|
||||
ctx := testutil.Context(t, testutil.WaitShort)
|
||||
store := &countingWebpushStore{Store: rawStore}
|
||||
var delivered atomic.Int32
|
||||
manager, _, serverURL := setupPushTestWithOptions(ctx, t, store, func(w http.ResponseWriter, r *http.Request) {
|
||||
@@ -443,9 +435,9 @@ func TestPush(t *testing.T) {
|
||||
t.Run("PrunesStaleSubscriptionsFromCache", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
ctx := testutil.Context(t, testutil.WaitShort)
|
||||
clock := quartz.NewMock(t)
|
||||
rawStore, _ := dbtestutil.NewDB(t)
|
||||
ctx := testutil.Context(t, testutil.WaitShort)
|
||||
store := &countingWebpushStore{Store: rawStore}
|
||||
var okCalls atomic.Int32
|
||||
var goneCalls atomic.Int32
|
||||
@@ -512,9 +504,8 @@ func TestPush(t *testing.T) {
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
ctx := testutil.Context(t, testutil.WaitShort)
|
||||
var written atomic.Int64
|
||||
manager, store, serverURL := setupPushTest(ctx, t, func(w http.ResponseWriter, r *http.Request) {
|
||||
ctx, manager, store, serverURL := setupPushTest(t, func(w http.ResponseWriter, r *http.Request) {
|
||||
assertWebpushPayload(t, r)
|
||||
w.WriteHeader(tc.statusCode)
|
||||
flusher, _ := w.(http.Flusher)
|
||||
@@ -563,7 +554,6 @@ func TestPush(t *testing.T) {
|
||||
// is read.
|
||||
t.Run("HeaderFloodIsRejected", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
ctx := testutil.Context(t, testutil.WaitShort)
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
assertWebpushPayload(t, r)
|
||||
w.Header().Set("X-Padding", strings.Repeat("x", 1<<20))
|
||||
@@ -576,6 +566,7 @@ func TestPush(t *testing.T) {
|
||||
transport.MaxResponseHeaderBytes = 64 << 10
|
||||
|
||||
db, _ := dbtestutil.NewDB(t)
|
||||
ctx := testutil.Context(t, testutil.WaitShort)
|
||||
logger := slogtest.Make(t, &slogtest.Options{IgnoreErrors: true}).Leveled(slog.LevelDebug)
|
||||
manager, err := webpush.New(ctx, &logger, db, "http://example.com", webpush.WithHTTPClient(client))
|
||||
require.NoError(t, err)
|
||||
@@ -602,7 +593,6 @@ func TestPush(t *testing.T) {
|
||||
// connection and never responds. The client timeout must end the call.
|
||||
t.Run("StalledServerTimesOut", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
ctx := testutil.Context(t, testutil.WaitShort)
|
||||
release := make(chan struct{})
|
||||
server := httptest.NewServer(http.HandlerFunc(func(_ http.ResponseWriter, _ *http.Request) {
|
||||
<-release
|
||||
@@ -615,6 +605,7 @@ func TestPush(t *testing.T) {
|
||||
client.Timeout = time.Second
|
||||
|
||||
db, _ := dbtestutil.NewDB(t)
|
||||
ctx := testutil.Context(t, testutil.WaitShort)
|
||||
logger := slogtest.Make(t, &slogtest.Options{IgnoreErrors: true}).Leveled(slog.LevelDebug)
|
||||
manager, err := webpush.New(ctx, &logger, db, "http://example.com", webpush.WithHTTPClient(client))
|
||||
require.NoError(t, err)
|
||||
@@ -660,11 +651,14 @@ func assertWebpushPayload(t testing.TB, r *http.Request) {
|
||||
|
||||
// setupPushTest creates a common test setup for webpush notification tests.
|
||||
// The test HTTP client bypasses SSRF protection so that httptest.Server
|
||||
// (bound to 127.0.0.1) can be reached.
|
||||
func setupPushTest(ctx context.Context, t *testing.T, handlerFunc func(w http.ResponseWriter, r *http.Request)) (webpush.Dispatcher, database.Store, string) {
|
||||
// (bound to 127.0.0.1) can be reached. It returns the context so that the
|
||||
// deadline starts after the database exists.
|
||||
func setupPushTest(t *testing.T, handlerFunc func(w http.ResponseWriter, r *http.Request)) (context.Context, webpush.Dispatcher, database.Store, string) {
|
||||
t.Helper()
|
||||
db, _ := dbtestutil.NewDB(t)
|
||||
return setupPushTestWithOptions(ctx, t, db, handlerFunc)
|
||||
ctx := testutil.Context(t, testutil.WaitShort)
|
||||
manager, store, serverURL := setupPushTestWithOptions(ctx, t, db, handlerFunc)
|
||||
return ctx, manager, store, serverURL
|
||||
}
|
||||
|
||||
func setupPushTestWithOptions(ctx context.Context, t *testing.T, db database.Store, handlerFunc func(w http.ResponseWriter, r *http.Request), opts ...webpush.Option) (webpush.Dispatcher, database.Store, string) {
|
||||
@@ -707,8 +701,6 @@ func TestNoopWebpusher(t *testing.T) {
|
||||
func TestSSRFPrevention(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
ctx := testutil.Context(t, testutil.WaitShort)
|
||||
|
||||
// Start a server that records whether it received a request.
|
||||
var received atomic.Bool
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
|
||||
@@ -720,6 +712,7 @@ func TestSSRFPrevention(t *testing.T) {
|
||||
// Create a dispatcher via New() WITHOUT WithHTTPClient so it
|
||||
// uses the default SSRF-safe client that blocks loopback.
|
||||
db, _ := dbtestutil.NewDB(t)
|
||||
ctx := testutil.Context(t, testutil.WaitShort)
|
||||
logger := slogtest.Make(t, &slogtest.Options{IgnoreErrors: true}).Leveled(slog.LevelDebug)
|
||||
manager, err := webpush.New(ctx, &logger, db, "http://example.com")
|
||||
require.NoError(t, err)
|
||||
|
||||
@@ -390,12 +390,12 @@ func (tc testCase) run(t *testing.T) {
|
||||
}
|
||||
})
|
||||
clock := quartz.NewMock(t)
|
||||
ctx := testutil.Context(t, testutil.WaitShort)
|
||||
cfg := codersdk.PrebuildsConfig{}
|
||||
logger := slogtest.Make(
|
||||
t, &slogtest.Options{IgnoreErrors: true},
|
||||
).Leveled(slog.LevelDebug)
|
||||
db, pubSub := dbtestutil.NewDB(t)
|
||||
ctx := testutil.Context(t, testutil.WaitShort)
|
||||
|
||||
ownerID := uuid.New()
|
||||
dbgen.User(t, db, database.User{
|
||||
|
||||
Reference in new issue
Block a user