Browse Source

Merge pull request #6054 from nevill/refactor/manager_test

Refactor on discovery/manager_test.go
pull/6113/head
Björn Rabenstein 5 years ago committed by GitHub
parent
commit
706f3b2d17
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 251
      discovery/manager_test.go

251
discovery/manager_test.go

@ -25,11 +25,13 @@ import (
"time" "time"
"github.com/go-kit/kit/log" "github.com/go-kit/kit/log"
"github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus/testutil"
dto "github.com/prometheus/client_model/go" common_config "github.com/prometheus/common/config"
"github.com/prometheus/common/model" "github.com/prometheus/common/model"
"github.com/prometheus/prometheus/config" "github.com/prometheus/prometheus/config"
sd_config "github.com/prometheus/prometheus/discovery/config" sd_config "github.com/prometheus/prometheus/discovery/config"
"github.com/prometheus/prometheus/discovery/consul"
"github.com/prometheus/prometheus/discovery/file"
"github.com/prometheus/prometheus/discovery/targetgroup" "github.com/prometheus/prometheus/discovery/targetgroup"
"gopkg.in/yaml.v2" "gopkg.in/yaml.v2"
) )
@ -749,27 +751,33 @@ func verifyPresence(t *testing.T, tSets map[poolKey]map[string]*targetgroup.Grou
} }
func TestTargetSetRecreatesTargetGroupsEveryRun(t *testing.T) { func TestTargetSetRecreatesTargetGroupsEveryRun(t *testing.T) {
cfg := &config.Config{}
sOne := `
scrape_configs:
- job_name: 'prometheus'
static_configs:
- targets: ["foo:9090"]
- targets: ["bar:9090"]
`
if err := yaml.UnmarshalStrict([]byte(sOne), cfg); err != nil {
t.Fatalf("Unable to load YAML config sOne: %s", err)
}
ctx, cancel := context.WithCancel(context.Background()) ctx, cancel := context.WithCancel(context.Background())
defer cancel() defer cancel()
discoveryManager := NewManager(ctx, log.NewNopLogger()) discoveryManager := NewManager(ctx, log.NewNopLogger())
discoveryManager.updatert = 100 * time.Millisecond discoveryManager.updatert = 100 * time.Millisecond
go discoveryManager.Run() go discoveryManager.Run()
c := make(map[string]sd_config.ServiceDiscoveryConfig) c := map[string]sd_config.ServiceDiscoveryConfig{
for _, v := range cfg.ScrapeConfigs { "prometheus": sd_config.ServiceDiscoveryConfig{
c[v.JobName] = v.ServiceDiscoveryConfig StaticConfigs: []*targetgroup.Group{
&targetgroup.Group{
Source: "0",
Targets: []model.LabelSet{
model.LabelSet{
model.AddressLabel: model.LabelValue("foo:9090"),
},
},
},
&targetgroup.Group{
Source: "1",
Targets: []model.LabelSet{
model.LabelSet{
model.AddressLabel: model.LabelValue("bar:9090"),
},
},
},
},
},
} }
discoveryManager.ApplyConfig(c) discoveryManager.ApplyConfig(c)
@ -777,18 +785,17 @@ scrape_configs:
verifyPresence(t, discoveryManager.targets, poolKey{setName: "prometheus", provider: "string/0"}, "{__address__=\"foo:9090\"}", true) verifyPresence(t, discoveryManager.targets, poolKey{setName: "prometheus", provider: "string/0"}, "{__address__=\"foo:9090\"}", true)
verifyPresence(t, discoveryManager.targets, poolKey{setName: "prometheus", provider: "string/0"}, "{__address__=\"bar:9090\"}", true) verifyPresence(t, discoveryManager.targets, poolKey{setName: "prometheus", provider: "string/0"}, "{__address__=\"bar:9090\"}", true)
sTwo := ` c["prometheus"] = sd_config.ServiceDiscoveryConfig{
scrape_configs: StaticConfigs: []*targetgroup.Group{
- job_name: 'prometheus' &targetgroup.Group{
static_configs: Source: "0",
- targets: ["foo:9090"] Targets: []model.LabelSet{
` model.LabelSet{
if err := yaml.UnmarshalStrict([]byte(sTwo), cfg); err != nil { model.AddressLabel: model.LabelValue("foo:9090"),
t.Fatalf("Unable to load YAML config sTwo: %s", err) },
} },
c = make(map[string]sd_config.ServiceDiscoveryConfig) },
for _, v := range cfg.ScrapeConfigs { },
c[v.JobName] = v.ServiceDiscoveryConfig
} }
discoveryManager.ApplyConfig(c) discoveryManager.ApplyConfig(c)
@ -801,43 +808,33 @@ scrape_configs:
// removing all targets from the static_configs sends an update with empty targetGroups. // removing all targets from the static_configs sends an update with empty targetGroups.
// This is required to signal the receiver that this target set has no current targets. // This is required to signal the receiver that this target set has no current targets.
func TestTargetSetRecreatesEmptyStaticConfigs(t *testing.T) { func TestTargetSetRecreatesEmptyStaticConfigs(t *testing.T) {
cfg := &config.Config{}
sOne := `
scrape_configs:
- job_name: 'prometheus'
static_configs:
- targets: ["foo:9090"]
`
if err := yaml.UnmarshalStrict([]byte(sOne), cfg); err != nil {
t.Fatalf("Unable to load YAML config sOne: %s", err)
}
ctx, cancel := context.WithCancel(context.Background()) ctx, cancel := context.WithCancel(context.Background())
defer cancel() defer cancel()
discoveryManager := NewManager(ctx, log.NewNopLogger()) discoveryManager := NewManager(ctx, log.NewNopLogger())
discoveryManager.updatert = 100 * time.Millisecond discoveryManager.updatert = 100 * time.Millisecond
go discoveryManager.Run() go discoveryManager.Run()
c := make(map[string]sd_config.ServiceDiscoveryConfig) c := map[string]sd_config.ServiceDiscoveryConfig{
for _, v := range cfg.ScrapeConfigs { "prometheus": sd_config.ServiceDiscoveryConfig{
c[v.JobName] = v.ServiceDiscoveryConfig StaticConfigs: []*targetgroup.Group{
&targetgroup.Group{
Source: "0",
Targets: []model.LabelSet{
model.LabelSet{
model.AddressLabel: model.LabelValue("foo:9090"),
},
},
},
},
},
} }
discoveryManager.ApplyConfig(c) discoveryManager.ApplyConfig(c)
<-discoveryManager.SyncCh() <-discoveryManager.SyncCh()
verifyPresence(t, discoveryManager.targets, poolKey{setName: "prometheus", provider: "string/0"}, "{__address__=\"foo:9090\"}", true) verifyPresence(t, discoveryManager.targets, poolKey{setName: "prometheus", provider: "string/0"}, "{__address__=\"foo:9090\"}", true)
sTwo := ` c["prometheus"] = sd_config.ServiceDiscoveryConfig{
scrape_configs: StaticConfigs: []*targetgroup.Group{},
- job_name: 'prometheus'
static_configs:
`
if err := yaml.UnmarshalStrict([]byte(sTwo), cfg); err != nil {
t.Fatalf("Unable to load YAML config sTwo: %s", err)
}
c = make(map[string]sd_config.ServiceDiscoveryConfig)
for _, v := range cfg.ScrapeConfigs {
c[v.JobName] = v.ServiceDiscoveryConfig
} }
discoveryManager.ApplyConfig(c) discoveryManager.ApplyConfig(c)
@ -876,30 +873,33 @@ func TestIdenticalConfigurationsAreCoalesced(t *testing.T) {
} }
defer os.Remove(tmpFile2) defer os.Remove(tmpFile2)
cfg := &config.Config{}
sOne := `
scrape_configs:
- job_name: 'prometheus'
file_sd_configs:
- files: ["%s"]
- job_name: 'prometheus2'
file_sd_configs:
- files: ["%s"]
`
sOne = fmt.Sprintf(sOne, tmpFile2, tmpFile2)
if err := yaml.UnmarshalStrict([]byte(sOne), cfg); err != nil {
t.Fatalf("Unable to load YAML config sOne: %s", err)
}
ctx, cancel := context.WithCancel(context.Background()) ctx, cancel := context.WithCancel(context.Background())
defer cancel() defer cancel()
discoveryManager := NewManager(ctx, nil) discoveryManager := NewManager(ctx, nil)
discoveryManager.updatert = 100 * time.Millisecond discoveryManager.updatert = 100 * time.Millisecond
go discoveryManager.Run() go discoveryManager.Run()
c := make(map[string]sd_config.ServiceDiscoveryConfig) c := map[string]sd_config.ServiceDiscoveryConfig{
for _, v := range cfg.ScrapeConfigs { "prometheus": sd_config.ServiceDiscoveryConfig{
c[v.JobName] = v.ServiceDiscoveryConfig FileSDConfigs: []*file.SDConfig{
&file.SDConfig{
Files: []string{
tmpFile2,
},
RefreshInterval: file.DefaultSDConfig.RefreshInterval,
},
},
},
"prometheus2": sd_config.ServiceDiscoveryConfig{
FileSDConfigs: []*file.SDConfig{
&file.SDConfig{
Files: []string{
tmpFile2,
},
RefreshInterval: file.DefaultSDConfig.RefreshInterval,
},
},
},
} }
discoveryManager.ApplyConfig(c) discoveryManager.ApplyConfig(c)
@ -924,7 +924,6 @@ scrape_configs:
if err := yaml.UnmarshalStrict([]byte(cfgText), originalConfig); err != nil { if err := yaml.UnmarshalStrict([]byte(cfgText), originalConfig); err != nil {
t.Fatalf("Unable to load YAML config cfgYaml: %s", err) t.Fatalf("Unable to load YAML config cfgYaml: %s", err)
} }
origScrpCfg := originalConfig.ScrapeConfigs[0]
processedConfig := &config.Config{} processedConfig := &config.Config{}
if err := yaml.UnmarshalStrict([]byte(cfgText), processedConfig); err != nil { if err := yaml.UnmarshalStrict([]byte(cfgText), processedConfig); err != nil {
@ -936,100 +935,76 @@ scrape_configs:
discoveryManager.updatert = 100 * time.Millisecond discoveryManager.updatert = 100 * time.Millisecond
go discoveryManager.Run() go discoveryManager.Run()
c := make(map[string]sd_config.ServiceDiscoveryConfig) c := map[string]sd_config.ServiceDiscoveryConfig{
for _, v := range processedConfig.ScrapeConfigs { "prometheus": processedConfig.ScrapeConfigs[0].ServiceDiscoveryConfig,
c[v.JobName] = v.ServiceDiscoveryConfig
} }
discoveryManager.ApplyConfig(c) discoveryManager.ApplyConfig(c)
<-discoveryManager.SyncCh() <-discoveryManager.SyncCh()
origSdcfg := originalConfig.ScrapeConfigs[0].ServiceDiscoveryConfig
for _, sdcfg := range c { for _, sdcfg := range c {
if !reflect.DeepEqual(origScrpCfg.ServiceDiscoveryConfig.StaticConfigs, sdcfg.StaticConfigs) { if !reflect.DeepEqual(origSdcfg.StaticConfigs, sdcfg.StaticConfigs) {
t.Fatalf("discovery manager modified static config \n expected: %v\n got: %v\n", t.Fatalf("discovery manager modified static config \n expected: %v\n got: %v\n",
origScrpCfg.ServiceDiscoveryConfig.StaticConfigs, sdcfg.StaticConfigs) origSdcfg.StaticConfigs, sdcfg.StaticConfigs)
} }
} }
} }
func TestGaugeFailedConfigs(t *testing.T) { func TestGaugeFailedConfigs(t *testing.T) {
var (
fcGauge prometheus.Gauge
err error
)
cfgOneText := `
scrape_configs:
- job_name: prometheus
consul_sd_configs:
- server: "foo:8500"
tls_config:
cert_file: "/tmp/non_existent"
- server: "bar:8500"
tls_config:
cert_file: "/tmp/non_existent"
- server: "foo2:8500"
tls_config:
cert_file: "/tmp/non_existent"
`
cfgOne := &config.Config{}
err = yaml.UnmarshalStrict([]byte(cfgOneText), cfgOne)
if err != nil {
t.Fatalf("Unable to load YAML config cfgOne: %s", err)
}
ctx, cancel := context.WithCancel(context.Background()) ctx, cancel := context.WithCancel(context.Background())
defer cancel() defer cancel()
discoveryManager := NewManager(ctx, log.NewNopLogger()) discoveryManager := NewManager(ctx, log.NewNopLogger())
discoveryManager.updatert = 100 * time.Millisecond discoveryManager.updatert = 100 * time.Millisecond
go discoveryManager.Run() go discoveryManager.Run()
c := make(map[string]sd_config.ServiceDiscoveryConfig) c := map[string]sd_config.ServiceDiscoveryConfig{
for _, v := range cfgOne.ScrapeConfigs { "prometheus": sd_config.ServiceDiscoveryConfig{
c[v.JobName] = v.ServiceDiscoveryConfig ConsulSDConfigs: []*consul.SDConfig{
&consul.SDConfig{
Server: "foo:8500",
TLSConfig: common_config.TLSConfig{
CertFile: "/tmp/non_existent",
},
},
&consul.SDConfig{
Server: "bar:8500",
TLSConfig: common_config.TLSConfig{
CertFile: "/tmp/non_existent",
},
},
&consul.SDConfig{
Server: "foo2:8500",
TLSConfig: common_config.TLSConfig{
CertFile: "/tmp/non_existent",
},
},
},
},
} }
discoveryManager.ApplyConfig(c) discoveryManager.ApplyConfig(c)
<-discoveryManager.SyncCh() <-discoveryManager.SyncCh()
metricOne := &dto.Metric{} failedCount := testutil.ToFloat64(failedConfigs)
fcGauge, err = failedConfigs.GetMetricWithLabelValues(discoveryManager.name)
if err != nil {
t.Fatal(err)
}
fcGauge.Write(metricOne)
failedCount := metricOne.GetGauge().GetValue()
if failedCount != 3 { if failedCount != 3 {
t.Fatalf("Expected to have 3 failed configs, got: %v", failedCount) t.Fatalf("Expected to have 3 failed configs, got: %v", failedCount)
} }
cfgTwoText := ` c["prometheus"] = sd_config.ServiceDiscoveryConfig{
scrape_configs: StaticConfigs: []*targetgroup.Group{
- job_name: 'prometheus' &targetgroup.Group{
static_configs: Source: "0",
- targets: ["foo:9090"] Targets: []model.LabelSet{
` model.LabelSet{
cfgTwo := &config.Config{} model.AddressLabel: "foo:9090",
if err := yaml.UnmarshalStrict([]byte(cfgTwoText), cfgTwo); err != nil { },
t.Fatalf("Unable to load YAML config cfgTwo: %s", err) },
} },
c = make(map[string]sd_config.ServiceDiscoveryConfig) },
for _, v := range cfgTwo.ScrapeConfigs {
c[v.JobName] = v.ServiceDiscoveryConfig
} }
discoveryManager.ApplyConfig(c) discoveryManager.ApplyConfig(c)
<-discoveryManager.SyncCh() <-discoveryManager.SyncCh()
metricTwo := &dto.Metric{} failedCount = testutil.ToFloat64(failedConfigs)
fcGauge, err = failedConfigs.GetMetricWithLabelValues(discoveryManager.name)
if err != nil {
t.Fatal(err)
}
fcGauge.Write(metricTwo)
failedCount = metricTwo.GetGauge().GetValue()
if failedCount != 0 { if failedCount != 0 {
t.Fatalf("Expected to get no failed config, got: %v", failedCount) t.Fatalf("Expected to get no failed config, got: %v", failedCount)
} }

Loading…
Cancel
Save