Skip to content
This repository was archived by the owner on Aug 23, 2023. It is now read-only.

Commit f3283df

Browse files
committed
refactor tests
1 parent d58288b commit f3283df

File tree

1 file changed

+94
-79
lines changed

1 file changed

+94
-79
lines changed

input/input_test.go

+94-79
Original file line numberDiff line numberDiff line change
@@ -2,62 +2,93 @@ package input
22

33
import (
44
"fmt"
5-
"github.com/grafana/metrictank/idx"
65
"testing"
76
"time"
87

98
"github.com/grafana/metrictank/cluster"
109
"github.com/grafana/metrictank/conf"
10+
"github.com/grafana/metrictank/idx"
1111
"github.com/grafana/metrictank/idx/memory"
1212
"github.com/grafana/metrictank/mdata"
1313
"github.com/grafana/metrictank/mdata/cache"
1414
backendStore "github.com/grafana/metrictank/store"
1515
"github.com/raintank/schema"
1616
)
1717

18-
func TestIngestValidTagAndValueWithRejection(t *testing.T) {
19-
handler, index, reset := getDefaultHandler(t)
20-
defer reset()
21-
22-
rejectInvalidTags = false
23-
data := getTestMetricData()
24-
data.Tags = []string{"valid=tag"}
25-
testIngestMetricData(t, "valid_with_rejection_0", data, handler, index, 0, 0, 1)
26-
}
27-
28-
func TestIngestValidTagAndValueWithoutRejection(t *testing.T) {
29-
handler, index, reset := getDefaultHandler(t)
30-
defer reset()
31-
32-
rejectInvalidTags = true
33-
data := getTestMetricData()
34-
data.Tags = []string{"valid=tag"}
35-
testIngestMetricData(t, "valid_without_rejection_0", data, handler, index, 0, 0, 1)
36-
}
37-
38-
func TestInvalidTagsWithRejection(t *testing.T) {
39-
handler, index, reset := getDefaultHandler(t)
40-
defer reset()
41-
invalidTags := generateInvalidTags(t)
42-
43-
rejectInvalidTags = true
44-
data := getTestMetricData()
45-
for tc, invalidTag := range invalidTags {
46-
data.Tags = []string{fmt.Sprintf("%s=value", invalidTag)}
47-
testIngestMetricData(t, fmt.Sprintf("invalid_tags_with_rejection_%d", tc), data, handler, index, 1, 1, 0)
18+
func TestIngestValidAndInvalidTagsAndValuesWithAndWithoutRejection(t *testing.T) {
19+
type testCase struct {
20+
name string
21+
rejectInvalidTags bool
22+
tags []string
23+
expectedInvalidMdInc uint32
24+
expectedInvalidTagMdInc uint32
25+
expectedIndexSizeInc uint32
4826
}
49-
}
5027

51-
func TestIngestInvalidTagsWithoutRejection(t *testing.T) {
52-
handler, index, reset := getDefaultHandler(t)
53-
defer reset()
54-
invalidTags := generateInvalidTags(t)
28+
testCases := []testCase{
29+
{
30+
name: "valid_with_rejection",
31+
rejectInvalidTags: true,
32+
tags: []string{"valid=tag"},
33+
expectedInvalidMdInc: 0,
34+
expectedInvalidTagMdInc: 0,
35+
expectedIndexSizeInc: 1,
36+
}, {
37+
name: "valid_without_rejection",
38+
rejectInvalidTags: false,
39+
tags: []string{"valid=tag"},
40+
expectedInvalidMdInc: 0,
41+
expectedInvalidTagMdInc: 0,
42+
expectedIndexSizeInc: 1,
43+
}, {
44+
name: "invalid_tags_with_rejection",
45+
rejectInvalidTags: true,
46+
tags: generateInvalidTags(t),
47+
expectedInvalidMdInc: 1,
48+
expectedInvalidTagMdInc: 1,
49+
expectedIndexSizeInc: 0,
50+
}, {
51+
name: "invalid_tags_without_rejection",
52+
rejectInvalidTags: false,
53+
tags: generateInvalidTags(t),
54+
expectedInvalidMdInc: 1,
55+
expectedInvalidTagMdInc: 1,
56+
expectedIndexSizeInc: 1,
57+
}, {
58+
name: "invalid_tag_values_with_rejection",
59+
rejectInvalidTags: true,
60+
tags: generateInvalidTagValues(t),
61+
expectedInvalidMdInc: 1,
62+
expectedInvalidTagMdInc: 1,
63+
expectedIndexSizeInc: 0,
64+
}, {
65+
name: "invalid_tag_values_without_rejection",
66+
rejectInvalidTags: false,
67+
tags: generateInvalidTagValues(t),
68+
expectedInvalidMdInc: 1,
69+
expectedInvalidTagMdInc: 1,
70+
expectedIndexSizeInc: 1,
71+
},
72+
}
5573

56-
rejectInvalidTags = false
57-
data := getTestMetricData()
58-
for tc, invalidTag := range invalidTags {
59-
data.Tags = []string{fmt.Sprintf("%s=value", invalidTag)}
60-
testIngestMetricData(t, fmt.Sprintf("invalid_tags_without_rejection_%d", tc), data, handler, index, 1, 1, 1)
74+
for _, tc := range testCases {
75+
handler, index, reset := getDefaultHandler(t)
76+
rejectInvalidTags = tc.rejectInvalidTags
77+
for i, tag := range tc.tags {
78+
data := getTestMetricData()
79+
data.Tags = []string{tag}
80+
testIngestMetricData(
81+
t,
82+
fmt.Sprintf("%s_%d", tc.name, i),
83+
data,
84+
handler,
85+
index,
86+
tc.expectedInvalidMdInc,
87+
tc.expectedInvalidTagMdInc,
88+
tc.expectedIndexSizeInc,
89+
)
90+
}
91+
reset()
6192
}
6293
}
6394

@@ -67,33 +98,12 @@ func generateInvalidTags(t *testing.T) []string {
6798
invalidChars := ";!^"
6899
validChar := "a"
69100

70-
return generateInvalidStrings(t, invalidChars, validChar)
71-
}
72-
73-
func TestInvalidTagValuesWithRejection(t *testing.T) {
74-
handler, index, reset := getDefaultHandler(t)
75-
defer reset()
76-
invalidTagValues := generateInvalidTagValues(t)
77-
78-
rejectInvalidTags = true
79-
data := getTestMetricData()
80-
for tc, invalidTagValue := range invalidTagValues {
81-
data.Tags = []string{fmt.Sprintf("tag=%s", invalidTagValue)}
82-
testIngestMetricData(t, fmt.Sprintf("invalid_tag_values_with_rejection_%d", tc), data, handler, index, 1, 1, 0)
83-
}
84-
}
85-
86-
func TestIngestInvalidTagValuesWithoutRejection(t *testing.T) {
87-
handler, index, reset := getDefaultHandler(t)
88-
defer reset()
89-
invalidTagValues := generateInvalidTagValues(t)
90-
91-
rejectInvalidTags = false
92-
data := getTestMetricData()
93-
for tc, invalidTagValue := range invalidTagValues {
94-
data.Tags = []string{fmt.Sprintf("tag=%s", invalidTagValue)}
95-
testIngestMetricData(t, fmt.Sprintf("invalid_tag_values_without_rejection_%d", tc), data, handler, index, 1, 1, 1)
101+
tagKeys := generateInvalidStrings(t, invalidChars, validChar)
102+
res := make([]string, 0, len(tagKeys))
103+
for _, tagKey := range tagKeys {
104+
res = append(res, fmt.Sprintf("%s=value", tagKey))
96105
}
106+
return res
97107
}
98108

99109
func generateInvalidTagValues(t *testing.T) []string {
@@ -102,19 +112,12 @@ func generateInvalidTagValues(t *testing.T) []string {
102112
invalidChars := ";~"
103113
validChar := "a"
104114

105-
return generateInvalidStrings(t, invalidChars, validChar)
106-
}
107-
108-
func getTestMetricData() schema.MetricData {
109-
return schema.MetricData{
110-
Id: "1.12345678901234567890123456789012",
111-
OrgId: 1,
112-
Name: "abc",
113-
Interval: 1,
114-
Value: 2,
115-
Time: 3,
116-
Mtype: "gauge",
115+
tagValues := generateInvalidStrings(t, invalidChars, validChar)
116+
res := make([]string, 0, len(tagValues))
117+
for _, tagValue := range tagValues {
118+
res = append(res, fmt.Sprintf("tag=%s", tagValue))
117119
}
120+
return res
118121
}
119122

120123
func generateInvalidStrings(t *testing.T, invalidChars, validChar string) []string {
@@ -136,6 +139,18 @@ func generateInvalidStrings(t *testing.T, invalidChars, validChar string) []stri
136139
return res
137140
}
138141

142+
func getTestMetricData() schema.MetricData {
143+
return schema.MetricData{
144+
Id: "1.12345678901234567890123456789012",
145+
OrgId: 1,
146+
Name: "abc",
147+
Interval: 1,
148+
Value: 2,
149+
Time: 3,
150+
Mtype: "gauge",
151+
}
152+
}
153+
139154
func testIngestMetricData(t *testing.T, tc string, data schema.MetricData, handler DefaultHandler, index idx.MetricIndex, expectedInvalidMdInc, expectedInvalidTagMdInc, expectedIndexSizeInc uint32) {
140155
originalInvalidCnt := handler.invalidMD.Peek()
141156
originalInvalidTagCnt := handler.invalidTagMD.Peek()

0 commit comments

Comments
 (0)