1
0
Fork 0
mirror of https://github.com/kyverno/kyverno.git synced 2024-12-14 11:57:48 +00:00
kyverno/pkg/engine/anchor/utils_test.go
Vishal Choudhary 48d9ebba2c
Replaces manually written logic with regex for matching anchor elements (#6133)
* uses regular expressions

Signed-off-by: Vishal Choudhary <sendtovishalchoudhary@gmail.com>

* adds regex capture

Signed-off-by: Vishal Choudhary <sendtovishalchoudhary@gmail.com>

* creates anchor instance

Signed-off-by: Vishal Choudhary <sendtovishalchoudhary@gmail.com>

* remove IsAnchor

Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>

* more

Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>

* added interface

Signed-off-by: Vishal Choudhary <sendtovishalchoudhary@gmail.com>

* remove static funcs

Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>

* adapt

Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>

* value receiver

Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>

* simplify

Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>

* fix

Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>

* error

Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>

* renames

Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>

* private

Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>

* nit

Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>

* fix

Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>

* fix

Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>

* ficx

Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>

* refactor

Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>

* test

Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>

* tests

Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>

* test

Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>

* fix

Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>

* error

Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>

* unit tests

Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>

* refactor

Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>

* fix

Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>

* fix

Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>

* unit tests

Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>

* fix

Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>

* fix

Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>

* fix

Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>

---------

Signed-off-by: Vishal Choudhary <sendtovishalchoudhary@gmail.com>
Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>
Co-authored-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>
Co-authored-by: Charles-Edouard Brétéché <charled.breteche@gmail.com>
Co-authored-by: shuting <shuting@nirmata.com>
2023-01-30 12:17:19 +00:00

126 lines
2.6 KiB
Go

package anchor
import (
"reflect"
"testing"
)
func TestRemoveAnchorsFromPath(t *testing.T) {
tests := []struct {
name string
str string
want string
}{{
str: "/path/(to)/X(anchors)",
want: "/path/to/anchors",
}, {
str: "path/(to)/X(anchors)",
want: "path/to/anchors",
}, {
str: "../(to)/X(anchors)",
want: "../to/anchors",
}, {
str: "/path/(to)/X(anchors)",
want: "/path/to/anchors",
}}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := RemoveAnchorsFromPath(tt.str); got != tt.want {
t.Errorf("RemoveAnchorsFromPath() = %v, want %v", got, tt.want)
}
})
}
}
func TestGetAnchorsResourcesFromMap(t *testing.T) {
tests := []struct {
name string
patternMap map[string]interface{}
wantAnchors map[string]interface{}
wantResources map[string]interface{}
}{{
patternMap: map[string]interface{}{
"spec": "test",
},
wantAnchors: map[string]interface{}{},
wantResources: map[string]interface{}{
"spec": "test",
},
}, {
patternMap: map[string]interface{}{
"(spec)": "test",
},
wantAnchors: map[string]interface{}{
"(spec)": "test",
},
wantResources: map[string]interface{}{},
}}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
anchors, resources := GetAnchorsResourcesFromMap(tt.patternMap)
if !reflect.DeepEqual(anchors, tt.wantAnchors) {
t.Errorf("GetAnchorsResourcesFromMap() anchors = %v, want %v", anchors, tt.wantAnchors)
}
if !reflect.DeepEqual(resources, tt.wantResources) {
t.Errorf("GetAnchorsResourcesFromMap() resources = %v, want %v", resources, tt.wantResources)
}
})
}
}
func Test_resourceHasValueForKey(t *testing.T) {
type args struct {
resource interface{}
key string
}
tests := []struct {
name string
args args
want bool
}{{
args: args{
resource: map[string]interface{}{
"spec": 123,
},
key: "spec",
},
want: true,
}, {
args: args{
resource: map[string]interface{}{
"spec": 123,
},
key: "metadata",
},
want: false,
}, {
args: args{
resource: []interface{}{1, 2, 3},
key: "spec",
},
want: false,
}, {
args: args{
resource: []interface{}{
map[string]interface{}{
"spec": 123,
},
},
key: "spec",
},
want: true,
}, {
args: args{
resource: 123,
key: "spec",
},
want: false,
}}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := resourceHasValueForKey(tt.args.resource, tt.args.key); got != tt.want {
t.Errorf("resourceHasValueForKey() = %v, want %v", got, tt.want)
}
})
}
}