2015-01-21 19:07:45 +00:00
|
|
|
// Copyright 2013 The Prometheus Authors
|
2013-01-15 16:06:17 +00:00
|
|
|
// 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 retrieval
|
|
|
|
|
|
|
|
import (
|
2015-07-05 19:25:45 +00:00
|
|
|
"net/url"
|
2015-04-20 10:24:25 +00:00
|
|
|
"reflect"
|
2015-05-07 08:55:03 +00:00
|
|
|
"regexp"
|
2013-06-25 12:02:27 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2015-08-20 15:18:46 +00:00
|
|
|
"github.com/prometheus/common/model"
|
2013-06-25 12:02:27 +00:00
|
|
|
|
|
|
|
"github.com/prometheus/prometheus/config"
|
2013-01-15 16:06:17 +00:00
|
|
|
)
|
|
|
|
|
2015-08-07 11:18:19 +00:00
|
|
|
func TestPrefixedTargetProvider(t *testing.T) {
|
|
|
|
targetGroups := []*config.TargetGroup{
|
|
|
|
{
|
2015-08-20 15:18:46 +00:00
|
|
|
Targets: []model.LabelSet{
|
|
|
|
{model.AddressLabel: "test-1:1234"},
|
2015-08-07 11:18:19 +00:00
|
|
|
},
|
|
|
|
}, {
|
2015-08-20 15:18:46 +00:00
|
|
|
Targets: []model.LabelSet{
|
|
|
|
{model.AddressLabel: "test-1:1235"},
|
2015-08-07 11:18:19 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
tp := &prefixedTargetProvider{
|
|
|
|
job: "job-x",
|
|
|
|
mechanism: "static",
|
|
|
|
idx: 123,
|
|
|
|
TargetProvider: NewStaticProvider(targetGroups),
|
|
|
|
}
|
|
|
|
|
|
|
|
expSources := []string{
|
|
|
|
"job-x:static:123:0",
|
|
|
|
"job-x:static:123:1",
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(tp.Sources(), expSources) {
|
|
|
|
t.Fatalf("expected sources %v, got %v", expSources, tp.Sources())
|
|
|
|
}
|
|
|
|
|
|
|
|
ch := make(chan *config.TargetGroup)
|
2015-08-10 14:44:32 +00:00
|
|
|
done := make(chan struct{})
|
|
|
|
|
|
|
|
defer close(done)
|
|
|
|
go tp.Run(ch, done)
|
2015-08-07 11:18:19 +00:00
|
|
|
|
|
|
|
expGroup1 := *targetGroups[0]
|
|
|
|
expGroup2 := *targetGroups[1]
|
|
|
|
expGroup1.Source = "job-x:static:123:0"
|
|
|
|
expGroup2.Source = "job-x:static:123:1"
|
|
|
|
|
|
|
|
// The static target provider sends on the channel once per target group.
|
|
|
|
if tg := <-ch; !reflect.DeepEqual(tg, &expGroup1) {
|
|
|
|
t.Fatalf("expected target group %v, got %v", expGroup1, tg)
|
|
|
|
}
|
|
|
|
if tg := <-ch; !reflect.DeepEqual(tg, &expGroup2) {
|
|
|
|
t.Fatalf("expected target group %v, got %v", expGroup2, tg)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-20 10:24:25 +00:00
|
|
|
func TestTargetManagerChan(t *testing.T) {
|
2015-06-04 15:03:12 +00:00
|
|
|
testJob1 := &config.ScrapeConfig{
|
2015-05-07 08:55:03 +00:00
|
|
|
JobName: "test_job1",
|
|
|
|
ScrapeInterval: config.Duration(1 * time.Minute),
|
|
|
|
TargetGroups: []*config.TargetGroup{{
|
2015-08-20 15:18:46 +00:00
|
|
|
Targets: []model.LabelSet{
|
|
|
|
{model.AddressLabel: "example.org:80"},
|
|
|
|
{model.AddressLabel: "example.com:80"},
|
2015-05-07 08:55:03 +00:00
|
|
|
},
|
2015-04-25 10:59:05 +00:00
|
|
|
}},
|
2015-06-04 15:03:12 +00:00
|
|
|
}
|
2015-04-20 10:24:25 +00:00
|
|
|
prov1 := &fakeTargetProvider{
|
|
|
|
sources: []string{"src1", "src2"},
|
|
|
|
update: make(chan *config.TargetGroup),
|
|
|
|
}
|
2013-01-15 16:06:17 +00:00
|
|
|
|
2015-04-20 10:24:25 +00:00
|
|
|
targetManager := &TargetManager{
|
|
|
|
sampleAppender: nopAppender{},
|
2015-04-25 10:59:05 +00:00
|
|
|
providers: map[*config.ScrapeConfig][]TargetProvider{
|
2015-05-24 18:48:56 +00:00
|
|
|
testJob1: {prov1},
|
2015-04-20 10:24:25 +00:00
|
|
|
},
|
2015-05-18 11:14:41 +00:00
|
|
|
targets: make(map[string][]*Target),
|
2015-04-20 10:24:25 +00:00
|
|
|
}
|
|
|
|
go targetManager.Run()
|
|
|
|
defer targetManager.Stop()
|
2014-07-29 18:31:11 +00:00
|
|
|
|
2015-04-20 10:24:25 +00:00
|
|
|
sequence := []struct {
|
|
|
|
tgroup *config.TargetGroup
|
2015-08-20 15:18:46 +00:00
|
|
|
expected map[string][]model.LabelSet
|
2015-04-20 10:24:25 +00:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
tgroup: &config.TargetGroup{
|
|
|
|
Source: "src1",
|
2015-08-20 15:18:46 +00:00
|
|
|
Targets: []model.LabelSet{
|
|
|
|
{model.AddressLabel: "test-1:1234"},
|
|
|
|
{model.AddressLabel: "test-2:1234", "label": "set"},
|
|
|
|
{model.AddressLabel: "test-3:1234"},
|
2015-04-20 10:24:25 +00:00
|
|
|
},
|
|
|
|
},
|
2015-08-20 15:18:46 +00:00
|
|
|
expected: map[string][]model.LabelSet{
|
2015-08-07 11:18:19 +00:00
|
|
|
"src1": {
|
2015-08-20 15:18:46 +00:00
|
|
|
{model.JobLabel: "test_job1", model.InstanceLabel: "test-1:1234"},
|
|
|
|
{model.JobLabel: "test_job1", model.InstanceLabel: "test-2:1234", "label": "set"},
|
|
|
|
{model.JobLabel: "test_job1", model.InstanceLabel: "test-3:1234"},
|
2015-04-20 10:24:25 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}, {
|
|
|
|
tgroup: &config.TargetGroup{
|
|
|
|
Source: "src2",
|
2015-08-20 15:18:46 +00:00
|
|
|
Targets: []model.LabelSet{
|
|
|
|
{model.AddressLabel: "test-1:1235"},
|
|
|
|
{model.AddressLabel: "test-2:1235"},
|
|
|
|
{model.AddressLabel: "test-3:1235"},
|
2015-04-20 10:24:25 +00:00
|
|
|
},
|
2015-08-20 15:18:46 +00:00
|
|
|
Labels: model.LabelSet{"group": "label"},
|
2015-04-20 10:24:25 +00:00
|
|
|
},
|
2015-08-20 15:18:46 +00:00
|
|
|
expected: map[string][]model.LabelSet{
|
2015-08-07 11:18:19 +00:00
|
|
|
"src1": {
|
2015-08-20 15:18:46 +00:00
|
|
|
{model.JobLabel: "test_job1", model.InstanceLabel: "test-1:1234"},
|
|
|
|
{model.JobLabel: "test_job1", model.InstanceLabel: "test-2:1234", "label": "set"},
|
|
|
|
{model.JobLabel: "test_job1", model.InstanceLabel: "test-3:1234"},
|
2015-04-20 10:24:25 +00:00
|
|
|
},
|
2015-08-07 11:18:19 +00:00
|
|
|
"src2": {
|
2015-08-20 15:18:46 +00:00
|
|
|
{model.JobLabel: "test_job1", model.InstanceLabel: "test-1:1235", "group": "label"},
|
|
|
|
{model.JobLabel: "test_job1", model.InstanceLabel: "test-2:1235", "group": "label"},
|
|
|
|
{model.JobLabel: "test_job1", model.InstanceLabel: "test-3:1235", "group": "label"},
|
2015-04-20 10:24:25 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}, {
|
|
|
|
tgroup: &config.TargetGroup{
|
|
|
|
Source: "src2",
|
2015-08-20 15:18:46 +00:00
|
|
|
Targets: []model.LabelSet{},
|
2015-04-20 10:24:25 +00:00
|
|
|
},
|
2015-08-20 15:18:46 +00:00
|
|
|
expected: map[string][]model.LabelSet{
|
2015-08-07 11:18:19 +00:00
|
|
|
"src1": {
|
2015-08-20 15:18:46 +00:00
|
|
|
{model.JobLabel: "test_job1", model.InstanceLabel: "test-1:1234"},
|
|
|
|
{model.JobLabel: "test_job1", model.InstanceLabel: "test-2:1234", "label": "set"},
|
|
|
|
{model.JobLabel: "test_job1", model.InstanceLabel: "test-3:1234"},
|
2015-04-20 10:24:25 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}, {
|
|
|
|
tgroup: &config.TargetGroup{
|
|
|
|
Source: "src1",
|
2015-08-20 15:18:46 +00:00
|
|
|
Targets: []model.LabelSet{
|
|
|
|
{model.AddressLabel: "test-1:1234", "added": "label"},
|
|
|
|
{model.AddressLabel: "test-3:1234"},
|
|
|
|
{model.AddressLabel: "test-4:1234", "fancy": "label"},
|
2015-04-20 10:24:25 +00:00
|
|
|
},
|
|
|
|
},
|
2015-08-20 15:18:46 +00:00
|
|
|
expected: map[string][]model.LabelSet{
|
2015-08-07 11:18:19 +00:00
|
|
|
"src1": {
|
2015-08-20 15:18:46 +00:00
|
|
|
{model.JobLabel: "test_job1", model.InstanceLabel: "test-1:1234", "added": "label"},
|
|
|
|
{model.JobLabel: "test_job1", model.InstanceLabel: "test-3:1234"},
|
|
|
|
{model.JobLabel: "test_job1", model.InstanceLabel: "test-4:1234", "fancy": "label"},
|
2015-04-20 10:24:25 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2013-01-15 16:06:17 +00:00
|
|
|
|
2015-04-20 10:24:25 +00:00
|
|
|
for i, step := range sequence {
|
|
|
|
prov1.update <- step.tgroup
|
2013-01-15 16:06:17 +00:00
|
|
|
|
2015-04-20 10:24:25 +00:00
|
|
|
<-time.After(1 * time.Millisecond)
|
2013-01-15 16:06:17 +00:00
|
|
|
|
2015-04-20 10:24:25 +00:00
|
|
|
if len(targetManager.targets) != len(step.expected) {
|
|
|
|
t.Fatalf("step %d: sources mismatch %v, %v", targetManager.targets, step.expected)
|
|
|
|
}
|
2014-07-29 16:28:48 +00:00
|
|
|
|
2015-04-20 10:24:25 +00:00
|
|
|
for source, actTargets := range targetManager.targets {
|
|
|
|
expTargets, ok := step.expected[source]
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("step %d: unexpected source %q: %v", i, source, actTargets)
|
|
|
|
}
|
|
|
|
for _, expt := range expTargets {
|
|
|
|
found := false
|
|
|
|
for _, actt := range actTargets {
|
|
|
|
if reflect.DeepEqual(expt, actt.BaseLabels()) {
|
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !found {
|
|
|
|
t.Errorf("step %d: expected target %v not found in actual targets", i, expt)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-01-15 16:06:17 +00:00
|
|
|
}
|
|
|
|
|
2015-04-20 10:24:25 +00:00
|
|
|
func TestTargetManagerConfigUpdate(t *testing.T) {
|
2015-06-04 15:03:12 +00:00
|
|
|
testJob1 := &config.ScrapeConfig{
|
2015-05-07 08:55:03 +00:00
|
|
|
JobName: "test_job1",
|
|
|
|
ScrapeInterval: config.Duration(1 * time.Minute),
|
2015-07-05 19:25:45 +00:00
|
|
|
Params: url.Values{
|
|
|
|
"testParam": []string{"paramValue", "secondValue"},
|
|
|
|
},
|
2015-05-07 08:55:03 +00:00
|
|
|
TargetGroups: []*config.TargetGroup{{
|
2015-08-20 15:18:46 +00:00
|
|
|
Targets: []model.LabelSet{
|
|
|
|
{model.AddressLabel: "example.org:80"},
|
|
|
|
{model.AddressLabel: "example.com:80"},
|
2015-05-07 08:55:03 +00:00
|
|
|
},
|
|
|
|
}},
|
2015-07-05 19:25:45 +00:00
|
|
|
RelabelConfigs: []*config.RelabelConfig{
|
|
|
|
{
|
|
|
|
// Copy out the URL parameter.
|
2015-08-20 15:18:46 +00:00
|
|
|
SourceLabels: model.LabelNames{"__param_testParam"},
|
2015-07-05 19:25:45 +00:00
|
|
|
Regex: &config.Regexp{*regexp.MustCompile("^(.*)$")},
|
|
|
|
TargetLabel: "testParam",
|
|
|
|
Replacement: "$1",
|
|
|
|
Action: config.RelabelReplace,
|
|
|
|
},
|
|
|
|
},
|
2015-06-04 15:03:12 +00:00
|
|
|
}
|
|
|
|
testJob2 := &config.ScrapeConfig{
|
2015-05-07 08:55:03 +00:00
|
|
|
JobName: "test_job2",
|
|
|
|
ScrapeInterval: config.Duration(1 * time.Minute),
|
|
|
|
TargetGroups: []*config.TargetGroup{
|
|
|
|
{
|
2015-08-20 15:18:46 +00:00
|
|
|
Targets: []model.LabelSet{
|
|
|
|
{model.AddressLabel: "example.org:8080"},
|
|
|
|
{model.AddressLabel: "example.com:8081"},
|
2015-05-07 08:55:03 +00:00
|
|
|
},
|
2015-08-20 15:18:46 +00:00
|
|
|
Labels: model.LabelSet{
|
2015-05-07 08:55:03 +00:00
|
|
|
"foo": "bar",
|
|
|
|
"boom": "box",
|
|
|
|
},
|
|
|
|
},
|
2015-04-28 22:08:58 +00:00
|
|
|
{
|
2015-08-20 15:18:46 +00:00
|
|
|
Targets: []model.LabelSet{
|
|
|
|
{model.AddressLabel: "test.com:1234"},
|
2015-05-07 08:55:03 +00:00
|
|
|
},
|
2015-04-28 22:08:58 +00:00
|
|
|
},
|
|
|
|
{
|
2015-08-20 15:18:46 +00:00
|
|
|
Targets: []model.LabelSet{
|
|
|
|
{model.AddressLabel: "test.com:1235"},
|
2015-05-07 08:55:03 +00:00
|
|
|
},
|
2015-08-20 15:18:46 +00:00
|
|
|
Labels: model.LabelSet{"instance": "fixed"},
|
2015-04-28 22:08:58 +00:00
|
|
|
},
|
|
|
|
},
|
2015-05-07 08:55:03 +00:00
|
|
|
RelabelConfigs: []*config.RelabelConfig{
|
2015-06-04 15:03:12 +00:00
|
|
|
{
|
2015-08-20 15:18:46 +00:00
|
|
|
SourceLabels: model.LabelNames{model.AddressLabel},
|
2015-05-07 08:55:03 +00:00
|
|
|
Regex: &config.Regexp{*regexp.MustCompile(`^test\.(.*?):(.*)`)},
|
|
|
|
Replacement: "foo.${1}:${2}",
|
2015-08-20 15:18:46 +00:00
|
|
|
TargetLabel: model.AddressLabel,
|
2015-05-07 08:55:03 +00:00
|
|
|
Action: config.RelabelReplace,
|
2015-06-04 15:03:12 +00:00
|
|
|
},
|
|
|
|
{
|
2015-04-28 22:08:58 +00:00
|
|
|
// Add a new label for example.* targets.
|
2015-08-20 15:18:46 +00:00
|
|
|
SourceLabels: model.LabelNames{model.AddressLabel, "boom", "foo"},
|
2015-05-07 08:55:03 +00:00
|
|
|
Regex: &config.Regexp{*regexp.MustCompile("^example.*?-b([a-z-]+)r$")},
|
|
|
|
TargetLabel: "new",
|
|
|
|
Replacement: "$1",
|
|
|
|
Separator: "-",
|
|
|
|
Action: config.RelabelReplace,
|
2015-06-04 15:03:12 +00:00
|
|
|
},
|
|
|
|
{
|
2015-04-28 22:08:58 +00:00
|
|
|
// Drop an existing label.
|
2015-08-20 15:18:46 +00:00
|
|
|
SourceLabels: model.LabelNames{"boom"},
|
2015-05-07 08:55:03 +00:00
|
|
|
Regex: &config.Regexp{*regexp.MustCompile(".*")},
|
|
|
|
TargetLabel: "boom",
|
|
|
|
Replacement: "",
|
|
|
|
Action: config.RelabelReplace,
|
2015-06-04 15:03:12 +00:00
|
|
|
},
|
2013-04-30 18:20:14 +00:00
|
|
|
},
|
2015-06-04 15:03:12 +00:00
|
|
|
}
|
2013-01-15 16:06:17 +00:00
|
|
|
|
2015-04-20 10:24:25 +00:00
|
|
|
sequence := []struct {
|
2015-05-07 08:55:03 +00:00
|
|
|
scrapeConfigs []*config.ScrapeConfig
|
2015-08-20 15:18:46 +00:00
|
|
|
expected map[string][]model.LabelSet
|
2015-04-20 10:24:25 +00:00
|
|
|
}{
|
|
|
|
{
|
2015-05-07 08:55:03 +00:00
|
|
|
scrapeConfigs: []*config.ScrapeConfig{testJob1},
|
2015-08-20 15:18:46 +00:00
|
|
|
expected: map[string][]model.LabelSet{
|
2015-08-07 11:18:19 +00:00
|
|
|
"test_job1:static:0:0": {
|
2015-08-20 15:18:46 +00:00
|
|
|
{model.JobLabel: "test_job1", model.InstanceLabel: "example.org:80", "testParam": "paramValue"},
|
|
|
|
{model.JobLabel: "test_job1", model.InstanceLabel: "example.com:80", "testParam": "paramValue"},
|
2015-04-20 10:24:25 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}, {
|
2015-05-07 08:55:03 +00:00
|
|
|
scrapeConfigs: []*config.ScrapeConfig{testJob1},
|
2015-08-20 15:18:46 +00:00
|
|
|
expected: map[string][]model.LabelSet{
|
2015-08-07 11:18:19 +00:00
|
|
|
"test_job1:static:0:0": {
|
2015-08-20 15:18:46 +00:00
|
|
|
{model.JobLabel: "test_job1", model.InstanceLabel: "example.org:80", "testParam": "paramValue"},
|
|
|
|
{model.JobLabel: "test_job1", model.InstanceLabel: "example.com:80", "testParam": "paramValue"},
|
2015-04-20 10:24:25 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}, {
|
2015-05-07 08:55:03 +00:00
|
|
|
scrapeConfigs: []*config.ScrapeConfig{testJob1, testJob2},
|
2015-08-20 15:18:46 +00:00
|
|
|
expected: map[string][]model.LabelSet{
|
2015-08-07 11:18:19 +00:00
|
|
|
"test_job1:static:0:0": {
|
2015-08-20 15:18:46 +00:00
|
|
|
{model.JobLabel: "test_job1", model.InstanceLabel: "example.org:80", "testParam": "paramValue"},
|
|
|
|
{model.JobLabel: "test_job1", model.InstanceLabel: "example.com:80", "testParam": "paramValue"},
|
2015-04-20 10:24:25 +00:00
|
|
|
},
|
2015-08-07 11:18:19 +00:00
|
|
|
"test_job2:static:0:0": {
|
2015-08-20 15:18:46 +00:00
|
|
|
{model.JobLabel: "test_job2", model.InstanceLabel: "example.org:8080", "foo": "bar", "new": "ox-ba"},
|
|
|
|
{model.JobLabel: "test_job2", model.InstanceLabel: "example.com:8081", "foo": "bar", "new": "ox-ba"},
|
2015-04-20 10:24:25 +00:00
|
|
|
},
|
2015-08-07 11:18:19 +00:00
|
|
|
"test_job2:static:0:1": {
|
2015-08-20 15:18:46 +00:00
|
|
|
{model.JobLabel: "test_job2", model.InstanceLabel: "foo.com:1234"},
|
2015-04-28 22:08:58 +00:00
|
|
|
},
|
2015-08-07 11:18:19 +00:00
|
|
|
"test_job2:static:0:2": {
|
2015-08-20 15:18:46 +00:00
|
|
|
{model.JobLabel: "test_job2", model.InstanceLabel: "fixed"},
|
2015-04-20 10:24:25 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}, {
|
2015-05-07 08:55:03 +00:00
|
|
|
scrapeConfigs: []*config.ScrapeConfig{},
|
2015-08-20 15:18:46 +00:00
|
|
|
expected: map[string][]model.LabelSet{},
|
2015-04-20 10:24:25 +00:00
|
|
|
}, {
|
2015-05-07 08:55:03 +00:00
|
|
|
scrapeConfigs: []*config.ScrapeConfig{testJob2},
|
2015-08-20 15:18:46 +00:00
|
|
|
expected: map[string][]model.LabelSet{
|
2015-08-07 11:18:19 +00:00
|
|
|
"test_job2:static:0:0": {
|
2015-08-20 15:18:46 +00:00
|
|
|
{model.JobLabel: "test_job2", model.InstanceLabel: "example.org:8080", "foo": "bar", "new": "ox-ba"},
|
|
|
|
{model.JobLabel: "test_job2", model.InstanceLabel: "example.com:8081", "foo": "bar", "new": "ox-ba"},
|
2015-04-20 10:24:25 +00:00
|
|
|
},
|
2015-08-07 11:18:19 +00:00
|
|
|
"test_job2:static:0:1": {
|
2015-08-20 15:18:46 +00:00
|
|
|
{model.JobLabel: "test_job2", model.InstanceLabel: "foo.com:1234"},
|
2015-04-28 22:08:58 +00:00
|
|
|
},
|
2015-08-07 11:18:19 +00:00
|
|
|
"test_job2:static:0:2": {
|
2015-08-20 15:18:46 +00:00
|
|
|
{model.JobLabel: "test_job2", model.InstanceLabel: "fixed"},
|
2015-04-20 10:24:25 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2013-01-15 16:06:17 +00:00
|
|
|
}
|
2015-06-04 15:03:12 +00:00
|
|
|
conf := &config.Config{}
|
|
|
|
*conf = config.DefaultConfig
|
2013-01-15 16:06:17 +00:00
|
|
|
|
2015-05-12 14:52:56 +00:00
|
|
|
targetManager := NewTargetManager(nopAppender{})
|
|
|
|
targetManager.ApplyConfig(conf)
|
|
|
|
|
2015-04-20 10:24:25 +00:00
|
|
|
targetManager.Run()
|
|
|
|
defer targetManager.Stop()
|
2013-01-15 16:06:17 +00:00
|
|
|
|
2015-04-20 10:24:25 +00:00
|
|
|
for i, step := range sequence {
|
2015-05-07 08:55:03 +00:00
|
|
|
conf.ScrapeConfigs = step.scrapeConfigs
|
2015-05-12 14:52:56 +00:00
|
|
|
targetManager.ApplyConfig(conf)
|
2013-01-15 16:06:17 +00:00
|
|
|
|
2015-08-10 14:44:32 +00:00
|
|
|
time.Sleep(50 * time.Millisecond)
|
2015-04-20 10:24:25 +00:00
|
|
|
|
|
|
|
if len(targetManager.targets) != len(step.expected) {
|
2015-08-10 14:44:32 +00:00
|
|
|
t.Fatalf("step %d: sources mismatch: expected %v, got %v", i, step.expected, targetManager.targets)
|
2015-04-20 10:24:25 +00:00
|
|
|
}
|
2013-01-15 16:06:17 +00:00
|
|
|
|
2015-04-20 10:24:25 +00:00
|
|
|
for source, actTargets := range targetManager.targets {
|
|
|
|
expTargets, ok := step.expected[source]
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("step %d: unexpected source %q: %v", i, source, actTargets)
|
|
|
|
}
|
|
|
|
for _, expt := range expTargets {
|
|
|
|
found := false
|
|
|
|
for _, actt := range actTargets {
|
|
|
|
if reflect.DeepEqual(expt, actt.BaseLabels()) {
|
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !found {
|
|
|
|
t.Errorf("step %d: expected target %v for %q not found in actual targets", i, expt, source)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-01-15 16:06:17 +00:00
|
|
|
}
|
|
|
|
}
|
2015-08-24 14:42:28 +00:00
|
|
|
|
|
|
|
func TestHandleUpdatesReturnsWhenUpdateChanIsClosed(t *testing.T) {
|
|
|
|
tm := NewTargetManager(nopAppender{})
|
|
|
|
ch := make(chan targetGroupUpdate)
|
|
|
|
close(ch)
|
|
|
|
tm.handleUpdates(ch, make(chan struct{}))
|
|
|
|
}
|