| 1 | package hostrelocate |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "encoding/json" |
| 6 | "strings" |
| 7 | "testing" |
| 8 | "time" |
| 9 | |
| 10 | "github.com/carlosframework/platform/internal/genselect" |
| 11 | "github.com/carlosframework/platform/internal/hostfiles" |
| 12 | "github.com/carlosframework/platform/internal/lease/leasetest" |
| 13 | "github.com/carlosframework/platform/internal/release" |
| 14 | "github.com/superfly/ltx" |
| 15 | ) |
| 16 | |
| 17 | // Stage B Task 10's binding: the pass's StorePinner selects the database |
| 18 | // with genselect.Select over the region's versioned data bucket. The pin |
| 19 | // names exactly the versions the bucket holds for the holder's generation |
| 20 | // (its marker's snapshot and the level-0 file after it), the plan's MaxTXID, |
| 21 | // and the release the pinner's Release answers. |
| 22 | func TestRelocationPinnerSelectsOverTheVersionedBucket(t *testing.T) { |
| 23 | ctx := context.Background() |
| 24 | hdr := func(min, max uint64) []byte { |
| 25 | b, err := (<x.Header{Version: ltx.Version, Flags: ltx.HeaderFlagNoChecksum, PageSize: 4096, MinTXID: ltx.TXID(min), MaxTXID: ltx.TXID(max)}).MarshalBinary() |
| 26 | if err != nil { |
| 27 | t.Fatal(err) |
| 28 | } |
| 29 | return b |
| 30 | } |
| 31 | b := leasetest.NewVersionedBucket() |
| 32 | b.AddBucket("data") |
| 33 | snap := pinLocation + "/g-g3/0009/" + ltx.FormatFilename(1, 40) |
| 34 | snapVersion := b.Put("data", snap, hdr(1, 40)) |
| 35 | tail := pinLocation + "/g-g3/0000/" + ltx.FormatFilename(41, 41) |
| 36 | tailVersion := b.Put("data", tail, hdr(41, 41)) |
| 37 | marker, _ := json.Marshal(genselect.Marker{Op: "g3", Incarnation: "inc", Host: host, Box: "src", InstalledAt: time.Date(2026, 10, 7, 8, 0, 0, 0, time.UTC), |
| 38 | ManifestSHA256: strings.Repeat("a", 64), Pin: genselect.Pin{SnapshotKey: snap, SnapshotVersion: snapVersion, SnapshotTXID: 40, SnapshotCRC64: 1}}) |
| 39 | b.Put("data", pinLocation+"/generation.json", marker) |
| 40 | claims, data := release.NewDirStore(t.TempDir()), release.NewDirStore(t.TempDir()) |
| 41 | putManifest(t, data, "g3", keyed(hostfiles.Manifest{V: hostfiles.ManifestVersion, Seq: 5, Entries: map[string]hostfiles.Entry{}, Platform: map[string]hostfiles.Entry{}})) |
| 42 | p := StorePinner{Claims: claims, Data: data, Release: routeRelease, Bucket: "data", Region: "ie", |
| 43 | SelectDB: func(ctx context.Context, r genselect.Request) (*genselect.Plan, error) { |
| 44 | return genselect.Select(ctx, b, r) |
| 45 | }} |
| 46 | obs := pinClaim() |
| 47 | obs.Claim.Holder.Incarnation = "inc" |
| 48 | pin, err := p.Pin(ctx, obs, time.Date(2026, 10, 7, 9, 0, 0, 0, time.UTC)) |
| 49 | if err != nil { |
| 50 | t.Fatal(err) |
| 51 | } |
| 52 | if pin.Generation != "g3" || pin.MaxTXID != 41 || pin.Release != "v1.4.2" || pin.FilesSeq != 5 || pin.Empty { |
| 53 | t.Fatalf("pin %+v", pin) |
| 54 | } |
| 55 | obj, err := ReadPin(ctx, claims, pin) |
| 56 | if err != nil { |
| 57 | t.Fatal(err) |
| 58 | } |
| 59 | got := map[string]string{} |
| 60 | for _, f := range obj.DB.Files { |
| 61 | got[f.Key] = f.VersionID |
| 62 | } |
| 63 | if obj.DB.MaxTXID != 41 || got[snap] != snapVersion || got[tail] != tailVersion || len(got) != 2 { |
| 64 | t.Fatalf("pinned files %v (max %d)", got, obj.DB.MaxTXID) |
| 65 | } |
| 66 | } |
| 67 | |