api/v2: fix alerts with multiple receivers (#1948)

Signed-off-by: Simon Pasquier <spasquie@redhat.com>
This commit is contained in:
Simon Pasquier 2019-07-03 16:34:54 +02:00 committed by GitHub
parent 019ace3298
commit 98a604efa7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 48 additions and 2 deletions

View File

@ -432,8 +432,8 @@ func alertToOpenAPIAlert(alert *types.Alert, status types.AlertStatus, receivers
endsAt := strfmt.DateTime(alert.EndsAt)
apiReceivers := make([]*open_api_models.Receiver, 0, len(receivers))
for _, name := range receivers {
apiReceivers = append(apiReceivers, &open_api_models.Receiver{Name: &name})
for i := range receivers {
apiReceivers = append(apiReceivers, &open_api_models.Receiver{Name: &receivers[i]})
}
fp := alert.Fingerprint().String()

View File

@ -19,10 +19,13 @@ import (
"time"
"github.com/go-openapi/strfmt"
"github.com/prometheus/common/model"
"github.com/stretchr/testify/require"
open_api_models "github.com/prometheus/alertmanager/api/v2/models"
general_ops "github.com/prometheus/alertmanager/api/v2/restapi/operations/general"
"github.com/prometheus/alertmanager/config"
"github.com/prometheus/alertmanager/types"
)
// If api.peers == nil, Alertmanager cluster feature is disabled. Make sure to
@ -122,3 +125,46 @@ func TestGetSilencesHandler(t *testing.T) {
assertEqualStrings(t, "silence-"+strconv.Itoa(i)+"-"+*sil.Status.State, *sil.ID)
}
}
func convertDateTime(ts time.Time) *strfmt.DateTime {
dt := strfmt.DateTime(ts)
return &dt
}
func TestAlertToOpenAPIAlert(t *testing.T) {
var (
start = time.Now().Add(-time.Minute)
updated = time.Now()
active = "active"
fp = "0223b772b51c29e1"
receivers = []string{"receiver1", "receiver2"}
alert = &types.Alert{
Alert: model.Alert{
Labels: model.LabelSet{"severity": "critical", "alertname": "alert1"},
StartsAt: start,
},
UpdatedAt: updated,
}
)
openAPIAlert := alertToOpenAPIAlert(alert, types.AlertStatus{State: types.AlertStateActive}, receivers)
require.Equal(t, &open_api_models.GettableAlert{
Annotations: open_api_models.LabelSet{},
Alert: open_api_models.Alert{
Labels: open_api_models.LabelSet{"severity": "critical", "alertname": "alert1"},
},
Status: &open_api_models.AlertStatus{
State: &active,
InhibitedBy: []string{},
SilencedBy: []string{},
},
StartsAt: convertDateTime(start),
EndsAt: convertDateTime(time.Time{}),
UpdatedAt: convertDateTime(updated),
Fingerprint: &fp,
Receivers: []*open_api_models.Receiver{
&open_api_models.Receiver{Name: &receivers[0]},
&open_api_models.Receiver{Name: &receivers[1]},
},
}, openAPIAlert)
}