prometheus/config/config_test.go

200 lines
4.7 KiB
Go
Raw Normal View History

package config
import (
"encoding/json"
"io/ioutil"
2015-05-07 01:55:03 -07:00
"reflect"
"regexp"
"strings"
"testing"
2015-05-07 01:55:03 -07:00
"time"
"gopkg.in/yaml.v2"
clientmodel "github.com/prometheus/client_golang/model"
)
var expectedConf = &Config{
GlobalConfig: GlobalConfig{
2015-05-07 01:55:03 -07:00
ScrapeInterval: Duration(15 * time.Second),
ScrapeTimeout: DefaultGlobalConfig.ScrapeTimeout,
EvaluationInterval: Duration(30 * time.Second),
Labels: clientmodel.LabelSet{
"monitor": "codelab",
"foo": "bar",
},
},
2015-05-07 01:55:03 -07:00
RuleFiles: []string{
"first.rules",
"second.rules",
"my/*.rules",
2015-05-07 01:55:03 -07:00
},
ScrapeConfigs: []*ScrapeConfig{
{
2015-05-07 01:55:03 -07:00
JobName: "prometheus",
ScrapeInterval: Duration(15 * time.Second),
ScrapeTimeout: DefaultGlobalConfig.ScrapeTimeout,
MetricsPath: DefaultScrapeConfig.MetricsPath,
Scheme: DefaultScrapeConfig.Scheme,
TargetGroups: []*TargetGroup{
{
Targets: []clientmodel.LabelSet{
{clientmodel.AddressLabel: "localhost:9090"},
{clientmodel.AddressLabel: "localhost:9191"},
},
Labels: clientmodel.LabelSet{
"my": "label",
"your": "label",
},
},
},
2015-05-13 02:28:04 -07:00
FileSDConfigs: []*FileSDConfig{
{
Names: []string{"foo/*.slow.json", "foo/*.slow.yml", "single/file.yml"},
2015-05-13 02:28:04 -07:00
RefreshInterval: Duration(10 * time.Minute),
},
{
2015-05-13 02:28:04 -07:00
Names: []string{"bar/*.yaml"},
RefreshInterval: Duration(30 * time.Second),
},
2015-05-13 02:28:04 -07:00
},
2015-05-07 01:55:03 -07:00
RelabelConfigs: []*RelabelConfig{
{
2015-05-07 01:55:03 -07:00
SourceLabels: clientmodel.LabelNames{"job", "__meta_dns_srv_name"},
TargetLabel: "job",
Separator: ";",
Regex: &Regexp{*regexp.MustCompile("(.*)some-[regex]$")},
Replacement: "foo-${1}",
Action: RelabelReplace,
},
2015-05-07 01:55:03 -07:00
},
},
{
2015-05-07 01:55:03 -07:00
JobName: "service-x",
ScrapeInterval: Duration(50 * time.Second),
ScrapeTimeout: Duration(5 * time.Second),
BasicAuth: &BasicAuth{
Username: "admin",
Password: "password",
},
2015-05-07 01:55:03 -07:00
MetricsPath: "/my_path",
Scheme: "https",
2015-05-07 01:55:03 -07:00
DNSSDConfigs: []*DNSSDConfig{
{
2015-05-07 01:55:03 -07:00
Names: []string{
"first.dns.address.domain.com",
"second.dns.address.domain.com",
},
RefreshInterval: Duration(15 * time.Second),
},
{
2015-05-07 01:55:03 -07:00
Names: []string{
"first.dns.address.domain.com",
},
RefreshInterval: Duration(30 * time.Second),
},
2015-05-07 01:55:03 -07:00
},
RelabelConfigs: []*RelabelConfig{
{
2015-05-07 01:55:03 -07:00
SourceLabels: clientmodel.LabelNames{"job"},
Regex: &Regexp{*regexp.MustCompile("(.*)some-[regex]$")},
Separator: ";",
Action: RelabelDrop,
},
2015-05-07 01:55:03 -07:00
},
},
2015-05-07 01:55:03 -07:00
},
original: "",
}
2015-05-07 01:55:03 -07:00
func TestLoadConfig(t *testing.T) {
// Parse a valid file that sets a global scrape timeout. This tests whether parsing
// an overwritten default field in the global config permanently changes the default.
if _, err := LoadFromFile("testdata/global_timeout.good.yml"); err != nil {
t.Errorf("Error parsing %s: %s", "testdata/conf.good.yml", err)
}
2015-05-07 01:55:03 -07:00
c, err := LoadFromFile("testdata/conf.good.yml")
if err != nil {
t.Errorf("Error parsing %s: %s", "testdata/conf.good.yml", err)
}
bgot, err := yaml.Marshal(c)
if err != nil {
t.Errorf("%s", err)
}
bexp, err := yaml.Marshal(expectedConf)
if err != nil {
t.Errorf("%s", err)
}
expectedConf.original = c.original
2015-05-07 01:55:03 -07:00
if !reflect.DeepEqual(c, expectedConf) {
t.Errorf("%s: unexpected config result: \n\n%s\n expected\n\n%s", "testdata/conf.good.yml", bgot, bexp)
}
}
var expectedErrors = []struct {
filename string
errMsg string
}{
{
2015-05-07 01:55:03 -07:00
filename: "jobname.bad.yml",
errMsg: `"prom^etheus" is not a valid job name`,
}, {
2015-05-07 01:55:03 -07:00
filename: "jobname_dup.bad.yml",
errMsg: `found multiple scrape configs with job name "prometheus"`,
}, {
2015-05-07 01:55:03 -07:00
filename: "labelname.bad.yml",
errMsg: `"not$allowed" is not a valid label name`,
}, {
filename: "labelname2.bad.yml",
errMsg: `"not:allowed" is not a valid label name`,
2015-04-30 12:15:18 -07:00
}, {
2015-05-07 01:55:03 -07:00
filename: "regex.bad.yml",
errMsg: "error parsing regexp",
}, {
filename: "regex_missing.bad.yml",
errMsg: "relabel configuration requires a regular expression",
}, {
filename: "rules.bad.yml",
errMsg: "invalid rule file path",
},
}
2015-05-07 01:55:03 -07:00
func TestBadConfigs(t *testing.T) {
for _, ee := range expectedErrors {
_, err := LoadFromFile("testdata/" + ee.filename)
if err == nil {
t.Errorf("Expected error parsing %s but got none", ee.filename)
continue
2015-05-07 01:55:03 -07:00
}
if !strings.Contains(err.Error(), ee.errMsg) {
t.Errorf("Expected error for %s to contain %q but got: %s", ee.filename, ee.errMsg, err)
}
}
}
func TestBadTargetGroup(t *testing.T) {
content, err := ioutil.ReadFile("testdata/tgroup.bad.json")
if err != nil {
t.Fatal(err)
}
var tg TargetGroup
err = json.Unmarshal(content, &tg)
if err == nil {
t.Errorf("Expected unmarshal error but got none.")
}
}