//go:build unix package hostbox import ( "context" "crypto/sha256" "encoding/hex" "encoding/json" "github.com/carlosframework/platform/internal/genselect" "github.com/carlosframework/platform/internal/hostclaim" "github.com/carlosframework/platform/internal/termfence" "testing" "time" ) type plan12FenceLossAuthority struct{} func (plan12FenceLossAuthority) ValidateFence(context.Context, termfence.Record) (hostclaim.FenceBinding, error) { return hostclaim.FenceBinding{Region: "uk", RosterRevision: 1, RosterETag: "roster"}, nil } func TestPlan12FencedTargetLossMaximum(t *testing.T) { for _, mode := range []string{"ok", "missing-validator", "wrong-source", "bypass"} { t.Run(mode, func(t *testing.T) { _, s, o := workFixture(t) now := time.Now().UTC() record := termfence.Record{V: 1, Box: "target", Enrollment: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", State: "terminated", ObservedAt: now.Format(time.RFC3339Nano)} raw, _ := json.Marshal(record) sum := sha256.Sum256(raw) ref := hostclaim.FenceRef{Enrollment: record.Enrollment, Key: "control/box-fences/" + record.Enrollment + ".json", SHA256: hex.EncodeToString(sum[:])} if _, err := s.Put(t.Context(), ref.Key, raw, ""); err != nil { t.Fatal(err) } var err error o, err = hostclaim.Update(t.Context(), s, o, func(c *hostclaim.Claim) error { c.Op = hostclaim.Op{Kind: hostclaim.KindLanding, ID: "move", Phase: hostclaim.PhaseProbing, Exec: "target", Participants: []string{"box", "target"}, Intent: map[string]string{"source": "box", "target": "target", "to": "uk", "target_enrollment": record.Enrollment}, Journal: map[string]string{hostclaim.JournalSourceStamp: "1", hostclaim.JournalSrcGen: "src"}} c.Holder.Session, c.Holder.Stamp, c.Holder.WriterBox, c.Holder.Generation = 2, 2, "target", "target-gen" c.Holder.Admissions = []hostclaim.AdmissionRecord{{Session: 2, Box: "target", Generation: "target-gen", Region: "uk", Mode: "cold"}} return nil }) if err != nil { t.Fatal(err) } fence, err := hostclaim.VerifyMoveTargetFence(t.Context(), s, o, ref, plan12FenceLossAuthority{}) if err != nil { t.Fatal(err) } // Admission consumes a previously grant-released claim. The grant itself // is exercised in hostops.TestPlan12RecoveryFenceGrant. o, err = hostclaim.Update(t.Context(), s, o, func(c *hostclaim.Claim) error { raw, _ := json.Marshal(ref) sum := sha256.Sum256([]byte(c.Op.ID + "\x00target\x00" + ref.Enrollment + "\x00" + ref.SHA256)) id := "auto-" + hex.EncodeToString(sum[:16]) loss := hostclaim.AutomaticLoss{ID: id, Box: "target", Enrollment: ref.Enrollment, Fence: ref, ObservedAt: fence.ObservedAt().Format(time.RFC3339Nano), Op: c.Op.ID, Kind: c.Op.Kind, Phase: c.Op.Phase, Role: "target", Incarnation: c.Holder.Incarnation, Stamp: c.Holder.Stamp, Session: c.Holder.Session, Generation: c.Holder.Generation, Timers: hostclaim.TimersUnknown} if len(c.Holder.Admissions) > 0 { a := c.Holder.Admissions[len(c.Holder.Admissions)-1] loss.LastAdmission = &a } c.Op.Kind, c.Op.Phase, c.Op.Exec = hostclaim.KindRestoring, hostclaim.PhaseReleased, "none" c.Op.Intent["rollback"] = "rb_abcdefghijklmnopqrstuvwxyz" c.Op.Participants = []string{c.Op.Intent["source"]} c.Op.Journal["fence.target"], c.Op.Journal["fenced_target_released"] = string(raw), string(raw) c.Lost, c.LossRefs, c.EvidenceRefs = []string{"target"}, []string{id}, []hostclaim.AutomaticLoss{loss} return nil }) if err != nil { t.Fatal(err) } o, err = hostclaim.Update(t.Context(), s, o, hostclaim.ReturnToSource("move", "box", false, hostclaim.ReturnFence{})) if err != nil { t.Fatal(err) } o, err = hostclaim.Update(t.Context(), s, o, hostclaim.SetRestoringPhase("move", hostclaim.PhaseClaimed, hostclaim.PhaseProbing)) if err != nil { t.Fatal(err) } in := hostclaim.Admission{ObservedOpID: "move", ObservedPhase: o.Claim.Op.Phase, ObservedSession: 2, ObservedGeneration: o.Claim.Holder.Generation, Box: "box", Mode: "cold", NewGeneration: "return", Region: "ie", PredecessorRegion: "uk", RemoteMaxTXID: 3} w := Writer{Claims: s, Box: "box", Region: "ie", AdmissionEvidence: func(_ context.Context, _ hostclaim.Observed, a hostclaim.Admission, p *genselect.Plan) error { if a.RemoteMaxTXID != 7 || p.MaxTXID != 3 { t.Fatalf("loss maximum %d restore maximum %d", a.RemoteMaxTXID, p.MaxTXID) } return nil }, Loss: &LossAdmission{Sources: func(_ context.Context, _ hostclaim.Observed, l hostclaim.LossRecord, _ hostclaim.Admission, _ *genselect.Plan) ([]hostclaim.ValidatedWriteSource, error) { if len(l.WriteSources) != 1 { t.Fatalf("frozen sources: %+v", l) } source := l.WriteSources[0] if mode == "wrong-source" { source.Generation = "other" } return []hostclaim.ValidatedWriteSource{{Reason: source.Reason, Box: source.Box, Session: source.Session, Generation: source.Generation, Region: source.Region, Baseline: source.Baseline, RemoteMaxTXID: 7}}, nil }}} if mode == "missing-validator" { w.Loss = nil } var got hostclaim.Observed if mode == "bypass" { got, err = hostclaim.Update(t.Context(), s, o, hostclaim.AdmitWriter(in)) } else { got, err = w.Admit(t.Context(), o, in, restorePlan(o.Claim.Holder.Location, "", 3)) } if mode != "ok" { if err == nil { t.Fatal("accepted", mode) } return } if err != nil { t.Fatal(err) } a := got.Claim.Holder.Admissions[len(got.Claim.Holder.Admissions)-1] if a.WritesLost == nil || a.WritesLost.RemoteMaxTXID != 7 || a.WritesLost.Generation != "target-gen" { t.Fatalf("lost target maximum: %+v", a) } }) } }