2018-09-16 21:39:03 +00:00
|
|
|
// Copyright 2018 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 adapter
|
|
|
|
|
|
|
|
import (
|
2019-10-29 09:41:31 +00:00
|
|
|
"context"
|
|
|
|
"github.com/prometheus/prometheus/util/testutil"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2018-09-16 21:39:03 +00:00
|
|
|
"reflect"
|
|
|
|
"testing"
|
2018-09-18 09:08:38 +00:00
|
|
|
|
|
|
|
"github.com/prometheus/common/model"
|
|
|
|
"github.com/prometheus/prometheus/discovery/targetgroup"
|
2018-09-16 21:39:03 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// TestGenerateTargetGroups checks that the target is correctly generated.
|
2018-09-26 08:48:35 +00:00
|
|
|
// It covers the case when the target is empty.
|
2018-09-16 21:39:03 +00:00
|
|
|
func TestGenerateTargetGroups(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
title string
|
|
|
|
targetGroup map[string][]*targetgroup.Group
|
|
|
|
expectedCustomSD map[string]*customSD
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
title: "Empty targetGroup",
|
|
|
|
targetGroup: map[string][]*targetgroup.Group{
|
|
|
|
"customSD": {
|
|
|
|
{
|
|
|
|
Source: "Consul",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Source: "Kubernetes",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2018-11-30 09:32:17 +00:00
|
|
|
expectedCustomSD: map[string]*customSD{
|
|
|
|
"customSD:Consul:0000000000000000": {
|
|
|
|
Targets: []string{},
|
|
|
|
Labels: map[string]string{},
|
|
|
|
},
|
|
|
|
"customSD:Kubernetes:0000000000000000": {
|
|
|
|
Targets: []string{},
|
|
|
|
Labels: map[string]string{},
|
|
|
|
},
|
|
|
|
},
|
2018-09-16 21:39:03 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
title: "targetGroup filled",
|
|
|
|
targetGroup: map[string][]*targetgroup.Group{
|
|
|
|
"customSD": {
|
|
|
|
{
|
|
|
|
Source: "Azure",
|
|
|
|
Targets: []model.LabelSet{
|
|
|
|
{
|
|
|
|
model.AddressLabel: "host1",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
model.AddressLabel: "host2",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Labels: model.LabelSet{
|
|
|
|
model.LabelName("__meta_test_label"): model.LabelValue("label_test_1"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Source: "Openshift",
|
|
|
|
Targets: []model.LabelSet{
|
|
|
|
{
|
|
|
|
model.AddressLabel: "host3",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
model.AddressLabel: "host4",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Labels: model.LabelSet{
|
|
|
|
model.LabelName("__meta_test_label"): model.LabelValue("label_test_2"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expectedCustomSD: map[string]*customSD{
|
2018-11-30 09:32:17 +00:00
|
|
|
"customSD:Azure:282a007a18fadbbb": {
|
2018-09-16 21:39:03 +00:00
|
|
|
Targets: []string{
|
|
|
|
"host1",
|
|
|
|
"host2",
|
|
|
|
},
|
|
|
|
Labels: map[string]string{
|
|
|
|
"__meta_test_label": "label_test_1",
|
|
|
|
},
|
|
|
|
},
|
2018-11-30 09:32:17 +00:00
|
|
|
"customSD:Openshift:281c007a18ea2ad0": {
|
2018-09-16 21:39:03 +00:00
|
|
|
Targets: []string{
|
|
|
|
"host3",
|
|
|
|
"host4",
|
|
|
|
},
|
|
|
|
Labels: map[string]string{
|
|
|
|
"__meta_test_label": "label_test_2",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
title: "Mixed between empty targetGroup and targetGroup filled",
|
|
|
|
targetGroup: map[string][]*targetgroup.Group{
|
|
|
|
"customSD": {
|
|
|
|
{
|
|
|
|
Source: "GCE",
|
|
|
|
Targets: []model.LabelSet{
|
|
|
|
{
|
|
|
|
model.AddressLabel: "host1",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
model.AddressLabel: "host2",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Labels: model.LabelSet{
|
|
|
|
model.LabelName("__meta_test_label"): model.LabelValue("label_test_1"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Source: "Kubernetes",
|
|
|
|
Labels: model.LabelSet{
|
|
|
|
model.LabelName("__meta_test_label"): model.LabelValue("label_test_2"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expectedCustomSD: map[string]*customSD{
|
2018-11-30 09:32:17 +00:00
|
|
|
"customSD:GCE:282a007a18fadbbb": {
|
2018-09-16 21:39:03 +00:00
|
|
|
Targets: []string{
|
|
|
|
"host1",
|
|
|
|
"host2",
|
|
|
|
},
|
|
|
|
Labels: map[string]string{
|
|
|
|
"__meta_test_label": "label_test_1",
|
|
|
|
},
|
|
|
|
},
|
2018-11-30 09:32:17 +00:00
|
|
|
"customSD:Kubernetes:282e007a18fad483": {
|
|
|
|
Targets: []string{},
|
|
|
|
Labels: map[string]string{
|
|
|
|
"__meta_test_label": "label_test_2",
|
|
|
|
},
|
|
|
|
},
|
2018-09-16 21:39:03 +00:00
|
|
|
},
|
|
|
|
},
|
2019-08-22 09:49:45 +00:00
|
|
|
{
|
|
|
|
title: "Disordered Ips in Alibaba's application management system",
|
|
|
|
targetGroup: map[string][]*targetgroup.Group{
|
|
|
|
"cart": {
|
|
|
|
{
|
|
|
|
Source: "alibaba",
|
|
|
|
Targets: []model.LabelSet{
|
|
|
|
{
|
|
|
|
model.AddressLabel: "192.168.1.55",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
model.AddressLabel: "192.168.1.44",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Labels: model.LabelSet{
|
|
|
|
model.LabelName("__meta_test_label"): model.LabelValue("label_test_1"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"buy": {
|
|
|
|
{
|
|
|
|
Source: "alibaba",
|
|
|
|
Targets: []model.LabelSet{
|
|
|
|
{
|
|
|
|
model.AddressLabel: "192.168.1.22",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
model.AddressLabel: "192.168.1.33",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Labels: model.LabelSet{
|
|
|
|
model.LabelName("__meta_test_label"): model.LabelValue("label_test_1"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expectedCustomSD: map[string]*customSD{
|
|
|
|
"buy:alibaba:21c0d97a1e27e6fe": {
|
|
|
|
Targets: []string{
|
|
|
|
"192.168.1.22",
|
|
|
|
"192.168.1.33",
|
|
|
|
},
|
|
|
|
Labels: map[string]string{
|
|
|
|
"__meta_test_label": "label_test_1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"cart:alibaba:1112e97a13b159fa": {
|
|
|
|
Targets: []string{
|
|
|
|
"192.168.1.44",
|
|
|
|
"192.168.1.55",
|
|
|
|
},
|
|
|
|
Labels: map[string]string{
|
|
|
|
"__meta_test_label": "label_test_1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2018-09-16 21:39:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, testCase := range testCases {
|
|
|
|
result := generateTargetGroups(testCase.targetGroup)
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(result, testCase.expectedCustomSD) {
|
2018-09-18 09:08:38 +00:00
|
|
|
t.Errorf("%q failed\ngot: %#v\nexpected: %v",
|
2018-09-16 21:39:03 +00:00
|
|
|
testCase.title,
|
|
|
|
result,
|
|
|
|
testCase.expectedCustomSD)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
2019-10-29 09:41:31 +00:00
|
|
|
|
|
|
|
// TestWriteOutput checks the adapter can write a file to disk.
|
|
|
|
func TestWriteOutput(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
tmpfile, err := ioutil.TempFile("", "sd_adapter_test")
|
|
|
|
testutil.Ok(t, err)
|
|
|
|
defer os.Remove(tmpfile.Name())
|
|
|
|
tmpfile.Close()
|
|
|
|
adapter := NewAdapter(ctx, tmpfile.Name(), "test_sd", nil, nil)
|
|
|
|
testutil.Ok(t, adapter.writeOutput())
|
|
|
|
}
|