forked from moira-alert/moira
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers_test.go
234 lines (224 loc) · 8.48 KB
/
helpers_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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
package plotting
import (
"fmt"
"sort"
"strconv"
"strings"
"testing"
"time"
"github.com/go-graphite/carbonapi/expr/types"
. "github.com/smartystreets/goconvey/convey"
"github.com/moira-alert/moira"
)
// TestSortByLen tests simple string array sorting by length
func TestSortByLen(t *testing.T) {
labelsUnsorted := []string{
"CategoryName.CategoryCounterName.CategoryCounterType.MetricName",
"ServiceName.HostName.CategoryName.CategoryCounterName.CategoryCounterType.MetricName",
"MetricPrefix.ServiceName.HostName.CategoryName.CategoryCounterName.CategoryCounterType.MetricName",
"CategoryCounterType.MetricName",
"MetricName",
"CategoryCounterName.CategoryCounterType.MetricName",
"HostName.CategoryName.CategoryCounterName.CategoryCounterType.MetricName",
}
labelsSorted := []string{
"MetricName",
"CategoryCounterType.MetricName",
"CategoryCounterName.CategoryCounterType.MetricName",
"CategoryName.CategoryCounterName.CategoryCounterType.MetricName",
"HostName.CategoryName.CategoryCounterName.CategoryCounterType.MetricName",
"ServiceName.HostName.CategoryName.CategoryCounterName.CategoryCounterType.MetricName",
"MetricPrefix.ServiceName.HostName.CategoryName.CategoryCounterName.CategoryCounterType.MetricName",
}
Convey("Sort initial unsorted string array", t, func() {
sort.Sort(sortedByLen(labelsUnsorted))
So(len(labelsUnsorted), ShouldEqual, len(labelsSorted))
So(labelsUnsorted, ShouldResemble, labelsSorted)
})
}
// TestSanitizeLabelName tests simple label names shortener
func TestSanitizeLabelName(t *testing.T) {
labelsCompleteForm := []string{
"MetricName",
"CategoryCounterType.MetricName",
"CategoryCounterName.CategoryCounterType.MetricName",
"CategoryName.CategoryCounterName.CategoryCounterType.MetricName",
"HostName.CategoryName.CategoryCounterName.CategoryCounterType.MetricName",
"ServiceName.HostName.CategoryName.CategoryCounterName.CategoryCounterType.MetricName",
"MetricPrefix.ServiceName.HostName.CategoryName.CategoryCounterName.CategoryCounterType.MetricName",
}
labelsShortForm := []string{
"MetricName",
"CategoryCounterType.MetricName",
"CategoryCounterName.Categor...",
"CategoryName.CategoryCounte...",
"HostName.CategoryName.Categ...",
"ServiceName.HostName.Catego...",
"MetricPrefix.ServiceName.Ho...",
}
Convey("sanitize lables names", t, func() {
maxLabelLength := 30
shortLablelsList := make([]string, 0)
for _, label := range labelsCompleteForm {
shortLabel := sanitizeLabelName(label, maxLabelLength)
shortLablelsList = append(shortLablelsList, shortLabel)
}
So(len(shortLablelsList), ShouldEqual, len(labelsShortForm))
So(shortLablelsList, ShouldResemble, labelsShortForm)
})
}
// TestPercentsOfRange is a simple test of percentsOfRange method
func TestPercentsOfRange(t *testing.T) {
Convey("Test nth percent is calculated correctly", t, func() {
for i := 0; i < 100; i++ {
expected := i
actual := percentsOfRange(float64(0), float64(100), float64(i))
So(actual, ShouldEqual, expected)
}
})
}
// TestTimeValueFormatter tests time.Time to formatted string converter
func TestTimeValueFormatter(t *testing.T) {
dateTimeFormat, separator := "15:04", ":"
timeValue := moira.Int64ToTime(int64(1527330278))
locationIncrements := map[string]int{
"Europe/Moscow": 3,
"Asia/Yekaterinburg": 5,
}
Convey("Format int64 timestamps into correct strings", t, func() {
for name, increment := range locationIncrements {
location, _ := time.LoadLocation(name)
storage := &locationStorage{location: location}
formatted := storage.formatTimeWithLocation(timeValue, dateTimeFormat)
formattedHourAndMinute := strings.Split(formatted, separator)
formattedHour, _ := strconv.Atoi(formattedHourAndMinute[0])
formattedMinute, _ := strconv.Atoi(formattedHourAndMinute[1])
fmt.Printf("%s: %s,\n%s: %s\n\n",
timeValue.Location().String(), timeValue.String(), location.String(), formatted)
So(formattedMinute, ShouldEqual, timeValue.Minute())
So(formattedHour, ShouldEqual, timeValue.Add(time.Duration(increment) * time.Hour).Hour())
}
})
}
// TestFloatToHumanizedValueFormatter tests custom value formatter based on go-humanize library
func TestFloatToHumanizedValueFormatter(t *testing.T) {
metricValues := []float64{
999,
1000,
1000000,
1000000000,
1000000000000,
}
metricValuesFormatted := []string{
"999",
"1.00 K",
"1.00 M",
"1.00 G",
"1.00 T",
}
Convey("format metric values", t, func() {
formattedValues := make([]string, 0)
for _, metricValue := range metricValues {
formattedMetricValue := floatToHumanizedValueFormatter(metricValue)
formattedValues = append(formattedValues, formattedMetricValue)
}
So(len(formattedValues), ShouldEqual, len(metricValuesFormatted))
So(formattedValues, ShouldResemble, metricValuesFormatted)
})
}
// TestGetYAxisValuesFormatter tests all metric values will be formatted respectively with resolved plot limits
func TestGetYAxisValuesFormatter(t *testing.T) {
lowLimits := plotLimits{
lowest: 0,
highest: 10,
}
mediumLimits := plotLimits{
lowest: -10,
highest: 10,
}
highLimits := plotLimits{
lowest: -1000,
highest: 1000,
}
Convey("format metric values with low limits", t, func() {
metricValues := []float64{
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
}
formattedMetricValues := []string{
"0.00", "1.00", "2.00", "3.00", "4.00", "5.00", "6.00", "7.00", "8.00", "9.00", "10.00",
}
valueFormatter, maxMarkLen := getYAxisValuesFormatter(lowLimits)
formattedValues := make([]string, 0)
for _, metricValue := range metricValues {
formattedValue := valueFormatter(metricValue)
formattedValues = append(formattedValues, formattedValue)
}
So(maxMarkLen, ShouldEqual, 5)
So(len(formattedValues), ShouldEqual, len(formattedMetricValues))
So(formattedValues, ShouldResemble, formattedMetricValues)
})
Convey("format metric values with medium limits", t, func() {
metricValues := []float64{
-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5,
}
formattedMetricValues := []string{
"-5", "-4", "-3", "-2", "-1", "0", "1", "2", "3", "4", "5",
}
valueFormatter, maxMarkLen := getYAxisValuesFormatter(mediumLimits)
formattedValues := make([]string, 0)
for _, metricValue := range metricValues {
formattedValue := valueFormatter(metricValue)
formattedValues = append(formattedValues, formattedValue)
}
So(maxMarkLen, ShouldEqual, 3)
So(len(formattedValues), ShouldEqual, len(formattedMetricValues))
So(formattedValues, ShouldResemble, formattedMetricValues)
})
Convey("format metric values with high limits", t, func() {
metricValues := []float64{
-1000, -100, 0, 100, 1000,
}
formattedMetricValues := []string{
"-1.00 K", "-100", "0", "100", "1.00 K",
}
valueFormatter, maxMarkLen := getYAxisValuesFormatter(highLimits)
formattedValues := make([]string, 0)
for _, metricValue := range metricValues {
formattedValue := valueFormatter(metricValue)
formattedValues = append(formattedValues, formattedValue)
}
So(maxMarkLen, ShouldEqual, 7)
So(len(formattedValues), ShouldEqual, len(formattedMetricValues))
So(formattedValues, ShouldResemble, formattedMetricValues)
})
}
// TestToLimitedMetricsData tests to limited metricsData returns only necessary metricsData
func TestToLimitedMetricsData(t *testing.T) {
givenSeries := []*types.MetricData{
types.MakeMetricData("metricPrefix.metricName1", []float64{1}, 1, 1),
types.MakeMetricData("metricPrefix.metricName2", []float64{2}, 2, 2),
types.MakeMetricData("metricPrefix.metricName3", []float64{3}, 3, 3),
}
Convey("Limit series by non-empty whitelist", t, func() {
Convey("MetricsData has necessary series", func() {
metricsWhiteList := []string{"metricPrefix.metricName1", "metricPrefix.metricName2"}
metricsData := toLimitedMetricsData(givenSeries, metricsWhiteList)
So(len(metricsData), ShouldEqual, len(metricsWhiteList))
So(metricsData[0].Name, ShouldEqual, metricsWhiteList[0])
So(metricsData[1].Name, ShouldEqual, metricsWhiteList[1])
})
Convey("MetricsData has no necessary series", func() {
metricsWhiteList := []string{"metricPrefix.metricName4"}
metricsData := toLimitedMetricsData(givenSeries, metricsWhiteList)
So(len(metricsData), ShouldEqual, 0)
})
})
Convey("Limit series by an empty whitelist", t, func() {
metricsWhiteList := make([]string, 0)
metricsData := toLimitedMetricsData(givenSeries, metricsWhiteList)
for metricDataInd := range metricsData {
So(metricsData[metricDataInd].Name, ShouldEqual, givenSeries[metricDataInd].Name)
}
So(len(metricsData), ShouldEqual, len(givenSeries))
})
}