-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathmapper_test.go
133 lines (127 loc) · 3.1 KB
/
mapper_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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
// 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 main
import "testing"
func TestMetricMapper(t *testing.T) {
scenarios := []struct {
config string
configBad bool
mappings map[string]map[string]string
}{
// Empty config.
{},
// Config with several mapping definitions.
{
config: `
some-tube-(\d*)-(\w*)
name="some-tube"
identifier="$1"
action="$2"
job="test_dispatcher"
another-tube-(\w*)-name
name="another-tube-name"
processor="$1"
job="test_dispatcher2"
`,
mappings: map[string]map[string]string{
"some-tube-773-open": map[string]string{
"name": "some-tube",
"identifier": "773",
"action": "open",
"job": "test_dispatcher",
},
"another-tube-ImageProcessor-name": map[string]string{
"name": "another-tube-name",
"processor": "ImageProcessor",
"job": "test_dispatcher2",
},
},
},
// Config with bad regex reference.
{
config: `
test.(\w*)
name="name"
label="$1_foo"
`,
mappings: map[string]map[string]string{
"test.a": map[string]string{
"name": "name",
"label": "",
},
},
},
// Config with good regex reference.
{
config: `
test.(\w*)
name="name"
label="${1}_foo"
`,
mappings: map[string]map[string]string{
"test.a": map[string]string{
"name": "name",
"label": "a_foo",
},
},
},
// Config with bad label line.
{
config: `
test.(\d*).(\d*)
name=foo
`,
configBad: true,
},
// Config with bad label line.
{
config: `
test.(\d*).(\d*)
name="foo.name"
`,
configBad: true,
},
// Config with bad tube name.
{
config: `
test.(\d*).(\d*)
name="0foo"
`,
configBad: true,
},
}
mapper := newTubeMapper()
for i, scenario := range scenarios {
err := mapper.initFromString(scenario.config)
if err != nil && !scenario.configBad {
t.Fatalf("%d. Config load error: %s", i, err)
}
if err == nil && scenario.configBad {
t.Fatalf("%d. Expected bad config, but loaded ok", i)
}
for tube, mapping := range scenario.mappings {
labels, present := mapper.getMapping(tube)
if len(labels) == 0 && present {
t.Fatalf("%d.%q: Expected tube to not be present", i, tube)
}
if len(labels) != len(mapping) {
t.Fatalf("%d.%q: Expected %d labels, got %d", i, tube, len(mapping), len(labels))
}
for label, value := range labels {
if mapping[label] != value {
t.Fatalf("%d.%q: Expected labels %v, got %v", i, tube, mapping, labels)
}
}
}
}
}