| index 285b7acfe..dbedf07fb 100644 |
| --- a/internal/console/apidbforward_test.go |
| +++ b/internal/console/apidbforward_test.go |
| @@ -1,10 +1,15 @@ |
| package console |
| |
| import ( |
| + "context" |
| "crypto/ed25519" |
| "crypto/rand" |
| + "encoding/json" |
| "testing" |
| "time" |
| + |
| + "github.com/carlosframework/platform/internal/dbreads" |
| + "github.com/carlosframework/platform/internal/release" |
| ) |
| |
| // The console forwards only with the switch on, a root key whose public |
| @@ -37,3 +42,94 @@ func TestNewHTTPServerKeepsTheConsolesTimeouts(t *testing.T) { |
| t.Fatalf("%+v", srv) |
| } |
| } |
| + |
| +// putFixtureObject creates key, or replaces it under its current etag: a |
| +// fixture may republish a box's files, as a box does every pass. Protocol |
| +// writes stay create-only; this helper is for fixtures only (plan review |
| +// finding 6: DirStore.Put refuses a create over an existing key). |
| +func putFixtureObject(t *testing.T, s release.Store, key string, body []byte) { |
| + t.Helper() |
| + etag := "" |
| + if _, cur, err := s.Get(context.Background(), key); err == nil { |
| + etag = cur |
| + } |
| + if _, err := s.Put(context.Background(), key, body, etag); err != nil { |
| + t.Fatal(err) |
| + } |
| +} |
| + |
| +// putEdgesFile writes control/edges/<box>.json as a box publishes it. |
| +func putEdgesFile(t *testing.T, s release.Store, box, placement string, caps []string, draining bool) { |
| + t.Helper() |
| + body, _ := json.Marshal(map[string]any{"v": 1, "box": box, "eip": "203.0.113.10", "placement": placement, |
| + "capabilities": caps, "draining": draining, "hosts": map[string]any{}}) |
| + putFixtureObject(t, s, "control/edges/"+box+".json", body) |
| +} |
| + |
| +// putBoxStatus writes status/boxes/<box>.json generated at at. |
| +func putBoxStatus(t *testing.T, s release.Store, box string, at time.Time) { |
| + t.Helper() |
| + body, _ := json.Marshal(map[string]any{"generated_at": at.UTC(), "label": box}) |
| + putFixtureObject(t, s, "status/boxes/"+box+".json", body) |
| +} |
| + |
| +// T14 (codex 10): only complete evidence, both flags, refuses up front. |
| +// Incomplete evidence of any kind admits the read and lets the pickup |
| +// window decide. Mutation: check Complete and not BoxesComplete (red: the |
| +// missing, unmatched and unreadable status cases refuse). |
| +func TestDBForwardEvidenceRefusesOnlyOnCompleteEvidence(t *testing.T) { |
| + t.Parallel() |
| + now := time.Now() |
| + capable := []string{dbreads.Capability} |
| + for name, c := range map[string]struct { |
| + setup func(t *testing.T, s release.Store) |
| + want string |
| + }{ |
| + "a capable live box": {func(t *testing.T, s release.Store) { |
| + putEdgesFile(t, s, "uk-1", "uk", capable, false) |
| + putBoxStatus(t, s, "uk-1", now) |
| + }, ""}, |
| + "no capable box": {func(t *testing.T, s release.Store) { |
| + putEdgesFile(t, s, "uk-1", "uk", nil, false) |
| + putBoxStatus(t, s, "uk-1", now) |
| + }, dbForwardNone}, |
| + "a capable box that is draining": {func(t *testing.T, s release.Store) { |
| + putEdgesFile(t, s, "uk-1", "uk", capable, true) |
| + putBoxStatus(t, s, "uk-1", now) |
| + }, dbForwardNone}, |
| + "capable but stale": {func(t *testing.T, s release.Store) { |
| + putEdgesFile(t, s, "uk-1", "uk", capable, false) |
| + putBoxStatus(t, s, "uk-1", now.Add(-time.Hour)) |
| + }, dbForwardDead}, |
| + "capable in another region only": {func(t *testing.T, s release.Store) { |
| + putEdgesFile(t, s, "ie-1", "ie", capable, false) |
| + putBoxStatus(t, s, "ie-1", now) |
| + }, dbForwardNone}, |
| + "a status snapshot missing": {func(t *testing.T, s release.Store) { |
| + putEdgesFile(t, s, "uk-1", "uk", nil, false) |
| + }, ""}, |
| + "an edges file unreadable": {func(t *testing.T, s release.Store) { |
| + s.Put(context.Background(), "control/edges/uk-1.json", []byte("{not json"), "") |
| + putBoxStatus(t, s, "uk-1", now) |
| + }, ""}, |
| + "a fresh snapshot with no edges file": {func(t *testing.T, s release.Store) { |
| + putEdgesFile(t, s, "uk-1", "uk", nil, false) |
| + putBoxStatus(t, s, "uk-1", now) |
| + putBoxStatus(t, s, "uk-2", now) |
| + }, ""}, |
| + "status snapshots unreadable": {func(t *testing.T, s release.Store) { |
| + putEdgesFile(t, s, "uk-1", "uk", nil, false) |
| + s.Put(context.Background(), "status/boxes/uk-1.json", []byte("{not json"), "") |
| + }, ""}, |
| + } { |
| + t.Run(name, func(t *testing.T) { |
| + t.Parallel() |
| + s := release.NewDirStore(t.TempDir()) |
| + c.setup(t, s) |
| + snap, err := readLiveEdgesSnapshot(context.Background(), s, now) |
| + if got := dbForwardEvidence(snap, err, "uk"); got != c.want { |
| + t.Fatalf("evidence %q, want %q (complete %v, boxes %v)", got, c.want, snap.Complete, snap.BoxesComplete) |
| + } |
| + }) |
| + } |
| +} |