Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
test(coderd): enable custom dbrolluper in insights tests
  • Loading branch information
mafredri committed Mar 22, 2024
commit 60d812db1b94d3c4c3391dd04780969167fe005b
86 changes: 71 additions & 15 deletions coderd/insights_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"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/dbrollup"
"github.com/coder/coder/v2/coderd/database/dbtestutil"
"github.com/coder/coder/v2/coderd/rbac"
"github.com/coder/coder/v2/coderd/workspaceapps"
Expand All @@ -44,10 +45,19 @@ func TestDeploymentInsights(t *testing.T) {
clientTz, err := time.LoadLocation("America/Chicago")
require.NoError(t, err)

db, ps := dbtestutil.NewDB(t)
logger := slogtest.Make(t, nil)
client := coderdtest.New(t, &coderdtest.Options{
IncludeProvisionerDaemon: true,
AgentStatsRefreshInterval: time.Millisecond * 100,
MetricsCacheRefreshInterval: time.Millisecond * 100,
Database: db,
Pubsub: ps,
Logger: &logger,
IncludeProvisionerDaemon: true,
AgentStatsRefreshInterval: time.Millisecond * 50,
DatabaseRolluper: dbrollup.New(
logger.Named("dbrollup"),
db,
dbrollup.WithInterval(time.Millisecond*100),
),
})

user := coderdtest.CreateFirstUser(t, client)
Expand Down Expand Up @@ -119,10 +129,19 @@ func TestDeploymentInsights(t *testing.T) {
func TestUserActivityInsights_SanityCheck(t *testing.T) {
t.Parallel()

db, ps := dbtestutil.NewDB(t)
logger := slogtest.Make(t, nil)
client := coderdtest.New(t, &coderdtest.Options{
Database: db,
Pubsub: ps,
Logger: &logger,
IncludeProvisionerDaemon: true,
AgentStatsRefreshInterval: time.Millisecond * 100,
AgentStatsRefreshInterval: time.Millisecond * 50,
DatabaseRolluper: dbrollup.New(
logger.Named("dbrollup"),
db,
dbrollup.WithInterval(time.Millisecond*100),
),
})

// Create two users, one that will appear in the report and another that
Expand Down Expand Up @@ -207,10 +226,19 @@ func TestUserActivityInsights_SanityCheck(t *testing.T) {
func TestUserLatencyInsights(t *testing.T) {
t.Parallel()

db, ps := dbtestutil.NewDB(t)
logger := slogtest.Make(t, nil)
client := coderdtest.New(t, &coderdtest.Options{
Database: db,
Pubsub: ps,
Logger: &logger,
IncludeProvisionerDaemon: true,
AgentStatsRefreshInterval: time.Millisecond * 100,
AgentStatsRefreshInterval: time.Millisecond * 50,
DatabaseRolluper: dbrollup.New(
logger.Named("dbrollup"),
db,
dbrollup.WithInterval(time.Millisecond*100),
),
})

// Create two users, one that will appear in the report and another that
Expand Down Expand Up @@ -474,16 +502,24 @@ func TestTemplateInsights_Golden(t *testing.T) {
return templates, users, testData
}

prepare := func(t *testing.T, templates []*testTemplate, users []*testUser, testData map[*testWorkspace]testDataGen) *codersdk.Client {
prepare := func(t *testing.T, templates []*testTemplate, users []*testUser, testData map[*testWorkspace]testDataGen) (*codersdk.Client, chan dbrollup.Event) {
logger := slogtest.Make(t, &slogtest.Options{IgnoreErrors: false}).Leveled(slog.LevelDebug)
db, pubsub := dbtestutil.NewDB(t)
db, ps := dbtestutil.NewDB(t)
events := make(chan dbrollup.Event)
client := coderdtest.New(t, &coderdtest.Options{
Database: db,
Pubsub: pubsub,
Pubsub: ps,
Logger: &logger,
IncludeProvisionerDaemon: true,
AgentStatsRefreshInterval: time.Hour, // Not relevant for this test.
DatabaseRolluper: dbrollup.New(
logger.Named("dbrollup"),
db,
dbrollup.WithInterval(time.Millisecond*50),
dbrollup.WithEventChannel(events),
),
})

firstUser := coderdtest.CreateFirstUser(t, client)

// Prepare all test users.
Expand Down Expand Up @@ -706,7 +742,7 @@ func TestTemplateInsights_Golden(t *testing.T) {
err = reporter.Report(dbauthz.AsSystemRestricted(ctx), stats)
require.NoError(t, err, "want no error inserting app stats")

return client
return client, events
}

baseTemplateAndUserFixture := func() ([]*testTemplate, []*testUser) {
Expand Down Expand Up @@ -1200,7 +1236,7 @@ func TestTemplateInsights_Golden(t *testing.T) {
require.NotNil(t, tt.makeFixture, "test bug: makeFixture must be set")
require.NotNil(t, tt.makeTestData, "test bug: makeTestData must be set")
templates, users, testData := prepareFixtureAndTestData(t, tt.makeFixture, tt.makeTestData)
client := prepare(t, templates, users, testData)
client, events := prepare(t, templates, users, testData)

for _, req := range tt.requests {
req := req
Expand All @@ -1209,6 +1245,11 @@ func TestTemplateInsights_Golden(t *testing.T) {

ctx := testutil.Context(t, testutil.WaitMedium)

// Drain two events, the first one resumes rolluper
// operation and the second one waits for the rollup
// to complete.
_, _ = <-events, <-events

report, err := client.TemplateInsights(ctx, req.makeRequest(templates))
require.NoError(t, err, "want no error getting template insights")

Expand Down Expand Up @@ -1381,15 +1422,22 @@ func TestUserActivityInsights_Golden(t *testing.T) {
return templates, users, testData
}

prepare := func(t *testing.T, templates []*testTemplate, users []*testUser, testData map[*testWorkspace]testDataGen) *codersdk.Client {
prepare := func(t *testing.T, templates []*testTemplate, users []*testUser, testData map[*testWorkspace]testDataGen) (*codersdk.Client, chan dbrollup.Event) {
logger := slogtest.Make(t, &slogtest.Options{IgnoreErrors: false}).Leveled(slog.LevelDebug)
db, pubsub := dbtestutil.NewDB(t)
db, ps := dbtestutil.NewDB(t)
events := make(chan dbrollup.Event)
client := coderdtest.New(t, &coderdtest.Options{
Database: db,
Pubsub: pubsub,
Pubsub: ps,
Logger: &logger,
IncludeProvisionerDaemon: true,
AgentStatsRefreshInterval: time.Hour, // Not relevant for this test.
DatabaseRolluper: dbrollup.New(
logger.Named("dbrollup"),
db,
dbrollup.WithInterval(time.Millisecond*50),
dbrollup.WithEventChannel(events),
),
})
firstUser := coderdtest.CreateFirstUser(t, client)

Expand Down Expand Up @@ -1593,7 +1641,7 @@ func TestUserActivityInsights_Golden(t *testing.T) {
err = reporter.Report(dbauthz.AsSystemRestricted(ctx), stats)
require.NoError(t, err, "want no error inserting app stats")

return client
return client, events
}

baseTemplateAndUserFixture := func() ([]*testTemplate, []*testUser) {
Expand Down Expand Up @@ -1974,7 +2022,10 @@ func TestUserActivityInsights_Golden(t *testing.T) {
require.NotNil(t, tt.makeFixture, "test bug: makeFixture must be set")
require.NotNil(t, tt.makeTestData, "test bug: makeTestData must be set")
templates, users, testData := prepareFixtureAndTestData(t, tt.makeFixture, tt.makeTestData)
client := prepare(t, templates, users, testData)
client, events := prepare(t, templates, users, testData)

// TODO(mafredri): Remove the need for this.
time.Sleep(3 * time.Second)

for _, req := range tt.requests {
req := req
Expand All @@ -1983,6 +2034,11 @@ func TestUserActivityInsights_Golden(t *testing.T) {

ctx := testutil.Context(t, testutil.WaitMedium)

// Drain two events, the first one resumes rolluper
// operation and the second one waits for the rollup
// to complete.
_, _ = <-events, <-events

report, err := client.UserActivityInsights(ctx, req.makeRequest(templates))
require.NoError(t, err, "want no error getting template insights")

Expand Down