| 1 | package host |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "os" |
| 6 | "strings" |
| 7 | "testing" |
| 8 | |
| 9 | "github.com/carlosframework/platform/internal/hostfiles" |
| 10 | "github.com/carlosframework/platform/internal/lease" |
| 11 | "github.com/carlosframework/platform/internal/lease/leasetest" |
| 12 | "github.com/carlosframework/platform/internal/park" |
| 13 | "github.com/carlosframework/platform/internal/release" |
| 14 | ) |
| 15 | |
| 16 | // bucketStore is a release.Store over one bucket of a versioned bucket, |
| 17 | // as production's S3 store is over the data bucket its versioned client |
| 18 | // lists: current reads with the current version as the token, create-only |
| 19 | // and compare-and-swap writes, deletes as delete markers. |
| 20 | type bucketStore struct { |
| 21 | b *leasetest.VersionedBucket |
| 22 | bucket string |
| 23 | } |
| 24 | |
| 25 | func (s bucketStore) Get(ctx context.Context, key string) ([]byte, string, error) { |
| 26 | return casBucketStore(s).Get(ctx, key) |
| 27 | } |
| 28 | |
| 29 | func (s bucketStore) Put(ctx context.Context, key string, body []byte, ifMatch string) (string, error) { |
| 30 | return casBucketStore(s).Put(ctx, key, body, ifMatch) |
| 31 | } |
| 32 | |
| 33 | func (s bucketStore) List(ctx context.Context, prefix string) ([]string, error) { |
| 34 | return casBucketStore(s).List(ctx, prefix) |
| 35 | } |
| 36 | |
| 37 | func (s bucketStore) ListDirs(ctx context.Context, prefix string) ([]string, error) { |
| 38 | keys, err := s.List(ctx, prefix) |
| 39 | if err != nil { |
| 40 | return nil, err |
| 41 | } |
| 42 | seen := map[string]bool{} |
| 43 | var out []string |
| 44 | for _, k := range keys { |
| 45 | dir, _, ok := strings.Cut(strings.TrimPrefix(k, prefix), "/") |
| 46 | if ok && !seen[dir] { |
| 47 | seen[dir] = true |
| 48 | out = append(out, dir) |
| 49 | } |
| 50 | } |
| 51 | return out, nil |
| 52 | } |
| 53 | |
| 54 | func (s bucketStore) Delete(_ context.Context, key string) error { |
| 55 | if _, ok := s.b.Current(s.bucket, key); !ok { |
| 56 | return os.ErrNotExist |
| 57 | } |
| 58 | s.b.BareDelete(s.bucket, key) |
| 59 | return nil |
| 60 | } |
| 61 | |
| 62 | var ( |
| 63 | _ release.Store = bucketStore{} |
| 64 | _ lease.VersionedStore = (*leasetest.VersionedBucket)(nil) |
| 65 | ) |
| 66 | |
| 67 | // Merge round 6, finding 1: relocation names a generation's files by the |
| 68 | // one files prefix rule (moves.GenerationFilesPrefix), so what its build |
| 69 | // writes is what main's provider and the park view read. P's prepared |
| 70 | // manifest is at movesFilesPrefix(location, P); every files object under |
| 71 | // the location belongs to a generation by the park view's classifier |
| 72 | // (landingGenerationOf), where the old files/<generation>/ layout |
| 73 | // belonged to none and refused every park of the location; and the files |
| 74 | // live set pins every object P's manifest names, which it never read |
| 75 | // under the old layout. |
| 76 | func TestRelocationFilesUseTheParkViewsPrefix(t *testing.T) { |
| 77 | f := newBuildFixtureWith(t, buildOptions{Files: map[string]string{"public/a.txt": "a", "uploads/b.bin": "b"}}) |
| 78 | if err := f.build(); err != nil { |
| 79 | t.Fatal(err) |
| 80 | } |
| 81 | ctx := t.Context() |
| 82 | location := f.obs.Claim.Holder.Location |
| 83 | m, found, err := hostfiles.Load(ctx, f.files, movesFilesPrefix(location, "p1")) |
| 84 | if err != nil || !found || m.Seq != 0 || len(m.Entries) != 2 { |
| 85 | t.Fatalf("P's prepared manifest at the files prefix rule: found=%v %+v %v", found, m, err) |
| 86 | } |
| 87 | listing, err := f.bucket.ListVersions(ctx, "data", location+"/files/") |
| 88 | if err != nil || len(listing) == 0 { |
| 89 | t.Fatalf("listing: %v", err) |
| 90 | } |
| 91 | for _, x := range listing { |
| 92 | if g, ok := landingGenerationOf(location, x.Key); !ok || (g != "g1" && g != "p1") { |
| 93 | t.Fatalf("%s belongs to %q (%v), not a generation", x.Key, g, ok) |
| 94 | } |
| 95 | } |
| 96 | if _, ok := landingGenerationOf(location, location+"/files/p1/manifest.json"); ok { |
| 97 | t.Fatal("the old files/<generation>/ layout is classified") |
| 98 | } |
| 99 | v := park.View{GenerationOf: func(r park.Ref) (string, bool) { return landingGenerationOf(location, r.Key) }} |
| 100 | if err := pinFilesLiveSet(ctx, f.bucket, "data", location, listing, &v, "p1"); err != nil { |
| 101 | t.Fatal(err) |
| 102 | } |
| 103 | for _, e := range m.Entries { |
| 104 | if !v.Pinned[park.Ref{Key: e.Object, VersionID: f.bucket.LatestID("data", e.Object)}] { |
| 105 | t.Fatalf("%s, which P's manifest names, is not pinned: %v", e.Object, v.Pinned) |
| 106 | } |
| 107 | } |
| 108 | } |
| 109 | |