feat(alerting): add monitor alert groups with default routing
This commit is contained in:
1 parent
52819e64bb
commit
8544e5a585
26 files changed
+2938
-1473
No files matched your search
@@ -0,0 +1,72 @@
|
||||
package migrations
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/jackc/pgx/v5"
|
||||
)
|
||||
|
||||
func TestAlertGroupsBackfill(t *testing.T) {
|
||||
dsn := os.Getenv("UPTIME_TEST_DATABASE_URL")
|
||||
if dsn == "" {
|
||||
t.Skip("UPTIME_TEST_DATABASE_URL not set")
|
||||
}
|
||||
ctx := context.Background()
|
||||
if _, err := Up(ctx, dsn); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
conn, err := pgx.Connect(ctx, dsn)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer func() { _ = conn.Close(ctx) }()
|
||||
tx, err := conn.Begin(ctx)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// Exercise both directions inside a transaction; existing test data and
|
||||
// goose's migration history remain unchanged after rollback.
|
||||
defer func() { _ = tx.Rollback(ctx) }()
|
||||
if _, err := tx.Exec(ctx, "SET LOCAL app.scope = 'system'"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
raw, err := files.ReadFile("sql/00039_alert_groups.sql")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
up, down, ok := strings.Cut(string(raw), "-- +goose Down")
|
||||
if !ok {
|
||||
t.Fatal("missing down migration")
|
||||
}
|
||||
if _, err := tx.Exec(ctx, down); err != nil {
|
||||
t.Fatalf("down: %v", err)
|
||||
}
|
||||
orgID := uuid.New()
|
||||
if _, err := tx.Exec(ctx, `INSERT INTO orgs (id,slug,name) VALUES ($1,$2,'Existing organization')`, orgID, "migration-"+orgID.String()); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := tx.Exec(ctx, `INSERT INTO monitors (org_id,name,type,target,interval_seconds,timeout_ms) VALUES ($1,'Existing monitor','tcp','example.test:443',60,1000)`, orgID); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := tx.Exec(ctx, `INSERT INTO notification_channels (org_id,name,type,config) VALUES ($1,'Existing channel','webhook','{}')`, orgID); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := tx.Exec(ctx, up); err != nil {
|
||||
t.Fatalf("up: %v", err)
|
||||
}
|
||||
var groups, monitors, channels int
|
||||
err = tx.QueryRow(ctx, `SELECT
|
||||
(SELECT count(*) FROM alert_groups WHERE org_id=$1 AND is_default),
|
||||
(SELECT count(*) FROM monitor_alert_groups WHERE org_id=$1),
|
||||
(SELECT count(*) FROM alert_group_channels WHERE org_id=$1)`, orgID).Scan(&groups, &monitors, &channels)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if groups != 1 || monitors != 1 || channels != 1 {
|
||||
t.Fatalf("backfilled groups/monitors/channels = %d/%d/%d, want 1/1/1", groups, monitors, channels)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user