From ebda6072cfed5d5b8381caf56487d91269d4f8a2 Mon Sep 17 00:00:00 2001 From: Muhittin Kesikli Date: Tue, 1 Sep 2026 21:10:39 +0200 Subject: [PATCH] sort partition capacity response deterministically calcPartitionCapacity collects partitions in a map and appends them to the result slice by ranging over it. Go randomizes map iteration, so the response order varies between calls and TestPartitionCapacity fails once a fixture contains two partitions: 17 of 100 runs on afae26f with go1.26.0. The map is the only unstable axis in the test, but not in production: SearchMachines builds its term from Filter calls only and never calls OrderBy, so RethinkDB row order is unspecified and ServerCapacities, FaultyMachines and OtherMachines inherit that order as well. Sorting in the service rather than in the test makes the API response stable instead of only silencing cmp.Diff. This changes the ordering clients see in the response. Generated-By: Claude Code (Opus 5) Generated-By: deepseek-v4-flash --- cmd/metal-api/internal/service/partition-service.go | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/cmd/metal-api/internal/service/partition-service.go b/cmd/metal-api/internal/service/partition-service.go index 7cae3189..03e1e99a 100644 --- a/cmd/metal-api/internal/service/partition-service.go +++ b/cmd/metal-api/internal/service/partition-service.go @@ -5,6 +5,8 @@ import ( "fmt" "log/slog" "net/http" + "slices" + "strings" "github.com/metal-stack/metal-api/cmd/metal-api/internal/datastore" "github.com/metal-stack/metal-api/cmd/metal-api/internal/issues" @@ -555,10 +557,20 @@ func (r *partitionResource) calcPartitionCapacity(pcr *v1.PartitionCapacityReque for _, cap := range pc.ServerCapacities { cap.RemainingReservations = cap.Reservations - cap.UsedReservations + slices.Sort(cap.FaultyMachines) + slices.Sort(cap.OtherMachines) } + slices.SortFunc(pc.ServerCapacities, func(a, b *v1.ServerCapacity) int { + return strings.Compare(a.Size, b.Size) + }) + res = append(res, *pc) } + slices.SortFunc(res, func(a, b v1.PartitionCapacity) int { + return strings.Compare(a.ID, b.ID) + }) + return res, nil }