2015-01-21 11:07:45 -08:00
|
|
|
// Copyright 2013 The Prometheus Authors
|
2013-08-09 10:32:55 -07:00
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package notification
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
clientmodel "github.com/prometheus/client_golang/model"
|
|
|
|
)
|
|
|
|
|
2014-12-10 07:16:49 -08:00
|
|
|
type testHTTPPoster struct {
|
2013-08-09 10:32:55 -07:00
|
|
|
message string
|
|
|
|
receivedPost chan<- bool
|
|
|
|
}
|
|
|
|
|
2014-12-10 07:16:49 -08:00
|
|
|
func (p *testHTTPPoster) Post(url string, bodyType string, body io.Reader) (*http.Response, error) {
|
2013-08-09 10:32:55 -07:00
|
|
|
var buf bytes.Buffer
|
|
|
|
buf.ReadFrom(body)
|
|
|
|
p.message = buf.String()
|
|
|
|
p.receivedPost <- true
|
|
|
|
return &http.Response{
|
|
|
|
Body: ioutil.NopCloser(&bytes.Buffer{}),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type testNotificationScenario struct {
|
|
|
|
description string
|
|
|
|
summary string
|
|
|
|
message string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *testNotificationScenario) test(i int, t *testing.T) {
|
2014-10-10 05:19:02 -07:00
|
|
|
h := NewNotificationHandler("alertmanager_url", 0)
|
|
|
|
defer h.Stop()
|
2013-08-09 10:32:55 -07:00
|
|
|
|
|
|
|
receivedPost := make(chan bool, 1)
|
2014-12-10 07:16:49 -08:00
|
|
|
poster := testHTTPPoster{receivedPost: receivedPost}
|
2013-08-09 10:32:55 -07:00
|
|
|
h.httpClient = &poster
|
|
|
|
|
|
|
|
go h.Run()
|
|
|
|
|
2014-10-10 05:19:02 -07:00
|
|
|
h.SubmitReqs(NotificationReqs{
|
2013-08-09 10:32:55 -07:00
|
|
|
{
|
|
|
|
Summary: s.summary,
|
|
|
|
Description: s.description,
|
|
|
|
Labels: clientmodel.LabelSet{
|
|
|
|
clientmodel.LabelName("instance"): clientmodel.LabelValue("testinstance"),
|
|
|
|
},
|
2013-08-20 06:42:06 -07:00
|
|
|
Value: clientmodel.SampleValue(1.0 / 3.0),
|
|
|
|
ActiveSince: time.Time{},
|
|
|
|
RuleString: "Test rule string",
|
2014-11-20 09:10:43 -08:00
|
|
|
GeneratorURL: "prometheus_url",
|
2013-08-09 10:32:55 -07:00
|
|
|
},
|
2014-10-10 05:19:02 -07:00
|
|
|
})
|
2013-08-09 10:32:55 -07:00
|
|
|
|
|
|
|
<-receivedPost
|
|
|
|
if poster.message != s.message {
|
|
|
|
t.Fatalf("%d. Expected '%s', received '%s'", i, s.message, poster.message)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNotificationHandler(t *testing.T) {
|
|
|
|
scenarios := []testNotificationScenario{
|
|
|
|
{
|
|
|
|
// Correct message.
|
2014-05-28 10:44:54 -07:00
|
|
|
summary: "Summary",
|
|
|
|
description: "Description",
|
2015-05-30 06:35:51 -07:00
|
|
|
message: `[{"description":"Description","labels":{"instance":"testinstance"},"payload":{"activeSince":"0001-01-01T00:00:00Z","alertingRule":"Test rule string","generatorURL":"prometheus_url","value":"0.3333333333333333"},"summary":"Summary"}]`,
|
2013-08-09 10:32:55 -07:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, s := range scenarios {
|
|
|
|
s.test(i, t)
|
|
|
|
}
|
|
|
|
}
|