Merge pull request #1926 from dmilstein/add-tests-for-targetmanager

Add basic test for TargetManager.targetSet
This commit is contained in:
Fabian Reinartz 2016-08-30 08:14:04 +02:00 committed by GitHub
commit a83c8156b5
1 changed files with 60 additions and 0 deletions

View File

@ -12,3 +12,63 @@
// 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) {
verifyPresence := func(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)
}
}
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(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(ts.tgroups, "dns/0/srv.name.one.example.org", false)
verifyPresence(ts.tgroups, "dns/0/srv.name.two.example.org", true)
}