test/with_api_v1: add test for route prefix

Signed-off-by: Simon Pasquier <spasquie@redhat.com>
This commit is contained in:
Simon Pasquier 2018-11-21 17:45:50 +01:00
parent d6f8437b9b
commit 5fd944a603
2 changed files with 72 additions and 16 deletions

View File

@ -50,8 +50,9 @@ type AcceptanceTest struct {
// AcceptanceOpts defines configuration paramters for an acceptance test.
type AcceptanceOpts struct {
Tolerance time.Duration
baseTime time.Time
RoutePrefix string
Tolerance time.Duration
baseTime time.Time
}
func (opts *AcceptanceOpts) alertString(a *model.Alert) string {
@ -135,7 +136,7 @@ func (t *AcceptanceTest) Alertmanager(conf string) *Alertmanager {
t.Logf("AM on %s", am.apiAddr)
c, err := api.NewClient(api.Config{
Address: fmt.Sprintf("http://%s", am.apiAddr),
Address: am.getURL(""),
})
if err != nil {
t.Fatal(err)
@ -257,14 +258,18 @@ type Alertmanager struct {
// Start the alertmanager and wait until it is ready to receive.
func (am *Alertmanager) Start() {
cmd := exec.Command("../../../alertmanager",
args := []string{
"--config.file", am.confFile.Name(),
"--log.level", "debug",
"--web.listen-address", am.apiAddr,
"--storage.path", am.dir,
"--cluster.listen-address", am.clusterAddr,
"--cluster.settle-timeout", "0s",
)
}
if am.opts.RoutePrefix != "" {
args = append(args, "--web.route-prefix", am.opts.RoutePrefix)
}
cmd := exec.Command("../../../alertmanager", args...)
if am.cmd == nil {
var outb, errb buffer
@ -288,16 +293,21 @@ func (am *Alertmanager) Start() {
time.Sleep(50 * time.Millisecond)
for i := 0; i < 10; i++ {
resp, err := http.Get(fmt.Sprintf("http://%s/status", am.apiAddr))
if err == nil {
_, err := ioutil.ReadAll(resp.Body)
if err != nil {
am.t.Fatalf("Starting alertmanager failed: %s", err)
}
resp.Body.Close()
return
resp, err := http.Get(am.getURL("/"))
if err != nil {
time.Sleep(500 * time.Millisecond)
continue
}
time.Sleep(500 * time.Millisecond)
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
am.t.Fatalf("Starting alertmanager failed: expected HTTP status '200', got '%d'", resp.StatusCode)
}
_, err = ioutil.ReadAll(resp.Body)
if err != nil {
am.t.Fatalf("Starting alertmanager failed: %s", err)
}
resp.Body.Close()
return
}
am.t.Fatalf("Starting alertmanager failed: timeout")
}
@ -363,7 +373,7 @@ func (am *Alertmanager) SetSilence(at float64, sil *TestSilence) {
return
}
resp, err := http.Post(fmt.Sprintf("http://%s/api/v1/silences", am.apiAddr), "application/json", &buf)
resp, err := http.Post(am.getURL("/api/v1/silences"), "application/json", &buf)
if err != nil {
am.t.Errorf("Error setting silence %v: %s", sil, err)
return
@ -392,7 +402,7 @@ func (am *Alertmanager) SetSilence(at float64, sil *TestSilence) {
// DelSilence deletes the silence with the sid at the given time.
func (am *Alertmanager) DelSilence(at float64, sil *TestSilence) {
am.t.Do(at, func() {
req, err := http.NewRequest("DELETE", fmt.Sprintf("http://%s/api/v1/silence/%s", am.apiAddr, sil.ID()), nil)
req, err := http.NewRequest("DELETE", am.getURL(fmt.Sprintf("/api/v1/silence/%s", sil.ID())), nil)
if err != nil {
am.t.Errorf("Error deleting silence %v: %s", sil, err)
return
@ -418,3 +428,7 @@ func (am *Alertmanager) UpdateConfig(conf string) {
return
}
}
func (am *Alertmanager) getURL(path string) string {
return fmt.Sprintf("http://%s%s%s", am.apiAddr, am.opts.RoutePrefix, path)
}

View File

@ -0,0 +1,42 @@
// Copyright 2018 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.
package test
import (
"testing"
a "github.com/prometheus/alertmanager/test/with_api_v1"
)
func TestWebWithPrefix(t *testing.T) {
t.Parallel()
conf := `
route:
receiver: "default"
group_by: []
group_wait: 1s
group_interval: 1s
repeat_interval: 1h
receivers:
- name: "default"
`
// The test framework polls the API with the given prefix during
// Alertmanager startup and thereby ensures proper configuration.
at := a.NewAcceptanceTest(t, &a.AcceptanceOpts{RoutePrefix: "/foo"})
at.Alertmanager(conf)
at.Run()
}