This commit is contained in:
@@ -0,0 +1,56 @@
|
||||
package onboarding
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/itworx/pulse/internal/alert"
|
||||
"github.com/itworx/pulse/internal/alertdefaults"
|
||||
"github.com/itworx/pulse/internal/dashboard"
|
||||
"github.com/itworx/pulse/internal/database"
|
||||
"github.com/itworx/pulse/internal/metriccatalog"
|
||||
)
|
||||
|
||||
func TestPostgreSQLOnboardingIsResumableAndIdempotent(t *testing.T) {
|
||||
dsn := os.Getenv("PULSE_TEST_DATABASE_URL")
|
||||
if dsn == "" {
|
||||
t.Skip("PULSE_TEST_DATABASE_URL is not set")
|
||||
}
|
||||
ctx := context.Background()
|
||||
pool, err := database.NewPool(ctx, database.Config{URL: dsn})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer pool.Close()
|
||||
if err := database.Migrate(ctx, pool); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
registry, err := metriccatalog.DefaultRegistry()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
alerts := alert.Repository{Pool: pool, Registry: registry}
|
||||
if _, err := alertdefaults.Seed(ctx, alerts, registry, "system-defaults"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
service := Service{State: StateStore{Pool: pool}, Pool: pool, Dashboards: dashboard.Repository{Pool: pool}, Alerts: alerts, AuthMode: "mock"}
|
||||
first, err := service.Complete(ctx, Choice{Dashboard: "default", Rules: "default"})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
second, err := service.Complete(ctx, Choice{Dashboard: "default", Rules: "default"})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !first.State.Completed || !second.State.Completed || first.State.DashboardID == "" || first.State.DashboardID != second.State.DashboardID {
|
||||
t.Fatalf("non-idempotent completion: first=%+v second=%+v", first.State, second.State)
|
||||
}
|
||||
var dashboards int
|
||||
if err := pool.QueryRow(ctx, `SELECT count(*) FROM dashboards WHERE slug=$1 AND archived_at IS NULL`, DefaultSlug).Scan(&dashboards); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if dashboards != 1 {
|
||||
t.Fatalf("default dashboard count=%d, want 1", dashboards)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user