2015-10-11 15:24:49 +00:00
|
|
|
// Copyright 2015 Prometheus Team
|
|
|
|
// 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.
|
|
|
|
|
2015-09-30 14:13:00 +00:00
|
|
|
package test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2015-10-01 13:46:39 +00:00
|
|
|
"github.com/prometheus/common/model"
|
2015-09-30 14:13:00 +00:00
|
|
|
)
|
|
|
|
|
2015-11-10 12:47:04 +00:00
|
|
|
// Collector gathers alerts received by a notification receiver
|
2015-09-30 14:13:00 +00:00
|
|
|
// and verifies whether all arrived and within the correct time boundaries.
|
|
|
|
type Collector struct {
|
|
|
|
t *testing.T
|
|
|
|
name string
|
|
|
|
opts *AcceptanceOpts
|
|
|
|
|
2015-10-01 13:46:39 +00:00
|
|
|
collected map[float64][]model.Alerts
|
2015-10-05 11:22:23 +00:00
|
|
|
expected map[Interval][]model.Alerts
|
2015-09-30 14:13:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Collector) String() string {
|
|
|
|
return c.name
|
|
|
|
}
|
|
|
|
|
2015-10-01 13:46:39 +00:00
|
|
|
func batchesEqual(as, bs model.Alerts, opts *AcceptanceOpts) bool {
|
2015-09-30 16:45:49 +00:00
|
|
|
if len(as) != len(bs) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2015-10-05 14:51:34 +00:00
|
|
|
for _, a := range as {
|
|
|
|
found := false
|
|
|
|
for _, b := range bs {
|
|
|
|
if equalAlerts(a, b, opts) {
|
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !found {
|
2015-09-30 16:45:49 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2015-09-30 14:13:00 +00:00
|
|
|
// latest returns the latest relative point in time where a notification is
|
|
|
|
// expected.
|
|
|
|
func (c *Collector) latest() float64 {
|
|
|
|
var latest float64
|
2015-10-05 11:22:23 +00:00
|
|
|
for iv := range c.expected {
|
2015-09-30 14:13:00 +00:00
|
|
|
if iv.end > latest {
|
|
|
|
latest = iv.end
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return latest
|
|
|
|
}
|
|
|
|
|
2015-11-20 14:10:38 +00:00
|
|
|
// Want declares that the Collector expects to receive the given alerts
|
2015-09-30 14:13:00 +00:00
|
|
|
// within the given time boundaries.
|
|
|
|
func (c *Collector) Want(iv Interval, alerts ...*TestAlert) {
|
2015-10-01 13:46:39 +00:00
|
|
|
var nas model.Alerts
|
2015-09-30 14:13:00 +00:00
|
|
|
for _, a := range alerts {
|
|
|
|
nas = append(nas, a.nativeAlert(c.opts))
|
|
|
|
}
|
|
|
|
|
2015-10-05 11:22:23 +00:00
|
|
|
c.expected[iv] = append(c.expected[iv], nas)
|
2015-09-30 14:13:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// add the given alerts to the collected alerts.
|
2015-10-01 13:46:39 +00:00
|
|
|
func (c *Collector) add(alerts ...*model.Alert) {
|
2015-09-30 14:13:00 +00:00
|
|
|
arrival := c.opts.relativeTime(time.Now())
|
|
|
|
|
2015-10-01 13:46:39 +00:00
|
|
|
c.collected[arrival] = append(c.collected[arrival], model.Alerts(alerts))
|
2015-09-30 14:13:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Collector) check() string {
|
2015-09-30 16:45:49 +00:00
|
|
|
report := fmt.Sprintf("\ncollector %q:\n\n", c)
|
2015-09-30 14:13:00 +00:00
|
|
|
|
2015-10-05 11:22:23 +00:00
|
|
|
for iv, expected := range c.expected {
|
2015-09-30 14:13:00 +00:00
|
|
|
report += fmt.Sprintf("interval %v\n", iv)
|
|
|
|
|
|
|
|
for _, exp := range expected {
|
2015-10-01 13:46:39 +00:00
|
|
|
var found model.Alerts
|
|
|
|
|
2015-09-30 16:45:49 +00:00
|
|
|
report += fmt.Sprintf("---\n")
|
|
|
|
|
|
|
|
for _, e := range exp {
|
2015-10-05 14:51:34 +00:00
|
|
|
report += fmt.Sprintf("- %v\n", c.opts.alertString(e))
|
2015-09-30 16:45:49 +00:00
|
|
|
}
|
2015-09-30 14:13:00 +00:00
|
|
|
|
|
|
|
for at, got := range c.collected {
|
|
|
|
if !iv.contains(at) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
for _, a := range got {
|
2015-09-30 16:45:49 +00:00
|
|
|
if batchesEqual(exp, a, c.opts) {
|
2015-09-30 14:13:00 +00:00
|
|
|
found = a
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if found != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if found != nil {
|
2015-09-30 16:45:49 +00:00
|
|
|
report += fmt.Sprintf(" [ ✓ ]\n")
|
2015-09-30 14:13:00 +00:00
|
|
|
} else {
|
|
|
|
c.t.Fail()
|
2015-09-30 16:45:49 +00:00
|
|
|
report += fmt.Sprintf(" [ ✗ ]\n")
|
2015-09-30 14:13:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Detect unexpected notifications.
|
|
|
|
var totalExp, totalAct int
|
2015-10-05 11:22:23 +00:00
|
|
|
for _, exp := range c.expected {
|
2015-09-30 16:45:49 +00:00
|
|
|
for _, e := range exp {
|
|
|
|
totalExp += len(e)
|
|
|
|
}
|
2015-09-30 14:13:00 +00:00
|
|
|
}
|
|
|
|
for _, act := range c.collected {
|
2015-09-30 16:45:49 +00:00
|
|
|
for _, a := range act {
|
|
|
|
if len(a) == 0 {
|
|
|
|
c.t.Error("received empty notifications")
|
|
|
|
}
|
|
|
|
totalAct += len(a)
|
|
|
|
}
|
2015-09-30 14:13:00 +00:00
|
|
|
}
|
|
|
|
if totalExp != totalAct {
|
|
|
|
c.t.Fail()
|
|
|
|
report += fmt.Sprintf("\nExpected total of %d alerts, got %d", totalExp, totalAct)
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.t.Failed() {
|
|
|
|
report += "\nreceived:\n"
|
|
|
|
|
|
|
|
for at, col := range c.collected {
|
2015-09-30 16:45:49 +00:00
|
|
|
for _, alerts := range col {
|
|
|
|
report += fmt.Sprintf("@ %v\n", at)
|
|
|
|
for _, a := range alerts {
|
2015-10-05 14:51:34 +00:00
|
|
|
report += fmt.Sprintf("- %v\n", c.opts.alertString(a))
|
2015-09-30 16:45:49 +00:00
|
|
|
}
|
2015-09-30 14:13:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return report
|
|
|
|
}
|