diff --git a/CHANGELOG.md b/CHANGELOG.md index 904c160f5b61..f5def7b3c51a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -44,6 +44,7 @@ __BACKWARDS INCOMPATIBILITIES:__ BUG FIXES: * agent (Enterprise): Fixed a bug where audit logging caused websocket and streaming http endpoints to fail [[GH-9319](https://github.com/hashicorp/nomad/issues/9319)] + * api: Fixed a bug where AllocatedResources contained increasingly duplicated ports [[GH-9368](https://github.com/hashicorp/nomad/issues/9368)] * core: Fixed a bug where ACL handling prevented cross-namespace allocation listing [[GH-9278](https://github.com/hashicorp/nomad/issues/9278)] * core: Fixed a bug where scaling policy filtering would ignore type query if job query was present [[GH-9312](https://github.com/hashicorp/nomad/issues/9312)] * core: Fixed a bug where a request to scale a job would fail if the job was not in the default namespace. [[GH-9296](https://github.com/hashicorp/nomad/pull/9296)] diff --git a/command/agent/alloc_endpoint.go b/command/agent/alloc_endpoint.go index 5b7b8ee086c7..20865144864a 100644 --- a/command/agent/alloc_endpoint.go +++ b/command/agent/alloc_endpoint.go @@ -127,6 +127,7 @@ func (s *HTTPServer) allocGet(allocID string, resp http.ResponseWriter, req *htt alloc.SetEventDisplayMessages() // Handle 0.12 ports upgrade path + alloc = alloc.Copy() alloc.AllocatedResources.Canonicalize() return alloc, nil diff --git a/command/agent/alloc_endpoint_test.go b/command/agent/alloc_endpoint_test.go index 6d36d2658e6b..cf94c791dfe2 100644 --- a/command/agent/alloc_endpoint_test.go +++ b/command/agent/alloc_endpoint_test.go @@ -158,47 +158,42 @@ func TestHTTP_AllocsPrefixList(t *testing.T) { func TestHTTP_AllocQuery(t *testing.T) { t.Parallel() + require := require.New(t) httpTest(t, nil, func(s *TestAgent) { // Directly manipulate the state state := s.Agent.server.State() alloc := mock.Alloc() - if err := state.UpsertJobSummary(999, mock.JobSummary(alloc.JobID)); err != nil { - t.Fatal(err) - } - err := state.UpsertAllocs(structs.MsgTypeTestSetup, 1000, []*structs.Allocation{alloc}) - if err != nil { - t.Fatalf("err: %v", err) - } + require.NoError(state.UpsertJobSummary(999, mock.JobSummary(alloc.JobID))) + require.NoError(state.UpsertAllocs(structs.MsgTypeTestSetup, 1000, []*structs.Allocation{alloc})) // Make the HTTP request req, err := http.NewRequest("GET", "/v1/allocation/"+alloc.ID, nil) - if err != nil { - t.Fatalf("err: %v", err) - } + require.NoError(err) respW := httptest.NewRecorder() // Make the request obj, err := s.Server.AllocSpecificRequest(respW, req) - if err != nil { - t.Fatalf("err: %v", err) - } + require.NoError(err) // Check for the index - if respW.HeaderMap.Get("X-Nomad-Index") == "" { - t.Fatalf("missing index") - } - if respW.HeaderMap.Get("X-Nomad-KnownLeader") != "true" { - t.Fatalf("missing known leader") - } - if respW.HeaderMap.Get("X-Nomad-LastContact") == "" { - t.Fatalf("missing last contact") - } + require.NotEmpty(respW.Header().Get("X-Nomad-Index"), "missing index") + require.Equal("true", respW.Header().Get("X-Nomad-KnownLeader"), "missing known leader") + require.NotEmpty(respW.Header().Get("X-Nomad-LastContact"), "missing last contact") // Check the job a := obj.(*structs.Allocation) - if a.ID != alloc.ID { - t.Fatalf("bad: %#v", a) - } + require.Equal(a.ID, alloc.ID) + + // Check the number of ports + require.Len(a.AllocatedResources.Shared.Ports, 2) + + // Make the request again + respW = httptest.NewRecorder() + obj, err = s.Server.AllocSpecificRequest(respW, req) + require.NoError(err) + a = obj.(*structs.Allocation) + // Check the number of ports again + require.Len(a.AllocatedResources.Shared.Ports, 2) }) }