This commit is contained in:
@@ -0,0 +1,64 @@
|
||||
package servicedefaults
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/netip"
|
||||
"os"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/itworx/pulse/internal/database"
|
||||
"github.com/itworx/pulse/internal/probe"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
)
|
||||
|
||||
func TestSeedPostgreSQLIsAtomicAndIdempotent(t *testing.T) {
|
||||
dsn := os.Getenv("PULSE_TEST_DATABASE_URL")
|
||||
if dsn == "" {
|
||||
t.Skip("PULSE_TEST_DATABASE_URL is not set")
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 90*time.Second)
|
||||
defer cancel()
|
||||
pool, err := database.NewPool(ctx, database.Config{URL: dsn, MaxConns: 4, MinConns: 1})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer pool.Close()
|
||||
if err := database.Migrate(ctx, pool); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
resolve := resolver{"pulse.example": {netip.MustParseAddr("203.0.113.10")}, "auth.example": {netip.MustParseAddr("198.51.100.20")}}
|
||||
options := Options{PublicURL: "https://pulse.example", OIDCIssuer: "https://auth.example/application/o/pulse/"}
|
||||
for run := 0; run < 2; run++ {
|
||||
summary, err := Seed(ctx, pool, options, probe.NetworkPolicy{}, resolve)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if summary != (Summary{Services: 2, Endpoints: 2, Probes: 3, Dependencies: 1}) {
|
||||
t.Fatalf("unexpected summary: %#v", summary)
|
||||
}
|
||||
}
|
||||
assertCount(t, ctx, pool, `SELECT count(*) FROM services WHERE labels->>'managedBy'='pulse-system-defaults'`, 2)
|
||||
assertCount(t, ctx, pool, `SELECT count(*) FROM probes WHERE service_id IN ($1,$2) AND archived_at IS NULL`, 3, stableID("service:pulse"), stableID("service:authentik"))
|
||||
assertCount(t, ctx, pool, `SELECT count(*) FROM service_dependencies WHERE service_id=$1 AND depends_on_service_id=$2 AND confirmed=true`, 1, stableID("service:pulse"), stableID("service:authentik"))
|
||||
assertCount(t, ctx, pool, `SELECT count(*) FROM services WHERE id IN ($1,$2) AND revision=1`, 2, stableID("service:pulse"), stableID("service:authentik"))
|
||||
assertCount(t, ctx, pool, `SELECT count(*) FROM probes WHERE service_id IN ($1,$2) AND revision=1`, 3, stableID("service:pulse"), stableID("service:authentik"))
|
||||
|
||||
// Invalid replacement configuration fails before the transaction and leaves
|
||||
// the accepted baseline unchanged.
|
||||
if _, err := Seed(ctx, pool, Options{PublicURL: "https://10.0.0.2", OIDCIssuer: options.OIDCIssuer}, probe.NetworkPolicy{}, resolve); err == nil {
|
||||
t.Fatal("expected blocked private replacement")
|
||||
}
|
||||
assertCount(t, ctx, pool, `SELECT count(*) FROM probes WHERE service_id IN ($1,$2) AND archived_at IS NULL`, 3, stableID("service:pulse"), stableID("service:authentik"))
|
||||
}
|
||||
|
||||
func assertCount(t *testing.T, ctx context.Context, pool *pgxpool.Pool, query string, expected int, args ...any) {
|
||||
t.Helper()
|
||||
var count int
|
||||
if err := pool.QueryRow(ctx, query, args...).Scan(&count); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if count != expected {
|
||||
t.Fatalf("count=%d, want %d for %s", count, expected, query)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user