2015-01-21 11:07:45 -08:00
|
|
|
// Copyright 2013 The Prometheus Authors
|
2013-01-15 08:06:17 -08: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-04-20 03:24:25 -07:00
|
|
|
"reflect"
|
2013-06-25 05:02:27 -07:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2015-02-13 11:24:17 -08:00
|
|
|
"github.com/golang/protobuf/proto"
|
2013-06-25 05:02:27 -07:00
|
|
|
|
|
|
|
clientmodel "github.com/prometheus/client_golang/model"
|
|
|
|
|
|
|
|
"github.com/prometheus/prometheus/config"
|
2015-04-20 03:24:25 -07:00
|
|
|
pb "github.com/prometheus/prometheus/config/generated"
|
2013-01-15 08:06:17 -08:00
|
|
|
)
|
|
|
|
|
2015-04-20 03:24:25 -07:00
|
|
|
func TestTargetManagerChan(t *testing.T) {
|
|
|
|
testJob1 := pb.JobConfig{
|
|
|
|
Name: proto.String("test_job1"),
|
|
|
|
ScrapeInterval: proto.String("1m"),
|
|
|
|
TargetGroup: []*pb.TargetGroup{
|
|
|
|
{Target: []string{"example.org:80", "example.com:80"}},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
prov1 := &fakeTargetProvider{
|
|
|
|
sources: []string{"src1", "src2"},
|
|
|
|
update: make(chan *config.TargetGroup),
|
|
|
|
}
|
2013-01-15 08:06:17 -08:00
|
|
|
|
2015-04-20 03:24:25 -07:00
|
|
|
targetManager := &TargetManager{
|
|
|
|
sampleAppender: nopAppender{},
|
|
|
|
providers: map[string][]TargetProvider{
|
|
|
|
*testJob1.Name: []TargetProvider{prov1},
|
|
|
|
},
|
|
|
|
configs: map[string]config.JobConfig{
|
|
|
|
*testJob1.Name: config.JobConfig{testJob1},
|
|
|
|
},
|
|
|
|
targets: make(map[string][]Target),
|
|
|
|
}
|
|
|
|
go targetManager.Run()
|
|
|
|
defer targetManager.Stop()
|
2014-07-29 11:31:11 -07:00
|
|
|
|
2015-04-20 03:24:25 -07:00
|
|
|
sequence := []struct {
|
|
|
|
tgroup *config.TargetGroup
|
|
|
|
expected map[string][]clientmodel.LabelSet
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
tgroup: &config.TargetGroup{
|
|
|
|
Source: "src1",
|
|
|
|
Targets: []clientmodel.LabelSet{
|
|
|
|
{clientmodel.AddressLabel: "test-1:1234"},
|
|
|
|
{clientmodel.AddressLabel: "test-2:1234", "label": "set"},
|
|
|
|
{clientmodel.AddressLabel: "test-3:1234"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expected: map[string][]clientmodel.LabelSet{
|
|
|
|
"test_job1:src1": {
|
|
|
|
{clientmodel.JobLabel: "test_job1", clientmodel.InstanceLabel: "test-1:1234"},
|
|
|
|
{clientmodel.JobLabel: "test_job1", clientmodel.InstanceLabel: "test-2:1234", "label": "set"},
|
|
|
|
{clientmodel.JobLabel: "test_job1", clientmodel.InstanceLabel: "test-3:1234"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, {
|
|
|
|
tgroup: &config.TargetGroup{
|
|
|
|
Source: "src2",
|
|
|
|
Targets: []clientmodel.LabelSet{
|
|
|
|
{clientmodel.AddressLabel: "test-1:1235"},
|
|
|
|
{clientmodel.AddressLabel: "test-2:1235"},
|
|
|
|
{clientmodel.AddressLabel: "test-3:1235"},
|
|
|
|
},
|
|
|
|
Labels: clientmodel.LabelSet{"group": "label"},
|
|
|
|
},
|
|
|
|
expected: map[string][]clientmodel.LabelSet{
|
|
|
|
"test_job1:src1": {
|
|
|
|
{clientmodel.JobLabel: "test_job1", clientmodel.InstanceLabel: "test-1:1234"},
|
|
|
|
{clientmodel.JobLabel: "test_job1", clientmodel.InstanceLabel: "test-2:1234", "label": "set"},
|
|
|
|
{clientmodel.JobLabel: "test_job1", clientmodel.InstanceLabel: "test-3:1234"},
|
|
|
|
},
|
|
|
|
"test_job1:src2": {
|
|
|
|
{clientmodel.JobLabel: "test_job1", clientmodel.InstanceLabel: "test-1:1235", "group": "label"},
|
|
|
|
{clientmodel.JobLabel: "test_job1", clientmodel.InstanceLabel: "test-2:1235", "group": "label"},
|
|
|
|
{clientmodel.JobLabel: "test_job1", clientmodel.InstanceLabel: "test-3:1235", "group": "label"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, {
|
|
|
|
tgroup: &config.TargetGroup{
|
|
|
|
Source: "src2",
|
|
|
|
Targets: []clientmodel.LabelSet{},
|
|
|
|
},
|
|
|
|
expected: map[string][]clientmodel.LabelSet{
|
|
|
|
"test_job1:src1": {
|
|
|
|
{clientmodel.JobLabel: "test_job1", clientmodel.InstanceLabel: "test-1:1234"},
|
|
|
|
{clientmodel.JobLabel: "test_job1", clientmodel.InstanceLabel: "test-2:1234", "label": "set"},
|
|
|
|
{clientmodel.JobLabel: "test_job1", clientmodel.InstanceLabel: "test-3:1234"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, {
|
|
|
|
tgroup: &config.TargetGroup{
|
|
|
|
Source: "src1",
|
|
|
|
Targets: []clientmodel.LabelSet{
|
|
|
|
{clientmodel.AddressLabel: "test-1:1234", "added": "label"},
|
|
|
|
{clientmodel.AddressLabel: "test-3:1234"},
|
|
|
|
{clientmodel.AddressLabel: "test-4:1234", "fancy": "label"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expected: map[string][]clientmodel.LabelSet{
|
|
|
|
"test_job1:src1": {
|
|
|
|
{clientmodel.JobLabel: "test_job1", clientmodel.InstanceLabel: "test-1:1234", "added": "label"},
|
|
|
|
{clientmodel.JobLabel: "test_job1", clientmodel.InstanceLabel: "test-3:1234"},
|
|
|
|
{clientmodel.JobLabel: "test_job1", clientmodel.InstanceLabel: "test-4:1234", "fancy": "label"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2013-01-15 08:06:17 -08:00
|
|
|
|
2015-04-20 03:24:25 -07:00
|
|
|
for i, step := range sequence {
|
|
|
|
prov1.update <- step.tgroup
|
2013-01-15 08:06:17 -08:00
|
|
|
|
2015-04-20 03:24:25 -07:00
|
|
|
<-time.After(1 * time.Millisecond)
|
2013-01-15 08:06:17 -08:00
|
|
|
|
2015-04-20 03:24:25 -07:00
|
|
|
if len(targetManager.targets) != len(step.expected) {
|
|
|
|
t.Fatalf("step %d: sources mismatch %v, %v", targetManager.targets, step.expected)
|
|
|
|
}
|
2014-07-29 09:28:48 -07:00
|
|
|
|
2015-04-20 03:24:25 -07: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 08:06:17 -08:00
|
|
|
}
|
|
|
|
|
2015-04-20 03:24:25 -07:00
|
|
|
func TestTargetManagerConfigUpdate(t *testing.T) {
|
|
|
|
testJob1 := &pb.JobConfig{
|
|
|
|
Name: proto.String("test_job1"),
|
|
|
|
ScrapeInterval: proto.String("1m"),
|
|
|
|
TargetGroup: []*pb.TargetGroup{
|
|
|
|
{Target: []string{"example.org:80", "example.com:80"}},
|
2013-04-30 11:20:14 -07:00
|
|
|
},
|
2013-02-22 12:07:35 -08:00
|
|
|
}
|
2015-04-20 03:24:25 -07:00
|
|
|
testJob2 := &pb.JobConfig{
|
|
|
|
Name: proto.String("test_job2"),
|
|
|
|
ScrapeInterval: proto.String("1m"),
|
|
|
|
TargetGroup: []*pb.TargetGroup{
|
|
|
|
{Target: []string{"example.org:8080", "example.com:8081"}},
|
|
|
|
{Target: []string{"test.com:1234"}},
|
2013-04-30 11:20:14 -07:00
|
|
|
},
|
2013-02-22 12:07:35 -08:00
|
|
|
}
|
2013-01-15 08:06:17 -08:00
|
|
|
|
2015-04-20 03:24:25 -07:00
|
|
|
sequence := []struct {
|
|
|
|
jobConfigs []*pb.JobConfig
|
|
|
|
expected map[string][]clientmodel.LabelSet
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
jobConfigs: []*pb.JobConfig{testJob1},
|
|
|
|
expected: map[string][]clientmodel.LabelSet{
|
|
|
|
"test_job1:static:0": {
|
|
|
|
{clientmodel.JobLabel: "test_job1", clientmodel.InstanceLabel: "example.org:80"},
|
|
|
|
{clientmodel.JobLabel: "test_job1", clientmodel.InstanceLabel: "example.com:80"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, {
|
|
|
|
jobConfigs: []*pb.JobConfig{testJob1},
|
|
|
|
expected: map[string][]clientmodel.LabelSet{
|
|
|
|
"test_job1:static:0": {
|
|
|
|
{clientmodel.JobLabel: "test_job1", clientmodel.InstanceLabel: "example.org:80"},
|
|
|
|
{clientmodel.JobLabel: "test_job1", clientmodel.InstanceLabel: "example.com:80"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, {
|
|
|
|
jobConfigs: []*pb.JobConfig{testJob1, testJob2},
|
|
|
|
expected: map[string][]clientmodel.LabelSet{
|
|
|
|
"test_job1:static:0": {
|
|
|
|
{clientmodel.JobLabel: "test_job1", clientmodel.InstanceLabel: "example.org:80"},
|
|
|
|
{clientmodel.JobLabel: "test_job1", clientmodel.InstanceLabel: "example.com:80"},
|
|
|
|
},
|
|
|
|
"test_job2:static:0": {
|
|
|
|
{clientmodel.JobLabel: "test_job2", clientmodel.InstanceLabel: "example.org:8080"},
|
|
|
|
{clientmodel.JobLabel: "test_job2", clientmodel.InstanceLabel: "example.com:8081"},
|
|
|
|
},
|
|
|
|
"test_job2:static:1": {
|
|
|
|
{clientmodel.JobLabel: "test_job2", clientmodel.InstanceLabel: "test.com:1234"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, {
|
|
|
|
jobConfigs: []*pb.JobConfig{},
|
|
|
|
expected: map[string][]clientmodel.LabelSet{},
|
|
|
|
}, {
|
|
|
|
jobConfigs: []*pb.JobConfig{testJob2},
|
|
|
|
expected: map[string][]clientmodel.LabelSet{
|
|
|
|
"test_job2:static:0": {
|
|
|
|
{clientmodel.JobLabel: "test_job2", clientmodel.InstanceLabel: "example.org:8080"},
|
|
|
|
{clientmodel.JobLabel: "test_job2", clientmodel.InstanceLabel: "example.com:8081"},
|
|
|
|
},
|
|
|
|
"test_job2:static:1": {
|
|
|
|
{clientmodel.JobLabel: "test_job2", clientmodel.InstanceLabel: "test.com:1234"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2013-01-15 08:06:17 -08:00
|
|
|
}
|
|
|
|
|
2015-04-20 03:24:25 -07:00
|
|
|
targetManager, err := NewTargetManager(config.Config{}, nopAppender{})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
2013-01-15 08:06:17 -08:00
|
|
|
}
|
2015-04-20 03:24:25 -07:00
|
|
|
targetManager.Run()
|
|
|
|
defer targetManager.Stop()
|
2013-01-15 08:06:17 -08:00
|
|
|
|
2015-04-20 03:24:25 -07:00
|
|
|
for i, step := range sequence {
|
|
|
|
cfg := pb.PrometheusConfig{
|
|
|
|
Job: step.jobConfigs,
|
|
|
|
}
|
|
|
|
err := targetManager.ApplyConfig(config.Config{cfg})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-01-15 08:06:17 -08:00
|
|
|
|
2015-04-20 03:24:25 -07:00
|
|
|
<-time.After(1 * time.Millisecond)
|
|
|
|
|
|
|
|
if len(targetManager.targets) != len(step.expected) {
|
|
|
|
t.Fatalf("step %d: sources mismatch %v, %v", targetManager.targets, step.expected)
|
|
|
|
}
|
2013-01-15 08:06:17 -08:00
|
|
|
|
2015-04-20 03:24:25 -07: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 08:06:17 -08:00
|
|
|
}
|
|
|
|
}
|