package hostops import ( "encoding/json" "errors" "fmt" "testing" "time" "github.com/carlosframework/platform/internal/domains" "github.com/carlosframework/platform/internal/hostclaim" "github.com/carlosframework/platform/internal/release" ) // publishOwned writes a collecting manifest that names no box or region // (backfill-collect's plan), one signed report per box listing the hosts // owners gives it, and runs Publish. func (a *activation) publishOwned(owners map[string][]string, named map[string]BackfillHost) (string, error) { a.t.Helper() p := BackfillPlan{V: 1, ID: "backfill", Phase: "collecting", DirectoryRevision: 1, CreatedAt: a.f.now, Hosts: map[string]BackfillHost{}} byHost := map[string]Record{} for _, r := range a.records { byHost[r.Holder.Host] = r h := BackfillHost{Key: r.Holder.Key, ETag: r.ETag, Acquisition: "acquisition"} if n, ok := named[r.Holder.Host]; ok { h.Box, h.Region = n.Box, n.Region } p.Hosts[r.Holder.Host] = h } raw, err := release.SignRootEnvelope(a.sg, p) if err != nil { a.t.Fatal(err) } tag, err := a.f.store.Put(a.ctx, BackfillKey, raw, "") if err != nil { a.t.Fatal(err) } for ref, box := range a.roster.Boxes { report := CensusReport{Box: ref, Enrollment: box.Enrollment, BootID: "boot", Reservation: p.ID, DirectoryRevision: 1, CapturedAt: a.f.now, Complete: true, Installations: []CensusInstallation{}, Seeds: &CensusSeeds{Journaled: []string{}, Gated: []string{}}} for _, host := range owners[ref] { r := byHost[host] inc, err := r.Incarnation() if err != nil { a.t.Fatal(err) } report.Installations = append(report.Installations, CensusInstallation{Host: host, Account: r.Holder.Account, App: r.Holder.App, Family: r.Holder.Family, Key: r.Holder.Key, Incarnation: inc, Route: true}) } if err := PublishCensusReport(a.ctx, a.f.store, a.d, report, a.boxKeys[ref]); err != nil { a.t.Fatalf("publish %s's census report: %v", ref, err) } } b := &Backfill{Store: a.f.store, Directory: a.d} return tag, b.Publish(a.ctx, a.sg, tag) } func (a *activation) plan() BackfillPlan { a.t.Helper() b := &Backfill{Store: a.f.store, Directory: a.d} p, _, err := b.readPlan(a.ctx) if err != nil { a.t.Fatal(err) } return p } // Publish takes each host's box and region from the one box whose signed // report names it. A host no box reports keeps no box, so it is never // adopted. The adopters' own check then accepts the published plan, and the // console's adoption pass gives the reported host its claim on that box. func TestPublishTakesOwnersFromTheCensus(t *testing.T) { a := newActivation(t) a.record("legacy.shop.example.com", nil) a.record("stale.shop.example.com", nil) if _, err := a.publishOwned(map[string][]string{"box-a": {"legacy.shop.example.com"}}, nil); err != nil { t.Fatalf("Publish: %v", err) } p := a.plan() if p.Phase != "adopting" { t.Fatalf("phase %q", p.Phase) } if h := p.Hosts["legacy.shop.example.com"]; h.Box != "box-a" || h.Region != "ie" { t.Fatalf("reported host = %+v, want box-a in ie", h) } if h := p.Hosts["stale.shop.example.com"]; h.Box != "" || h.Region != "" { t.Fatalf("unreported host = %+v, want no box", h) } b := &Backfill{Store: a.f.store, Directory: a.d} if _, err := b.VerifyAdopting(a.ctx); err != nil { t.Fatalf("VerifyAdopting: %v", err) } // The console's adoption pass, as production builds it. for _, r := range a.records { inc, err := r.Incarnation() if err != nil { t.Fatal(err) } raw, _ := json.Marshal(domains.Claim{Schema: 1, Host: r.Holder.Host, Account: r.Holder.Account, App: r.Holder.App, Acquisition: "acquisition", RecordIncarnation: inc, OwnershipState: "committed"}) if _, err := a.f.store.Put(a.ctx, domains.ClaimKey(r.Holder.Host), raw, ""); err != nil { t.Fatal(err) } } serial := 0 c := NewBackfillController(a.f.store, a.d, func() time.Time { return a.f.now }, func() string { serial++; return fmt.Sprintf("op-%d", serial) }) err := c.ReconcileClaims(a.ctx) if !errors.Is(err, ErrIdentity) { t.Fatalf("ReconcileClaims = %v; want the unreported host refused", err) } obs, err := hostclaim.Read(a.ctx, a.f.store, "legacy.shop.example.com") if err != nil || obs.Absent || obs.Claim.Op.Exec != "box-a" || obs.Claim.Holder.WriterBox != "box-a" || obs.Claim.Op.Kind != hostclaim.KindServing { t.Fatalf("reported host's claim = %+v, %v; want serving on box-a", obs.Claim, err) } if obs, err := hostclaim.Read(a.ctx, a.f.store, "stale.shop.example.com"); err != nil || !obs.Absent { t.Fatalf("unreported host gained a claim: %+v %v", obs.Claim, err) } // A census report changed after Publish no longer matches its digest. key, _ := reportKey("backfill", "box-b") old, tag, err := a.f.store.Get(a.ctx, key) if err != nil { t.Fatal(err) } if _, err := a.f.store.Put(a.ctx, key, append(old, ' '), tag); err != nil { t.Fatal(err) } if _, err := b.VerifyAdopting(a.ctx); !errors.Is(err, ErrIdentity) { t.Fatalf("VerifyAdopting over a changed report = %v", err) } } // Two boxes reporting one host, or a census that disagrees with a box or // region the collecting plan named, refuse and leave the plan collecting. func TestPublishRefusesConflictingOwners(t *testing.T) { for name, c := range map[string]struct { owners map[string][]string named map[string]BackfillHost }{ "two boxes": {map[string][]string{"box-a": {"legacy.shop.example.com"}, "box-b": {"legacy.shop.example.com"}}, nil}, "other box": {map[string][]string{"box-a": {"legacy.shop.example.com"}}, map[string]BackfillHost{"legacy.shop.example.com": {Box: "box-b"}}}, "other region": {map[string][]string{"box-a": {"legacy.shop.example.com"}}, map[string]BackfillHost{"legacy.shop.example.com": {Region: "uk"}}}, } { t.Run(name, func(t *testing.T) { a := newActivation(t) a.record("legacy.shop.example.com", nil) if _, err := a.publishOwned(c.owners, c.named); !errors.Is(err, ErrIdentity) { t.Fatalf("Publish = %v, want ErrIdentity", err) } if got := a.phase(); got != "collecting" { t.Fatalf("phase %q after a refusal", got) } }) } // A plan that already names the census's own box and region publishes. a := newActivation(t) a.record("legacy.shop.example.com", nil) if _, err := a.publishOwned(map[string][]string{"box-a": {"legacy.shop.example.com"}}, map[string]BackfillHost{"legacy.shop.example.com": {Box: "box-a", Region: "ie"}}); err != nil { t.Fatal(err) } }