package activator import ( "context" "errors" "github.com/carlosframework/platform/internal/hostclaim" "github.com/carlosframework/platform/internal/hostwrite" "github.com/carlosframework/platform/internal/registry" "testing" "time" ) type claimRoutes struct{ called *bool } func (r claimRoutes) Get(string) (*registry.Route, error) { *r.called = true return nil, errors.New("unexpected registry read") } func TestClaimK6StopsBeforeRegistryOrAdmission(t *testing.T) { stopped, readRoute, admitted := false, false, false a := &Service{cfg: Config{HostWrites: hostwrite.Config{Enabled: true}, Claims: &ClaimRuntime{ Box: "here", Read: func(context.Context, string) (hostclaim.Observed, error) { return hostclaim.Observed{Claim: &hostclaim.Claim{Op: hostclaim.Op{Exec: "elsewhere"}}}, nil }, StopForeign: func(context.Context, string) error { stopped = true; return nil }, Admit: func(context.Context, registry.Route, hostclaim.Observed, startPolicy) (hostclaim.Observed, error) { admitted = true return hostclaim.Observed{}, nil }, }}, Routes: claimRoutes{&readRoute}} if _, err := a.doWake(context.Background(), &actEntry{host: "x.example"}); err == nil { t.Fatal("foreign wake") } if !stopped || readRoute || admitted { t.Fatalf("stop %v route %v admit %v", stopped, readRoute, admitted) } } func TestClaimReadFailureDoesNotInventForeignAuthority(t *testing.T) { stopped := false a := &Service{cfg: Config{HostWrites: hostwrite.Config{Enabled: true}, Claims: &ClaimRuntime{Box: "box", Read: func(context.Context, string) (hostclaim.Observed, error) { return hostclaim.Observed{}, errors.New("offline") }, StopForeign: func(context.Context, string) error { stopped = true; return nil }}}} if _, err := a.doWake(context.Background(), &actEntry{host: "x.example"}); err == nil || stopped { t.Fatal(err, stopped) } } func TestStartAdmittedRejectsMovedObservationBeforeRestore(t *testing.T) { prepared := false obs := hostclaim.Observed{Host: "x.example", ETag: "old", Claim: &hostclaim.Claim{Holder: &hostclaim.Holder{Session: 1, Generation: "g"}, Op: hostclaim.Op{Kind: "landing", ID: "op", Phase: "probing", Exec: "box"}}} a := &Service{cfg: Config{HostWrites: hostwrite.Config{Enabled: true}, Claims: &ClaimRuntime{Box: "box", Read: func(context.Context, string) (hostclaim.Observed, error) { n := obs; n.ETag = "new"; return n, nil }, Prepare: func(context.Context, registry.Route, hostclaim.Observed, startPolicy) (ClaimStart, error) { prepared = true return ClaimStart{}, nil }}}} err := a.startAdmitted(context.Background(), &actEntry{host: obs.Host}, obs, startPolicy{MaxStarts: 5, Window: 30 * time.Second, Probe: true}) if err == nil || prepared { t.Fatal(err, prepared) } } func TestSameAdmissionAllowsBookkeepingButRefusesSessionChange(t *testing.T) { h := &hostclaim.Holder{Incarnation: "i", Stamp: 2, Session: 2, Generation: "g"} a := hostclaim.Observed{Host: "x.example", ETag: "a", Claim: &hostclaim.Claim{Holder: h, Op: hostclaim.Op{Kind: "serving", Exec: "box"}}} b := a b.ETag = "b" if !sameAdmission(a, b) { t.Fatal("bookkeeping") } c := *a.Claim holder := *h holder.Session++ c.Holder = &holder b.Claim = &c if sameAdmission(a, b) { t.Fatal("session") } }