mirror of
https://github.com/coder/coder.git
synced 2026-09-24 15:04:27 +08:00
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:
@@ -8,13 +8,12 @@ import (
|
||||
"github.com/stretchr/testify/require"
|
||||
"go.uber.org/mock/gomock"
|
||||
|
||||
"cdr.dev/slog/sloggers/slogtest"
|
||||
|
||||
agentproto "github.com/coder/coder/v2/agent/proto"
|
||||
"github.com/coder/coder/v2/coderd/agentapi"
|
||||
"github.com/coder/coder/v2/coderd/database"
|
||||
"github.com/coder/coder/v2/coderd/database/dbmock"
|
||||
"github.com/coder/coder/v2/coderd/wspubsub"
|
||||
"github.com/coder/coder/v2/testutil"
|
||||
)
|
||||
|
||||
func TestBatchUpdateAppHealths(t *testing.T) {
|
||||
@@ -62,7 +61,7 @@ func TestBatchUpdateAppHealths(t *testing.T) {
|
||||
return agent, nil
|
||||
},
|
||||
Database: dbM,
|
||||
Log: slogtest.Make(t, nil),
|
||||
Log: testutil.Logger(t),
|
||||
PublishWorkspaceUpdateFn: func(ctx context.Context, wa *database.WorkspaceAgent, kind wspubsub.WorkspaceEventKind) error {
|
||||
publishCalled = true
|
||||
return nil
|
||||
@@ -100,7 +99,7 @@ func TestBatchUpdateAppHealths(t *testing.T) {
|
||||
return agent, nil
|
||||
},
|
||||
Database: dbM,
|
||||
Log: slogtest.Make(t, nil),
|
||||
Log: testutil.Logger(t),
|
||||
PublishWorkspaceUpdateFn: func(ctx context.Context, wa *database.WorkspaceAgent, kind wspubsub.WorkspaceEventKind) error {
|
||||
publishCalled = true
|
||||
return nil
|
||||
@@ -139,7 +138,7 @@ func TestBatchUpdateAppHealths(t *testing.T) {
|
||||
return agent, nil
|
||||
},
|
||||
Database: dbM,
|
||||
Log: slogtest.Make(t, nil),
|
||||
Log: testutil.Logger(t),
|
||||
PublishWorkspaceUpdateFn: func(ctx context.Context, wa *database.WorkspaceAgent, kind wspubsub.WorkspaceEventKind) error {
|
||||
publishCalled = true
|
||||
return nil
|
||||
@@ -174,7 +173,7 @@ func TestBatchUpdateAppHealths(t *testing.T) {
|
||||
return agent, nil
|
||||
},
|
||||
Database: dbM,
|
||||
Log: slogtest.Make(t, nil),
|
||||
Log: testutil.Logger(t),
|
||||
PublishWorkspaceUpdateFn: nil,
|
||||
}
|
||||
|
||||
@@ -203,7 +202,7 @@ func TestBatchUpdateAppHealths(t *testing.T) {
|
||||
return agent, nil
|
||||
},
|
||||
Database: dbM,
|
||||
Log: slogtest.Make(t, nil),
|
||||
Log: testutil.Logger(t),
|
||||
PublishWorkspaceUpdateFn: nil,
|
||||
}
|
||||
|
||||
@@ -233,7 +232,7 @@ func TestBatchUpdateAppHealths(t *testing.T) {
|
||||
return agent, nil
|
||||
},
|
||||
Database: dbM,
|
||||
Log: slogtest.Make(t, nil),
|
||||
Log: testutil.Logger(t),
|
||||
PublishWorkspaceUpdateFn: nil,
|
||||
}
|
||||
|
||||
|
||||
@@ -13,13 +13,13 @@ import (
|
||||
"go.uber.org/mock/gomock"
|
||||
"google.golang.org/protobuf/types/known/timestamppb"
|
||||
|
||||
"cdr.dev/slog/sloggers/slogtest"
|
||||
agentproto "github.com/coder/coder/v2/agent/proto"
|
||||
"github.com/coder/coder/v2/coderd/agentapi"
|
||||
"github.com/coder/coder/v2/coderd/database"
|
||||
"github.com/coder/coder/v2/coderd/database/dbmock"
|
||||
"github.com/coder/coder/v2/coderd/database/dbtime"
|
||||
"github.com/coder/coder/v2/coderd/wspubsub"
|
||||
"github.com/coder/coder/v2/testutil"
|
||||
)
|
||||
|
||||
func TestUpdateLifecycle(t *testing.T) {
|
||||
@@ -72,7 +72,7 @@ func TestUpdateLifecycle(t *testing.T) {
|
||||
},
|
||||
WorkspaceID: workspaceID,
|
||||
Database: dbM,
|
||||
Log: slogtest.Make(t, nil),
|
||||
Log: testutil.Logger(t),
|
||||
PublishWorkspaceUpdateFn: func(ctx context.Context, agent *database.WorkspaceAgent, kind wspubsub.WorkspaceEventKind) error {
|
||||
publishCalled = true
|
||||
return nil
|
||||
@@ -112,7 +112,7 @@ func TestUpdateLifecycle(t *testing.T) {
|
||||
},
|
||||
WorkspaceID: workspaceID,
|
||||
Database: dbM,
|
||||
Log: slogtest.Make(t, nil),
|
||||
Log: testutil.Logger(t),
|
||||
// Test that nil publish fn works.
|
||||
PublishWorkspaceUpdateFn: nil,
|
||||
}
|
||||
@@ -155,7 +155,7 @@ func TestUpdateLifecycle(t *testing.T) {
|
||||
},
|
||||
WorkspaceID: workspaceID,
|
||||
Database: dbM,
|
||||
Log: slogtest.Make(t, nil),
|
||||
Log: testutil.Logger(t),
|
||||
PublishWorkspaceUpdateFn: func(ctx context.Context, agent *database.WorkspaceAgent, kind wspubsub.WorkspaceEventKind) error {
|
||||
publishCalled = true
|
||||
return nil
|
||||
@@ -201,7 +201,7 @@ func TestUpdateLifecycle(t *testing.T) {
|
||||
},
|
||||
WorkspaceID: workspaceID,
|
||||
Database: dbM,
|
||||
Log: slogtest.Make(t, nil),
|
||||
Log: testutil.Logger(t),
|
||||
PublishWorkspaceUpdateFn: nil,
|
||||
TimeNowFn: func() time.Time {
|
||||
return now
|
||||
@@ -234,7 +234,7 @@ func TestUpdateLifecycle(t *testing.T) {
|
||||
},
|
||||
WorkspaceID: workspaceID,
|
||||
Database: dbM,
|
||||
Log: slogtest.Make(t, nil),
|
||||
Log: testutil.Logger(t),
|
||||
PublishWorkspaceUpdateFn: func(ctx context.Context, agent *database.WorkspaceAgent, kind wspubsub.WorkspaceEventKind) error {
|
||||
atomic.AddInt64(&publishCalled, 1)
|
||||
return nil
|
||||
@@ -307,7 +307,7 @@ func TestUpdateLifecycle(t *testing.T) {
|
||||
},
|
||||
WorkspaceID: workspaceID,
|
||||
Database: dbM,
|
||||
Log: slogtest.Make(t, nil),
|
||||
Log: testutil.Logger(t),
|
||||
PublishWorkspaceUpdateFn: func(ctx context.Context, agent *database.WorkspaceAgent, kind wspubsub.WorkspaceEventKind) error {
|
||||
publishCalled = true
|
||||
return nil
|
||||
@@ -345,7 +345,7 @@ func TestUpdateStartup(t *testing.T) {
|
||||
},
|
||||
WorkspaceID: workspaceID,
|
||||
Database: dbM,
|
||||
Log: slogtest.Make(t, nil),
|
||||
Log: testutil.Logger(t),
|
||||
// Not used by UpdateStartup.
|
||||
PublishWorkspaceUpdateFn: nil,
|
||||
}
|
||||
@@ -391,7 +391,7 @@ func TestUpdateStartup(t *testing.T) {
|
||||
},
|
||||
WorkspaceID: workspaceID,
|
||||
Database: dbM,
|
||||
Log: slogtest.Make(t, nil),
|
||||
Log: testutil.Logger(t),
|
||||
// Not used by UpdateStartup.
|
||||
PublishWorkspaceUpdateFn: nil,
|
||||
}
|
||||
@@ -422,7 +422,7 @@ func TestUpdateStartup(t *testing.T) {
|
||||
},
|
||||
WorkspaceID: workspaceID,
|
||||
Database: dbM,
|
||||
Log: slogtest.Make(t, nil),
|
||||
Log: testutil.Logger(t),
|
||||
// Not used by UpdateStartup.
|
||||
PublishWorkspaceUpdateFn: nil,
|
||||
}
|
||||
|
||||
@@ -13,7 +13,6 @@ import (
|
||||
"go.uber.org/mock/gomock"
|
||||
"google.golang.org/protobuf/types/known/timestamppb"
|
||||
|
||||
"cdr.dev/slog/sloggers/slogtest"
|
||||
agentproto "github.com/coder/coder/v2/agent/proto"
|
||||
"github.com/coder/coder/v2/coderd/agentapi"
|
||||
"github.com/coder/coder/v2/coderd/database"
|
||||
@@ -21,6 +20,7 @@ import (
|
||||
"github.com/coder/coder/v2/coderd/database/dbtime"
|
||||
"github.com/coder/coder/v2/coderd/wspubsub"
|
||||
"github.com/coder/coder/v2/codersdk/agentsdk"
|
||||
"github.com/coder/coder/v2/testutil"
|
||||
)
|
||||
|
||||
func TestBatchCreateLogs(t *testing.T) {
|
||||
@@ -50,7 +50,7 @@ func TestBatchCreateLogs(t *testing.T) {
|
||||
return agent, nil
|
||||
},
|
||||
Database: dbM,
|
||||
Log: slogtest.Make(t, nil),
|
||||
Log: testutil.Logger(t),
|
||||
PublishWorkspaceUpdateFn: func(ctx context.Context, wa *database.WorkspaceAgent, kind wspubsub.WorkspaceEventKind) error {
|
||||
publishWorkspaceUpdateCalled = true
|
||||
return nil
|
||||
@@ -154,7 +154,7 @@ func TestBatchCreateLogs(t *testing.T) {
|
||||
return agentWithLogs, nil
|
||||
},
|
||||
Database: dbM,
|
||||
Log: slogtest.Make(t, nil),
|
||||
Log: testutil.Logger(t),
|
||||
PublishWorkspaceUpdateFn: func(ctx context.Context, wa *database.WorkspaceAgent, kind wspubsub.WorkspaceEventKind) error {
|
||||
publishWorkspaceUpdateCalled = true
|
||||
return nil
|
||||
@@ -202,7 +202,7 @@ func TestBatchCreateLogs(t *testing.T) {
|
||||
return overflowedAgent, nil
|
||||
},
|
||||
Database: dbM,
|
||||
Log: slogtest.Make(t, nil),
|
||||
Log: testutil.Logger(t),
|
||||
PublishWorkspaceUpdateFn: func(ctx context.Context, wa *database.WorkspaceAgent, kind wspubsub.WorkspaceEventKind) error {
|
||||
publishWorkspaceUpdateCalled = true
|
||||
return nil
|
||||
@@ -233,7 +233,7 @@ func TestBatchCreateLogs(t *testing.T) {
|
||||
return agent, nil
|
||||
},
|
||||
Database: dbM,
|
||||
Log: slogtest.Make(t, nil),
|
||||
Log: testutil.Logger(t),
|
||||
// Test that they are ignored when nil.
|
||||
PublishWorkspaceUpdateFn: nil,
|
||||
PublishWorkspaceAgentLogsUpdateFn: nil,
|
||||
@@ -295,7 +295,7 @@ func TestBatchCreateLogs(t *testing.T) {
|
||||
return agent, nil
|
||||
},
|
||||
Database: dbM,
|
||||
Log: slogtest.Make(t, nil),
|
||||
Log: testutil.Logger(t),
|
||||
PublishWorkspaceUpdateFn: func(ctx context.Context, wa *database.WorkspaceAgent, kind wspubsub.WorkspaceEventKind) error {
|
||||
publishWorkspaceUpdateCalled = true
|
||||
return nil
|
||||
@@ -339,7 +339,7 @@ func TestBatchCreateLogs(t *testing.T) {
|
||||
return agent, nil
|
||||
},
|
||||
Database: dbM,
|
||||
Log: slogtest.Make(t, nil),
|
||||
Log: testutil.Logger(t),
|
||||
PublishWorkspaceUpdateFn: func(ctx context.Context, wa *database.WorkspaceAgent, kind wspubsub.WorkspaceEventKind) error {
|
||||
publishWorkspaceUpdateCalled = true
|
||||
return nil
|
||||
@@ -386,7 +386,7 @@ func TestBatchCreateLogs(t *testing.T) {
|
||||
return agent, nil
|
||||
},
|
||||
Database: dbM,
|
||||
Log: slogtest.Make(t, nil),
|
||||
Log: testutil.Logger(t),
|
||||
PublishWorkspaceUpdateFn: func(ctx context.Context, wa *database.WorkspaceAgent, kind wspubsub.WorkspaceEventKind) error {
|
||||
publishWorkspaceUpdateCalled = true
|
||||
return nil
|
||||
|
||||
@@ -12,14 +12,13 @@ import (
|
||||
"go.uber.org/mock/gomock"
|
||||
"google.golang.org/protobuf/types/known/timestamppb"
|
||||
|
||||
"cdr.dev/slog/sloggers/slogtest"
|
||||
|
||||
agentproto "github.com/coder/coder/v2/agent/proto"
|
||||
"github.com/coder/coder/v2/coderd/agentapi"
|
||||
"github.com/coder/coder/v2/coderd/database"
|
||||
"github.com/coder/coder/v2/coderd/database/dbmock"
|
||||
"github.com/coder/coder/v2/coderd/database/dbtime"
|
||||
"github.com/coder/coder/v2/coderd/database/pubsub"
|
||||
"github.com/coder/coder/v2/testutil"
|
||||
)
|
||||
|
||||
type fakePublisher struct {
|
||||
@@ -87,7 +86,7 @@ func TestBatchUpdateMetadata(t *testing.T) {
|
||||
},
|
||||
Database: dbM,
|
||||
Pubsub: pub,
|
||||
Log: slogtest.Make(t, nil),
|
||||
Log: testutil.Logger(t),
|
||||
TimeNowFn: func() time.Time {
|
||||
return now
|
||||
},
|
||||
@@ -172,7 +171,7 @@ func TestBatchUpdateMetadata(t *testing.T) {
|
||||
},
|
||||
Database: dbM,
|
||||
Pubsub: pub,
|
||||
Log: slogtest.Make(t, nil),
|
||||
Log: testutil.Logger(t),
|
||||
TimeNowFn: func() time.Time {
|
||||
return now
|
||||
},
|
||||
@@ -241,7 +240,7 @@ func TestBatchUpdateMetadata(t *testing.T) {
|
||||
},
|
||||
Database: dbM,
|
||||
Pubsub: pub,
|
||||
Log: slogtest.Make(t, nil),
|
||||
Log: testutil.Logger(t),
|
||||
TimeNowFn: func() time.Time {
|
||||
return now
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user