mirror of
https://github.com/kyverno/kyverno.git
synced 2025-03-06 16:06:56 +00:00
* initial commit * variable substitution * update tests * update test * refactor engine packages for validate & generate * update vendor * update toml * support variable substitution in overlay mutation * missing update * fix indentation in logs * store context values as single JSON document using merge patches. * remove duplicate functions * fix message string * Handle processing of policies in background (#569) * remove condition check while generating mutation patch as conditions are verified in the first iteration * initial commit * background policy validation * correct message * skip non-background policy process for add/update * fix order to correct policy registration * update comment Co-authored-by: shuting <shutting06@gmail.com> * refactor Co-authored-by: shuting <shutting06@gmail.com>
278 lines
5.2 KiB
Go
278 lines
5.2 KiB
Go
package variables
|
|
|
|
import (
|
|
"encoding/json"
|
|
"reflect"
|
|
"testing"
|
|
|
|
"github.com/nirmata/kyverno/pkg/engine/context"
|
|
)
|
|
|
|
func Test_variableSubstitutionValue(t *testing.T) {
|
|
|
|
resourceRaw := []byte(`
|
|
{
|
|
"metadata": {
|
|
"name": "temp",
|
|
"namespace": "n1"
|
|
},
|
|
"spec": {
|
|
"namespace": "n1",
|
|
"name": "temp1"
|
|
}
|
|
}
|
|
`)
|
|
patternMap := []byte(`
|
|
{
|
|
"spec": {
|
|
"name": "{{request.object.metadata.name}}"
|
|
}
|
|
}
|
|
`)
|
|
|
|
resultMap := []byte(`{"spec":{"name":"temp"}}`)
|
|
|
|
var pattern, resource interface{}
|
|
json.Unmarshal(patternMap, &pattern)
|
|
json.Unmarshal(resourceRaw, &resource)
|
|
|
|
// context
|
|
ctx := context.NewContext()
|
|
ctx.AddResource(resourceRaw)
|
|
value := SubstituteVariables(ctx, pattern)
|
|
resultRaw, err := json.Marshal(value)
|
|
if err != nil {
|
|
t.Error(err)
|
|
}
|
|
if !reflect.DeepEqual(resultMap, resultRaw) {
|
|
t.Error("result does not match")
|
|
}
|
|
}
|
|
|
|
func Test_variableSubstitutionValueOperatorNotEqual(t *testing.T) {
|
|
|
|
resourceRaw := []byte(`
|
|
{
|
|
"metadata": {
|
|
"name": "temp",
|
|
"namespace": "n1"
|
|
},
|
|
"spec": {
|
|
"namespace": "n1",
|
|
"name": "temp1"
|
|
}
|
|
}
|
|
`)
|
|
patternMap := []byte(`
|
|
{
|
|
"spec": {
|
|
"name": "!{{request.object.metadata.name}}"
|
|
}
|
|
}
|
|
`)
|
|
resultMap := []byte(`{"spec":{"name":"!temp"}}`)
|
|
|
|
var pattern, resource interface{}
|
|
json.Unmarshal(patternMap, &pattern)
|
|
json.Unmarshal(resourceRaw, &resource)
|
|
|
|
// context
|
|
ctx := context.NewContext()
|
|
ctx.AddResource(resourceRaw)
|
|
value := SubstituteVariables(ctx, pattern)
|
|
resultRaw, err := json.Marshal(value)
|
|
if err != nil {
|
|
t.Error(err)
|
|
}
|
|
t.Log(string(resultRaw))
|
|
t.Log(string(resultMap))
|
|
if !reflect.DeepEqual(resultMap, resultRaw) {
|
|
t.Error("result does not match")
|
|
}
|
|
}
|
|
|
|
func Test_variableSubstitutionValueFail(t *testing.T) {
|
|
|
|
resourceRaw := []byte(`
|
|
{
|
|
"metadata": {
|
|
"name": "temp",
|
|
"namespace": "n1"
|
|
},
|
|
"spec": {
|
|
"namespace": "n1",
|
|
"name": "temp1"
|
|
}
|
|
}
|
|
`)
|
|
patternMap := []byte(`
|
|
{
|
|
"spec": {
|
|
"name": "{{request.object.metadata.name1}}"
|
|
}
|
|
}
|
|
`)
|
|
resultMap := []byte(`{"spec":{"name":null}}`)
|
|
|
|
var pattern, resource interface{}
|
|
json.Unmarshal(patternMap, &pattern)
|
|
json.Unmarshal(resourceRaw, &resource)
|
|
|
|
// context
|
|
ctx := context.NewContext()
|
|
ctx.AddResource(resourceRaw)
|
|
value := SubstituteVariables(ctx, pattern)
|
|
resultRaw, err := json.Marshal(value)
|
|
if err != nil {
|
|
t.Error(err)
|
|
}
|
|
t.Log(string(resultRaw))
|
|
t.Log(string(resultMap))
|
|
if !reflect.DeepEqual(resultMap, resultRaw) {
|
|
t.Error("result does not match")
|
|
}
|
|
}
|
|
|
|
func Test_variableSubstitutionObject(t *testing.T) {
|
|
resourceRaw := []byte(`
|
|
{
|
|
"metadata": {
|
|
"name": "temp",
|
|
"namespace": "n1"
|
|
},
|
|
"spec": {
|
|
"namespace": "n1",
|
|
"variable": {
|
|
"var1": "temp1",
|
|
"var2": "temp2",
|
|
"varNested": {
|
|
"var1": "temp1"
|
|
}
|
|
}
|
|
}
|
|
}
|
|
`)
|
|
patternMap := []byte(`
|
|
{
|
|
"spec": {
|
|
"variable": "{{request.object.spec.variable}}"
|
|
}
|
|
}
|
|
`)
|
|
resultMap := []byte(`{"spec":{"variable":{"var1":"temp1","var2":"temp2","varNested":{"var1":"temp1"}}}}`)
|
|
|
|
var pattern, resource interface{}
|
|
json.Unmarshal(patternMap, &pattern)
|
|
json.Unmarshal(resourceRaw, &resource)
|
|
|
|
// context
|
|
ctx := context.NewContext()
|
|
ctx.AddResource(resourceRaw)
|
|
value := SubstituteVariables(ctx, pattern)
|
|
resultRaw, err := json.Marshal(value)
|
|
if err != nil {
|
|
t.Error(err)
|
|
}
|
|
t.Log(string(resultRaw))
|
|
t.Log(string(resultMap))
|
|
if !reflect.DeepEqual(resultMap, resultRaw) {
|
|
t.Error("result does not match")
|
|
}
|
|
}
|
|
|
|
func Test_variableSubstitutionObjectOperatorNotEqualFail(t *testing.T) {
|
|
resourceRaw := []byte(`
|
|
{
|
|
"metadata": {
|
|
"name": "temp",
|
|
"namespace": "n1"
|
|
},
|
|
"spec": {
|
|
"namespace": "n1",
|
|
"variable": {
|
|
"var1": "temp1",
|
|
"var2": "temp2",
|
|
"varNested": {
|
|
"var1": "temp1"
|
|
}
|
|
}
|
|
}
|
|
}
|
|
`)
|
|
patternMap := []byte(`
|
|
{
|
|
"spec": {
|
|
"variable": "!{{request.object.spec.variable}}"
|
|
}
|
|
}
|
|
`)
|
|
|
|
resultMap := []byte(`{"spec":{"variable":null}}`)
|
|
|
|
var pattern, resource interface{}
|
|
json.Unmarshal(patternMap, &pattern)
|
|
json.Unmarshal(resourceRaw, &resource)
|
|
|
|
// context
|
|
ctx := context.NewContext()
|
|
ctx.AddResource(resourceRaw)
|
|
value := SubstituteVariables(ctx, pattern)
|
|
resultRaw, err := json.Marshal(value)
|
|
if err != nil {
|
|
t.Error(err)
|
|
}
|
|
t.Log(string(resultRaw))
|
|
t.Log(string(resultMap))
|
|
if !reflect.DeepEqual(resultMap, resultRaw) {
|
|
t.Error("result does not match")
|
|
}
|
|
}
|
|
|
|
func Test_variableSubstitutionMultipleObject(t *testing.T) {
|
|
resourceRaw := []byte(`
|
|
{
|
|
"metadata": {
|
|
"name": "temp",
|
|
"namespace": "n1"
|
|
},
|
|
"spec": {
|
|
"namespace": "n1",
|
|
"variable": {
|
|
"var1": "temp1",
|
|
"var2": "temp2",
|
|
"varNested": {
|
|
"var1": "temp1"
|
|
}
|
|
}
|
|
}
|
|
}
|
|
`)
|
|
patternMap := []byte(`
|
|
{
|
|
"spec": {
|
|
"var": "{{request.object.spec.variable.varNested.var1}}",
|
|
"variable": "{{request.object.spec.variable}}"
|
|
}
|
|
}
|
|
`)
|
|
|
|
resultMap := []byte(`{"spec":{"var":"temp1","variable":{"var1":"temp1","var2":"temp2","varNested":{"var1":"temp1"}}}}`)
|
|
|
|
var pattern, resource interface{}
|
|
json.Unmarshal(patternMap, &pattern)
|
|
json.Unmarshal(resourceRaw, &resource)
|
|
|
|
// context
|
|
ctx := context.NewContext()
|
|
ctx.AddResource(resourceRaw)
|
|
value := SubstituteVariables(ctx, pattern)
|
|
resultRaw, err := json.Marshal(value)
|
|
if err != nil {
|
|
t.Error(err)
|
|
}
|
|
t.Log(string(resultRaw))
|
|
t.Log(string(resultMap))
|
|
if !reflect.DeepEqual(resultMap, resultRaw) {
|
|
t.Error("result does not match")
|
|
}
|
|
}
|