chore: add standard test logger ignoring db canceled (#15556)

Refactors our use of `slogtest` to instantiate a "standard logger" across most of our tests.  This standard logger incorporates https://github.com/coder/slog/pull/217 to also ignore database query canceled errors by default, which are a source of low-severity flakes.

Any test that has set non-default `slogtest.Options` is left alone. In particular, `coderdtest` defaults to ignoring all errors. We might consider revisiting that decision now that we have better tools to target the really common flaky Error logs on shutdown.
This commit is contained in:
Spike Curtis
2024-11-18 14:09:22 +04:00
committed by GitHub
parent 4719d2406f
commit 5861e516b9
95 changed files with 443 additions and 501 deletions
+4 -6
View File
@@ -13,15 +13,13 @@ import (
"github.com/stretchr/testify/require"
"golang.org/x/xerrors"
"cdr.dev/slog"
"cdr.dev/slog/sloggers/slogtest"
"github.com/coder/coder/v2/coderd/database/dbtestutil"
"github.com/coder/quartz"
"github.com/coder/serpent"
"github.com/coder/coder/v2/coderd/database"
"github.com/coder/coder/v2/coderd/database/dbauthz"
"github.com/coder/coder/v2/coderd/database/dbgen"
"github.com/coder/coder/v2/coderd/database/dbtestutil"
"github.com/coder/coder/v2/coderd/notifications"
"github.com/coder/coder/v2/coderd/notifications/dispatch"
"github.com/coder/coder/v2/coderd/notifications/types"
@@ -36,7 +34,7 @@ func TestBufferedUpdates(t *testing.T) {
// nolint:gocritic // Unit test.
ctx := dbauthz.AsSystemRestricted(testutil.Context(t, testutil.WaitSuperLong))
store, _ := dbtestutil.NewDB(t)
logger := slogtest.Make(t, nil).Leveled(slog.LevelDebug)
logger := testutil.Logger(t)
interceptor := &syncInterceptor{Store: store}
santa := &santaHandler{}
@@ -108,7 +106,7 @@ func TestBuildPayload(t *testing.T) {
// nolint:gocritic // Unit test.
ctx := dbauthz.AsSystemRestricted(testutil.Context(t, testutil.WaitSuperLong))
store, _ := dbtestutil.NewDB(t)
logger := slogtest.Make(t, nil).Leveled(slog.LevelDebug)
logger := testutil.Logger(t)
// GIVEN: a set of helpers to be injected into the templates
const label = "Click here!"
@@ -166,7 +164,7 @@ func TestStopBeforeRun(t *testing.T) {
// nolint:gocritic // Unit test.
ctx := dbauthz.AsSystemRestricted(testutil.Context(t, testutil.WaitSuperLong))
store, _ := dbtestutil.NewDB(t)
logger := slogtest.Make(t, nil).Leveled(slog.LevelDebug)
logger := testutil.Logger(t)
// GIVEN: a standard manager
mgr, err := notifications.NewManager(defaultNotificationsConfig(database.NotificationMethodSmtp), store, defaultHelpers(), createMetrics(), logger.Named("notifications-manager"))
+4 -6
View File
@@ -16,8 +16,6 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"cdr.dev/slog"
"cdr.dev/slog/sloggers/slogtest"
"github.com/coder/quartz"
"github.com/coder/serpent"
@@ -41,7 +39,7 @@ func TestMetrics(t *testing.T) {
// nolint:gocritic // Unit test.
ctx := dbauthz.AsSystemRestricted(testutil.Context(t, testutil.WaitSuperLong))
store, _ := dbtestutil.NewDB(t)
logger := slogtest.Make(t, nil).Leveled(slog.LevelDebug)
logger := testutil.Logger(t)
reg := prometheus.NewRegistry()
metrics := notifications.NewMetrics(reg)
@@ -215,7 +213,7 @@ func TestPendingUpdatesMetric(t *testing.T) {
// nolint:gocritic // Unit test.
ctx := dbauthz.AsSystemRestricted(testutil.Context(t, testutil.WaitSuperLong))
store, _ := dbtestutil.NewDB(t)
logger := slogtest.Make(t, nil).Leveled(slog.LevelDebug)
logger := testutil.Logger(t)
reg := prometheus.NewRegistry()
metrics := notifications.NewMetrics(reg)
@@ -306,7 +304,7 @@ func TestInflightDispatchesMetric(t *testing.T) {
// nolint:gocritic // Unit test.
ctx := dbauthz.AsSystemRestricted(testutil.Context(t, testutil.WaitSuperLong))
store, _ := dbtestutil.NewDB(t)
logger := slogtest.Make(t, nil).Leveled(slog.LevelDebug)
logger := testutil.Logger(t)
reg := prometheus.NewRegistry()
metrics := notifications.NewMetrics(reg)
@@ -385,7 +383,7 @@ func TestCustomMethodMetricCollection(t *testing.T) {
// nolint:gocritic // Unit test.
ctx := dbauthz.AsSystemRestricted(testutil.Context(t, testutil.WaitSuperLong))
store, _ := dbtestutil.NewDB(t)
logger := slogtest.Make(t, nil).Leveled(slog.LevelDebug)
logger := testutil.Logger(t)
var (
reg = prometheus.NewRegistry()
+12 -13
View File
@@ -35,7 +35,6 @@ import (
"golang.org/x/xerrors"
"cdr.dev/slog"
"cdr.dev/slog/sloggers/slogtest"
"github.com/coder/coder/v2/coderd/coderdtest"
"github.com/coder/coder/v2/coderd/database"
"github.com/coder/coder/v2/coderd/database/dbauthz"
@@ -73,7 +72,7 @@ func TestBasicNotificationRoundtrip(t *testing.T) {
// nolint:gocritic // Unit test.
ctx := dbauthz.AsNotifier(testutil.Context(t, testutil.WaitSuperLong))
store, _ := dbtestutil.NewDB(t)
logger := slogtest.Make(t, nil).Leveled(slog.LevelDebug)
logger := testutil.Logger(t)
method := database.NotificationMethodSmtp
// GIVEN: a manager with standard config but a faked dispatch handler
@@ -137,7 +136,7 @@ func TestSMTPDispatch(t *testing.T) {
// nolint:gocritic // Unit test.
ctx := dbauthz.AsNotifier(testutil.Context(t, testutil.WaitSuperLong))
store, _ := dbtestutil.NewDB(t)
logger := slogtest.Make(t, nil).Leveled(slog.LevelDebug)
logger := testutil.Logger(t)
// start mock SMTP server
mockSMTPSrv := smtpmock.New(smtpmock.ConfigurationAttr{
@@ -199,7 +198,7 @@ func TestWebhookDispatch(t *testing.T) {
// nolint:gocritic // Unit test.
ctx := dbauthz.AsNotifier(testutil.Context(t, testutil.WaitSuperLong))
store, _ := dbtestutil.NewDB(t)
logger := slogtest.Make(t, nil).Leveled(slog.LevelDebug)
logger := testutil.Logger(t)
sent := make(chan dispatch.WebhookPayload, 1)
// Mock server to simulate webhook endpoint.
@@ -279,7 +278,7 @@ func TestBackpressure(t *testing.T) {
}
store, _ := dbtestutil.NewDB(t)
logger := slogtest.Make(t, nil).Leveled(slog.LevelDebug)
logger := testutil.Logger(t)
// nolint:gocritic // Unit test.
ctx := dbauthz.AsNotifier(testutil.Context(t, testutil.WaitShort))
@@ -409,7 +408,7 @@ func TestRetries(t *testing.T) {
// nolint:gocritic // Unit test.
ctx := dbauthz.AsNotifier(testutil.Context(t, testutil.WaitSuperLong))
store, _ := dbtestutil.NewDB(t)
logger := slogtest.Make(t, nil).Leveled(slog.LevelDebug)
logger := testutil.Logger(t)
// GIVEN: a mock HTTP server which will receive webhooksand a map to track the dispatch attempts
@@ -503,7 +502,7 @@ func TestExpiredLeaseIsRequeued(t *testing.T) {
// nolint:gocritic // Unit test.
ctx := dbauthz.AsNotifier(testutil.Context(t, testutil.WaitSuperLong))
store, _ := dbtestutil.NewDB(t)
logger := slogtest.Make(t, nil).Leveled(slog.LevelDebug)
logger := testutil.Logger(t)
// GIVEN: a manager which has its updates intercepted and paused until measurements can be taken
@@ -602,7 +601,7 @@ func TestInvalidConfig(t *testing.T) {
t.Parallel()
store, _ := dbtestutil.NewDB(t)
logger := slogtest.Make(t, nil).Leveled(slog.LevelDebug)
logger := testutil.Logger(t)
// GIVEN: invalid config with dispatch period <= lease period
const (
@@ -628,7 +627,7 @@ func TestNotifierPaused(t *testing.T) {
// nolint:gocritic // Unit test.
ctx := dbauthz.AsNotifier(testutil.Context(t, testutil.WaitSuperLong))
store, _ := dbtestutil.NewDB(t)
logger := slogtest.Make(t, nil).Leveled(slog.LevelDebug)
logger := testutil.Logger(t)
// Prepare the test.
handler := &fakeHandler{}
@@ -1380,7 +1379,7 @@ func TestDisabledBeforeEnqueue(t *testing.T) {
// nolint:gocritic // Unit test.
ctx := dbauthz.AsNotifier(testutil.Context(t, testutil.WaitSuperLong))
store, _ := dbtestutil.NewDB(t)
logger := slogtest.Make(t, nil).Leveled(slog.LevelDebug)
logger := testutil.Logger(t)
// GIVEN: an enqueuer & a sample user
cfg := defaultNotificationsConfig(database.NotificationMethodSmtp)
@@ -1416,7 +1415,7 @@ func TestDisabledAfterEnqueue(t *testing.T) {
// nolint:gocritic // Unit test.
ctx := dbauthz.AsNotifier(testutil.Context(t, testutil.WaitSuperLong))
store, _ := dbtestutil.NewDB(t)
logger := slogtest.Make(t, nil).Leveled(slog.LevelDebug)
logger := testutil.Logger(t)
method := database.NotificationMethodSmtp
cfg := defaultNotificationsConfig(method)
@@ -1473,7 +1472,7 @@ func TestCustomNotificationMethod(t *testing.T) {
// nolint:gocritic // Unit test.
ctx := dbauthz.AsNotifier(testutil.Context(t, testutil.WaitSuperLong))
store, _ := dbtestutil.NewDB(t)
logger := slogtest.Make(t, nil).Leveled(slog.LevelDebug)
logger := testutil.Logger(t)
received := make(chan uuid.UUID, 1)
@@ -1614,7 +1613,7 @@ func TestNotificationDuplicates(t *testing.T) {
// nolint:gocritic // Unit test.
ctx := dbauthz.AsNotifier(testutil.Context(t, testutil.WaitSuperLong))
store, _ := dbtestutil.NewDB(t)
logger := slogtest.Make(t, nil).Leveled(slog.LevelDebug)
logger := testutil.Logger(t)
method := database.NotificationMethodSmtp
cfg := defaultNotificationsConfig(method)