-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Martin Chodur <m.chodur@seznam.cz>
- Loading branch information
Showing
7 changed files
with
260 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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("") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters