| 1 | package park |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "errors" |
| 6 | "testing" |
| 7 | "time" |
| 8 | |
| 9 | "github.com/carlosframework/platform/internal/hostclaim" |
| 10 | ) |
| 11 | |
| 12 | func TestSweepObservedRefusesChangedCompleteCensus(t *testing.T) { |
| 13 | f, obs, view, versions := sweepFixture(t) |
| 14 | f.e.View = func(_ context.Context, current hostclaim.Observed) (View, error) { |
| 15 | v := view |
| 16 | v.ClaimETag = current.ETag |
| 17 | return v, nil |
| 18 | } |
| 19 | // A later version of an already-listed key must be detected too; checking |
| 20 | // only current keys would silently attach old evidence to this residue. |
| 21 | f.b.Put("data", versions[0].Key, []byte("late noncurrent version")) |
| 22 | if _, err := f.e.SweepObserved(t.Context(), obs, versions, f.ob.Park.Incarnation, f.ob.Park.EvidenceAt, time.Second); !errors.Is(err, ErrChanged) { |
| 23 | t.Fatalf("SweepObserved = %v", err) |
| 24 | } |
| 25 | current, err := hostclaim.Read(t.Context(), f.e.Claims, f.w.Host) |
| 26 | if err != nil || len(current.Claim.Obligations) != 0 { |
| 27 | t.Fatalf("claim mutated: %+v, %v", current.Claim.Obligations, err) |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | func TestSweepObservedRefusesChangedClaim(t *testing.T) { |
| 32 | f, obs, view, versions := sweepFixture(t) |
| 33 | f.e.View = func(_ context.Context, current hostclaim.Observed) (View, error) { |
| 34 | v := view |
| 35 | v.ClaimETag = current.ETag |
| 36 | return v, nil |
| 37 | } |
| 38 | if _, err := hostclaim.Update(t.Context(), f.e.Claims, obs, func(c *hostclaim.Claim) error { |
| 39 | c.Op.Phase = "changed" |
| 40 | return nil |
| 41 | }); err != nil { |
| 42 | t.Fatal(err) |
| 43 | } |
| 44 | if _, err := f.e.SweepObserved(t.Context(), obs, versions, f.ob.Park.Incarnation, f.ob.Park.EvidenceAt, time.Second); !errors.Is(err, ErrChanged) { |
| 45 | t.Fatalf("SweepObserved = %v", err) |
| 46 | } |
| 47 | current, err := hostclaim.Read(t.Context(), f.e.Claims, f.w.Host) |
| 48 | if err != nil || len(current.Claim.Obligations) != 0 { |
| 49 | t.Fatalf("claim mutated: %+v, %v", current.Claim.Obligations, err) |
| 50 | } |
| 51 | } |
| 52 | |