This repository has been archived by the owner on Aug 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 105
Add unit test to test various special characters in tag values #1217
Closed
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -559,6 +559,108 @@ func TestExpressionParsing(t *testing.T) { | |
} | ||
} | ||
|
||
func TestSpecialCharactersInTags(t *testing.T) { | ||
type testCase struct { | ||
id schema.MKey | ||
lastUpdate int64 | ||
tags []string | ||
} | ||
|
||
ids = []schema.MKey{ | ||
test.GetMKey(0), | ||
test.GetMKey(1), | ||
test.GetMKey(2), | ||
test.GetMKey(3), | ||
test.GetMKey(4), | ||
test.GetMKey(5), | ||
test.GetMKey(6), | ||
} | ||
|
||
data := []testCase{} | ||
|
||
for i := 0; i < 6; i++ { | ||
data = append(data, testCase{ids[i], 1, []string{}}) | ||
} | ||
|
||
blackList := []rune{ | ||
';', // not allowed in values | ||
'~', // can't query values starting with ~ because =~ looks like a regular expression query | ||
} | ||
|
||
isBlacklisted := func(char rune) bool { | ||
for i := 0; i < len(blackList); i++ { | ||
if char == blackList[i] { | ||
return true | ||
} | ||
} | ||
return false | ||
} | ||
|
||
for i := 0; i < 128; i++ { | ||
firstChar := i | ||
for isBlacklisted(rune(firstChar)) { | ||
firstChar = (firstChar + 1) % 128 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. seems like There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe change to |
||
i++ | ||
if i >= 127 { | ||
break | ||
} | ||
} | ||
secondChar := (firstChar + 1) % 128 | ||
for isBlacklisted(rune(secondChar)) { | ||
secondChar = (secondChar + 1) % 128 | ||
} | ||
thirdChar := (secondChar + 1) % 128 | ||
for isBlacklisted(rune(thirdChar)) { | ||
thirdChar = (thirdChar + 1) % 128 | ||
} | ||
|
||
tagValue := string(firstChar) + string(secondChar) + string(thirdChar) | ||
data[i%6].tags = append(data[i%6].tags, fmt.Sprintf("testTag%d=%s", i, tagValue)) | ||
} | ||
|
||
data = append(data, testCase{ids[6], 1, []string{"key1=你好", "tiếng chào=value2", "key1=مرحبا", "123해답=value3"}}) | ||
|
||
tagIdx := make(TagIndex) | ||
byId := make(map[schema.MKey]*idx.Archive) | ||
|
||
for i, d := range data { | ||
byId[d.id] = &idx.Archive{} | ||
byId[d.id].Name = fmt.Sprintf("metric%d", i) | ||
byId[d.id].Tags = d.tags | ||
byId[d.id].LastUpdate = d.lastUpdate | ||
for _, tag := range d.tags { | ||
tagSplits := strings.SplitN(tag, "=", 2) | ||
tagIdx.addTagId(tagSplits[0], tagSplits[1], d.id) | ||
} | ||
tagIdx.addTagId("name", byId[d.id].Name, d.id) | ||
} | ||
|
||
queryAndCompare := func(query []string, id int) { | ||
q, _ := NewTagQuery(query, 0) | ||
expect := make(IdSet) | ||
expect[ids[id]] = struct{}{} | ||
|
||
// uncomment to get details about queries | ||
// fmt.Println(fmt.Sprintf("querying for tag value %d %d %d", int(query[0][len(query[0])-3]), int(query[0][len(query[0])-2]), int(query[0][len(query[0])-1]))) | ||
res := q.Run(tagIdx, byId) | ||
|
||
if !reflect.DeepEqual(expect, res) { | ||
t.Fatalf("Returned data does not match expected data:\nExpected: %s\nGot: %s", expect, res) | ||
} | ||
} | ||
|
||
for i := 0; i < 6; i++ { | ||
for tag := 0; tag < len(data[i].tags); tag++ { | ||
queryAndCompare([]string{data[i].tags[tag]}, i) | ||
} | ||
} | ||
|
||
queryAndCompare([]string{"key1=~.*好$"}, 6) | ||
queryAndCompare([]string{"tiếng chào!="}, 6) | ||
queryAndCompare([]string{"123해답=~val.*"}, 6) | ||
queryAndCompare([]string{"key1=مرحبا"}, 6) | ||
} | ||
|
||
func BenchmarkExpressionParsing(b *testing.B) { | ||
expressions := [][]string{ | ||
{"key=value", "key!=value"}, | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what is the purpose of setting these very low lastUpdate values?
seems it has nothing to do with what is being tested, and i would emit this from the testCase types
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
also, having all these predeclared id's complicates things needlessly. just generate them as you need them