| 1 | package hostops |
| 2 | |
| 3 | import ( |
| 4 | "errors" |
| 5 | "testing" |
| 6 | |
| 7 | "github.com/carlosframework/platform/internal/hostclaim" |
| 8 | ) |
| 9 | |
| 10 | func TestPurgeRetirementRequiresFree(t *testing.T) { |
| 11 | holder := &hostclaim.Holder{Incarnation: "old"} |
| 12 | p := PurgeHost{Holder: holder, FreeFromGen: 10} |
| 13 | c := hostclaim.Claim{Gen: 11, Holder: holder, RetiredIncarnations: []string{"old"}, |
| 14 | Op: hostclaim.Op{Kind: hostclaim.KindParking, ID: "purge"}, |
| 15 | Obligations: []hostclaim.Obligation{{ID: "park", Op: "purge", Kind: "park-local", Box: "box", Owner: "box:box", Retirement: "retirement"}}} |
| 16 | if purgeHostFreed(c, p) { |
| 17 | t.Fatal("history bypassed unfinished parking") |
| 18 | } |
| 19 | c.Obligations = nil |
| 20 | if purgeHostFreed(c, p) { |
| 21 | t.Fatal("empty chores bypassed FreeAfterPark") |
| 22 | } |
| 23 | c.Holder = nil |
| 24 | c.Op = hostclaim.Op{} |
| 25 | if !purgeHostFreed(c, p) { |
| 26 | t.Fatal("completed free not recognized") |
| 27 | } |
| 28 | c.Holder = &hostclaim.Holder{Incarnation: "successor"} |
| 29 | if !purgeHostFreed(c, p) { |
| 30 | t.Fatal("successor hid completed retirement") |
| 31 | } |
| 32 | c.RetiredIncarnations = nil |
| 33 | p.FreeFromGen = 0 |
| 34 | if purgeHostFreed(c, p) { |
| 35 | t.Fatal("unproven successor accepted") |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | func TestRetirePurgeHostsDoesNotCompleteParking(t *testing.T) { |
| 40 | f := newFixture(t) |
| 41 | host := "one.bab.example.com" |
| 42 | record := f.record(t, host) |
| 43 | before, err := hostclaim.Read(f.ctx, f.store, host) |
| 44 | if err != nil { |
| 45 | t.Fatal(err) |
| 46 | } |
| 47 | obs, err := hostclaim.Update(f.ctx, f.store, before, hostclaim.BeginCreate(record.Holder, record.Body, "create", "test", f.now)) |
| 48 | if err != nil { |
| 49 | t.Fatal(err) |
| 50 | } |
| 51 | obs, err = hostclaim.Update(f.ctx, f.store, obs, hostclaim.CompleteCreate("create", "box-a")) |
| 52 | if err != nil { |
| 53 | t.Fatal(err) |
| 54 | } |
| 55 | obs, err = hostclaim.Update(f.ctx, f.store, obs, hostclaim.BeginDelete("purge", "test", "record-tag", f.now, nil, nil, "")) |
| 56 | if err != nil { |
| 57 | t.Fatal(err) |
| 58 | } |
| 59 | obs, err = hostclaim.Update(f.ctx, f.store, obs, hostclaim.ToParking("purge", nil, nil, f.c.NewID)) |
| 60 | if err != nil { |
| 61 | t.Fatal(err) |
| 62 | } |
| 63 | if len(obs.Claim.Obligations) == 0 { |
| 64 | t.Fatal("fixture has no outstanding parking") |
| 65 | } |
| 66 | j := PurgeJournal{V: 1, Purge: Purge{ID: "purge", Phase: "torn-down"}, Account: "bab", App: "jam", AppID: "app-id", |
| 67 | Hosts: map[string]PurgeHost{host: {Holder: obs.Claim.Holder}}} |
| 68 | f.c.Control = seedPurge(t, f.ctx, f.store, f.now, j) |
| 69 | if err = f.c.RetirePurgeHosts(f.ctx, "bab", "jam", "purge", "app-id"); !errors.Is(err, ErrPending) { |
| 70 | t.Fatalf("parking completion: %v", err) |
| 71 | } |
| 72 | saved, _, err := ReadPurgeJournal(f.ctx, f.store, "bab", "jam", "purge", "app-id") |
| 73 | if err != nil { |
| 74 | t.Fatal(err) |
| 75 | } |
| 76 | if saved.Hosts[host].Retired { |
| 77 | t.Fatal("journal completed before freeing") |
| 78 | } |
| 79 | } |
| 80 | |