api: Implement OpenAPI generated Alertmanager API V2
The current Alertmanager API v1 is undocumented and written by hand.
This patch introduces a new Alertmanager API - v2. The API is fully
generated via an OpenAPI 2.0 [1] specification (see
`api/v2/openapi.yaml`) with the exception of the http handlers itself.
Pros:
- Generated server code
- Ability to generate clients in all major languages
(Go, Java, JS, Python, Ruby, Haskell, *elm* [3] ...)
- Strict contract (OpenAPI spec) between server and clients.
- Instant feedback on frontend-breaking changes, due to strictly
typed frontend language elm.
- Generated documentation (See Alertmanager online Swagger UI [4])
Cons:
- Dependency on open api ecosystem including go-swagger [2]
In addition this patch includes the following changes.
- README.md: Add API section
- test: Duplicate acceptance test to API v1 & API v2 version
The Alertmanager acceptance test framework has a decent test coverage
on the Alertmanager API. Introducing the Alertmanager API v2 does not go
hand in hand with deprecating API v1. They should live alongside each
other for a couple of minor Alertmanager versions.
Instead of porting the acceptance test framework to use the new API v2,
this patch duplicates the acceptance tests, one using the API v1, the
other API v2.
Once API v1 is removed we can simply remove `test/with_api_v1` and bring
`test/with_api_v2` to `test/`.
[1]
https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md
[2] https://github.com/go-swagger/go-swagger/
[3] https://github.com/ahultgren/swagger-elm
[4]
http://petstore.swagger.io/?url=https://raw.githubusercontent.com/mxinden/alertmanager/apiv2/api/v2/openapi.yaml
Signed-off-by: Max Leonard Inden <IndenML@gmail.com>
2018-04-26 06:12:49 +00:00
|
|
|
// Copyright 2018 Prometheus Team
|
2015-10-11 15:24:49 +00: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.
|
|
|
|
|
2015-09-29 18:45:38 +00:00
|
|
|
package test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2018-11-09 09:00:23 +00:00
|
|
|
"context"
|
2015-09-29 20:40:44 +00:00
|
|
|
"fmt"
|
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"
|
2023-11-24 10:01:40 +00:00
|
|
|
"strings"
|
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"
|
|
|
|
|
2019-03-12 16:11:23 +00:00
|
|
|
apiclient "github.com/prometheus/alertmanager/api/v2/client"
|
|
|
|
"github.com/prometheus/alertmanager/api/v2/client/alert"
|
|
|
|
"github.com/prometheus/alertmanager/api/v2/client/general"
|
2019-06-20 15:37:08 +00:00
|
|
|
"github.com/prometheus/alertmanager/api/v2/client/silence"
|
2019-03-12 16:11:23 +00:00
|
|
|
"github.com/prometheus/alertmanager/api/v2/models"
|
2018-03-28 17:19:04 +00:00
|
|
|
|
api: Implement OpenAPI generated Alertmanager API V2
The current Alertmanager API v1 is undocumented and written by hand.
This patch introduces a new Alertmanager API - v2. The API is fully
generated via an OpenAPI 2.0 [1] specification (see
`api/v2/openapi.yaml`) with the exception of the http handlers itself.
Pros:
- Generated server code
- Ability to generate clients in all major languages
(Go, Java, JS, Python, Ruby, Haskell, *elm* [3] ...)
- Strict contract (OpenAPI spec) between server and clients.
- Instant feedback on frontend-breaking changes, due to strictly
typed frontend language elm.
- Generated documentation (See Alertmanager online Swagger UI [4])
Cons:
- Dependency on open api ecosystem including go-swagger [2]
In addition this patch includes the following changes.
- README.md: Add API section
- test: Duplicate acceptance test to API v1 & API v2 version
The Alertmanager acceptance test framework has a decent test coverage
on the Alertmanager API. Introducing the Alertmanager API v2 does not go
hand in hand with deprecating API v1. They should live alongside each
other for a couple of minor Alertmanager versions.
Instead of porting the acceptance test framework to use the new API v2,
this patch duplicates the acceptance tests, one using the API v1, the
other API v2.
Once API v1 is removed we can simply remove `test/with_api_v1` and bring
`test/with_api_v2` to `test/`.
[1]
https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md
[2] https://github.com/go-swagger/go-swagger/
[3] https://github.com/ahultgren/swagger-elm
[4]
http://petstore.swagger.io/?url=https://raw.githubusercontent.com/mxinden/alertmanager/apiv2/api/v2/openapi.yaml
Signed-off-by: Max Leonard Inden <IndenML@gmail.com>
2018-04-26 06:12:49 +00:00
|
|
|
httptransport "github.com/go-openapi/runtime/client"
|
|
|
|
"github.com/go-openapi/strfmt"
|
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
|
|
|
|
2018-08-04 21:04:00 +00:00
|
|
|
amc *AlertmanagerCluster
|
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
|
|
|
}
|
|
|
|
|
2019-03-12 09:29:26 +00:00
|
|
|
// AcceptanceOpts defines configuration parameters for an acceptance test.
|
2015-09-30 14:13:00 +00:00
|
|
|
type AcceptanceOpts struct {
|
2023-11-24 10:01:40 +00:00
|
|
|
FeatureFlags []string
|
|
|
|
RoutePrefix string
|
|
|
|
Tolerance time.Duration
|
|
|
|
baseTime time.Time
|
2015-09-29 18:45:38 +00:00
|
|
|
}
|
|
|
|
|
2018-11-20 15:45:56 +00:00
|
|
|
func (opts *AcceptanceOpts) alertString(a *models.GettableAlert) string {
|
|
|
|
if a.EndsAt == nil || time.Time(*a.EndsAt).IsZero() {
|
|
|
|
return fmt.Sprintf("%v[%v:]", a, opts.relativeTime(time.Time(*a.StartsAt)))
|
2015-10-05 14:51:34 +00:00
|
|
|
}
|
2018-11-20 15:45:56 +00:00
|
|
|
return fmt.Sprintf("%v[%v:%v]", a, opts.relativeTime(time.Time(*a.StartsAt)), opts.relativeTime(time.Time(*a.EndsAt)))
|
2015-10-05 14:51:34 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
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.
|
2016-06-04 07:18:43 +00:00
|
|
|
l, err := net.Listen("tcp4", "localhost:0")
|
2015-10-01 20:15:27 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
2015-09-30 15:33:49 +00:00
|
|
|
}
|
2018-08-14 17:14:00 +00:00
|
|
|
defer func() {
|
|
|
|
if err := l.Close(); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}()
|
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)
|
|
|
|
}
|
|
|
|
|
2018-08-04 21:04:00 +00:00
|
|
|
// AlertmanagerCluster returns a new AlertmanagerCluster that allows starting a
|
|
|
|
// cluster of Alertmanager instances on random ports.
|
|
|
|
func (t *AcceptanceTest) AlertmanagerCluster(conf string, size int) *AlertmanagerCluster {
|
|
|
|
amc := AlertmanagerCluster{}
|
2015-09-29 18:45:38 +00:00
|
|
|
|
2018-08-04 21:04:00 +00:00
|
|
|
for i := 0; i < size; i++ {
|
|
|
|
am := &Alertmanager{
|
|
|
|
t: t,
|
|
|
|
opts: t.opts,
|
|
|
|
}
|
2015-10-07 14:18:55 +00:00
|
|
|
|
2022-07-18 12:25:32 +00:00
|
|
|
dir, err := os.MkdirTemp("", "am_test")
|
2018-08-04 21:04:00 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
am.dir = dir
|
2015-09-30 12:54:54 +00:00
|
|
|
|
2018-08-04 21:04:00 +00:00
|
|
|
cf, err := os.Create(filepath.Join(dir, "config.yml"))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
am.confFile = cf
|
|
|
|
am.UpdateConfig(conf)
|
2015-10-01 18:58:46 +00:00
|
|
|
|
2018-08-04 21:04:00 +00:00
|
|
|
am.apiAddr = freeAddress()
|
|
|
|
am.clusterAddr = freeAddress()
|
2015-10-01 19:28:18 +00:00
|
|
|
|
2018-11-21 16:22:35 +00:00
|
|
|
transport := httptransport.New(am.apiAddr, t.opts.RoutePrefix+"/api/v2/", nil)
|
2018-08-04 21:04:00 +00:00
|
|
|
am.clientV2 = apiclient.New(transport, strfmt.Default)
|
2015-09-30 15:33:49 +00:00
|
|
|
|
2018-08-04 21:04:00 +00:00
|
|
|
amc.ams = append(amc.ams, am)
|
|
|
|
}
|
|
|
|
|
|
|
|
t.amc = &amc
|
2015-09-29 20:40:44 +00:00
|
|
|
|
2018-08-04 21:04:00 +00:00
|
|
|
return &amc
|
2015-09-29 20:40:44 +00:00
|
|
|
}
|
|
|
|
|
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,
|
2018-11-20 15:45:56 +00:00
|
|
|
collected: map[float64][]models.GettableAlerts{},
|
|
|
|
expected: map[Interval][]models.GettableAlerts{},
|
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)
|
|
|
|
|
2018-08-04 21:04:00 +00:00
|
|
|
for _, am := range t.amc.ams {
|
2015-10-15 14:15:37 +00:00
|
|
|
am.errc = errc
|
2024-02-13 15:38:44 +00:00
|
|
|
t.T.Cleanup(am.Terminate)
|
|
|
|
t.T.Cleanup(am.cleanup)
|
2015-09-29 20:40:44 +00:00
|
|
|
}
|
|
|
|
|
2018-08-04 21:04:00 +00:00
|
|
|
err := t.amc.Start()
|
|
|
|
if err != nil {
|
2024-02-13 15:38:44 +00:00
|
|
|
t.T.Log(err)
|
|
|
|
t.T.Fail()
|
|
|
|
return
|
2018-08-04 21:04:00 +00:00
|
|
|
}
|
|
|
|
|
2023-01-13 10:58:56 +00:00
|
|
|
// Set the reference time right before running the test actions to avoid
|
|
|
|
// test failures due to slow setup of the test environment.
|
|
|
|
t.opts.baseTime = time.Now()
|
|
|
|
|
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 {
|
2019-01-04 14:37:33 +00:00
|
|
|
case <-time.After(time.Until(deadline)):
|
2015-10-15 14:15:37 +00:00
|
|
|
// 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
|
|
|
}
|
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()) {
|
2019-01-04 14:37:33 +00:00
|
|
|
time.Sleep(time.Until(ts))
|
2015-10-02 10:18:02 +00:00
|
|
|
f()
|
|
|
|
wg.Done()
|
|
|
|
}(f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
wg.Wait()
|
|
|
|
}
|
|
|
|
|
2018-02-27 17:18:53 +00:00
|
|
|
type buffer struct {
|
|
|
|
b bytes.Buffer
|
|
|
|
mtx sync.Mutex
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *buffer) Write(p []byte) (int, error) {
|
|
|
|
b.mtx.Lock()
|
|
|
|
defer b.mtx.Unlock()
|
|
|
|
return b.b.Write(p)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *buffer) String() string {
|
|
|
|
b.mtx.Lock()
|
|
|
|
defer b.mtx.Unlock()
|
|
|
|
return b.b.String()
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2018-02-07 15:36:47 +00:00
|
|
|
apiAddr string
|
|
|
|
clusterAddr string
|
2022-11-30 16:06:57 +00:00
|
|
|
clientV2 *apiclient.AlertmanagerAPI
|
2018-02-07 15:36:47 +00:00
|
|
|
confFile *os.File
|
|
|
|
dir string
|
2015-10-15 14:15:37 +00:00
|
|
|
|
2024-02-13 15:38:44 +00:00
|
|
|
cmd *exec.Cmd
|
2015-10-15 14:15:37 +00:00
|
|
|
errc chan<- error
|
2015-09-29 20:40:44 +00:00
|
|
|
}
|
|
|
|
|
2018-08-04 21:04:00 +00:00
|
|
|
// AlertmanagerCluster represents a group of Alertmanager instances
|
|
|
|
// acting as a cluster.
|
|
|
|
type AlertmanagerCluster struct {
|
|
|
|
ams []*Alertmanager
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start the Alertmanager cluster and wait until it is ready to receive.
|
|
|
|
func (amc *AlertmanagerCluster) Start() error {
|
|
|
|
var peerFlags []string
|
|
|
|
for _, am := range amc.ams {
|
|
|
|
peerFlags = append(peerFlags, "--cluster.peer="+am.clusterAddr)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, am := range amc.ams {
|
|
|
|
err := am.Start(peerFlags)
|
|
|
|
if err != nil {
|
2023-11-24 21:17:35 +00:00
|
|
|
return fmt.Errorf("failed to start alertmanager cluster: %w", err)
|
2018-08-04 21:04:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, am := range amc.ams {
|
|
|
|
err := am.WaitForCluster(len(amc.ams))
|
|
|
|
if err != nil {
|
2023-11-24 21:17:35 +00:00
|
|
|
return fmt.Errorf("failed to wait for Alertmanager instance %q to join cluster: %w", am.clusterAddr, err)
|
2018-08-04 21:04:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-02-18 16:06:51 +00:00
|
|
|
// Members returns the underlying slice of cluster members.
|
|
|
|
func (amc *AlertmanagerCluster) Members() []*Alertmanager {
|
|
|
|
return amc.ams
|
|
|
|
}
|
|
|
|
|
2015-10-02 10:28:27 +00:00
|
|
|
// Start the alertmanager and wait until it is ready to receive.
|
2018-08-04 21:04:00 +00:00
|
|
|
func (am *Alertmanager) Start(additionalArg []string) error {
|
2018-11-21 16:22:35 +00:00
|
|
|
am.t.Helper()
|
2018-08-04 21:04:00 +00:00
|
|
|
args := []string{
|
2018-01-06 10:22:26 +00:00
|
|
|
"--config.file", am.confFile.Name(),
|
|
|
|
"--log.level", "debug",
|
2018-02-07 15:36:47 +00:00
|
|
|
"--web.listen-address", am.apiAddr,
|
2018-01-06 10:22:26 +00:00
|
|
|
"--storage.path", am.dir,
|
2018-02-12 10:31:55 +00:00
|
|
|
"--cluster.listen-address", am.clusterAddr,
|
2018-03-02 14:45:21 +00:00
|
|
|
"--cluster.settle-timeout", "0s",
|
2018-08-04 21:04:00 +00:00
|
|
|
}
|
2023-11-24 10:01:40 +00:00
|
|
|
if len(am.opts.FeatureFlags) > 0 {
|
|
|
|
args = append(args, "--enable-feature", strings.Join(am.opts.FeatureFlags, ","))
|
|
|
|
}
|
2018-11-21 16:22:35 +00:00
|
|
|
if am.opts.RoutePrefix != "" {
|
|
|
|
args = append(args, "--web.route-prefix", am.opts.RoutePrefix)
|
|
|
|
}
|
2018-08-04 21:04:00 +00:00
|
|
|
args = append(args, additionalArg...)
|
|
|
|
|
|
|
|
cmd := exec.Command("../../../alertmanager", args...)
|
2015-10-07 14:18:55 +00:00
|
|
|
|
|
|
|
if am.cmd == nil {
|
2018-02-27 17:18:53 +00:00
|
|
|
var outb, errb buffer
|
2015-10-07 14:18:55 +00:00
|
|
|
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 {
|
2022-07-07 10:07:13 +00:00
|
|
|
return err
|
2015-10-02 10:28:27 +00:00
|
|
|
}
|
|
|
|
|
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)
|
2022-07-07 10:07:13 +00:00
|
|
|
var lastErr error
|
2017-01-16 12:32:27 +00:00
|
|
|
for i := 0; i < 10; i++ {
|
2022-07-07 10:07:13 +00:00
|
|
|
_, lastErr = am.clientV2.General.GetStatus(nil)
|
|
|
|
if lastErr == nil {
|
2019-06-20 15:37:08 +00:00
|
|
|
return nil
|
2017-01-16 12:32:27 +00:00
|
|
|
}
|
2019-06-20 15:37:08 +00:00
|
|
|
time.Sleep(500 * time.Millisecond)
|
2017-01-16 12:32:27 +00:00
|
|
|
}
|
2023-11-24 21:17:35 +00:00
|
|
|
return fmt.Errorf("unable to get a successful response from the Alertmanager: %w", lastErr)
|
2018-08-04 21:04:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// WaitForCluster waits for the Alertmanager instance to join a cluster with the
|
|
|
|
// given size.
|
|
|
|
func (am *Alertmanager) WaitForCluster(size int) error {
|
|
|
|
params := general.NewGetStatusParams()
|
|
|
|
params.WithContext(context.Background())
|
2019-05-15 08:49:59 +00:00
|
|
|
var status *general.GetStatusOK
|
2018-08-04 21:04:00 +00:00
|
|
|
|
|
|
|
// Poll for 2s
|
|
|
|
for i := 0; i < 20; i++ {
|
2019-05-15 08:49:59 +00:00
|
|
|
var err error
|
|
|
|
status, err = am.clientV2.General.GetStatus(params)
|
2018-08-04 21:04:00 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-11-08 17:46:04 +00:00
|
|
|
if len(status.Payload.Cluster.Peers) == size {
|
2018-08-04 21:04:00 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Errorf(
|
2022-07-07 10:07:13 +00:00
|
|
|
"expected %v peers, but got %v",
|
2018-08-04 21:04:00 +00:00
|
|
|
size,
|
2018-11-08 17:46:04 +00:00
|
|
|
len(status.Payload.Cluster.Peers),
|
2018-08-04 21:04:00 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Terminate kills the underlying Alertmanager cluster processes and removes intermediate
|
|
|
|
// data.
|
|
|
|
func (amc *AlertmanagerCluster) Terminate() {
|
|
|
|
for _, am := range amc.ams {
|
|
|
|
am.Terminate()
|
|
|
|
}
|
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() {
|
2018-11-21 16:22:35 +00:00
|
|
|
am.t.Helper()
|
2024-02-13 15:38:44 +00:00
|
|
|
if am.cmd.Process != nil {
|
|
|
|
if err := syscall.Kill(am.cmd.Process.Pid, syscall.SIGTERM); err != nil {
|
|
|
|
am.t.Logf("Error sending SIGTERM to Alertmanager process: %v", err)
|
|
|
|
}
|
|
|
|
am.t.Logf("stdout:\n%v", am.cmd.Stdout)
|
|
|
|
am.t.Logf("stderr:\n%v", am.cmd.Stderr)
|
2018-08-05 13:38:25 +00:00
|
|
|
}
|
2015-10-02 10:28:27 +00:00
|
|
|
}
|
|
|
|
|
2018-08-04 21:04:00 +00:00
|
|
|
// Reload sends the reloading signal to the Alertmanager instances.
|
|
|
|
func (amc *AlertmanagerCluster) Reload() {
|
|
|
|
for _, am := range amc.ams {
|
|
|
|
am.Reload()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-02 10:28:27 +00:00
|
|
|
// Reload sends the reloading signal to the Alertmanager process.
|
|
|
|
func (am *Alertmanager) Reload() {
|
2018-11-21 16:22:35 +00:00
|
|
|
am.t.Helper()
|
2024-02-13 15:38:44 +00:00
|
|
|
if am.cmd.Process != nil {
|
|
|
|
if err := syscall.Kill(am.cmd.Process.Pid, syscall.SIGHUP); err != nil {
|
|
|
|
am.t.Fatalf("Error sending SIGHUP to Alertmanager process: %v", err)
|
|
|
|
}
|
2018-08-05 13:38:25 +00:00
|
|
|
}
|
2015-10-02 10:28:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (am *Alertmanager) cleanup() {
|
2018-11-21 16:22:35 +00:00
|
|
|
am.t.Helper()
|
2018-08-05 13:38:25 +00:00
|
|
|
if err := os.RemoveAll(am.confFile.Name()); err != nil {
|
|
|
|
am.t.Errorf("Error removing test config file %q: %v", am.confFile.Name(), err)
|
|
|
|
}
|
2015-10-02 10:28:27 +00:00
|
|
|
}
|
|
|
|
|
2018-08-04 21:04:00 +00:00
|
|
|
// Push declares alerts that are to be pushed to the Alertmanager
|
|
|
|
// servers at a relative point in time.
|
|
|
|
func (amc *AlertmanagerCluster) Push(at float64, alerts ...*TestAlert) {
|
|
|
|
for _, am := range amc.ams {
|
|
|
|
am.Push(at, alerts...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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) {
|
2023-01-13 10:58:56 +00:00
|
|
|
am.t.Do(at, func() {
|
|
|
|
var cas models.PostableAlerts
|
|
|
|
for i := range alerts {
|
|
|
|
a := alerts[i].nativeAlert(am.opts)
|
|
|
|
alert := &models.PostableAlert{
|
|
|
|
Alert: models.Alert{
|
|
|
|
Labels: a.Labels,
|
|
|
|
GeneratorURL: a.GeneratorURL,
|
|
|
|
},
|
|
|
|
Annotations: a.Annotations,
|
|
|
|
}
|
|
|
|
if a.StartsAt != nil {
|
|
|
|
alert.StartsAt = *a.StartsAt
|
|
|
|
}
|
|
|
|
if a.EndsAt != nil {
|
|
|
|
alert.EndsAt = *a.EndsAt
|
|
|
|
}
|
|
|
|
cas = append(cas, alert)
|
2018-11-20 15:45:56 +00:00
|
|
|
}
|
2015-09-30 15:45:37 +00:00
|
|
|
|
api: Implement OpenAPI generated Alertmanager API V2
The current Alertmanager API v1 is undocumented and written by hand.
This patch introduces a new Alertmanager API - v2. The API is fully
generated via an OpenAPI 2.0 [1] specification (see
`api/v2/openapi.yaml`) with the exception of the http handlers itself.
Pros:
- Generated server code
- Ability to generate clients in all major languages
(Go, Java, JS, Python, Ruby, Haskell, *elm* [3] ...)
- Strict contract (OpenAPI spec) between server and clients.
- Instant feedback on frontend-breaking changes, due to strictly
typed frontend language elm.
- Generated documentation (See Alertmanager online Swagger UI [4])
Cons:
- Dependency on open api ecosystem including go-swagger [2]
In addition this patch includes the following changes.
- README.md: Add API section
- test: Duplicate acceptance test to API v1 & API v2 version
The Alertmanager acceptance test framework has a decent test coverage
on the Alertmanager API. Introducing the Alertmanager API v2 does not go
hand in hand with deprecating API v1. They should live alongside each
other for a couple of minor Alertmanager versions.
Instead of porting the acceptance test framework to use the new API v2,
this patch duplicates the acceptance tests, one using the API v1, the
other API v2.
Once API v1 is removed we can simply remove `test/with_api_v1` and bring
`test/with_api_v2` to `test/`.
[1]
https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md
[2] https://github.com/go-swagger/go-swagger/
[3] https://github.com/ahultgren/swagger-elm
[4]
http://petstore.swagger.io/?url=https://raw.githubusercontent.com/mxinden/alertmanager/apiv2/api/v2/openapi.yaml
Signed-off-by: Max Leonard Inden <IndenML@gmail.com>
2018-04-26 06:12:49 +00:00
|
|
|
params := alert.PostAlertsParams{}
|
|
|
|
params.WithContext(context.Background()).WithAlerts(cas)
|
|
|
|
|
|
|
|
_, err := am.clientV2.Alert.PostAlerts(¶ms)
|
|
|
|
if err != nil {
|
2018-11-08 17:46:04 +00:00
|
|
|
am.t.Errorf("Error pushing %v: %v", cas, err)
|
2015-09-30 15:45:37 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-08-04 21:04:00 +00:00
|
|
|
// SetSilence updates or creates the given Silence.
|
|
|
|
func (amc *AlertmanagerCluster) SetSilence(at float64, sil *TestSilence) {
|
|
|
|
for _, am := range amc.ams {
|
|
|
|
am.SetSilence(at, sil)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-01 18:58:46 +00:00
|
|
|
// SetSilence updates or creates the given Silence.
|
|
|
|
func (am *Alertmanager) SetSilence(at float64, sil *TestSilence) {
|
2015-10-02 10:18:02 +00:00
|
|
|
am.t.Do(at, func() {
|
2019-06-20 15:37:08 +00:00
|
|
|
resp, err := am.clientV2.Silence.PostSilences(
|
|
|
|
silence.NewPostSilencesParams().WithSilence(
|
|
|
|
&models.PostableSilence{
|
|
|
|
Silence: *sil.nativeSilence(am.opts),
|
|
|
|
},
|
|
|
|
),
|
|
|
|
)
|
2015-10-01 18:58:46 +00:00
|
|
|
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
|
|
|
|
}
|
2019-06-20 15:37:08 +00:00
|
|
|
sil.SetID(resp.Payload.SilenceID)
|
2015-10-01 18:58:46 +00:00
|
|
|
})
|
2015-10-01 13:46:39 +00:00
|
|
|
}
|
|
|
|
|
2018-08-04 21:04:00 +00:00
|
|
|
// DelSilence deletes the silence with the sid at the given time.
|
|
|
|
func (amc *AlertmanagerCluster) DelSilence(at float64, sil *TestSilence) {
|
|
|
|
for _, am := range amc.ams {
|
|
|
|
am.DelSilence(at, sil)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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) {
|
2015-10-02 10:18:02 +00:00
|
|
|
am.t.Do(at, func() {
|
2019-06-20 15:37:08 +00:00
|
|
|
_, err := am.clientV2.Silence.DeleteSilence(
|
|
|
|
silence.NewDeleteSilenceParams().WithSilenceID(strfmt.UUID(sil.ID())),
|
|
|
|
)
|
2016-05-29 23:12:05 +00:00
|
|
|
if err != nil {
|
|
|
|
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
|
|
|
|
2018-08-04 21:04:00 +00:00
|
|
|
// UpdateConfig rewrites the configuration file for the Alertmanager cluster. It
|
|
|
|
// does not initiate config reloading.
|
|
|
|
func (amc *AlertmanagerCluster) UpdateConfig(conf string) {
|
|
|
|
for _, am := range amc.ams {
|
|
|
|
am.UpdateConfig(conf)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
api: Implement OpenAPI generated Alertmanager API V2
The current Alertmanager API v1 is undocumented and written by hand.
This patch introduces a new Alertmanager API - v2. The API is fully
generated via an OpenAPI 2.0 [1] specification (see
`api/v2/openapi.yaml`) with the exception of the http handlers itself.
Pros:
- Generated server code
- Ability to generate clients in all major languages
(Go, Java, JS, Python, Ruby, Haskell, *elm* [3] ...)
- Strict contract (OpenAPI spec) between server and clients.
- Instant feedback on frontend-breaking changes, due to strictly
typed frontend language elm.
- Generated documentation (See Alertmanager online Swagger UI [4])
Cons:
- Dependency on open api ecosystem including go-swagger [2]
In addition this patch includes the following changes.
- README.md: Add API section
- test: Duplicate acceptance test to API v1 & API v2 version
The Alertmanager acceptance test framework has a decent test coverage
on the Alertmanager API. Introducing the Alertmanager API v2 does not go
hand in hand with deprecating API v1. They should live alongside each
other for a couple of minor Alertmanager versions.
Instead of porting the acceptance test framework to use the new API v2,
this patch duplicates the acceptance tests, one using the API v1, the
other API v2.
Once API v1 is removed we can simply remove `test/with_api_v1` and bring
`test/with_api_v2` to `test/`.
[1]
https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md
[2] https://github.com/go-swagger/go-swagger/
[3] https://github.com/ahultgren/swagger-elm
[4]
http://petstore.swagger.io/?url=https://raw.githubusercontent.com/mxinden/alertmanager/apiv2/api/v2/openapi.yaml
Signed-off-by: Max Leonard Inden <IndenML@gmail.com>
2018-04-26 06:12:49 +00:00
|
|
|
|
2019-06-20 15:37:08 +00:00
|
|
|
// Client returns a client to interact with the API v2 endpoint.
|
2022-11-30 16:06:57 +00:00
|
|
|
func (am *Alertmanager) Client() *apiclient.AlertmanagerAPI {
|
2019-02-18 16:06:51 +00:00
|
|
|
return am.clientV2
|
|
|
|
}
|