-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiscovery_test.go
86 lines (75 loc) · 1.88 KB
/
discovery_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package main
import (
"context"
"testing"
"github.com/prometheus/common/model"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/vshn/exporter-filterproxy/target"
)
func TestMultiTargetConfigFetcher(t *testing.T) {
fa := fakeTargetConfigFetcher{
t: t,
path: "/a",
target: "proxy.example.com",
configs: []target.StaticConfig{
{
Targets: []string{"proxy.example.com"},
Labels: map[model.LabelName]model.LabelValue{
"foo": "a1",
},
},
{
Targets: []string{"proxy.example.com"},
Labels: map[model.LabelName]model.LabelValue{
"foo": "a2",
},
},
},
}
fb := fakeTargetConfigFetcher{
t: t,
path: "/b",
target: "proxy.example.com",
configs: []target.StaticConfig{
{
Targets: []string{"proxy.example.com"},
Labels: map[model.LabelName]model.LabelValue{
"foo": "b1",
},
},
{
Targets: []string{"proxy.example.com"},
Labels: map[model.LabelName]model.LabelValue{
"foo": "b2",
},
},
},
}
mf := multiTargetConfigFetcher{
"/a": fa,
"/b": fb,
}
conf, err := mf.FetchTargetConfigs(context.TODO(), "proxy.example.com", "")
require.NoError(t, err)
require.Len(t, conf, 4)
seenFoolabels := []string{}
for _, c := range conf {
require.Len(t, c.Targets, 1)
assert.Equal(t, "proxy.example.com", c.Targets[0])
require.Len(t, c.Labels, 1)
seenFoolabels = append(seenFoolabels, string(c.Labels["foo"]))
}
assert.ElementsMatch(t, seenFoolabels, []string{"b1", "b2", "a1", "a2"})
}
type fakeTargetConfigFetcher struct {
configs []target.StaticConfig
t *testing.T
path string
target string
}
func (f fakeTargetConfigFetcher) FetchTargetConfigs(ctx context.Context, baseTarget string, basePath string) ([]target.StaticConfig, error) {
assert.Equal(f.t, f.path, basePath)
assert.Equal(f.t, f.target, baseTarget)
return f.configs, nil
}