1
0
mirror of https://github.com/prometheus/alertmanager.git synced 2026-02-05 15:45:34 +01:00

fix: change receiver model pointer to value (#3338)

* fix: change receiver pointer to value and add test

Signed-off-by: François Gouteroux <francois.gouteroux@gmail.com>
---------

Signed-off-by: François Gouteroux <francois.gouteroux@gmail.com>
This commit is contained in:
Francois Gouteroux
2023-04-28 17:00:19 +02:00
committed by GitHub
parent 4dc20154f6
commit 29ed85891f
2 changed files with 44 additions and 2 deletions

View File

@@ -223,8 +223,8 @@ func (api *API) getReceiversHandler(params receiver_ops.GetReceiversParams) midd
defer api.mtx.RUnlock()
receivers := make([]*open_api_models.Receiver, 0, len(api.alertmanagerConfig.Receivers))
for _, r := range api.alertmanagerConfig.Receivers {
receivers = append(receivers, &open_api_models.Receiver{Name: &r.Name})
for i := range api.alertmanagerConfig.Receivers {
receivers = append(receivers, &open_api_models.Receiver{Name: &api.alertmanagerConfig.Receivers[i].Name})
}
return receiver_ops.NewGetReceiversOK().WithPayload(receivers)

View File

@@ -30,6 +30,7 @@ import (
open_api_models "github.com/prometheus/alertmanager/api/v2/models"
general_ops "github.com/prometheus/alertmanager/api/v2/restapi/operations/general"
receiver_ops "github.com/prometheus/alertmanager/api/v2/restapi/operations/receiver"
silence_ops "github.com/prometheus/alertmanager/api/v2/restapi/operations/silence"
"github.com/prometheus/alertmanager/config"
"github.com/prometheus/alertmanager/pkg/labels"
@@ -468,3 +469,44 @@ func TestMatchFilterLabels(t *testing.T) {
require.Equal(t, tc.expected, matchFilterLabels(ms, sms))
}
}
func TestGetReceiversHandler(t *testing.T) {
in := `
route:
receiver: team-X
receivers:
- name: 'team-X'
- name: 'team-Y'
`
cfg, _ := config.Load(in)
api := API{
uptime: time.Now(),
logger: log.NewNopLogger(),
alertmanagerConfig: cfg,
}
for _, tc := range []struct {
body string
expectedCode int
}{
{
`[{"name":"team-X"},{"name":"team-Y"}]`,
200,
},
} {
r, err := http.NewRequest("GET", "/api/v2/receivers", nil)
require.NoError(t, err)
w := httptest.NewRecorder()
p := runtime.TextProducer()
responder := api.getReceiversHandler(receiver_ops.GetReceiversParams{
HTTPRequest: r,
})
responder.WriteResponse(w, p)
body, _ := io.ReadAll(w.Result().Body)
require.Equal(t, tc.expectedCode, w.Code)
require.Equal(t, tc.body, string(body))
}
}