| new file mode 100644 |
| index 000000000..5ddff5fcb |
| --- /dev/null |
| +++ b/internal/console/fleethealth_test.go |
| @@ -0,0 +1,114 @@ |
| +package console |
| + |
| +import ( |
| + "context" |
| + "encoding/json" |
| + "net/http" |
| + "strings" |
| + "testing" |
| + "time" |
| + |
| + "github.com/carlosframework/platform/internal/provision" |
| + "github.com/carlosframework/platform/internal/registry" |
| + "github.com/carlosframework/platform/internal/release" |
| + "github.com/carlosframework/platform/internal/status" |
| +) |
| + |
| +// writeFleetRolloutBox registers fleet to owner and writes, under the |
| +// fleet's own prefix, a box named machine reporting ro (and versions as its |
| +// route versions, which an Unavailable report's hosts come from). |
| +func writeFleetRolloutBox(t *testing.T, fx *consoleFixture, fleet, owner, machine string, ro *status.RolloutReport, versions map[string]string) { |
| + t.Helper() |
| + reg, err := registry.Open(fx.regPath) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + defer reg.Close() |
| + if err := reg.UpsertFleet(registry.Fleet{Name: fleet, Account: owner, Bucket: fleet + "-bucket"}); err != nil { |
| + t.Fatal(err) |
| + } |
| + box := status.CollectBox(nil, status.Probes{MachineID: machine}, time.Now()) |
| + box.Metrics = &status.BoxMetrics{Rollout: ro, RouteVersions: versions} |
| + if err := status.WriteBox(context.Background(), release.NewPrefixStore(fx.rel, "fleets/"+fleet+"/"), box); err != nil { |
| + t.Fatal(err) |
| + } |
| +} |
| + |
| +// listHealth is the instance list's health word per host. |
| +func listHealth(t *testing.T, fx *consoleFixture, token, app string) map[string]string { |
| + t.Helper() |
| + code, _, out := rawReq(t, http.MethodGet, fx.ts.URL+"/api/cli/apps/"+fx.sqid+"/"+app+"/instances", token, "") |
| + if code != http.StatusOK { |
| + t.Fatalf("list = %d %s", code, out) |
| + } |
| + var list struct { |
| + Instances []struct { |
| + Host string `json:"host"` |
| + Health string `json:"health"` |
| + } `json:"instances"` |
| + } |
| + if err := json.Unmarshal([]byte(out), &list); err != nil { |
| + t.Fatal(err) |
| + } |
| + got := map[string]string{} |
| + for _, r := range list.Instances { |
| + got[r.Host] = r.Health |
| + } |
| + return got |
| +} |
| + |
| +// #366 Task 11 follow-up: a record placed on the caller's own fleet takes |
| +// its health word from that fleet box's rollout report (the join the |
| +// list's rollout column reads), as a platform-remote record takes its word |
| +// from its box: awake is Running, a hibernating instance not awake is |
| +// Asleep, a host held down is Failed. Everything the join cannot vouch for |
| +// stays "Can't tell right now": a host starting or stopping, a report |
| +// naming another app, a box that could not arm its scheduler, a host no |
| +// box reports, and another account's fleet box naming this app's host. |
| +func TestFleetPlacedHealthComesFromItsFleetBox(t *testing.T) { |
| + fx := newConsoleFixture(t) |
| + const app = "kass" |
| + host := func(slug string) string { return slug + ".kass.carlosapp.dev" } |
| + asleep := true |
| + for _, slug := range []string{"run", "nap", "down", "busy", "other", "gone", "stranger", "unavail"} { |
| + place := registry.PlacementTarget("pi") |
| + if slug == "stranger" { |
| + place = registry.PlacementTarget("stranger") |
| + } |
| + if err := provision.CreateRecord(context.Background(), fx.rel, provision.Record{ |
| + Schema: 1, Host: host(slug), Slug: slug, App: app, Account: fx.sqid, Region: place, |
| + Channel: "stable", Hibernate: &asleep, CreatedAt: time.Now().UTC(), CreatedBy: "paul@keymail.dev", |
| + }); err != nil { |
| + t.Fatal(err) |
| + } |
| + } |
| + mine := func(hr status.HostRollout) status.HostRollout { |
| + hr.Account, hr.App, hr.Installed = fx.sqid, app, "v1" |
| + return hr |
| + } |
| + writeFleetRolloutBox(t, fx, "pi", fx.sqid, "pi-box", &status.RolloutReport{Hosts: map[string]status.HostRollout{ |
| + host("run"): mine(status.HostRollout{Awake: true}), |
| + host("nap"): mine(status.HostRollout{}), |
| + host("down"): mine(status.HostRollout{Attempt: status.AttemptDown, AttemptBuild: "v1"}), |
| + host("busy"): mine(status.HostRollout{Transient: true}), |
| + host("other"): {Account: fx.sqid, App: "other", Installed: "v1", Awake: true}, |
| + }}, nil) |
| + writeFleetRolloutBox(t, fx, "pi", fx.sqid, "pi-box-2", &status.RolloutReport{Unavailable: "flag-off"}, |
| + map[string]string{host("unavail"): "v1"}) |
| + writeFleetRolloutBox(t, fx, "stranger", "zzz", "stranger-box", &status.RolloutReport{Hosts: map[string]status.HostRollout{ |
| + host("stranger"): mine(status.HostRollout{Awake: true}), |
| + }}, nil) |
| + token := cliToken(t, fx, "paul@keymail.dev", "code-fleet-health") |
| + |
| + got := listHealth(t, fx, token, app) |
| + want := map[string]string{ |
| + host("run"): "Running", host("nap"): "Asleep", host("down"): "Failed", |
| + host("busy"): healthUnknown, host("other"): healthUnknown, host("gone"): healthUnknown, |
| + host("stranger"): healthUnknown, host("unavail"): healthUnknown, |
| + } |
| + for h, w := range want { |
| + if got[h] != w { |
| + t.Errorf("%s health = %q, want %q", strings.SplitN(h, ".", 2)[0], got[h], w) |
| + } |
| + } |
| +} |