package hostfence import ( "crypto/ed25519" "github.com/carlosframework/platform/internal/hostactors" "github.com/carlosframework/platform/internal/release" "strings" "testing" ) func TestPlan9OneBoxRegionCountsOtherUnretiredEnrollments(t *testing.T) { key := ed25519.NewKeyFromSeed(make([]byte, 32)) s := release.NewDirStore(t.TempDir()) source := hostactors.Box{BoxID: "source", Enrollment: strings.Repeat("a", 32), Namespace: "platform", Label: "source", Region: "ie", Pool: "default", Cloud: hostactors.Cloud{Provider: "aws", Account: "123", Region: "eu-west-1", InstanceID: "i-source"}, EvidenceSHA256: strings.Repeat("c", 64), State: "active"} roster := hostactors.Roster{V: 1, Revision: 1, DeploymentBucket: "bucket", Boxes: map[string]hostactors.Box{"source": source}, Consoles: map[string]hostactors.Console{}, Holds: map[string]hostactors.Reservation{}, Activations: map[string]hostactors.Reservation{}, Regions: map[string]hostactors.Region{"ie": {Namespaces: []string{"platform"}, Pools: []string{"default"}, Account: "123", AWSRegion: "eu-west-1"}}} write := func() { wire, err := release.SignRootEnvelope(release.RootSigner(key), roster) if err != nil { t.Fatal(err) } _, tag, _ := s.Get(t.Context(), hostactors.Key) if _, err = s.Put(t.Context(), hostactors.Key, wire, tag); err != nil { t.Fatal(err) } } write() authority := ClaimFenceAuthority{Directory: hostactors.Directory{Store: s, PublicKey: key.Public().(ed25519.PublicKey), Bucket: "bucket"}} if err := authority.CheckOneBoxRegion(t.Context(), "source", "ie"); err != nil { t.Fatal(err) } other := source other.BoxID = "other" other.Label = "other" other.Enrollment = strings.Repeat("d", 32) other.Cloud.InstanceID = "i-other" roster.Boxes["other"] = other write() if err := authority.CheckOneBoxRegion(t.Context(), "source", "ie"); err == nil { t.Fatal("two-box region accepted") } other.State = "quarantined" other.QuarantineRevision = 1 other.LossIDs = []string{"loss"} roster.Boxes["other"] = other write() if err := authority.CheckOneBoxRegion(t.Context(), "source", "ie"); err == nil { t.Fatal("quarantine erased cardinality") } delete(roster.Regions, "ie") write() if err := authority.CheckOneBoxRegion(t.Context(), "source", "ie"); err == nil { t.Fatal("missing placement accepted") } }