Files

209 lines
7.9 KiB
Go
Raw Permalink Normal View History

package orchestrator
import (
"context"
"fmt"
"net/http"
"path/filepath"
"slices"
"testing"
"time"
"cloudipvalidator/internal/config"
"cloudipvalidator/internal/db"
"cloudipvalidator/internal/openstack"
)
type floatingIPReadRecorder struct {
*openstack.MockClient
addressReads []string
uuidReads []string
uuidErrors map[string]error
}
func (c *floatingIPReadRecorder) GetFloatingIPByAddress(ctx context.Context, address string) (*openstack.FloatingIP, error) {
c.addressReads = append(c.addressReads, address)
return c.MockClient.GetFloatingIPByAddress(ctx, address)
}
func (c *floatingIPReadRecorder) GetFloatingIP(ctx context.Context, id string) (*openstack.FloatingIP, error) {
c.uuidReads = append(c.uuidReads, id)
if err := c.uuidErrors[id]; err != nil {
return nil, err
}
return c.MockClient.GetFloatingIP(ctx, id)
}
func TestScannedFloatingIPsAreReadByUUID(t *testing.T) {
o, d, ctx, mock := diagSetup(t)
o.FIPScope = db.FloatingIPScope{ProjectID: "svc", Region: "region"}
cloud := &floatingIPReadRecorder{MockClient: mock}
o.OS = cloud
mock.SeedWithPort("fip-occupied", "3.3.3.3", "svc", "port-x")
mock.Seed("fip-other-project", "4.4.4.4", "other")
mock.Seed("fip-ambiguous-a", "5.5.5.5", "svc")
mock.Seed("fip-ambiguous-b", "5.5.5.5", "svc")
if _, _, err := o.ScanFloatingIPs(ctx); err != nil {
t.Fatal(err)
}
for address, want := range map[string]string{"3.3.3.3": "fip-occupied", "4.4.4.4": "", "5.5.5.5": ""} {
if id, err := d.FloatingIPCandidate(ctx, o.FIPScope, address); err != nil || id != want {
t.Fatalf("mapping for %s: %q, err=%v, want %q", address, id, err, want)
}
}
got := o.DiagnosticAttach(ctx, []DiagnosticItem{{Address: "1.1.1.1", ValidatorID: "validator-1"}}, "")
if got[0].FIPID != "fip-a" || got[0].Status != DiagAttached {
t.Fatalf("attach: %+v", got)
}
// The held first address is skipped, so Tick assigns the other scanned
// address to the second validator.
o.Tick(ctx)
item, err := d.GetIPByAddress(ctx, "2.2.2.2")
if err != nil || item.FIPID != "fip-b" || item.State != db.IPAwaitingSelfCheck {
t.Fatalf("assignment: %+v, err=%v", item, err)
}
if len(cloud.addressReads) != 0 || !slices.Equal(cloud.uuidReads, []string{"fip-a", "fip-b"}) {
t.Fatalf("lookups: by address %v, by UUID %v", cloud.addressReads, cloud.uuidReads)
}
}
func TestManuallyDiscoveredUUIDSurvivesQueueClearAndRestart(t *testing.T) {
ctx := context.Background()
path := filepath.Join(t.TempDir(), "reopen.db")
o, _, mock := newTestOrchestrator(t, 180)
d, err := db.Open(ctx, path)
if err != nil {
t.Fatal(err)
}
defer d.Close()
o.DB = d
o.FIPScope = db.FloatingIPScope{ProjectID: "svc", Region: "region"}
cloud := &floatingIPReadRecorder{MockClient: mock}
o.OS = cloud
const address = "1.1.1.1"
mock.Seed("fip", address, "svc")
if err := d.RegisterValidator(ctx, "validator", "", "port", ""); err != nil {
t.Fatal(err)
}
if err := d.SeedQueue(ctx, []string{address}); err != nil {
t.Fatal(err)
}
items := []DiagnosticItem{{Address: address, ValidatorID: "validator"}}
if got := o.DiagnosticAttach(ctx, items, ""); got[0].Status != DiagAttached {
t.Fatalf("initial attach: %+v", got)
}
if got := o.DiagnosticDetach(ctx, []string{address}, false); got[0].Status != DiagDetached {
t.Fatalf("detach: %+v", got)
}
if _, err := o.ClearQueue(ctx); err != nil {
t.Fatal(err)
}
if err := d.Close(); err != nil {
t.Fatal(err)
}
reopened, err := db.Open(ctx, path)
if err != nil {
t.Fatal(err)
}
defer reopened.Close()
restarted := New(reopened, cloud, &config.ControlAPI{}, o.Log)
restarted.FIPScope = o.FIPScope
if got := restarted.DiagnosticAttach(ctx, items, ""); got[0].Status != DiagAttached {
t.Fatalf("attach after restart: %+v", got)
}
if !slices.Equal(cloud.addressReads, []string{address}) || !slices.Equal(cloud.uuidReads, []string{"fip", "fip"}) {
t.Fatalf("lookups: by address %v, by UUID %v", cloud.addressReads, cloud.uuidReads)
}
}
type neutronStatus int
func (e neutronStatus) GetStatusCode() int { return int(e) }
func (e neutronStatus) Error() string { return fmt.Sprintf("Neutron returned HTTP %d", e) }
func TestFloatingIPStaleMappingAndTransientErrors(t *testing.T) {
for _, tc := range []struct {
name string
address string
project string
deleted bool
}{
{name: "404", deleted: true},
{name: "wrong_address", address: "3.3.3.3", project: "svc"},
{name: "wrong_project", address: "1.1.1.1", project: "other"},
} {
t.Run(tc.name, func(t *testing.T) {
o, d, ctx, mock := diagSetup(t)
o.FIPScope = db.FloatingIPScope{ProjectID: "svc", Region: "region"}
address := "1.1.1.1"
if !tc.deleted {
mock.Seed("old-fip", tc.address, tc.project)
}
mock.Seed("new-fip", address, "svc")
cloud := &floatingIPReadRecorder{
MockClient: mock, uuidErrors: map[string]error{"old-fip": neutronStatus(http.StatusServiceUnavailable)},
}
o.OS = cloud
if err := d.ConfirmFloatingIPs(ctx, o.FIPScope, []db.FloatingIPMapping{{Address: address, FIPID: "old-fip"}}, time.Now()); err != nil {
t.Fatal(err)
}
items := []DiagnosticItem{{Address: address, ValidatorID: "validator-1"}}
if got := o.DiagnosticAttach(ctx, items, ""); got[0].Status != DiagRejected {
t.Fatalf("attach during outage: %+v", got)
}
if got, err := d.FloatingIPCandidate(ctx, o.FIPScope, address); err != nil || got != "old-fip" || len(cloud.addressReads) != 0 {
t.Fatalf("candidate after 503=%q, err=%v, by-address lookups=%v", got, err, cloud.addressReads)
}
delete(cloud.uuidErrors, "old-fip")
if got := o.DiagnosticAttach(ctx, items, ""); got[0].Status != DiagAttached || got[0].FIPID != "new-fip" {
t.Fatalf("attach after stale UUID: %+v", got)
}
if got, err := d.FloatingIPCandidate(ctx, o.FIPScope, address); err != nil || got != "new-fip" ||
!slices.Equal(cloud.addressReads, []string{address}) || !slices.Equal(cloud.uuidReads, []string{"old-fip", "old-fip"}) {
t.Fatalf("candidate after refresh=%q, err=%v, by address=%v, by UUID=%v",
got, err, cloud.addressReads, cloud.uuidReads)
}
})
}
}
func TestDiagnosticDetachDoesNotChangeReplacementOrAnotherPort(t *testing.T) {
o, d, ctx, mock := diagSetup(t)
const address = "1.1.1.1"
cloud := &floatingIPReadRecorder{MockClient: mock, uuidErrors: make(map[string]error)}
o.OS = cloud
if got := o.DiagnosticAttach(ctx, []DiagnosticItem{{Address: address, ValidatorID: "validator-1"}}, ""); got[0].Status != DiagAttached {
t.Fatalf("attach: %+v", got)
}
if err := mock.AssociateFloatingIP(ctx, "fip-a", "other-port"); err != nil {
t.Fatal(err)
}
if got := o.DiagnosticDetach(ctx, []string{address}, false); got[0].Status != DiagRejected {
t.Fatalf("detach moved FIP: %+v", got)
}
cloud.uuidErrors["fip-a"] = neutronStatus(http.StatusServiceUnavailable)
if got := o.DiagnosticDetach(ctx, []string{address}, false); got[0].Status != DiagRejected {
t.Fatalf("detach during outage: %+v", got)
}
if state := validatorState(t, d, "validator-1"); state != db.ValidatorDiagnostic {
t.Fatalf("validator state after unsuccessful detach: %s", state)
}
mock.SeedWithPort("replacement", address, "svc", "replacement-port")
cloud.uuidErrors["fip-a"] = openstack.ErrNotFound("fip-a")
if err := d.ConfirmFloatingIPs(ctx, o.FIPScope, []db.FloatingIPMapping{{Address: address, FIPID: "replacement"}}, time.Now()); err != nil {
t.Fatal(err)
}
if got := o.DiagnosticDetach(ctx, []string{address}, false); got[0].Status != DiagDetached || got[0].FIPID != "fip-a" {
t.Fatalf("detach removed FIP: %+v", got)
}
fip, err := mock.GetFloatingIP(ctx, "replacement")
if err != nil || fip.PortID != "replacement-port" {
t.Fatalf("replacement FIP changed: %+v, %v", fip, err)
}
if state := validatorState(t, d, "validator-1"); state != db.ValidatorIdle {
t.Fatalf("validator state after removed FIP: %s", state)
}
if !slices.Equal(cloud.addressReads, []string{address}) || !slices.Equal(cloud.uuidReads, []string{"fip-a", "fip-a", "fip-a"}) {
t.Fatalf("lookups: by address %v, by UUID %v", cloud.addressReads, cloud.uuidReads)
}
}