1
0
Fork 0
mirror of https://github.com/kyverno/kyverno.git synced 2024-12-14 11:57:48 +00:00

support for Add Variable unit test (#9124)

Signed-off-by: Kanha gupta <kanhag4163@gmail.com>
Co-authored-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>
This commit is contained in:
kanha gupta 2023-12-19 05:44:17 +05:30 committed by GitHub
parent 6bcc1dccaa
commit cdc68a629a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -7,6 +7,7 @@ import (
urkyverno "github.com/kyverno/kyverno/api/kyverno/v1beta1"
"github.com/kyverno/kyverno/pkg/config"
"github.com/kyverno/kyverno/pkg/engine/jmespath"
"github.com/stretchr/testify/assert"
authenticationv1 "k8s.io/api/authentication/v1"
)
@ -123,3 +124,99 @@ func Test_addResourceAndUserContext(t *testing.T) {
t.Error("expected result does not match")
}
}
func TestAddVariable(t *testing.T) {
conf := config.NewDefaultConfiguration(false)
jp := jmespath.New(conf)
ctx := NewContext(jp)
tests := []struct {
name string
key string
value interface{}
wantErr bool
wantQueryErr bool
expected interface{}
}{
{
name: "Simple variable",
key: "simpleKey",
value: "simpleValue",
wantErr: false,
wantQueryErr: false,
expected: "simpleValue",
},
{
name: "Nested variable",
key: "nested.key",
value: 123,
wantErr: false,
wantQueryErr: false,
expected: 123,
},
{
name: "Invalid key format",
key: "invalid,key",
value: "someValue",
wantErr: false,
wantQueryErr: true,
expected: nil,
},
{
name: "Complex nested variable",
key: "complex.nested.key",
value: map[string]interface{}{"innerKey": "innerValue"},
wantErr: false,
wantQueryErr: false,
expected: map[string]interface{}{"innerKey": "innerValue"},
},
{
name: "Array value",
key: "arrayKey",
value: []int{1, 2, 3},
wantErr: false,
wantQueryErr: false,
expected: []int{1, 2, 3},
},
{
name: "Boolean value",
key: "boolKey",
value: true,
wantErr: false,
wantQueryErr: false,
expected: true,
},
{
name: "Empty key",
key: "",
value: "someValue",
wantErr: true,
wantQueryErr: false,
expected: nil,
},
{
name: "Nil value",
key: "nilKey",
value: nil,
wantErr: false,
wantQueryErr: false,
expected: nil,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := ctx.AddVariable(tt.key, tt.value)
if tt.wantErr {
assert.Error(t, err)
} else {
result, queryErr := ctx.Query(tt.key)
if tt.wantQueryErr {
assert.Error(t, queryErr)
} else {
assert.NoError(t, err)
assert.Equal(t, tt.expected, result)
}
}
})
}
}