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-29 18:45:38 +00:00
|
|
|
package test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2015-09-29 20:40:44 +00:00
|
|
|
"fmt"
|
2015-09-29 18:45:38 +00:00
|
|
|
"io/ioutil"
|
2015-09-30 15:33:49 +00:00
|
|
|
"net"
|
2015-09-29 18:45:38 +00:00
|
|
|
"os"
|
|
|
|
"os/exec"
|
2015-10-06 10:41:20 +00:00
|
|
|
"path/filepath"
|
2015-09-29 18:45:38 +00:00
|
|
|
"sync"
|
2015-10-02 10:28:27 +00:00
|
|
|
"syscall"
|
2015-09-29 18:45:38 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2015-10-01 18:58:46 +00:00
|
|
|
"github.com/prometheus/client_golang/api/alertmanager"
|
2015-10-01 13:46:39 +00:00
|
|
|
"github.com/prometheus/common/model"
|
2015-10-01 18:58:46 +00:00
|
|
|
"golang.org/x/net/context"
|
2015-09-29 18:45:38 +00:00
|
|
|
)
|
|
|
|
|
2015-10-02 10:32:19 +00:00
|
|
|
// AcceptanceTest provides declarative definition of given inputs and expected
|
|
|
|
// output of an Alertmanager setup.
|
2015-09-30 14:13:00 +00:00
|
|
|
type AcceptanceTest struct {
|
2015-09-29 18:45:38 +00:00
|
|
|
*testing.T
|
|
|
|
|
2015-09-30 14:13:00 +00:00
|
|
|
opts *AcceptanceOpts
|
2015-09-29 18:45:38 +00:00
|
|
|
|
2015-09-29 20:40:44 +00:00
|
|
|
ams []*Alertmanager
|
2015-09-30 14:13:00 +00:00
|
|
|
collectors []*Collector
|
2015-10-02 10:18:02 +00:00
|
|
|
|
|
|
|
actions map[float64][]func()
|
2015-09-29 18:45:38 +00:00
|
|
|
}
|
|
|
|
|
2015-10-02 10:32:19 +00:00
|
|
|
// AcceptanceOpts defines configuration paramters for an acceptance test.
|
2015-09-30 14:13:00 +00:00
|
|
|
type AcceptanceOpts struct {
|
|
|
|
Tolerance time.Duration
|
2015-10-02 12:10:04 +00:00
|
|
|
baseTime time.Time
|
2015-09-29 18:45:38 +00:00
|
|
|
}
|
|
|
|
|
2015-10-05 14:51:34 +00:00
|
|
|
func (opts *AcceptanceOpts) alertString(a *model.Alert) string {
|
|
|
|
if a.EndsAt.IsZero() {
|
|
|
|
return fmt.Sprintf("%s[%v:]", a, opts.relativeTime(a.StartsAt))
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("%s[%v:%v]", a, opts.relativeTime(a.StartsAt), opts.relativeTime(a.EndsAt))
|
|
|
|
}
|
|
|
|
|
2015-10-02 10:32:19 +00:00
|
|
|
// expandTime returns the absolute time for the relative time
|
|
|
|
// calculated from the test's base time.
|
2015-09-30 14:13:00 +00:00
|
|
|
func (opts *AcceptanceOpts) expandTime(rel float64) time.Time {
|
2015-09-30 13:35:52 +00:00
|
|
|
return opts.baseTime.Add(time.Duration(rel * float64(time.Second)))
|
|
|
|
}
|
|
|
|
|
2015-10-02 10:32:19 +00:00
|
|
|
// expandTime returns the relative time for the given time
|
|
|
|
// calculated from the test's base time.
|
2015-09-30 14:13:00 +00:00
|
|
|
func (opts *AcceptanceOpts) relativeTime(act time.Time) float64 {
|
2015-09-30 13:35:52 +00:00
|
|
|
return float64(act.Sub(opts.baseTime)) / float64(time.Second)
|
|
|
|
}
|
|
|
|
|
2015-10-02 10:32:19 +00:00
|
|
|
// NewAcceptanceTest returns a new acceptance test with the base time
|
|
|
|
// set to the current time.
|
2015-09-30 14:13:00 +00:00
|
|
|
func NewAcceptanceTest(t *testing.T, opts *AcceptanceOpts) *AcceptanceTest {
|
|
|
|
test := &AcceptanceTest{
|
2015-10-02 10:18:02 +00:00
|
|
|
T: t,
|
|
|
|
opts: opts,
|
|
|
|
actions: map[float64][]func(){},
|
2015-09-29 18:45:38 +00:00
|
|
|
}
|
2015-09-29 20:40:44 +00:00
|
|
|
opts.baseTime = time.Now()
|
|
|
|
|
|
|
|
return test
|
2015-09-29 18:45:38 +00:00
|
|
|
}
|
|
|
|
|
2015-10-02 10:32:19 +00:00
|
|
|
// freeAddress returns a new listen address not currently in use.
|
2015-09-30 15:33:49 +00:00
|
|
|
func freeAddress() string {
|
2015-10-01 20:15:27 +00:00
|
|
|
// Let the OS allocate a free address, close it and hope
|
|
|
|
// it is still free when starting Alertmanager.
|
2015-10-29 13:34:24 +00:00
|
|
|
l, err := net.Listen("tcp4", ":0")
|
2015-10-01 20:15:27 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
2015-09-30 15:33:49 +00:00
|
|
|
}
|
2015-10-01 20:15:27 +00:00
|
|
|
defer l.Close()
|
2015-09-30 15:33:49 +00:00
|
|
|
|
2015-10-01 20:15:27 +00:00
|
|
|
return l.Addr().String()
|
2015-09-30 15:33:49 +00:00
|
|
|
}
|
|
|
|
|
2015-10-02 10:18:02 +00:00
|
|
|
// Do sets the given function to be executed at the given time.
|
|
|
|
func (t *AcceptanceTest) Do(at float64, f func()) {
|
|
|
|
t.actions[at] = append(t.actions[at], f)
|
|
|
|
}
|
|
|
|
|
2015-09-30 14:13:00 +00:00
|
|
|
// Alertmanager returns a new structure that allows starting an instance
|
2015-09-29 20:40:44 +00:00
|
|
|
// of Alertmanager on a random port.
|
2015-10-02 12:10:04 +00:00
|
|
|
func (t *AcceptanceTest) Alertmanager(conf string) *Alertmanager {
|
2015-09-29 20:40:44 +00:00
|
|
|
am := &Alertmanager{
|
2015-10-02 10:18:02 +00:00
|
|
|
t: t,
|
|
|
|
opts: t.opts,
|
2015-09-29 20:40:44 +00:00
|
|
|
}
|
2015-09-29 18:45:38 +00:00
|
|
|
|
2015-10-06 10:41:20 +00:00
|
|
|
dir, err := ioutil.TempDir("", "am_test")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-10-07 14:18:55 +00:00
|
|
|
am.dir = dir
|
|
|
|
|
2015-10-06 10:41:20 +00:00
|
|
|
cf, err := os.Create(filepath.Join(dir, "config.yml"))
|
2015-09-29 18:45:38 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-09-29 20:40:44 +00:00
|
|
|
am.confFile = cf
|
2015-10-02 12:14:30 +00:00
|
|
|
am.UpdateConfig(conf)
|
2015-09-30 12:54:54 +00:00
|
|
|
|
2015-10-01 18:58:46 +00:00
|
|
|
am.addr = freeAddress()
|
|
|
|
|
2015-10-01 19:28:18 +00:00
|
|
|
t.Logf("AM on %s", am.addr)
|
|
|
|
|
2015-10-01 18:58:46 +00:00
|
|
|
client, err := alertmanager.New(alertmanager.Config{
|
|
|
|
Address: fmt.Sprintf("http://%s", am.addr),
|
|
|
|
})
|
|
|
|
if err != nil {
|
2015-10-08 08:50:37 +00:00
|
|
|
t.Fatal(err)
|
2015-10-01 18:58:46 +00:00
|
|
|
}
|
|
|
|
am.client = client
|
2015-09-30 15:33:49 +00:00
|
|
|
|
2015-09-29 20:40:44 +00:00
|
|
|
t.ams = append(t.ams, am)
|
|
|
|
|
|
|
|
return am
|
|
|
|
}
|
|
|
|
|
2015-10-02 10:32:19 +00:00
|
|
|
// Collector returns a new collector bound to the test instance.
|
2015-09-30 14:13:00 +00:00
|
|
|
func (t *AcceptanceTest) Collector(name string) *Collector {
|
|
|
|
co := &Collector{
|
2015-09-29 20:40:44 +00:00
|
|
|
t: t.T,
|
2015-09-30 13:02:07 +00:00
|
|
|
name: name,
|
2015-09-29 20:40:44 +00:00
|
|
|
opts: t.opts,
|
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-29 20:40:44 +00:00
|
|
|
}
|
|
|
|
t.collectors = append(t.collectors, co)
|
|
|
|
|
|
|
|
return co
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run starts all Alertmanagers and runs queries against them. It then checks
|
2015-11-10 12:47:04 +00:00
|
|
|
// whether all expected notifications have arrived at the expected receiver.
|
2015-09-30 14:13:00 +00:00
|
|
|
func (t *AcceptanceTest) Run() {
|
2015-10-15 14:15:37 +00:00
|
|
|
errc := make(chan error)
|
|
|
|
|
2015-09-29 20:40:44 +00:00
|
|
|
for _, am := range t.ams {
|
2015-10-15 14:15:37 +00:00
|
|
|
am.errc = errc
|
|
|
|
|
2015-10-02 10:28:27 +00:00
|
|
|
am.Start()
|
|
|
|
defer func(am *Alertmanager) {
|
|
|
|
am.Terminate()
|
|
|
|
am.cleanup()
|
|
|
|
}(am)
|
2015-09-29 20:40:44 +00:00
|
|
|
}
|
|
|
|
|
2015-10-15 14:15:37 +00:00
|
|
|
go t.runActions()
|
2015-09-29 20:40:44 +00:00
|
|
|
|
|
|
|
var latest float64
|
|
|
|
for _, coll := range t.collectors {
|
|
|
|
if l := coll.latest(); l > latest {
|
|
|
|
latest = l
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-30 13:35:52 +00:00
|
|
|
deadline := t.opts.expandTime(latest)
|
2015-10-15 14:15:37 +00:00
|
|
|
|
|
|
|
select {
|
|
|
|
case <-time.After(deadline.Sub(time.Now())):
|
|
|
|
// continue
|
|
|
|
case err := <-errc:
|
2015-10-15 14:17:04 +00:00
|
|
|
t.Error(err)
|
2015-10-15 14:15:37 +00:00
|
|
|
}
|
2015-09-29 20:40:44 +00:00
|
|
|
|
|
|
|
for _, coll := range t.collectors {
|
|
|
|
report := coll.check()
|
|
|
|
t.Log(report)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, am := range t.ams {
|
2015-09-30 13:02:07 +00:00
|
|
|
t.Logf("stdout:\n%v", am.cmd.Stdout)
|
|
|
|
t.Logf("stderr:\n%v", am.cmd.Stderr)
|
2015-09-29 18:45:38 +00:00
|
|
|
}
|
2015-09-29 20:40:44 +00:00
|
|
|
}
|
2015-09-29 18:45:38 +00:00
|
|
|
|
2015-10-02 10:18:02 +00:00
|
|
|
// runActions performs the stored actions at the defined times.
|
|
|
|
func (t *AcceptanceTest) runActions() {
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
|
|
|
|
for at, fs := range t.actions {
|
|
|
|
ts := t.opts.expandTime(at)
|
|
|
|
wg.Add(len(fs))
|
|
|
|
|
|
|
|
for _, f := range fs {
|
|
|
|
go func(f func()) {
|
|
|
|
time.Sleep(ts.Sub(time.Now()))
|
|
|
|
f()
|
|
|
|
wg.Done()
|
|
|
|
}(f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
wg.Wait()
|
|
|
|
}
|
|
|
|
|
2015-09-29 20:40:44 +00:00
|
|
|
// Alertmanager encapsulates an Alertmanager process and allows
|
|
|
|
// declaring alerts being pushed to it at fixed points in time.
|
|
|
|
type Alertmanager struct {
|
2015-10-02 10:18:02 +00:00
|
|
|
t *AcceptanceTest
|
2015-09-30 14:13:00 +00:00
|
|
|
opts *AcceptanceOpts
|
2015-09-29 18:45:38 +00:00
|
|
|
|
2015-10-01 18:58:46 +00:00
|
|
|
addr string
|
|
|
|
client alertmanager.Client
|
|
|
|
cmd *exec.Cmd
|
2015-09-29 20:40:44 +00:00
|
|
|
confFile *os.File
|
2015-10-07 14:18:55 +00:00
|
|
|
dir string
|
2015-10-15 14:15:37 +00:00
|
|
|
|
|
|
|
errc chan<- error
|
2015-09-29 20:40:44 +00:00
|
|
|
}
|
|
|
|
|
2015-10-02 10:28:27 +00:00
|
|
|
// Start the alertmanager and wait until it is ready to receive.
|
|
|
|
func (am *Alertmanager) Start() {
|
2015-10-07 14:18:55 +00:00
|
|
|
cmd := exec.Command("../../alertmanager",
|
|
|
|
"-config.file", am.confFile.Name(),
|
|
|
|
"-log.level", "debug",
|
|
|
|
"-web.listen-address", am.addr,
|
2015-11-12 14:03:09 +00:00
|
|
|
"-storage.path", am.dir,
|
2015-10-07 14:18:55 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
if am.cmd == nil {
|
|
|
|
var outb, errb bytes.Buffer
|
|
|
|
cmd.Stdout = &outb
|
|
|
|
cmd.Stderr = &errb
|
|
|
|
} else {
|
|
|
|
cmd.Stdout = am.cmd.Stdout
|
|
|
|
cmd.Stderr = am.cmd.Stderr
|
|
|
|
}
|
|
|
|
am.cmd = cmd
|
|
|
|
|
2015-10-02 10:28:27 +00:00
|
|
|
if err := am.cmd.Start(); err != nil {
|
|
|
|
am.t.Fatalf("Starting alertmanager failed: %s", err)
|
|
|
|
}
|
|
|
|
|
2015-10-15 14:15:37 +00:00
|
|
|
go func() {
|
|
|
|
if err := am.cmd.Wait(); err != nil {
|
|
|
|
am.errc <- err
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2015-10-07 14:18:55 +00:00
|
|
|
time.Sleep(50 * time.Millisecond)
|
2015-10-02 10:28:27 +00:00
|
|
|
}
|
|
|
|
|
2015-11-20 14:10:38 +00:00
|
|
|
// Terminate kills the underlying Alertmanager process and remove intermediate
|
|
|
|
// data.
|
2015-10-02 10:28:27 +00:00
|
|
|
func (am *Alertmanager) Terminate() {
|
|
|
|
syscall.Kill(am.cmd.Process.Pid, syscall.SIGTERM)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reload sends the reloading signal to the Alertmanager process.
|
|
|
|
func (am *Alertmanager) Reload() {
|
|
|
|
syscall.Kill(am.cmd.Process.Pid, syscall.SIGHUP)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (am *Alertmanager) cleanup() {
|
|
|
|
os.RemoveAll(am.confFile.Name())
|
|
|
|
}
|
|
|
|
|
2015-10-01 18:58:46 +00:00
|
|
|
// Push declares alerts that are to be pushed to the Alertmanager
|
2015-09-29 20:40:44 +00:00
|
|
|
// server at a relative point in time.
|
2015-09-30 14:13:00 +00:00
|
|
|
func (am *Alertmanager) Push(at float64, alerts ...*TestAlert) {
|
2015-10-01 13:46:39 +00:00
|
|
|
var nas model.Alerts
|
2015-09-29 20:40:44 +00:00
|
|
|
for _, a := range alerts {
|
2015-09-30 13:35:52 +00:00
|
|
|
nas = append(nas, a.nativeAlert(am.opts))
|
2015-09-29 18:45:38 +00:00
|
|
|
}
|
2015-09-30 15:45:37 +00:00
|
|
|
|
2015-10-02 10:45:52 +00:00
|
|
|
alertAPI := alertmanager.NewAlertAPI(am.client)
|
2015-09-30 15:45:37 +00:00
|
|
|
|
2015-10-02 10:45:52 +00:00
|
|
|
am.t.Do(at, func() {
|
|
|
|
if err := alertAPI.Push(context.Background(), nas...); err != nil {
|
2015-12-08 10:53:28 +00:00
|
|
|
am.t.Errorf("Error pushing %v: %s", nas, err)
|
2015-09-30 15:45:37 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-10-01 18:58:46 +00:00
|
|
|
// SetSilence updates or creates the given Silence.
|
|
|
|
func (am *Alertmanager) SetSilence(at float64, sil *TestSilence) {
|
|
|
|
silences := alertmanager.NewSilenceAPI(am.client)
|
2015-10-01 13:46:39 +00:00
|
|
|
|
2015-10-02 10:18:02 +00:00
|
|
|
am.t.Do(at, func() {
|
2015-10-01 18:58:46 +00:00
|
|
|
sid, err := silences.Set(context.Background(), sil.nativeSilence(am.opts))
|
|
|
|
if err != nil {
|
2015-12-08 10:53:28 +00:00
|
|
|
am.t.Errorf("Error setting silence %v: %s", sil, err)
|
2015-10-01 18:58:46 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
sil.ID = sid
|
|
|
|
})
|
2015-10-01 13:46:39 +00:00
|
|
|
}
|
|
|
|
|
2015-10-01 18:58:46 +00:00
|
|
|
// DelSilence deletes the silence with the sid at the given time.
|
|
|
|
func (am *Alertmanager) DelSilence(at float64, sil *TestSilence) {
|
|
|
|
silences := alertmanager.NewSilenceAPI(am.client)
|
2015-10-01 13:46:39 +00:00
|
|
|
|
2015-10-02 10:18:02 +00:00
|
|
|
am.t.Do(at, func() {
|
2015-10-01 18:58:46 +00:00
|
|
|
if err := silences.Del(context.Background(), sil.ID); err != nil {
|
2015-12-08 10:53:28 +00:00
|
|
|
am.t.Errorf("Error deleting silence %v: %s", sil, err)
|
2015-10-01 18:58:46 +00:00
|
|
|
}
|
|
|
|
})
|
2015-10-01 13:46:39 +00:00
|
|
|
}
|
2015-10-02 12:14:30 +00:00
|
|
|
|
|
|
|
// UpdateConfig rewrites the configuration file for the Alertmanager. It does not
|
|
|
|
// initiate config reloading.
|
|
|
|
func (am *Alertmanager) UpdateConfig(conf string) {
|
|
|
|
if _, err := am.confFile.WriteString(conf); err != nil {
|
2015-10-08 08:50:37 +00:00
|
|
|
am.t.Fatal(err)
|
2015-10-02 12:14:30 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if err := am.confFile.Sync(); err != nil {
|
2015-10-08 08:50:37 +00:00
|
|
|
am.t.Fatal(err)
|
2015-10-02 12:14:30 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|