| index 4251ab780..fcec2c191 100644 |
| --- a/internal/edgesfile/addrs_test.go |
| +++ b/internal/edgesfile/addrs_test.go |
| @@ -259,3 +259,87 @@ func (e *etagForwarding) ListETags(ctx context.Context, prefix string) ([]releas |
| e.listETags++ |
| return e.lister.ListETags(ctx, prefix) |
| } |
| + |
| +// A published IPv4-mapped entry is held as IPv4, as a query is, so the |
| +// published address and its range find their box either way. |
| +func TestReadAddrsHolderMatchesMappedPublications(t *testing.T) { |
| + t.Parallel() |
| + dir := release.NewDirStore(t.TempDir()) |
| + putEdgesBody(t, dir, "annie", `{"v":1,"box":"annie","eip":"","addrs":["::ffff:10.0.1.5","::ffff:10.0.2.0/120"],"hosts":{}}`) |
| + inv, err := ReadAddrs(context.Background(), dir) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + if got := fmt.Sprint(inv.Boxes["annie"]); got != "[10.0.1.5/32 10.0.2.0/24]" { |
| + t.Errorf("annie = %s, want the entries held as IPv4", got) |
| + } |
| + for addr, want := range map[string]string{ |
| + "10.0.1.5": "annie", |
| + "::ffff:10.0.1.5": "annie", |
| + "10.0.2.7": "annie", |
| + "::ffff:10.0.2.7": "annie", |
| + "10.0.1.6": "", |
| + } { |
| + box, ok := inv.Holder(netip.MustParseAddr(addr)) |
| + if box != want || ok != (want != "") { |
| + t.Errorf("Holder(%s) = %q, %v; want %q", addr, box, ok, want) |
| + } |
| + } |
| +} |
| + |
| +// rewriteBeforeGet lists with ETags and, once armed, replaces the object |
| +// with next just before the Get answers: a box's write landing between the |
| +// listing and the fetch. |
| +type rewriteBeforeGet struct { |
| + *release.DirStore |
| + t *testing.T |
| + box, next string |
| + gets int |
| +} |
| + |
| +func (r *rewriteBeforeGet) Get(ctx context.Context, key string) ([]byte, string, error) { |
| + r.gets++ |
| + if r.next != "" && key == Key(r.box) { |
| + replaceEdgesBody(r.t, r.DirStore, r.box, r.next) |
| + r.next = "" |
| + } |
| + return r.DirStore.Get(ctx, key) |
| +} |
| + |
| +// The cache keeps the body under the ETag the Get returned with it, not |
| +// the listing's: a body that changed between the two is never filed under |
| +// the older version, so the object returning to that version is read again. |
| +func TestReadAddrsCachedKeepsTheETagItsBodyCameWith(t *testing.T) { |
| + t.Parallel() |
| + ctx := context.Background() |
| + const ( |
| + bodyA = `{"v":1,"box":"annie","eip":"","addrs":["10.0.1.5"],"hosts":{}}` |
| + bodyB = `{"v":1,"box":"annie","eip":"","addrs":["10.0.1.6"],"hosts":{}}` |
| + ) |
| + dir := release.NewDirStore(t.TempDir()) |
| + putEdgesBody(t, dir, "annie", bodyA) |
| + s := &rewriteBeforeGet{DirStore: dir, t: t, box: "annie", next: bodyB} |
| + var c AddrCache |
| + read := func(step string, wantGets int, want string) { |
| + t.Helper() |
| + s.gets = 0 |
| + inv, err := ReadAddrsCached(ctx, s, &c) |
| + if err != nil { |
| + t.Fatalf("%s: %v", step, err) |
| + } |
| + if s.gets != wantGets || fmt.Sprint(inv.Boxes["annie"]) != want { |
| + t.Fatalf("%s: %d Get, annie = %v; want %d, %s", step, s.gets, inv.Boxes["annie"], wantGets, want) |
| + } |
| + } |
| + // Listed as A, fetched as B. |
| + read("rewritten between list and get", 1, "[10.0.1.6/32]") |
| + // Listed as B: the cache already holds B. |
| + read("listed as the fetched version", 0, "[10.0.1.6/32]") |
| + // Again listed as A, fetched as B; then the object returns to A. |
| + replaceEdgesBody(t, dir, "annie", bodyA) |
| + s.next = bodyB |
| + read("rewritten again", 1, "[10.0.1.6/32]") |
| + replaceEdgesBody(t, dir, "annie", bodyA) |
| + read("returned to A", 1, "[10.0.1.5/32]") |
| + read("A unchanged", 0, "[10.0.1.5/32]") |
| +} |