prometheus/retrieval/targetmanager_test.go
Dan Milstein 79216011cb Add basic test for TargetManager.targetSet
Verify that if the configs change, target groups are cleaned on
TargetManager.reload (rather than having old ones linger around, even if
they are no longer present in the configs).

This covers the bug fixed in #1907 -- I verified that by checking out
source from before that commit.

This is a start on #1906
2016-08-26 14:30:26 -04:00

75 lines
2 KiB
Go

// Copyright 2013 The Prometheus Authors
// 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 (
"testing"
"golang.org/x/net/context"
"gopkg.in/yaml.v2"
"github.com/prometheus/prometheus/config"
"github.com/prometheus/prometheus/storage/local"
)
func TestTargetSetRecreatesTargetGroupsEveryRun(t *testing.T) {
scrapeConfig := &config.ScrapeConfig{}
sOne := `
job_name: "foo"
dns_sd_configs:
- names:
- "srv.name.one.example.org"
`
if err := yaml.Unmarshal([]byte(sOne), scrapeConfig); err != nil {
t.Fatalf("Unable to load YAML config sOne: %s", err)
}
// Not properly setting it up, but that seems okay
mss := &local.MemorySeriesStorage{}
ts := newTargetSet(scrapeConfig, mss)
ts.runProviders(context.Background(), providersFromConfig(scrapeConfig))
verifyPresence(t, ts.tgroups, "dns/0/srv.name.one.example.org", true)
sTwo := `
job_name: "foo"
dns_sd_configs:
- names:
- "srv.name.two.example.org"
`
if err := yaml.Unmarshal([]byte(sTwo), scrapeConfig); err != nil {
t.Fatalf("Unable to load YAML config sTwo: %s", err)
}
ts.runProviders(context.Background(), providersFromConfig(scrapeConfig))
verifyPresence(t, ts.tgroups, "dns/0/srv.name.one.example.org", false)
verifyPresence(t, ts.tgroups, "dns/0/srv.name.two.example.org", true)
}
func verifyPresence(t *testing.T, tgroups map[string][]*Target, name string, present bool) {
if _, ok := tgroups[name]; ok != present {
msg := ""
if !present {
msg = "not "
}
t.Fatalf("'%s' should %sbe present in TargetSet.tgroups: %s", name, msg, tgroups)
}
}