-
Notifications
You must be signed in to change notification settings - Fork 342
/
exporter_test.go
170 lines (160 loc) · 3.9 KB
/
exporter_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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package main
import (
"reflect"
"testing"
"time"
"github.com/alecthomas/kingpin/v2"
"github.com/prometheus/exporter-toolkit/web/kingpinflag"
)
func TestParsePositiveDuration(t *testing.T) {
t.Parallel()
tests := []struct {
name string
testInput string
want positiveDuration
wantErr bool
}{
{
"ParsePositiveDuration returns a positiveDuration",
"15ms",
positiveDuration{15 * time.Millisecond},
false,
},
{
"ParsePositiveDuration returns error for trying to parse negative value",
"-15ms",
positiveDuration{},
true,
},
{
"ParsePositiveDuration returns error for trying to parse empty string",
"",
positiveDuration{},
true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
got, err := parsePositiveDuration(tt.testInput)
if (err != nil) != tt.wantErr {
t.Errorf("parsePositiveDuration() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("parsePositiveDuration() = %v, want %v", got, tt.want)
}
})
}
}
func TestParseUnixSocketAddress(t *testing.T) {
t.Parallel()
tests := []struct {
name string
testInput string
wantSocketPath string
wantRequestPath string
wantErr bool
}{
{
"Normal unix socket address",
"unix:/path/to/socket",
"/path/to/socket",
"",
false,
},
{
"Normal unix socket address with location",
"unix:/path/to/socket:/with/location",
"/path/to/socket",
"/with/location",
false,
},
{
"Unix socket address with trailing ",
"unix:/trailing/path:",
"/trailing/path",
"",
false,
},
{
"Unix socket address with too many colons",
"unix:/too:/many:colons:",
"",
"",
true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
socketPath, requestPath, err := parseUnixSocketAddress(tt.testInput)
if (err != nil) != tt.wantErr {
t.Errorf("parseUnixSocketAddress() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(socketPath, tt.wantSocketPath) {
t.Errorf("socket path: parseUnixSocketAddress() = %v, want %v", socketPath, tt.wantSocketPath)
}
if !reflect.DeepEqual(requestPath, tt.wantRequestPath) {
t.Errorf("request path: parseUnixSocketAddress() = %v, want %v", requestPath, tt.wantRequestPath)
}
})
}
}
func TestAddMissingEnvironmentFlags(t *testing.T) {
t.Parallel()
expectedMatches := map[string]string{
"non-matching-flag": "",
"web.missing-env": "MISSING_ENV",
"web.has-env": "HAS_ENV_ALREADY",
"web.listen-address": "LISTEN_ADDRESS",
"web.config.file": "CONFIG_FILE",
}
kingpinflag.AddFlags(kingpin.CommandLine, ":9113")
kingpin.Flag("non-matching-flag", "").String()
kingpin.Flag("web.missing-env", "").String()
kingpin.Flag("web.has-env", "").Envar("HAS_ENV_ALREADY").String()
addMissingEnvironmentFlags(kingpin.CommandLine)
// using Envar() on a flag returned from GetFlag()
// adds an additional flag, which is processed correctly
// at runtime but means that we need to check for a match
// instead of checking the envar of each matching flag name
for k, v := range expectedMatches {
matched := false
for _, f := range kingpin.CommandLine.Model().FlagGroupModel.Flags {
if f.Name == k && f.Envar == v {
matched = true
}
}
if !matched {
t.Errorf("missing %s envar for %s", v, k)
}
}
}
func TestConvertFlagToEnvar(t *testing.T) {
t.Parallel()
cases := []struct {
input string
output string
}{
{
input: "dot.separate",
output: "DOT_SEPARATE",
},
{
input: "underscore_separate",
output: "UNDERSCORE_SEPARATE",
},
{
input: "mixed_separate_options",
output: "MIXED_SEPARATE_OPTIONS",
},
}
for _, c := range cases {
res := convertFlagToEnvar(c.input)
if res != c.output {
t.Errorf("expected %s to resolve to %s but got %s", c.input, c.output, res)
}
}
}