diff --git a/pkg/engine/context/context_test.go b/pkg/engine/context/context_test.go index 3c78961bd1..ad0cceda51 100644 --- a/pkg/engine/context/context_test.go +++ b/pkg/engine/context/context_test.go @@ -126,90 +126,97 @@ func Test_addResourceAndUserContext(t *testing.T) { } 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 + query string expected interface{} - }{ - { - name: "Simple variable", - key: "simpleKey", - value: "simpleValue", - wantErr: false, - wantQueryErr: false, - expected: "simpleValue", + wantQueryErr bool + }{{ + 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, + }, { + name: "Escaped complex key", + key: `metadata.labels."com.example/my-label"`, + value: "foo", + wantErr: false, + query: "metadata", + expected: map[string]any{ + "labels": map[string]any{ + "com.example/my-label": "foo", + }, }, - { - 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, - }, - } + wantQueryErr: false, + }} for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { + conf := config.NewDefaultConfiguration(false) + jp := jmespath.New(conf) + ctx := NewContext(jp) err := ctx.AddVariable(tt.key, tt.value) if tt.wantErr { assert.Error(t, err) } else { - result, queryErr := ctx.Query(tt.key) + query := tt.query + if query == "" { + query = tt.key + } + result, queryErr := ctx.Query(query) if tt.wantQueryErr { assert.Error(t, queryErr) } else {