| index cc786630f..3f2cff1cb 100644 |
| --- a/internal/console/fleetbox_test.go |
| +++ b/internal/console/fleetbox_test.go |
| @@ -599,3 +599,158 @@ func TestFleetViewReadsBoxes(t *testing.T) { |
| t.Fatalf("boxes %+v", v.Boxes) |
| } |
| } |
| + |
| +// Review fix round 1, finding 1: a registered active box that has no |
| +// heartbeat has no version the gate can read, so the gate stays closed. |
| +func TestGateNeedsAHeartbeatFromEveryActiveBox(t *testing.T) { |
| + fx, token, reg := fleetBoxWorld(t, "code-fb-no-beat", "pi-1", "pi-2") |
| + wantLabel(t, createShopAt(t, fx, token, "z.example.com"), "pi-1") // open with both reporting |
| + registerFleetBox(t, reg, "pi", "pi-3") // attached, never reported |
| + wantNoLabel(t, createShopAt(t, fx, token, "a.example.com")) |
| + v, err := fx.srv.loadFleetView(context.Background(), "pi") |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + if v.DeploymentCurrent || v.labelActive() { |
| + t.Fatalf("view %+v; want the gate closed while pi-3 has no heartbeat", v) |
| + } |
| + piBeat(t, fx, "pi-3", true, time.Now(), release.FleetBoxLabelFloor) |
| + wantLabel(t, createShopAt(t, fx, token, "b.example.com"), "pi-2") |
| +} |
| + |
| +// failETagStore fails every listing with ETags under prefix; the plain |
| +// listing still answers. |
| +type failETagStore struct { |
| + release.Store |
| + prefix string |
| +} |
| + |
| +func (f failETagStore) ListETags(ctx context.Context, prefix string) ([]release.KeyETag, error) { |
| + if strings.HasPrefix(prefix, f.prefix) { |
| + return nil, fmt.Errorf("injected listing failure") |
| + } |
| + inner := f.Store |
| + if d, ok := inner.(*deletableStore); ok { |
| + inner = d.Store |
| + } |
| + return inner.(release.ETagLister).ListETags(ctx, prefix) |
| +} |
| + |
| +// Review fix round 1, finding 2: a failed listing with ETags is a failed |
| +// listing. The inventory does not fall back to a plain listing that |
| +// succeeds, and the gate stays closed. |
| +func TestInventoryListFailureClosesTheGate(t *testing.T) { |
| + fx, token, _ := fleetBoxWorld(t, "code-fb-list-fail", "pi-1", "pi-2") |
| + putFleetRecord(t, fx, "shop", "x.example.com", nil) |
| + base := fx.srv.cfg.Releases |
| + fx.srv.cfg.Releases = failETagStore{Store: base, prefix: "nothing/"} |
| + wantLabel(t, createShopAt(t, fx, token, "z.example.com"), "pi-1") // open through the wrapper |
| + fx.srv.cfg.Releases = failETagStore{Store: base, prefix: provision.RecordsPrefix(fx.sqid, "shop")} |
| + if _, _, complete, err := fx.srv.fleetInventoryFor(context.Background(), "pi"); err != nil || complete { |
| + t.Fatalf("inventory with a failed listing: complete %v, %v; want incomplete", complete, err) |
| + } |
| + wantNoLabel(t, createShopAt(t, fx, token, "a.example.com")) |
| + fx.srv.cfg.Releases = failETagStore{Store: base, prefix: provision.SidecarsPrefix(fx.sqid, "shop")} |
| + wantNoLabel(t, createShopAt(t, fx, token, "b.example.com")) |
| +} |
| + |
| +// Review fix round 1, finding 3 (controller ruling): a static record is |
| +// never labelled and does not close the gate, and it holds no load. |
| +func TestStaticRecordsAreNeitherLabelledNorCounted(t *testing.T) { |
| + fx, token, _ := fleetBoxWorld(t, "code-fb-static", "pi-1", "pi-2") |
| + putFleetRecord(t, fx, "shop", "site.example.com", func(r *provision.Record) { r.Kind = "static" }) |
| + if code, out := createShop(t, fx, token, `{"host":"www.example.com","kind":"static"}`); code != http.StatusCreated { |
| + t.Fatalf("static create = %d %s", code, out) |
| + } |
| + rec, _ := storedShop(t, fx, "www.example.com") |
| + wantNoLabel(t, rec) |
| + v, err := fx.srv.loadFleetView(context.Background(), "pi") |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + if !v.labelActive() || v.Unmovable || len(v.Load) != 0 { |
| + t.Fatalf("view %+v; want the gate open, nothing unmovable and no load from static records", v) |
| + } |
| + wantLabel(t, createShopAt(t, fx, token, "a.example.com"), "pi-1") |
| + wantLabel(t, createShopAt(t, fx, token, "b.example.com"), "pi-2") |
| + for _, tc := range []struct { |
| + rec provision.Record |
| + want bool |
| + }{ |
| + {provision.Record{Kind: "static"}, false}, |
| + {provision.Record{}, true}, |
| + {provision.Record{Kind: "instance"}, true}, |
| + {provision.Record{Runtime: provision.RuntimeRootfsV1}, true}, |
| + } { |
| + if got := fleetLabelsRecord(tc.rec); got != tc.want { |
| + t.Errorf("fleetLabelsRecord(kind %q, runtime %q) = %v; want %v", tc.rec.Kind, tc.rec.Runtime, got, tc.want) |
| + } |
| + } |
| +} |
| + |
| +// slotGets is how many incarnation-slot reads were made. |
| +func (c *countStore) slotGets() int { |
| + c.mu.Lock() |
| + defer c.mu.Unlock() |
| + n := 0 |
| + for k, v := range c.gets { |
| + if strings.HasPrefix(k, "control/host-incarnations/") { |
| + n += v |
| + } |
| + } |
| + return n |
| +} |
| + |
| +// Review fix round 1, finding 4: with lifetimes on, a warm load reads no |
| +// record body but judges every record again, one slot read each; the |
| +// verdict follows a slot that changed under an unchanged record; and a slot |
| +// that cannot be read makes the inventory incomplete and closes the gate. |
| +func TestInventoryJudgesEveryRecordEachLoad(t *testing.T) { |
| + fx, _, _ := fleetBoxWorld(t, "code-fb-lifetimes", "pi-1", "pi-2") |
| + hosts := []string{"a.example.com", "b.example.com", "c.example.com"} |
| + for _, h := range hosts { |
| + putFleetRecord(t, fx, "shop", h, func(r *provision.Record) { r.Incarnation = 1 }) |
| + writeSlot(t, fx.rel, h, provision.HostIncarnation{Incarnation: 1, State: provision.SlotLive, Account: fx.sqid, App: "shop"}) |
| + } |
| + cs := newCountStore(fx.rel) |
| + fx.srv.cfg.Releases = cs |
| + ctx := provision.WithLifetimes(context.Background(), provision.NewLifetimeJudge()) |
| + load := func(want int) { |
| + t.Helper() |
| + recs, _, complete, err := fx.srv.fleetInventoryFor(ctx, "pi") |
| + if err != nil || !complete || len(recs) != want { |
| + t.Fatalf("inventory: %d records, complete %v, %v; want %d", len(recs), complete, err, want) |
| + } |
| + } |
| + load(3) // cold |
| + cs.reset() |
| + load(3) // warm |
| + if r, s := cs.recordGets(fx.sqid, "shop"), cs.slotGets(); r != 0 || s != 3 { |
| + t.Fatalf("a warm judged load read %d records and %d slots; want 0 and one slot per record", r, s) |
| + } |
| + v, err := fx.srv.loadFleetView(ctx, "pi") |
| + if err != nil || !v.labelActive() { |
| + t.Fatalf("view %+v, %v; want the gate open", v, err) |
| + } |
| + |
| + // b's slot moves on to a later lifetime; its record's ETag does not. |
| + writeSlot(t, fx.rel, "b.example.com", provision.HostIncarnation{Incarnation: 2, State: provision.SlotLive, Account: fx.sqid, App: "shop"}) |
| + recs, _, complete, err := fx.srv.fleetInventoryFor(ctx, "pi") |
| + if err != nil || !complete || len(recs) != 2 { |
| + t.Fatalf("after b's slot moved: %d records, complete %v, %v; want a and c", len(recs), complete, err) |
| + } |
| + for _, r := range recs { |
| + if r.Host == "b.example.com" { |
| + t.Fatal("the inventory kept b, whose slot no longer vouches for it") |
| + } |
| + } |
| + |
| + // a's slot cannot be read. |
| + fx.srv.cfg.Releases = &failGetStore{Store: cs, key: provision.HostIncarnationKey("a.example.com")} |
| + if _, _, complete, err := fx.srv.fleetInventoryFor(ctx, "pi"); err != nil || complete { |
| + t.Fatalf("an unreadable slot: complete %v, %v; want incomplete", complete, err) |
| + } |
| + if v, err := fx.srv.loadFleetView(ctx, "pi"); err != nil || v.Active || v.labelActive() { |
| + t.Fatalf("view %+v, %v; want the gate closed on an unreadable slot", v, err) |
| + } |
| +} |