-
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.
added test checking for empty or null fields and blank regexps
- Loading branch information
Showing
2 changed files
with
109 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,10 +15,13 @@ package config | |
|
||
import ( | ||
"encoding/json" | ||
"reflect" | ||
"regexp" | ||
"strings" | ||
"testing" | ||
"time" | ||
|
||
"github.com/prometheus/common/model" | ||
"github.com/stretchr/testify/require" | ||
"gopkg.in/yaml.v2" | ||
) | ||
|
@@ -68,7 +71,7 @@ receivers: | |
func TestHideConfigSecrets(t *testing.T) { | ||
c, _, err := LoadFile("testdata/conf.good.yml") | ||
if err != nil { | ||
t.Errorf("Error parsing %s: %s", "testdata/good.yml", err) | ||
t.Errorf("Error parsing %s: %s", "testdata/conf.good.yml", err) | ||
} | ||
|
||
// String method must not reveal authentication credentials. | ||
|
@@ -83,7 +86,7 @@ func TestHideConfigSecrets(t *testing.T) { | |
func TestJSONMarshal(t *testing.T) { | ||
c, _, err := LoadFile("testdata/conf.good.yml") | ||
if err != nil { | ||
t.Errorf("Error parsing %s: %s", "testdata/good.yml", err) | ||
t.Errorf("Error parsing %s: %s", "testdata/conf.good.yml", err) | ||
} | ||
|
||
_, err = json.Marshal(c) | ||
|
@@ -112,7 +115,7 @@ func TestJSONMarshalSecret(t *testing.T) { | |
func TestJSONUnmarshalMarshaled(t *testing.T) { | ||
c, _, err := LoadFile("testdata/conf.good.yml") | ||
if err != nil { | ||
t.Errorf("Error parsing %s: %s", "testdata/good.yml", err) | ||
t.Errorf("Error parsing %s: %s", "testdata/conf.good.yml", err) | ||
} | ||
|
||
plainCfg, err := json.Marshal(c) | ||
|
@@ -126,3 +129,79 @@ func TestJSONUnmarshalMarshaled(t *testing.T) { | |
t.Fatal("JSON Unmarshaling failed:", err) | ||
} | ||
} | ||
|
||
func TestEmptyFieldsAndRegex(t *testing.T) { | ||
|
||
boolFoo := true | ||
var regexpFoo Regexp | ||
regexpFoo.Regexp, _ = regexp.Compile("^(?:^(foo1|foo2|baz)$)$") | ||
|
||
var expectedConf = Config{ | ||
|
||
Global: &GlobalConfig{ | ||
ResolveTimeout: model.Duration(5 * time.Minute), | ||
SMTPSmarthost: "localhost:25", | ||
SMTPFrom: "[email protected]", | ||
HipchatAuthToken: "mysecret", | ||
HipchatURL: "https://hipchat.foobar.org/", | ||
SlackAPIURL: "mysecret", | ||
SMTPRequireTLS: true, | ||
PagerdutyURL: "https://events.pagerduty.com/generic/2010-04-15/create_event.json", | ||
OpsGenieAPIHost: "https://api.opsgenie.com/", | ||
VictorOpsAPIURL: "https://alert.victorops.com/integrations/generic/20131114/alert/", | ||
}, | ||
|
||
Templates: []string{ | ||
"/etc/alertmanager/template/*.tmpl", | ||
}, | ||
Route: &Route{ | ||
Receiver: "team-X-mails", | ||
GroupBy: []model.LabelName{ | ||
"alertname", | ||
"cluster", | ||
"service", | ||
}, | ||
Routes: []*Route{ | ||
{ | ||
Receiver: "team-X-mails", | ||
MatchRE: map[string]Regexp{ | ||
"service": regexpFoo, | ||
}, | ||
}, | ||
}, | ||
}, | ||
Receivers: []*Receiver{ | ||
{ | ||
Name: "team-X-mails", | ||
EmailConfigs: []*EmailConfig{ | ||
{ | ||
To: "[email protected]", | ||
From: "[email protected]", | ||
Smarthost: "localhost:25", | ||
HTML: "{{ template \"email.default.html\" . }}", | ||
RequireTLS: &boolFoo, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
config, _, err := LoadFile("testdata/conf.empty-fields.yml") | ||
if err != nil { | ||
t.Errorf("Error parsing %s: %s", "testdata/conf.empty-fields.yml", err) | ||
} | ||
|
||
configGot, err := yaml.Marshal(config) | ||
if err != nil { | ||
t.Fatal("YAML Marshaling failed:", err) | ||
} | ||
|
||
configExp, err := yaml.Marshal(expectedConf) | ||
if err != nil { | ||
t.Fatalf("%s", err) | ||
} | ||
|
||
if !reflect.DeepEqual(configGot, configExp) { | ||
t.Fatalf("%s: unexpected config result: \n\n%s\n expected\n\n%s", "testdata/conf.empty-fields.yml", configGot, configExp) | ||
} | ||
} |
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,27 @@ | ||
global: | ||
smtp_smarthost: 'localhost:25' | ||
smtp_from: '[email protected]' | ||
smtp_auth_username: '' | ||
smtp_auth_password: '' | ||
hipchat_auth_token: 'mysecret' | ||
hipchat_url: 'https://hipchat.foobar.org/' | ||
slack_api_url: 'mysecret' | ||
|
||
|
||
|
||
templates: | ||
- '/etc/alertmanager/template/*.tmpl' | ||
|
||
route: | ||
group_by: ['alertname', 'cluster', 'service'] | ||
|
||
receiver: team-X-mails | ||
routes: | ||
- match_re: | ||
service: ^(foo1|foo2|baz)$ | ||
receiver: team-X-mails | ||
|
||
receivers: | ||
- name: 'team-X-mails' | ||
email_configs: | ||
- to: '[email protected]' |