-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtag_test.go
157 lines (139 loc) · 4.1 KB
/
tag_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
// Copyright (c) 2017 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package metrics
import (
"regexp"
"sync"
"testing"
"testing/quick"
"github.com/prometheus/common/model"
"github.com/stretchr/testify/assert"
)
func TestDigester(t *testing.T) {
const (
goroutines = 10
iterations = 100
)
expected := []byte{'f', 'o', 'o', 0, 'b', 'a', 'r'}
var wg sync.WaitGroup
for i := 0; i < goroutines; i++ {
wg.Add(1)
go func() {
defer wg.Done()
for i := 0; i < iterations; i++ {
d := newDigester()
defer d.free()
assert.Equal(t, 0, len(d.digest()), "Expected fresh digester to have no internal state.")
assert.True(t, cap(d.digest()) > 0, "Expected fresh digester to have available capacity.")
d.add("", "foo")
d.add("", "bar")
assert.Equal(
t,
string(expected),
string(d.digest()),
"Expected digest to be null-separated concatenation of inputs.",
)
}
}()
}
wg.Wait()
}
func TestIsValidName(t *testing.T) {
// Regexp is a slower, but more easily verifiable, description of the Tally
// name specification.
tallyRe := regexp.MustCompile(`^[0-9A-z_\-]+$`)
isValid := func(s string) bool {
prom := model.IsValidMetricName(model.LabelValue(s)) && model.LabelName(s).IsValid()
tally := tallyRe.MatchString(s)
return prom && tally
}
assert.NoError(t, quick.CheckEqual(
isValid,
IsValidName,
nil, /* config */
), "Hand-rolled validation doesn't match Tally regexp && stock Prometheus validators.")
}
func TestIsValidTagValue(t *testing.T) {
tallyRe := regexp.MustCompile(`^[0-9A-z_.\-]+$`)
isValid := func(s string) bool {
prom := model.LabelValue(s).IsValid()
tally := tallyRe.MatchString(s)
return prom && tally
}
assert.NoError(t, quick.CheckEqual(
isValid,
IsValidName,
nil, /* config */
), "Hand-rolled validation doesn't match Tally regexp && stock Prometheus validators.")
}
func TestScrubTagValue(t *testing.T) {
tests := []struct {
in string
out string
}{
{"foo", "foo"},
{"", DefaultTagValue},
{"foo!", "foo_"},
{"!foo", "_foo"},
{"fOo1.!FoO", "fOo1._FoO"},
}
for _, tt := range tests {
assert.Equal(t, tt.out, scrubTagValue(tt.in), "Unexpected result from scrubTagValue.")
}
}
func TestScrubName(t *testing.T) {
tests := []struct {
in string
out string
}{
{"", DefaultTagValue},
{"foo", "foo"},
{"foo!", "foo_"},
{"!foo", "_foo"},
{"fOo1_.!FoO", "fOo1___FoO"},
{"FOo1.", "FOo1_"},
{"_Oo1.", "_Oo1_"},
}
for _, tt := range tests {
assert.Equal(t, tt.out, scrubName(tt.in), "Unexpected result from scrubName.")
}
}
func TestScrubQuick(t *testing.T) {
// Generative tests to ensure that scrubbing always produces valid names and
// values.
t.Run("scrubName", func(t *testing.T) {
quick.Check(func(s string) bool {
scrubbed := scrubName(s)
if IsValidName(s) {
return s == scrubbed
}
return IsValidName(scrubbed)
}, nil)
})
t.Run("scrubTagValue", func(t *testing.T) {
quick.Check(func(s string) bool {
scrubbed := scrubTagValue(s)
if IsValidTagValue(s) {
return s == scrubbed
}
return IsValidTagValue(scrubbed)
}, nil)
})
}