| index d055094da..135eea120 100644 |
| --- a/internal/provision/applisting_test.go |
| +++ b/internal/provision/applisting_test.go |
| @@ -12,28 +12,74 @@ import ( |
| "github.com/carlosframework/platform/internal/release" |
| ) |
| |
| -// listCountingStore records every List prefix asked of it. |
| -type listCountingStore struct { |
| +// bucketShaped is a store that promises bucket-shaped ListDirs, as the S3 |
| +// store does, and records every listing asked of it. DirStore's own ListDirs |
| +// has that shape; it just does not advertise it. |
| +type bucketShaped struct { |
| release.Store |
| mu sync.Mutex |
| lists []string |
| + dirs []string |
| + // failDirs and failList fail one prefix each. |
| + failDirs, failList string |
| } |
| |
| -func (s *listCountingStore) List(ctx context.Context, prefix string) ([]string, error) { |
| +func (s *bucketShaped) BucketShapedListDirs() bool { return true } |
| + |
| +func (s *bucketShaped) List(ctx context.Context, prefix string) ([]string, error) { |
| s.mu.Lock() |
| s.lists = append(s.lists, prefix) |
| s.mu.Unlock() |
| + if prefix == s.failList { |
| + return nil, errors.New("list refused") |
| + } |
| return s.Store.List(ctx, prefix) |
| } |
| |
| +func (s *bucketShaped) ListDirs(ctx context.Context, prefix string) ([]string, error) { |
| + s.mu.Lock() |
| + s.dirs = append(s.dirs, prefix) |
| + s.mu.Unlock() |
| + if prefix == s.failDirs { |
| + return nil, errors.New("listdirs refused") |
| + } |
| + return s.Store.ListDirs(ctx, prefix) |
| +} |
| + |
| +func (s *bucketShaped) listedAllOfApps() bool { |
| + s.mu.Lock() |
| + defer s.mu.Unlock() |
| + for _, p := range s.lists { |
| + if p == "apps/" { |
| + return true |
| + } |
| + } |
| + return false |
| +} |
| + |
| +// fleetShaped answers ListDirs the way the fleet door does: scoped to one |
| +// account, ListDirs("apps/") is that account's APP directories. It makes no |
| +// bucket-shape promise, which is the point. |
| +type fleetShaped struct { |
| + release.Store |
| + acct string |
| +} |
| + |
| +func (s fleetShaped) ListDirs(ctx context.Context, prefix string) ([]string, error) { |
| + if prefix == "apps/" { |
| + return s.Store.ListDirs(ctx, "apps/"+s.acct+"/") |
| + } |
| + return s.Store.ListDirs(ctx, prefix) |
| +} |
| + |
| // A bucket with everything a production one holds under apps/: records of |
| // both kinds, the logs and release artifacts that dwarf them, an account that |
| -// has not materialized, a legacy account-less app, and an app named like a |
| -// release-plane word. |
| +// has not materialized, a legacy account-less app, and apps named like the |
| +// release plane's own words. |
| func seedAppListingBucket(t *testing.T) release.Store { |
| t.Helper() |
| s := release.NewDirStore(t.TempDir()) |
| - keys := []string{ |
| + for _, k := range []string{ |
| "apps/bab/shop/instances/a.example.com.json", |
| "apps/bab/shop/instances/b.example.com.json", |
| "apps/bab/shop/instances.json", |
| @@ -50,8 +96,7 @@ func seedAppListingBucket(t *testing.T) release.Store { |
| "apps/legacyapp/channels/edge.json", // account-less |
| "apps/bab/channels/instances/f.example.com.json", |
| "apps/bab/releases/instances/g.example.com.json", |
| - } |
| - for _, k := range keys { |
| + } { |
| if _, err := s.Put(context.Background(), k, []byte(`{}`), ""); err != nil { |
| t.Fatalf("seed %s: %v", k, err) |
| } |
| @@ -59,14 +104,23 @@ func seedAppListingBucket(t *testing.T) release.Store { |
| return s |
| } |
| |
| -// The narrow walk finds exactly the keys the full listing did, for both |
| -// record kinds, and never lists the whole of apps/. |
| -func TestAppSubtreeListingMatchesTheFullListing(t *testing.T) { |
| +func parsedRecords(keys []string, parse func(string) bool) []string { |
| + var out []string |
| + for _, k := range keys { |
| + if parse(k) { |
| + out = append(out, k) |
| + } |
| + } |
| + sort.Strings(out) |
| + return out |
| +} |
| + |
| +// On a bucket-shaped store the walk finds exactly the records the full |
| +// listing did, for both record kinds, and never lists the whole of apps/. |
| +func TestAppSubtreeWalkMatchesTheFullListing(t *testing.T) { |
| inner := seedAppListingBucket(t) |
| known := map[string]bool{"bab": true, "bad": true} |
| - ctx := context.Background() |
| - |
| - all, err := inner.List(ctx, "apps/") |
| + all, err := inner.List(context.Background(), "apps/") |
| if err != nil { |
| t.Fatal(err) |
| } |
| @@ -77,27 +131,13 @@ func TestAppSubtreeListingMatchesTheFullListing(t *testing.T) { |
| {"instances/", func(k string) bool { _, _, _, ok := ParseRecordKey(k, known); return ok }}, |
| {"sidecars/", func(k string) bool { _, _, _, ok := ParseSidecarKey(k, known); return ok }}, |
| } { |
| - var want []string |
| - for _, k := range all { |
| - if c.parse(k) { |
| - want = append(want, k) |
| - } |
| - } |
| - sort.Strings(want) |
| - |
| - s := &listCountingStore{Store: inner} |
| - got, partial, err := listAppSubtree(ctx, s, known, c.sub) |
| + s := &bucketShaped{Store: inner} |
| + got, partial, err := listAppSubtree(context.Background(), s, known, c.sub) |
| if err != nil || partial { |
| t.Fatalf("%s: partial=%v err=%v", c.sub, partial, err) |
| } |
| - var parsed []string |
| - for _, k := range got { |
| - if c.parse(k) { |
| - parsed = append(parsed, k) |
| - } |
| - } |
| - if !reflect.DeepEqual(parsed, want) { |
| - t.Fatalf("%s: narrow walk found %v, the full listing %v", c.sub, parsed, want) |
| + if g, w := parsedRecords(got, c.parse), parsedRecords(all, c.parse); !reflect.DeepEqual(g, w) { |
| + t.Fatalf("%s: the walk found %v, the full listing %v", c.sub, g, w) |
| } |
| for _, p := range s.lists { |
| if !strings.HasSuffix(p, "/"+c.sub) { |
| @@ -107,61 +147,135 @@ func TestAppSubtreeListingMatchesTheFullListing(t *testing.T) { |
| } |
| } |
| |
| -// The passes that read records on every serial bump use the narrow walk, and |
| -// never the full listing: on the flagship, apps/ held 59,288 objects (50,695 |
| -// of them logs) to find 152 records. |
| -func TestRecordReadersNeverListAllOfApps(t *testing.T) { |
| - s := &listCountingStore{Store: seedAppListingBucket(t)} |
| - // No account manifests: ops is always known, so the walk has bab to do. |
| - if _, err := ListAllRecords(context.Background(), s); err != nil { |
| - t.Fatalf("ListAllRecords: %v", err) |
| +// A store that makes no bucket-shape promise is listed the old way. The fleet |
| +// door's ListDirs("apps/") is its account's app directories; walked as if it |
| +// were account directories, it yields no records at all, which reconcile |
| +// would read as every route retired. |
| +func TestAppSubtreeListsTheOldWayWithoutTheBucketShape(t *testing.T) { |
| + fleet := fleetShaped{Store: seedAppListingBucket(t), acct: "bab"} |
| + known := map[string]bool{"bab": true} |
| + parse := func(k string) bool { _, _, _, ok := ParseRecordKey(k, known); return ok } |
| + got, partial, err := listAppSubtree(context.Background(), fleet, known, "instances/") |
| + if err != nil || partial { |
| + t.Fatalf("partial=%v err=%v", partial, err) |
| } |
| - walked := false |
| - for _, p := range s.lists { |
| - if p == "apps/" { |
| - t.Fatal("ListAllRecords listed the whole of apps/") |
| - } |
| - if strings.HasPrefix(p, "apps/bab/") { |
| - walked = true |
| - } |
| + want := []string{ |
| + "apps/bab/blog/instances/c.example.com.json", |
| + "apps/bab/shop/instances/a.example.com.json", |
| + "apps/bab/shop/instances/b.example.com.json", |
| + } |
| + if g := parsedRecords(got, parse); !reflect.DeepEqual(g, want) { |
| + t.Fatalf("records on a fleet-shaped store = %v, want %v", g, want) |
| } |
| - if !walked { |
| - t.Fatalf("ListAllRecords listed %v and never an app of ops's", s.lists) |
| + if release.HasBucketListDirs(release.NewDirStore(t.TempDir())) { |
| + t.Fatal("DirStore promises bucket-shaped ListDirs; it walks its whole root per List, so it must be listed the old way") |
| } |
| } |
| |
| -// failListStore fails List for one prefix only. |
| -type failListStore struct { |
| - release.Store |
| - fail string |
| +// One account's or one app's listing failing is partial, not fatal: the rest |
| +// still come back, and the caller is told some were not seen. |
| +func TestAppSubtreeOneListingFailingIsPartial(t *testing.T) { |
| + known := map[string]bool{"bab": true, "bad": true} |
| + parse := func(k string) bool { _, _, _, ok := ParseRecordKey(k, known); return ok } |
| + for _, c := range []struct { |
| + name string |
| + s *bucketShaped |
| + want []string |
| + }{ |
| + {"an app", &bucketShaped{failList: "apps/bab/blog/instances/"}, []string{ |
| + "apps/bab/shop/instances/a.example.com.json", |
| + "apps/bab/shop/instances/b.example.com.json", |
| + "apps/bad/site/instances/d.example.com.json", |
| + }}, |
| + {"an account", &bucketShaped{failDirs: "apps/bab/"}, []string{ |
| + "apps/bad/site/instances/d.example.com.json", |
| + }}, |
| + } { |
| + c.s.Store = seedAppListingBucket(t) |
| + got, partial, err := listAppSubtree(context.Background(), c.s, known, "instances/") |
| + if err != nil { |
| + t.Fatalf("%s: one failed listing failed the walk: %v", c.name, err) |
| + } |
| + if !partial { |
| + t.Fatalf("%s: a failed listing was not reported as partial", c.name) |
| + } |
| + if g := parsedRecords(got, parse); !reflect.DeepEqual(g, c.want) { |
| + t.Fatalf("%s: records = %v, want %v", c.name, g, c.want) |
| + } |
| + } |
| } |
| |
| -func (s failListStore) List(ctx context.Context, prefix string) ([]string, error) { |
| - if prefix == s.fail { |
| - return nil, errors.New("list refused") |
| +// All three record readers, on a bucket-shaped store with real records: they |
| +// find the records, mint the same route they mint off a plain store, and |
| +// never list the whole of apps/. |
| +func TestRecordReadersWalkABucketShapedStore(t *testing.T) { |
| + inner, sqid := setup(t) |
| + if err := CreateRecord(ctx, inner, acmeRecord(sqid)); err != nil { |
| + t.Fatal(err) |
| } |
| - return s.Store.List(ctx, prefix) |
| -} |
| + cfg := ReconcileConfig{Region: "ie", Pools: []string{"tito"}, DataDir: "/data"} |
| |
| -// One app's listing failing is partial, not fatal: every other app's keys |
| -// still come back, and the caller is told some were not seen. |
| -func TestAppSubtreeListingOneAppFailingIsPartial(t *testing.T) { |
| - s := failListStore{Store: seedAppListingBucket(t), fail: "apps/bab/blog/instances/"} |
| - got, partial, err := listAppSubtree(context.Background(), s, map[string]bool{"bab": true}, "instances/") |
| + plainReg := regWith(t) |
| + if err := Reconcile(ctx, inner, plainReg, cfg); err != nil { |
| + t.Fatal(err) |
| + } |
| + s := &bucketShaped{Store: inner} |
| + reg := regWith(t) |
| + if err := Reconcile(ctx, s, reg, cfg); err != nil { |
| + t.Fatal(err) |
| + } |
| + want, got := *mustGet(t, plainReg, "acme.go.tito.io"), *mustGet(t, reg, "acme.go.tito.io") |
| + want.CreatedAt, got.CreatedAt = 0, 0 |
| + if got != want { |
| + t.Fatalf("minted over the walk = %+v, over the full listing %+v", got, want) |
| + } |
| + |
| + recs, err := ListAllRecords(ctx, s) |
| if err != nil { |
| - t.Fatalf("one app's failed listing failed the walk: %v", err) |
| + t.Fatal(err) |
| } |
| - if !partial { |
| - t.Fatal("a failed app listing was not reported as partial") |
| + if len(recs) != 1 || recs[0].Host != "acme.go.tito.io" { |
| + t.Fatalf("ListAllRecords = %+v, want the one acme record", recs) |
| } |
| - var records []string |
| - for _, k := range got { |
| - if _, _, _, ok := ParseRecordKey(k, map[string]bool{"bab": true}); ok { |
| - records = append(records, k) |
| - } |
| + if err := ReconcileSidecars(ctx, s, reg, cfg); err != nil { |
| + t.Fatal(err) |
| + } |
| + if s.listedAllOfApps() { |
| + t.Fatalf("a record reader listed the whole of apps/: %v", s.lists) |
| + } |
| +} |
| + |
| +// Reconcile treats a partial walk as it treats an unreadable record: the |
| +// routes it could not see are not retired. |
| +func TestReconcileKeepsRoutesItCouldNotList(t *testing.T) { |
| + inner, sqid := setup(t) |
| + if err := CreateRecord(ctx, inner, acmeRecord(sqid)); err != nil { |
| + t.Fatal(err) |
| + } |
| + cfg := ReconcileConfig{Region: "ie", Pools: []string{"tito"}, DataDir: "/data"} |
| + reg := regWith(t) |
| + if err := Reconcile(ctx, inner, reg, cfg); err != nil { |
| + t.Fatal(err) |
| + } |
| + s := &bucketShaped{Store: inner, failList: RecordsPrefix(sqid, "tito")} |
| + if err := Reconcile(ctx, s, reg, cfg); err != nil { |
| + t.Fatal(err) |
| + } |
| + if rt, err := reg.Get("acme.go.tito.io"); err != nil || rt == nil { |
| + t.Fatalf("a route whose records could not be listed was removed: %+v, %v", rt, err) |
| + } |
| +} |
| + |
| +// ListAllRecords refuses a partial walk rather than report a short record |
| +// set: the moves and steering passes act on what it returns, and a missing |
| +// record reads to them as an instance that no longer exists. |
| +func TestListAllRecordsRefusesAPartialWalk(t *testing.T) { |
| + inner, sqid := setup(t) |
| + if err := CreateRecord(ctx, inner, acmeRecord(sqid)); err != nil { |
| + t.Fatal(err) |
| } |
| - want := []string{"apps/bab/shop/instances/a.example.com.json", "apps/bab/shop/instances/b.example.com.json"} |
| - if !reflect.DeepEqual(records, want) { |
| - t.Fatalf("records = %v, want %v", records, want) |
| + s := &bucketShaped{Store: inner, failList: RecordsPrefix(sqid, "tito")} |
| + if recs, err := ListAllRecords(ctx, s); err == nil { |
| + t.Fatalf("ListAllRecords over a failed app listing = %+v, nil; want an error", recs) |
| } |
| } |