package host import ( "context" "errors" "testing" "time" "github.com/carlosframework/platform/internal/hostclaim" execlease "github.com/carlosframework/platform/internal/hostexec" "github.com/carlosframework/platform/internal/hostguard" ) func TestPlan14RetiredParticipationDischarge(t *testing.T) { for _, mode := range []string{"retired", "live", "foreign box", "foreign enrollment", "foreign session", "foreign op", "foreign stamp", "no receipt", "corrupt receipt", "barrier", "read failure"} { t.Run(mode, func(t *testing.T) { binding := execlease.Binding{Host: "shop.example", Incarnation: "inc", Op: "delete", Box: "box", Enrollment: "enrollment"} o := hostclaim.Observed{Host: binding.Host, Claim: &hostclaim.Claim{Host: binding.Host, Holder: &hostclaim.Holder{Incarnation: "inc", Stamp: 3, Session: 4}, Op: hostclaim.Op{Kind: hostclaim.KindParking, ID: "delete"}}} target := hostguard.FenceTarget{Host: binding.Host, Incarnation: "inc", Stamp: 3, Session: 4, Box: "box", Enrollment: "enrollment", Op: "delete", Phase: "park", ObligationID: "park-local", FenceID: "fence", BootID: "boot", ControllerID: "controller", ClaimETag: "etag", ClaimGen: 5, DirectoryRevision: "1", RosterRevision: 1, Region: "ie"} at := time.Now().UTC() s := StopReceipt{V: 1, Kind: "stopped", Host: target.Host, Incarnation: target.Incarnation, InstallationStamp: target.Stamp, WriterSession: target.Session, Box: target.Box, Enrollment: target.Enrollment, OperationID: target.Op, Phase: target.Phase, ObligationID: target.ObligationID, FenceID: target.FenceID, BootID: target.BootID, ControllerID: target.ControllerID, ClaimETag: target.ClaimETag, ClaimGen: target.ClaimGen, DirectoryRevision: testReceiptNumeral(target), Binding: target.DirectoryRevision, Region: target.Region, EvidenceAt: at} captured := hostguard.Captured{Incarnation: "inc", Stamp: 3} view := hostguard.FenceView{Installation: hostguard.RootInstallation{Host: binding.Host, Incarnation: "inc", Stamp: 3, Box: "box", Enrollment: "enrollment"}, Retired: []hostguard.Captured{captured}, Receipts: []hostguard.FenceReceipt{{Target: target, Bytes: mustMarshalStopReceipt(s), EvidenceAt: at}}} switch mode { case "live": view.Retired = nil case "foreign box": binding.Box = "other" case "foreign enrollment": binding.Enrollment = "other" case "foreign session": o.Claim.Holder.Session++ case "foreign op": view.Receipts[0].Target.Op = "other" case "foreign stamp": view.Receipts[0].Target.Stamp++ case "no receipt": view.Receipts = nil case "corrupt receipt": view.Receipts[0].Bytes = []byte("{}") case "barrier": view.Barrier = &target } a := ClaimAssembly{Guard: hostguard.Guard{FenceView: func(context.Context, string, hostguard.Captured) (hostguard.FenceView, error) { if mode == "read failure" { return hostguard.FenceView{}, errors.New("unreadable") } return view, nil }}} done, err := a.completedRetirementDischarge(t.Context(), o, binding) if mode == "retired" { if !done || err != nil { t.Fatalf("discharge: %v %v", done, err) } } else if mode == "live" { if done || err != nil { t.Fatalf("live cleanup bypass: %v %v", done, err) } } else if err == nil { t.Fatalf("unsafe discharge: %v", done) } }) } }