Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: change receiver model pointer to value #3338

Merged
merged 2 commits into from
Apr 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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})
fgouteroux marked this conversation as resolved.
Show resolved Hide resolved
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))
}
}