| 1 | package hostops |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "errors" |
| 6 | "fmt" |
| 7 | "testing" |
| 8 | "time" |
| 9 | |
| 10 | "github.com/carlosframework/platform/internal/domains" |
| 11 | "github.com/carlosframework/platform/internal/hostclaim" |
| 12 | "github.com/carlosframework/platform/internal/release" |
| 13 | ) |
| 14 | |
| 15 | // publishOwned writes a collecting manifest that names no box or region |
| 16 | // (backfill-collect's plan), one signed report per box listing the hosts |
| 17 | // owners gives it, and runs Publish. |
| 18 | func (a *activation) publishOwned(owners map[string][]string, named map[string]BackfillHost) (string, error) { |
| 19 | a.t.Helper() |
| 20 | p := BackfillPlan{V: 1, ID: "backfill", Phase: "collecting", DirectoryRevision: 1, CreatedAt: a.f.now, Hosts: map[string]BackfillHost{}} |
| 21 | byHost := map[string]Record{} |
| 22 | for _, r := range a.records { |
| 23 | byHost[r.Holder.Host] = r |
| 24 | h := BackfillHost{Key: r.Holder.Key, ETag: r.ETag, Acquisition: "acquisition"} |
| 25 | if n, ok := named[r.Holder.Host]; ok { |
| 26 | h.Box, h.Region = n.Box, n.Region |
| 27 | } |
| 28 | p.Hosts[r.Holder.Host] = h |
| 29 | } |
| 30 | raw, err := release.SignRootEnvelope(a.sg, p) |
| 31 | if err != nil { |
| 32 | a.t.Fatal(err) |
| 33 | } |
| 34 | tag, err := a.f.store.Put(a.ctx, BackfillKey, raw, "") |
| 35 | if err != nil { |
| 36 | a.t.Fatal(err) |
| 37 | } |
| 38 | for ref, box := range a.roster.Boxes { |
| 39 | report := CensusReport{Box: ref, Enrollment: box.Enrollment, BootID: "boot", Reservation: p.ID, DirectoryRevision: 1, |
| 40 | CapturedAt: a.f.now, Complete: true, Installations: []CensusInstallation{}, Seeds: &CensusSeeds{Journaled: []string{}, Gated: []string{}}} |
| 41 | for _, host := range owners[ref] { |
| 42 | r := byHost[host] |
| 43 | inc, err := r.Incarnation() |
| 44 | if err != nil { |
| 45 | a.t.Fatal(err) |
| 46 | } |
| 47 | 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}) |
| 48 | } |
| 49 | if err := PublishCensusReport(a.ctx, a.f.store, a.d, report, a.boxKeys[ref]); err != nil { |
| 50 | a.t.Fatalf("publish %s's census report: %v", ref, err) |
| 51 | } |
| 52 | } |
| 53 | b := &Backfill{Store: a.f.store, Directory: a.d} |
| 54 | return tag, b.Publish(a.ctx, a.sg, tag) |
| 55 | } |
| 56 | |
| 57 | func (a *activation) plan() BackfillPlan { |
| 58 | a.t.Helper() |
| 59 | b := &Backfill{Store: a.f.store, Directory: a.d} |
| 60 | p, _, err := b.readPlan(a.ctx) |
| 61 | if err != nil { |
| 62 | a.t.Fatal(err) |
| 63 | } |
| 64 | return p |
| 65 | } |
| 66 | |
| 67 | // Publish takes each host's box and region from the one box whose signed |
| 68 | // report names it. A host no box reports keeps no box, so it is never |
| 69 | // adopted. The adopters' own check then accepts the published plan, and the |
| 70 | // console's adoption pass gives the reported host its claim on that box. |
| 71 | func TestPublishTakesOwnersFromTheCensus(t *testing.T) { |
| 72 | a := newActivation(t) |
| 73 | a.record("legacy.shop.example.com", nil) |
| 74 | a.record("stale.shop.example.com", nil) |
| 75 | if _, err := a.publishOwned(map[string][]string{"box-a": {"legacy.shop.example.com"}}, nil); err != nil { |
| 76 | t.Fatalf("Publish: %v", err) |
| 77 | } |
| 78 | p := a.plan() |
| 79 | if p.Phase != "adopting" { |
| 80 | t.Fatalf("phase %q", p.Phase) |
| 81 | } |
| 82 | if h := p.Hosts["legacy.shop.example.com"]; h.Box != "box-a" || h.Region != "ie" { |
| 83 | t.Fatalf("reported host = %+v, want box-a in ie", h) |
| 84 | } |
| 85 | if h := p.Hosts["stale.shop.example.com"]; h.Box != "" || h.Region != "" { |
| 86 | t.Fatalf("unreported host = %+v, want no box", h) |
| 87 | } |
| 88 | b := &Backfill{Store: a.f.store, Directory: a.d} |
| 89 | if _, err := b.VerifyAdopting(a.ctx); err != nil { |
| 90 | t.Fatalf("VerifyAdopting: %v", err) |
| 91 | } |
| 92 | // The console's adoption pass, as production builds it. |
| 93 | for _, r := range a.records { |
| 94 | inc, err := r.Incarnation() |
| 95 | if err != nil { |
| 96 | t.Fatal(err) |
| 97 | } |
| 98 | 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"}) |
| 99 | if _, err := a.f.store.Put(a.ctx, domains.ClaimKey(r.Holder.Host), raw, ""); err != nil { |
| 100 | t.Fatal(err) |
| 101 | } |
| 102 | } |
| 103 | serial := 0 |
| 104 | c := NewBackfillController(a.f.store, a.d, func() time.Time { return a.f.now }, func() string { serial++; return fmt.Sprintf("op-%d", serial) }) |
| 105 | err := c.ReconcileClaims(a.ctx) |
| 106 | if !errors.Is(err, ErrIdentity) { |
| 107 | t.Fatalf("ReconcileClaims = %v; want the unreported host refused", err) |
| 108 | } |
| 109 | obs, err := hostclaim.Read(a.ctx, a.f.store, "legacy.shop.example.com") |
| 110 | if err != nil || obs.Absent || obs.Claim.Op.Exec != "box-a" || obs.Claim.Holder.WriterBox != "box-a" || obs.Claim.Op.Kind != hostclaim.KindServing { |
| 111 | t.Fatalf("reported host's claim = %+v, %v; want serving on box-a", obs.Claim, err) |
| 112 | } |
| 113 | if obs, err := hostclaim.Read(a.ctx, a.f.store, "stale.shop.example.com"); err != nil || !obs.Absent { |
| 114 | t.Fatalf("unreported host gained a claim: %+v %v", obs.Claim, err) |
| 115 | } |
| 116 | // A census report changed after Publish no longer matches its digest. |
| 117 | key, _ := reportKey("backfill", "box-b") |
| 118 | old, tag, err := a.f.store.Get(a.ctx, key) |
| 119 | if err != nil { |
| 120 | t.Fatal(err) |
| 121 | } |
| 122 | if _, err := a.f.store.Put(a.ctx, key, append(old, ' '), tag); err != nil { |
| 123 | t.Fatal(err) |
| 124 | } |
| 125 | if _, err := b.VerifyAdopting(a.ctx); !errors.Is(err, ErrIdentity) { |
| 126 | t.Fatalf("VerifyAdopting over a changed report = %v", err) |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | // Two boxes reporting one host, or a census that disagrees with a box or |
| 131 | // region the collecting plan named, refuse and leave the plan collecting. |
| 132 | func TestPublishRefusesConflictingOwners(t *testing.T) { |
| 133 | for name, c := range map[string]struct { |
| 134 | owners map[string][]string |
| 135 | named map[string]BackfillHost |
| 136 | }{ |
| 137 | "two boxes": {map[string][]string{"box-a": {"legacy.shop.example.com"}, "box-b": {"legacy.shop.example.com"}}, nil}, |
| 138 | "other box": {map[string][]string{"box-a": {"legacy.shop.example.com"}}, map[string]BackfillHost{"legacy.shop.example.com": {Box: "box-b"}}}, |
| 139 | "other region": {map[string][]string{"box-a": {"legacy.shop.example.com"}}, map[string]BackfillHost{"legacy.shop.example.com": {Region: "uk"}}}, |
| 140 | } { |
| 141 | t.Run(name, func(t *testing.T) { |
| 142 | a := newActivation(t) |
| 143 | a.record("legacy.shop.example.com", nil) |
| 144 | if _, err := a.publishOwned(c.owners, c.named); !errors.Is(err, ErrIdentity) { |
| 145 | t.Fatalf("Publish = %v, want ErrIdentity", err) |
| 146 | } |
| 147 | if got := a.phase(); got != "collecting" { |
| 148 | t.Fatalf("phase %q after a refusal", got) |
| 149 | } |
| 150 | }) |
| 151 | } |
| 152 | // A plan that already names the census's own box and region publishes. |
| 153 | a := newActivation(t) |
| 154 | a.record("legacy.shop.example.com", nil) |
| 155 | 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 { |
| 156 | t.Fatal(err) |
| 157 | } |
| 158 | } |
| 159 | |