package console import ( "context" "errors" "reflect" "testing" "time" "github.com/aws/aws-sdk-go-v2/service/cloudwatch" "github.com/carlosframework/platform/internal/boxchoice" "github.com/carlosframework/platform/internal/hostops" ) // The count runs without any cloud adapter (ยง25): it reads only the // edges snapshot and the chooser. Each region's AWS region is the // chooser's (the signed roster's), whatever a file claims: box z's file // names uk in eu-west-1. func TestRegionCapacitiesCountsEachPlacement(t *testing.T) { snap := boxchoice.Snapshot{Complete: true, Boxes: []boxchoice.Box{ {BoxID: "a", Placement: "uk", Region: "eu-west-2"}, {BoxID: "b", Placement: "uk", Region: "eu-west-2"}, {BoxID: "c", Placement: "ie", Region: "eu-west-1"}, {BoxID: "d", Placement: "fleet/pi", Region: "eu-west-1"}, {BoxID: "e", Placement: ""}, {BoxID: "z", Placement: "uk", Region: "eu-west-1"}, }} asked := []string{} capacity := func(_ context.Context, p string) (int, string, error) { asked = append(asked, p) switch p { case "uk": return 2, "eu-west-2", nil case "ie": return 0, "eu-west-1", nil } return 0, "", hostops.ErrContract // no roster region: left out } got, err := regionCapacities(context.Background(), snap, "", capacity) if err != nil { t.Fatal(err) } want := []regionCapacity{{Placement: "ie", AWSRegion: "eu-west-1", Eligible: 0}, {Placement: "uk", AWSRegion: "eu-west-2", Eligible: 2}} if !reflect.DeepEqual(got, want) || !reflect.DeepEqual(asked, []string{"fleet/pi", "ie", "uk"}) { t.Fatalf("got %+v asked %v", got, asked) } } // Controller ruling: a region whose count fails, or whose read is // incomplete, gets no data point (never a 0, which would trip the alarm on // a transient read failure; missing data is the alarm's own signal if it // lasts). The other regions' counts are unaffected, and the error is // returned so the pass logs it. func TestRegionCapacitiesOmitsARegionWhoseReadFailed(t *testing.T) { snap := boxchoice.Snapshot{Complete: true, Boxes: []boxchoice.Box{ {BoxID: "a", Placement: "ie"}, {BoxID: "b", Placement: "uk"}, {BoxID: "c", Placement: "us"}, }} for name, failure := range map[string]error{ "incomplete": hostops.ErrEvidenceIncomplete, "other": errors.New("SlowDown"), } { capacity := func(_ context.Context, p string) (int, string, error) { if p == "uk" { return 0, "", failure } return 1, "eu-" + p, nil } got, err := regionCapacities(context.Background(), snap, "", capacity) want := []regionCapacity{{Placement: "ie", AWSRegion: "eu-ie", Eligible: 1}, {Placement: "us", AWSRegion: "eu-us", Eligible: 1}} if !errors.Is(err, failure) || !reflect.DeepEqual(got, want) { t.Fatalf("%s: got %+v, %v; want %+v and the failure", name, got, err, want) } } } // Astra 2a final review, P2: a home box publishes the home's placement // code "". It is counted under the home's roster name, as the chooser // reads it, once whether the home's boxes publish "" or the name; with no // home name (or one that is not a placement code) the chooser places // nothing in the home, and it is not asked. func TestRegionCapacitiesCountsTheHomeUnderItsName(t *testing.T) { snap := boxchoice.Snapshot{Complete: true, Boxes: []boxchoice.Box{ {BoxID: "a", Placement: ""}, {BoxID: "b", Placement: ""}, {BoxID: "c", Placement: "ie"}, {BoxID: "d", Placement: "uk"}, }} for home, wantAsked := range map[string][]string{ "ie": {"ie", "uk"}, "": {"ie", "uk"}, "Not/Code": {"ie", "uk"}, "de": {"de", "ie", "uk"}, } { asked := []string{} capacity := func(_ context.Context, p string) (int, string, error) { asked = append(asked, p) return 2, "aws-" + p, nil } got, err := regionCapacities(context.Background(), snap, home, capacity) if err != nil || !reflect.DeepEqual(asked, wantAsked) || len(got) != len(wantAsked) { t.Fatalf("home %q: got %+v, %v, asked %v; want %v asked", home, got, err, asked, wantAsked) } } } // The same end to end, with only home boxes: each publishes placement "", // and the pass publishes the home's placeable count, non-zero, to the AWS // region the signed roster gives the home (ie, eu-west-1). func TestPublishCapacityCountsTheHomeRegion(t *testing.T) { cx := haClaimCase(t, "ie") home := func(m map[string]any) { m["placement"] = "" } publishHABox(t, cx, "box-ie-default", false, home) publishHABox(t, cx, "box-ie-tito", false, home) var pub capacityRecorder if err := cx.srv.publishCapacity(context.Background(), &pub); err != nil { t.Fatal(err) } if len(pub.got) != 1 || pub.got[0].Placement != "ie" || pub.got[0].AWSRegion != "eu-west-1" || pub.got[0].Eligible == 0 { t.Fatalf("published %+v (%d calls), want the home ie with a non-zero count in eu-west-1", pub.got, pub.calls) } } // capacityRecorder keeps what one pass would publish. type capacityRecorder struct { got []regionCapacity calls int } func (c *capacityRecorder) PublishCapacity(_ context.Context, counts []regionCapacity) error { c.got = counts c.calls++ return nil } // Astra 2a round 2 finding 3, end to end: a stray box's edges file naming // ie in another AWS region (and sorting after the real box) does not move // ie's count out of the roster's eu-west-1. func TestPublishCapacityTakesTheRegionFromTheRoster(t *testing.T) { cx := haClaimCase(t, "") publishHABox(t, cx, "box-ie-default", false, nil) publishHABox(t, cx, "box-ie-stray", false, func(m map[string]any) { m["region"] = "eu-west-2" }) var pub capacityRecorder if err := cx.srv.publishCapacity(context.Background(), &pub); err != nil { t.Fatal(err) } want := []regionCapacity{{Placement: "ie", AWSRegion: "eu-west-1", Eligible: 1}} if !reflect.DeepEqual(pub.got, want) { t.Fatalf("published %+v, want %+v", pub.got, want) } } // Controller ruling 1: the pass counts through the scoped chooser, so a // region with a placeable box publishes a non-zero count (an unscoped // chooser answers 0 for every region). func TestPublishCapacityCountsPlaceableBoxes(t *testing.T) { cx := haClaimCase(t, "") publishHABox(t, cx, "box-ie-default", false, nil) var pub capacityRecorder if err := cx.srv.publishCapacity(context.Background(), &pub); err != nil { t.Fatal(err) } if len(pub.got) != 1 || pub.got[0].Placement != "ie" || pub.got[0].Eligible != 1 { t.Fatalf("published %+v, want ie with 1", pub.got) } } // Controller ruling 2: an incomplete read publishes nothing for the // region (not 0), while a complete read with no placeable box publishes 0. func TestPublishCapacityIncompleteReadPublishesNothingButNoRoomPublishesZero(t *testing.T) { ctx := context.Background() cx := haClaimCase(t, "") publishHABox(t, cx, "box-ie-default", true, nil) // stale: named, not placeable var pub capacityRecorder if err := cx.srv.publishCapacity(ctx, &pub); err != nil { t.Fatal(err) } want := []regionCapacity{{Placement: "ie", AWSRegion: "eu-west-1", Eligible: 0}} if pub.calls != 1 || !reflect.DeepEqual(pub.got, want) { t.Fatalf("a complete read with no placeable box published %+v (%d calls), want %+v", pub.got, pub.calls, want) } publishHABox(t, cx, "box-ie-default", false, nil) pub = capacityRecorder{} edges := cx.srv.cfg.ClaimAuthority.Executors.Edges cx.srv.cfg.ClaimAuthority.Executors.Edges = func(ctx context.Context) (boxchoice.Snapshot, error) { snap, err := edges(ctx) snap.Complete = false return snap, err } err := cx.srv.publishCapacity(ctx, &pub) if !errors.Is(err, hostops.ErrEvidenceIncomplete) { t.Fatalf("an incomplete read: %v, want ErrEvidenceIncomplete", err) } if pub.calls != 0 || pub.got != nil { t.Fatalf("an incomplete read published %+v (%d calls), want nothing", pub.got, pub.calls) } } // The same, with the only box's own file naming another AWS region: the // chooser does not count a box whose file disagrees with the roster, and // the 0 still goes to the roster's eu-west-1, not the file's eu-west-2. func TestPublishCapacityIgnoresTheBoxsOwnAWSRegion(t *testing.T) { cx := haClaimCase(t, "") publishHABox(t, cx, "box-ie-default", false, func(m map[string]any) { m["region"] = "eu-west-2" }) var pub capacityRecorder if err := cx.srv.publishCapacity(context.Background(), &pub); err != nil { t.Fatal(err) } want := []regionCapacity{{Placement: "ie", AWSRegion: "eu-west-1", Eligible: 0}} if !reflect.DeepEqual(pub.got, want) { t.Fatalf("published %+v, want %+v", pub.got, want) } } func TestPlacementChooserIsScopedLikeCreate(t *testing.T) { s := &Server{cfg: Config{RegionHA: true}} if s.placementChooser("") != nil { t.Fatal("a chooser with no claim runtime") } x := &hostops.ExecutorChooser{Namespace: "stale", Pool: "acme"} s.cfg.ClaimAuthority = &hostops.ProductionAuthority{Namespace: "platform", Executors: x} got := s.placementChooser("") if got == nil || got.Namespace != "platform" || got.Pool != "" || x.Namespace != "stale" { t.Fatalf("chooser %+v, original %+v", got, x) } s.cfg.RegionHA = false if s.placementChooser("") != nil { t.Fatal("a chooser with region HA off") } } func TestCloudWatchCapacityPublishesEachRegionInItsOwnRegion(t *testing.T) { now := time.Date(2026, 10, 16, 12, 0, 0, 0, time.UTC) type call struct { region, ns, metric, dim string value float64 } var calls []call c := cloudWatchCapacity{now: func() time.Time { return now }, put: func(_ context.Context, region string, in *cloudwatch.PutMetricDataInput) error { d := in.MetricData[0] calls = append(calls, call{region, *in.Namespace, *d.MetricName, *d.Dimensions[0].Name + "=" + *d.Dimensions[0].Value, *d.Value}) if !d.Timestamp.Equal(now) || d.Unit != "Count" { t.Fatalf("datum %+v", d) } return nil }} err := c.PublishCapacity(context.Background(), []regionCapacity{{"ie", "eu-west-1", 2}, {"uk", "eu-west-2", 0}}) want := []call{{"eu-west-1", "Carlos/Region", "EligibleBoxes", "Region=ie", 2}, {"eu-west-2", "Carlos/Region", "EligibleBoxes", "Region=uk", 0}} if err != nil || !reflect.DeepEqual(calls, want) { t.Fatalf("calls %+v %v", calls, err) } if capacityPublisherFromEnv(context.Background(), func(string) string { return "" }) != nil { t.Fatal("published with CARLOS_REGION_METRICS unset") } }