-
Notifications
You must be signed in to change notification settings - Fork 208
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
connectivity: Add annotations map helper type
This type will be used in a subsequent commit to specify the wanted annotations for a specific deployment or namespace. Unit tests are added to ensure the wildcard matching behavior. Signed-off-by: Sebastian Wicki <[email protected]>
- Loading branch information
Showing
2 changed files
with
169 additions
and
0 deletions.
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
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 |
---|---|---|
@@ -0,0 +1,71 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// Copyright Authors of Cilium | ||
|
||
package check | ||
|
||
import ( | ||
"encoding/json" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestAnnotationMap(t *testing.T) { | ||
var a annotationsMap | ||
err := a.Set(`not json`) | ||
assert.IsType(t, err, &json.SyntaxError{}) | ||
|
||
err = a.Set(`{"foo*bar":{}}`) | ||
assert.ErrorContains(t, err, "wildcard only allowed at end of key") | ||
|
||
err = a.Set(`{"baz*qux*":{}}`) | ||
assert.ErrorContains(t, err, "wildcard only allowed at end of key") | ||
|
||
err = a.Set(`{"**":{}}`) | ||
assert.ErrorContains(t, err, "wildcard only allowed at end of key") | ||
|
||
var clientAnnotations = annotations{"baz": "qux"} | ||
var echoSameNodeAnnotations = annotations{"quux": "corge"} | ||
var echoWildcardAnnotations = annotations{"grault": "grault"} | ||
var wildcardAnnotations = annotations{"waldo": "fred"} | ||
|
||
err = a.Set(`{ | ||
"client": ` + clientAnnotations.String() + `, | ||
"echo-same-node": ` + echoSameNodeAnnotations.String() + `, | ||
"echo*": ` + echoWildcardAnnotations.String() + `, | ||
"*": ` + wildcardAnnotations.String() + ` | ||
}`) | ||
assert.NoError(t, err) | ||
assert.Equal(t, annotationsMap{ | ||
"client": clientAnnotations, | ||
"echo-same-node": echoSameNodeAnnotations, | ||
"echo*": echoWildcardAnnotations, | ||
"*": wildcardAnnotations, | ||
}, a) | ||
|
||
// Test wildcard fallback | ||
assert.Equal(t, a.Match("echo*"), annotations(nil)) // wildcard not allowed here | ||
assert.Equal(t, a.Match("*"), annotations(nil)) // wildcard not allowed here | ||
|
||
assert.Equal(t, a.Match("client"), clientAnnotations) | ||
assert.Equal(t, a.Match("echo-same-node"), echoSameNodeAnnotations) | ||
assert.Equal(t, a.Match("echo-other-node"), echoWildcardAnnotations) | ||
assert.Equal(t, a.Match("other"), wildcardAnnotations) | ||
|
||
err = a.Set(`{ | ||
"echo-same-*": ` + echoSameNodeAnnotations.String() + `, | ||
"echo*": ` + echoWildcardAnnotations.String() + ` | ||
}`) | ||
assert.NoError(t, err) | ||
assert.Equal(t, annotationsMap{ | ||
"echo-same-*": echoSameNodeAnnotations, | ||
"echo*": echoWildcardAnnotations, | ||
}, a) | ||
|
||
// Tests longest prefix match | ||
assert.Equal(t, a.Match("echo-same-node"), echoSameNodeAnnotations) | ||
assert.Equal(t, a.Match("echo-other-node"), echoWildcardAnnotations) | ||
assert.Equal(t, a.Match("echo"), echoWildcardAnnotations) | ||
assert.Equal(t, a.Match("other"), annotations(nil)) | ||
|
||
} |