Skip to content

Commit

Permalink
fix: change receiver model pointer to value (#3338)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
fgouteroux committed Apr 28, 2023
1 parent 4dc2015 commit 29ed858
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 2 deletions.
4 changes: 2 additions & 2 deletions api/v2/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
42 changes: 42 additions & 0 deletions api/v2/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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))
}
}

0 comments on commit 29ed858

Please sign in to comment.