| 1 | package hostops |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "errors" |
| 6 | "testing" |
| 7 | "time" |
| 8 | |
| 9 | "github.com/carlosframework/platform/internal/lease" |
| 10 | "github.com/carlosframework/platform/internal/lease/leasetest" |
| 11 | "github.com/carlosframework/platform/internal/release" |
| 12 | ) |
| 13 | |
| 14 | type delayedPurgeDelete struct { |
| 15 | lease.VersionedStore |
| 16 | control release.Store |
| 17 | key string |
| 18 | replacement string |
| 19 | calls int |
| 20 | } |
| 21 | |
| 22 | func (d *delayedPurgeDelete) DeleteVersion(ctx context.Context, bucket, key, version string) error { |
| 23 | d.calls++ |
| 24 | if d.replacement != "" { |
| 25 | _, tag, err := d.control.Get(ctx, d.key) |
| 26 | if err != nil { |
| 27 | return err |
| 28 | } |
| 29 | if _, err := d.control.Put(ctx, d.key, []byte(d.replacement), tag); err != nil { |
| 30 | return err |
| 31 | } |
| 32 | d.replacement = "" |
| 33 | } |
| 34 | return d.VersionedStore.DeleteVersion(ctx, bucket, key, version) |
| 35 | } |
| 36 | func TestClaimPurgeExactSweep(t *testing.T) { |
| 37 | ctx := t.Context() |
| 38 | control := release.NewDirStore(t.TempDir()) |
| 39 | versions := leasetest.NewVersionedBucket() |
| 40 | versions.AddBucket("control") |
| 41 | key := "apps/bab/demo/payload" |
| 42 | old := versions.Put("control", key, []byte("same")) |
| 43 | journal := PurgeJournal{V: 1, Purge: Purge{ID: "p", Phase: "retired"}, AppID: "old", Account: "bab", App: "demo", |
| 44 | Hosts: map[string]PurgeHost{}} |
| 45 | w := seedPurge(t, ctx, control, time.Now(), journal) |
| 46 | if err := PreparePurgeSweep(ctx, control, w, versions, "control", "bab", "demo", "p", "old"); err != nil { |
| 47 | t.Fatal(err) |
| 48 | } |
| 49 | successor := versions.Put("control", key, []byte("same")) |
| 50 | if old == successor { |
| 51 | t.Fatal("test requires distinct versions") |
| 52 | } |
| 53 | if err := DeletePurgeSweep(ctx, control, w, versions, "control", "bab", "demo", "p", "old"); err != nil { |
| 54 | t.Fatal(err) |
| 55 | } |
| 56 | if _, err := versions.GetVersion(ctx, "control", key, old); !errors.Is(err, lease.ErrMirrorNotFound) { |
| 57 | t.Fatalf("old version: %v", err) |
| 58 | } |
| 59 | if _, err := versions.GetVersion(ctx, "control", key, successor); err != nil { |
| 60 | t.Fatalf("successor deleted: %v", err) |
| 61 | } |
| 62 | if err := VerifyPurgeSweep(ctx, control, w, versions, "control", "bab", "demo", "p", "old"); !errors.Is(err, ErrPending) { |
| 63 | t.Fatalf("unexpected new key must block completion: %v", err) |
| 64 | } |
| 65 | } |
| 66 | func TestClaimPurgeSweepDoneCannotRestart(t *testing.T) { |
| 67 | ctx := t.Context() |
| 68 | control := release.NewDirStore(t.TempDir()) |
| 69 | versions := leasetest.NewVersionedBucket() |
| 70 | versions.AddBucket("control") |
| 71 | j := PurgeJournal{V: 1, Purge: Purge{ID: "p", Phase: "done"}, AppID: "old", Account: "bab", App: "demo", Hosts: map[string]PurgeHost{}} |
| 72 | w := seedPurge(t, ctx, control, time.Now(), j) |
| 73 | if err := PreparePurgeSweep(ctx, control, w, versions, "control", "bab", "demo", "p", "old"); !errors.Is(err, ErrPending) { |
| 74 | t.Fatalf("done: %v", err) |
| 75 | } |
| 76 | } |
| 77 | |