Skip to content

Commit

Permalink
added test checking for empty or null fields and blank regexps
Browse files Browse the repository at this point in the history
  • Loading branch information
conr committed Jun 15, 2017
1 parent 62dd545 commit f52a366
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 3 deletions.
85 changes: 82 additions & 3 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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.
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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)
}
}
27 changes: 27 additions & 0 deletions config/testdata/conf.empty-fields.yml
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]'

0 comments on commit f52a366

Please sign in to comment.