Skip to content

Commit

Permalink
feat: added routing test to amtool
Browse files Browse the repository at this point in the history
Signed-off-by: Martin Chodur <m.chodur@seznam.cz>
  • Loading branch information
FUSAKLA committed Aug 12, 2018
1 parent ec26348 commit 3764668
Show file tree
Hide file tree
Showing 7 changed files with 260 additions and 9 deletions.
10 changes: 1 addition & 9 deletions cli/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,9 @@ import (
"context"
"errors"

"github.com/prometheus/client_golang/api"
"gopkg.in/alecthomas/kingpin.v2"

"github.com/prometheus/alertmanager/cli/format"
"github.com/prometheus/alertmanager/client"
)

const configHelp = `View current config.
Expand All @@ -35,16 +33,10 @@ The amount of output is controlled by the output selection flag:
// configCmd represents the config command
func configureConfigCmd(app *kingpin.Application) {
app.Command("config", configHelp).Action(execWithTimeout(queryConfig)).PreAction(requireAlertManagerURL)

}

func queryConfig(ctx context.Context, _ *kingpin.ParseContext) error {
c, err := api.NewClient(api.Config{Address: alertmanagerURL.String()})
if err != nil {
return err
}
statusAPI := client.NewStatusAPI(c)
status, err := statusAPI.Get(ctx)
status, err := GetRemoteAlertmanagerConfigStatus(ctx, alertmanagerURL)
if err != nil {
return err
}
Expand Down
1 change: 1 addition & 0 deletions cli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ func Execute() {
configureSilenceCmd(app)
configureCheckConfigCmd(app)
configureConfigCmd(app)
configureRoutingTestCmd(app)

err = resolver.Bind(app, os.Args[1:])
if err != nil {
Expand Down
111 changes: 111 additions & 0 deletions cli/test_routing.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
// 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 cli

import (
"context"
"errors"
"fmt"
"net/url"
"os"
"strings"

"github.com/prometheus/alertmanager/config"
"gopkg.in/alecthomas/kingpin.v2"
)

type routingTestCmd struct {
configFile string
labels []string
expectedReceivers string
}

const routingTestHelp = `Test alert routing
Will return receiver names which the alert with given labels resolves to.
(If resolves to multiple receivers, they are printed out joined using ',' in order as defined in the routing tree)
Routing is loaded from configuration file or remote Alertmanager.
Use --config.file or --alertmanager.url flags to specify where to look for the configuration.
Than specify labels defining the alert.
Example:
./amtool routing-test --config.file=doc/examples/simple.yml --verify.receivers=team-DB-pager service=database
`

func configureRoutingTestCmd(app *kingpin.Application) {
var (
c = &routingTestCmd{}
testCmd = app.Command("routing-test", routingTestHelp)
)
testCmd.Flag("config.file", "Config file to be tested").ExistingFileVar(&c.configFile)
testCmd.Flag("verify.receivers", "Checks if specified receivers matches resolved receivers. (Returns exit status 1 if not)").StringVar(&c.expectedReceivers)
testCmd.Arg("labels", "Alert labels in name=value notation").StringsVar(&c.labels)
testCmd.Action(execWithTimeout(c.testAction))
}

func (c *routingTestCmd) testAction(ctx context.Context, _ *kingpin.ParseContext) error {

// Load configuration form file or URL.
cfg, err := loadAlertmanagerConfig(ctx, alertmanagerURL, c.configFile)
if err != nil {
kingpin.Fatalf("%s", err)
return err
}

// Parse lables to LabelSet.
ls, err := parseLabels(c.labels)
if err != nil {
kingpin.Fatalf("Failed to parse labels: %s", err)
}

receivers, err := ResolveAlertReceivers(cfg, &ls)
receiversSlug := strings.Join(receivers, ",")
fmt.Printf("%s\n", receiversSlug)
if c.expectedReceivers != "" && c.expectedReceivers != receiversSlug {
fmt.Printf("WARNING: Expected receivers did not match resolved receivers.\n")
os.Exit(1)
}
return err
}

func checkConfigInputFlags(alertmanagerURL *url.URL, configFile string) {
if alertmanagerURL != nil && configFile != "" {
kingpin.Fatalf("You can't use both --config.file and --alertmanager.url at the same time.")
}
if alertmanagerURL == nil && configFile == "" {
kingpin.Fatalf("You have to specify one of --config.file or --alertmanager.url flags.")
}
}

func loadAlertmanagerConfig(ctx context.Context, alertmanagerURL *url.URL, configFile string) (*config.Config, error) {
checkConfigInputFlags(alertmanagerURL, configFile)
if alertmanagerURL != nil {
status, err := GetRemoteAlertmanagerConfigStatus(ctx, alertmanagerURL)
if err != nil {
return nil, err
}
return status.ConfigJSON, nil
}
if configFile != "" {
cfg, _, err := config.LoadFile(configFile)
if err != nil {
return nil, err
}
return cfg, nil
}
return nil, errors.New("Failed to get Alertmanager configuration.")
}
64 changes: 64 additions & 0 deletions cli/test_routing_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// 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 cli

import (
"fmt"
"reflect"
"strings"
"testing"

"github.com/prometheus/alertmanager/client"
"github.com/prometheus/alertmanager/config"
)

type routingTestDefinition struct {
alert client.LabelSet
expectedReceivers []string
configFile string
}

func checkResolvedReceivers(cfg *config.Config, ls client.LabelSet, expectedReceivers []string) error {
resolvedReceivers, err := ResolveAlertReceivers(cfg, &ls)
if err != nil {
return err
}
if !reflect.DeepEqual(expectedReceivers, resolvedReceivers) {
return fmt.Errorf("Unexpected routing result want: `%s`, got: `%s`", strings.Join(expectedReceivers, ","), strings.Join(resolvedReceivers, ","))
}
return nil
}

func TestRoutingTest(t *testing.T) {
var tests []*routingTestDefinition
tests = append(tests, &routingTestDefinition{configFile: "testdata/conf.routing.yml", alert: client.LabelSet{"test": "1"}, expectedReceivers: []string{"test1"}})
tests = append(tests, &routingTestDefinition{configFile: "testdata/conf.routing.yml", alert: client.LabelSet{"test": "2"}, expectedReceivers: []string{"test1", "test2"}})
tests = append(tests, &routingTestDefinition{configFile: "testdata/conf.routing-reverted.yml", alert: client.LabelSet{"test": "2"}, expectedReceivers: []string{"test2", "test1"}})
tests = append(tests, &routingTestDefinition{configFile: "testdata/conf.routing.yml", alert: client.LabelSet{"test": "volovina"}, expectedReceivers: []string{"default"}})

fmt.Printf("Running routing-test testsuit:\n")

for _, test := range tests {
cfg, _, err := config.LoadFile(test.configFile)
if err != nil {
t.Fatalf("failed to load test configuration: %v", err)
}
err = checkResolvedReceivers(cfg, test.alert, test.expectedReceivers)
if err != nil {
t.Fatalf("%v", err)
}
fmt.Println(" OK")
}
fmt.Println("")
}
22 changes: 22 additions & 0 deletions cli/testdata/conf.routing-reverted.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
global:
smtp_smarthost: 'localhost:25'

templates:
- '/etc/alertmanager/template/*.tmpl'

route:
receiver: default
routes:
- match:
test: 2
receiver: test2
continue: true
- match_re:
test: ^[12]$
receiver: test1
continue: true

receivers:
- name: default
- name: test1
- name: test2
21 changes: 21 additions & 0 deletions cli/testdata/conf.routing.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
global:
smtp_smarthost: 'localhost:25'

templates:
- '/etc/alertmanager/template/*.tmpl'

route:
receiver: default
routes:
- match_re:
test: ^[12]$
receiver: test1
continue: true
- match:
test: 2
receiver: test2

receivers:
- name: default
- name: test1
- name: test2
40 changes: 40 additions & 0 deletions cli/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,11 @@ import (
"path"

"github.com/prometheus/alertmanager/client"
"github.com/prometheus/client_golang/api"
kingpin "gopkg.in/alecthomas/kingpin.v2"

"github.com/prometheus/alertmanager/config"
"github.com/prometheus/alertmanager/dispatch"
"github.com/prometheus/alertmanager/pkg/parse"
"github.com/prometheus/alertmanager/types"
"github.com/prometheus/common/model"
Expand Down Expand Up @@ -70,6 +73,43 @@ func parseMatchers(inputMatchers []string) ([]labels.Matcher, error) {
return matchers, nil
}

// GetRemoteAlertmanagerConfigStatus returns status responsecontaining configuration from remote Alertmanager
func GetRemoteAlertmanagerConfigStatus(ctx context.Context, alertmanagerURL *url.URL) (*client.ServerStatus, error) {
c, err := api.NewClient(api.Config{Address: alertmanagerURL.String()})
if err != nil {
return nil, err
}
statusAPI := client.NewStatusAPI(c)
status, err := statusAPI.Get(ctx)
if err != nil {
return nil, err
}
return status, nil
}

// ResolveAlertReceivers returns list of receiver names which given LabelSet resolves to.
func ResolveAlertReceivers(cfg *config.Config, labels *client.LabelSet) ([]string, error) {
var (
finalRoutes []*dispatch.Route
receivers []string
)
mainRoute := dispatch.NewRoute(cfg.Route, nil)
finalRoutes = mainRoute.Match(ConvertCientToCommonLabelSet(*labels))
for _, r := range finalRoutes {
receivers = append(receivers, r.RouteOpts.Receiver)
}
return receivers, nil
}

// ConvertCientToCommonLabelSet converts client.LabelSet to model.Labelset
func ConvertCientToCommonLabelSet(cls client.LabelSet) model.LabelSet {
mls := make(model.LabelSet, len(cls))
for ln, lv := range cls {
mls[model.LabelName(ln)] = model.LabelValue(lv)
}
return mls
}

// Parse a list of labels (cli arguments)
func parseLabels(inputLabels []string) (client.LabelSet, error) {
labelSet := make(client.LabelSet, len(inputLabels))
Expand Down

0 comments on commit 3764668

Please sign in to comment.