Files
cloud-ip-validator/internal/db/queries_floating_ips_test.go

171 lines
6.0 KiB
Go

package db
import (
"context"
"database/sql"
"path/filepath"
"testing"
"time"
)
func TestFloatingIPMigrationKeepsHoldsAndHistory(t *testing.T) {
ctx := context.Background()
path := filepath.Join(t.TempDir(), "v16.db")
conn, err := sql.Open("sqlite", dsn(path))
if err != nil {
t.Fatal(err)
}
conn.SetMaxOpenConns(1)
defer conn.Close()
oldDB := &DB{DB: conn}
for _, migration := range migrations {
if migration.version > 16 {
break
}
if _, err := oldDB.ExecContext(ctx, migration.sql); err != nil {
t.Fatalf("migration %d: %v", migration.version, err)
}
}
if _, err := oldDB.ExecContext(ctx, `PRAGMA user_version=16`); err != nil {
t.Fatal(err)
}
active, historical, held, pending := "192.0.2.1", "192.0.2.2", "192.0.2.3", "192.0.2.4"
if err := oldDB.SeedQueue(ctx, []string{active, historical}); err != nil {
t.Fatal(err)
}
if _, err := oldDB.ExecContext(ctx, `
UPDATE ip_queue SET fip_id='queued-uuid' WHERE ip_address=?
`, active); err != nil {
t.Fatal(err)
}
for _, id := range []string{"held-validator", "pending-validator"} {
if err := oldDB.RegisterValidator(ctx, id, "", id+"-port", ""); err != nil {
t.Fatal(err)
}
}
if _, err := oldDB.ExecContext(ctx, `
UPDATE validators SET state='diagnostic' WHERE validator_id IN ('held-validator', 'pending-validator');
INSERT INTO diagnostic_holds (address, validator_id, fip_id, note, created_at)
VALUES (?, 'held-validator', 'held-uuid', 'existing hold', ?);
`, held, timeToDB(Now())); err != nil {
t.Fatal(err)
}
if err := oldDB.CreateDiagnosticOperation(ctx, "pending-operation", "", "hash", []DiagnosticOperationItem{
{Address: pending, RequestedValidator: "pending-validator"},
}); err != nil {
t.Fatal(err)
}
item, err := oldDB.NextDiagnosticOperationItem(ctx)
if err != nil {
t.Fatal(err)
}
item.ValidatorID, item.PortID, item.FIPID = "pending-validator", "pending-validator-port", "pending-uuid"
if _, err := oldDB.ExecContext(ctx, `
UPDATE diagnostic_operation_items SET validator_id=?, port_id=?, fip_id=? WHERE id=?
`, item.ValidatorID, item.PortID, item.FIPID, item.ID); err != nil {
t.Fatal(err)
}
if _, err := oldDB.ExecContext(ctx, `
INSERT INTO diagnostic_holds (address, validator_id, fip_id, note, created_at, pending_item_id)
VALUES (?, 'pending-validator', 'pending-uuid', '', ?, ?)
`, pending, timeToDB(Now()), item.ID); err != nil {
t.Fatal(err)
}
history, err := oldDB.GetIPByAddress(ctx, historical)
if err != nil {
t.Fatal(err)
}
historicalEvent := Event{
IPID: &history.ID, SourceType: "control-api", EventType: "fip_associated", Payload: `{"fip_id":"old-uuid"}`,
OccurredAt: Now().Add(-time.Hour),
}
if err := oldDB.InsertEvent(ctx, historicalEvent); err != nil {
t.Fatal(err)
}
historicalEvent.Payload, historicalEvent.OccurredAt = `{"fip_id":"history-uuid"}`, Now()
if err := oldDB.InsertEvent(ctx, historicalEvent); err != nil {
t.Fatal(err)
}
historicalEvent.Payload = "invalid legacy JSON"
if err := oldDB.InsertEvent(ctx, historicalEvent); err != nil {
t.Fatal(err)
}
if _, err := oldDB.ExecContext(ctx, `
INSERT INTO checks (registry_id, cycle_id, ip_id, ip_address, attempt_number, source, check_type, success, checked_at)
VALUES (?, ?, ?, ?, 1, 'validator-agent', 'https', 1, ?)
`, history.RegistryID, history.CycleID, history.ID, historical, timeToDB(Now())); err != nil {
t.Fatal(err)
}
if err := oldDB.DeleteIP(ctx, history.ID); err != nil {
t.Fatal(err)
}
if err := oldDB.Close(); err != nil {
t.Fatal(err)
}
d, err := Open(ctx, path)
if err != nil {
t.Fatal(err)
}
defer d.Close()
scope := FloatingIPScope{ProjectID: "project", Region: "region"}
for address, expected := range map[string]string{
active: "queued-uuid", historical: "history-uuid", held: "held-uuid", pending: "pending-uuid",
} {
if got, err := d.FloatingIPCandidate(ctx, scope, address); err != nil || got != expected {
t.Errorf("candidate for %s = %q, want %q, err=%v", address, got, expected, err)
}
}
var mappings int
if err := d.QueryRowContext(ctx, `SELECT COUNT(*) FROM floating_ip_mappings`).Scan(&mappings); err != nil || mappings != 0 {
t.Errorf("confirmed mapping count = %d, err=%v", mappings, err)
}
holds, err := d.ListDiagnosticHolds(ctx)
if err != nil || len(holds) != 1 || holds[0].FIPID != "held-uuid" || holds[0].Note != "existing hold" {
t.Errorf("holds: %+v, err=%v", holds, err)
}
pendingItem, err := d.NextDiagnosticOperationItem(ctx)
if err != nil || pendingItem.FIPID != "pending-uuid" || pendingItem.PortID != item.PortID {
t.Errorf("pending item: %+v, err=%v", pendingItem, err)
}
var pendingPort string
if err := d.QueryRowContext(ctx, `SELECT port_id FROM diagnostic_holds WHERE pending_item_id=?`, item.ID).Scan(&pendingPort); err != nil ||
pendingPort != item.PortID {
t.Errorf("backfilled pending hold port = %q, err=%v", pendingPort, err)
}
var checks int
if err := d.QueryRowContext(ctx, `SELECT COUNT(*) FROM checks WHERE registry_id=? AND ip_id IS NULL`, history.RegistryID).
Scan(&checks); err != nil || checks != 1 {
t.Errorf("retained checks = %d, err=%v", checks, err)
}
}
func TestFloatingIPMappingsAreScopedAndKeepNewestObservation(t *testing.T) {
ctx := context.Background()
d, err := Open(ctx, filepath.Join(t.TempDir(), "mappings.db"))
if err != nil {
t.Fatal(err)
}
defer d.Close()
address := "192.0.2.10"
regions := []FloatingIPScope{
{ProjectID: "a", Region: "west"}, {ProjectID: "a", Region: "east"}, {ProjectID: "b", Region: "west"},
}
ids := []string{"west-uuid", "east-uuid", "other-project-uuid"}
now := time.Now()
for i, scope := range regions {
if err := d.ConfirmFloatingIPs(ctx, scope, []FloatingIPMapping{{Address: address, FIPID: ids[i]}}, now); err != nil {
t.Fatal(err)
}
}
if err := d.ConfirmFloatingIPs(ctx, regions[0], []FloatingIPMapping{{Address: address, FIPID: "old-scan-uuid"}}, now.Add(-time.Minute)); err != nil {
t.Fatal(err)
}
for i, scope := range regions {
if got, err := d.FloatingIPCandidate(ctx, scope, address); err != nil || got != ids[i] {
t.Errorf("candidate for %+v = %q, want %q, err=%v", scope, got, ids[i], err)
}
}
}