| 1 | package hostbox |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "encoding/json" |
| 6 | "github.com/carlosframework/platform/internal/hostguard" |
| 7 | "github.com/carlosframework/platform/internal/registry" |
| 8 | "os" |
| 9 | "path/filepath" |
| 10 | "testing" |
| 11 | ) |
| 12 | |
| 13 | type noFiles struct{} |
| 14 | |
| 15 | func (noFiles) Park(context.Context, string, Artifact) error { return ErrTypedInventory } |
| 16 | func TestRegistryRowsArchivesInRetirementJournal(t *testing.T) { |
| 17 | reg, err := registry.Open(filepath.Join(t.TempDir(), "registry.db")) |
| 18 | if err != nil { |
| 19 | t.Fatal(err) |
| 20 | } |
| 21 | defer reg.Close() |
| 22 | owner := registry.ClaimOwnership{ClaimIncarnation: "inc", ClaimStamp: 7, ClaimOwner: "owner", ClaimRowID: "route"} |
| 23 | route := registry.Route{Host: "shop.example", ClaimOwnership: owner, App: "app"} |
| 24 | if err = reg.Upsert(route); err != nil { |
| 25 | t.Fatal(err) |
| 26 | } |
| 27 | in := RetirementInventory{Host: route.Host, Captured: hostguard.Captured{Incarnation: "inc", Stamp: 7}, Ownership: "owner", Artifacts: []Artifact{{Kind: ArtifactRoute, Root: "registry", Name: route.Host, Owner: "owner", RowID: "route"}}} |
| 28 | intent, err := NewRetirementJournal("retire", in) |
| 29 | if err != nil { |
| 30 | t.Fatal(err) |
| 31 | } |
| 32 | target := hostguard.FenceTarget{Host: route.Host, Incarnation: "inc", Stamp: 7} |
| 33 | // Created by the retirement itself, 0700 whatever the test umask. |
| 34 | dir := filepath.Join(t.TempDir(), "journal") |
| 35 | acks := 0 |
| 36 | r := TypedRetirement{JournalDir: dir, Authority: &cleanupAuthority{}, Filesystem: noFiles{}, Rows: RegistryRows{Registry: reg, Inventory: in}, Ack: func(context.Context, RetirementJournal) error { acks++; return nil }} |
| 37 | if err = r.Run(context.Background(), target, intent); err != nil { |
| 38 | t.Fatal(err) |
| 39 | } |
| 40 | body, err := os.ReadFile(filepath.Join(dir, "retire.json")) |
| 41 | if err != nil { |
| 42 | t.Fatal(err) |
| 43 | } |
| 44 | var j RetirementJournal |
| 45 | if err = json.Unmarshal(body, &j); err != nil { |
| 46 | t.Fatal(err) |
| 47 | } |
| 48 | var archive []registry.ClaimRowArchive |
| 49 | if err = json.Unmarshal(j.Archives, &archive); err != nil { |
| 50 | t.Fatal(err) |
| 51 | } |
| 52 | if !j.Complete || len(archive) != 1 || archive[0].Row.Ownership != owner { |
| 53 | t.Fatalf("incomplete journal: %+v", j) |
| 54 | } |
| 55 | route.ClaimStamp++ |
| 56 | route.ClaimRowID = "successor" |
| 57 | route.ClaimOwner = "new-owner" |
| 58 | if err = reg.Upsert(route); err != nil { |
| 59 | t.Fatal(err) |
| 60 | } |
| 61 | if err = r.Run(context.Background(), target, intent); err != nil { |
| 62 | t.Fatal(err) |
| 63 | } |
| 64 | got, err := reg.Get(route.Host) |
| 65 | if err != nil || got == nil || got.ClaimRowID != "successor" || acks != 2 { |
| 66 | t.Fatalf("replay touched successor: %v", err) |
| 67 | } |
| 68 | } |
| 69 | |