@@ -17,6 +17,8 @@ import (
1717 "bytes"
1818 "encoding/json"
1919 "fmt"
20+ "github.com/prometheus/client_golang/prometheus"
21+
2022 "io"
2123 "net/http"
2224 "net/http/httptest"
@@ -31,6 +33,8 @@ import (
3133 "github.com/prometheus/common/promslog"
3234 "github.com/stretchr/testify/require"
3335
36+ "github.com/prometheus/alertmanager/alertobserver"
37+ "github.com/prometheus/alertmanager/api/metrics"
3438 alert_ops "github.com/prometheus/alertmanager/api/v2/restapi/operations/alert"
3539 alertgroup_ops "github.com/prometheus/alertmanager/api/v2/restapi/operations/alertgroup"
3640 alertgroupinfolist_ops "github.com/prometheus/alertmanager/api/v2/restapi/operations/alertgroupinfolist"
@@ -1196,6 +1200,67 @@ func TestListAlertInfosHandler(t *testing.T) {
11961200 }
11971201}
11981202
1203+ func TestPostAlertHandler (t * testing.T ) {
1204+ now := time .Now ()
1205+ for i , tc := range []struct {
1206+ start , end time.Time
1207+ err bool
1208+ code int
1209+ }{
1210+ {time.Time {}, time.Time {}, false , 200 },
1211+ {now , time.Time {}, false , 200 },
1212+ {time.Time {}, now .Add (time .Duration (- 1 ) * time .Second ), false , 200 },
1213+ {time.Time {}, now , false , 200 },
1214+ {time.Time {}, now .Add (time .Duration (1 ) * time .Second ), false , 200 },
1215+ {now .Add (time .Duration (- 2 ) * time .Second ), now .Add (time .Duration (- 1 ) * time .Second ), false , 200 },
1216+ {now .Add (time .Duration (1 ) * time .Second ), now .Add (time .Duration (2 ) * time .Second ), false , 200 },
1217+ {now .Add (time .Duration (1 ) * time .Second ), now , false , 400 },
1218+ } {
1219+ alerts , alertsBytes := createAlert (t , tc .start , tc .end )
1220+ api := API {
1221+ uptime : time .Now (),
1222+ alerts : newFakeAlerts ([]* types.Alert {}),
1223+ logger : log .NewNopLogger (),
1224+ m : metrics .NewAlerts (prometheus .NewRegistry ()),
1225+ }
1226+ api .Update (& config.Config {
1227+ Global : & config.GlobalConfig {
1228+ ResolveTimeout : model .Duration (5 ),
1229+ },
1230+ Route : & config.Route {},
1231+ }, nil )
1232+
1233+ r , err := http .NewRequest ("POST" , "/api/v2/alerts" , bytes .NewReader (alertsBytes ))
1234+ require .NoError (t , err )
1235+
1236+ w := httptest .NewRecorder ()
1237+ p := runtime .TextProducer ()
1238+ responder := api .postAlertsHandler (alert_ops.PostAlertsParams {
1239+ HTTPRequest : r ,
1240+ Alerts : alerts ,
1241+ })
1242+ responder .WriteResponse (w , p )
1243+ body , _ := io .ReadAll (w .Result ().Body )
1244+
1245+ require .Equal (t , tc .code , w .Code , fmt .Sprintf ("test case: %d, response: %s" , i , string (body )))
1246+
1247+ observer := alertobserver .NewFakeLifeCycleObserver ()
1248+ api .alertLCObserver = observer
1249+ r , err = http .NewRequest ("POST" , "/api/v2/alerts" , bytes .NewReader (alertsBytes ))
1250+ require .NoError (t , err )
1251+ api .postAlertsHandler (alert_ops.PostAlertsParams {
1252+ HTTPRequest : r ,
1253+ Alerts : alerts ,
1254+ })
1255+ amAlert := OpenAPIAlertsToAlerts (alerts )
1256+ if tc .code == 200 {
1257+ require .Equal (t , observer .AlertsPerEvent [alertobserver .EventAlertReceived ][0 ].Fingerprint (), amAlert [0 ].Fingerprint ())
1258+ } else {
1259+ require .Equal (t , observer .AlertsPerEvent [alertobserver .EventAlertRejected ][0 ].Fingerprint (), amAlert [0 ].Fingerprint ())
1260+ }
1261+ }
1262+ }
1263+
11991264type limitNumberOfAlertsReturnedCallback struct {
12001265 limit int
12011266}
0 commit comments