mirror of
https://github.com/kyverno/kyverno.git
synced 2025-03-06 07:57:07 +00:00
Signed-off-by: Vyom-Yadav <jackhammervyom@gmail.com> Signed-off-by: Vyom-Yadav <jackhammervyom@gmail.com> Co-authored-by: shuting <shuting@nirmata.com>
74 lines
3.3 KiB
Go
74 lines
3.3 KiB
Go
package match
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"gotest.tools/assert"
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
"k8s.io/apimachinery/pkg/runtime/schema"
|
|
)
|
|
|
|
func Test_CheckKind(t *testing.T) {
|
|
subresourceGVKToAPIResource := make(map[string]*metav1.APIResource)
|
|
match := CheckKind(subresourceGVKToAPIResource, []string{"*"}, schema.GroupVersionKind{Kind: "Deployment", Group: "", Version: "v1"}, "")
|
|
assert.Equal(t, match, true)
|
|
|
|
match = CheckKind(subresourceGVKToAPIResource, []string{"Pod"}, schema.GroupVersionKind{Kind: "Pod", Group: "", Version: "v1"}, "")
|
|
assert.Equal(t, match, true)
|
|
|
|
match = CheckKind(subresourceGVKToAPIResource, []string{"v1/Pod"}, schema.GroupVersionKind{Kind: "Pod", Group: "", Version: "v1"}, "")
|
|
assert.Equal(t, match, true)
|
|
|
|
match = CheckKind(subresourceGVKToAPIResource, []string{"tekton.dev/v1beta1/TaskRun"}, schema.GroupVersionKind{Kind: "TaskRun", Group: "tekton.dev", Version: "v1beta1"}, "")
|
|
assert.Equal(t, match, true)
|
|
|
|
match = CheckKind(subresourceGVKToAPIResource, []string{"tekton.dev/*/TaskRun"}, schema.GroupVersionKind{Kind: "TaskRun", Group: "tekton.dev", Version: "v1alpha1"}, "")
|
|
assert.Equal(t, match, true)
|
|
|
|
// Though both 'pods', 'pods/status' have same kind i.e. 'Pod' but they are different resources, 'subresourceInAdmnReview' is used in determining that.
|
|
match = CheckKind(subresourceGVKToAPIResource, []string{"v1/Pod"}, schema.GroupVersionKind{Kind: "Pod", Group: "", Version: "v1"}, "status")
|
|
assert.Equal(t, match, false)
|
|
|
|
// Though both 'pods', 'pods/status' have same kind i.e. 'Pod' but they are different resources, 'subresourceInAdmnReview' is used in determining that.
|
|
match = CheckKind(subresourceGVKToAPIResource, []string{"v1/Pod"}, schema.GroupVersionKind{Kind: "Pod", Group: "", Version: "v1"}, "ephemeralcontainers")
|
|
assert.Equal(t, match, false)
|
|
|
|
subresourceGVKToAPIResource["networking.k8s.io/v1/NetworkPolicy/status"] = &metav1.APIResource{
|
|
Name: "networkpolicies/status",
|
|
SingularName: "",
|
|
Namespaced: true,
|
|
Kind: "NetworkPolicy",
|
|
Group: "networking.k8s.io",
|
|
Version: "v1",
|
|
}
|
|
|
|
subresourceGVKToAPIResource["v1/Pod.status"] = &metav1.APIResource{
|
|
Name: "pods/status",
|
|
SingularName: "",
|
|
Namespaced: true,
|
|
Kind: "Pod",
|
|
Group: "",
|
|
Version: "v1",
|
|
}
|
|
|
|
subresourceGVKToAPIResource["*/Pod.eviction"] = &metav1.APIResource{
|
|
Name: "pods/eviction",
|
|
SingularName: "",
|
|
Namespaced: true,
|
|
Kind: "Eviction",
|
|
Group: "policy",
|
|
Version: "v1",
|
|
}
|
|
|
|
match = CheckKind(subresourceGVKToAPIResource, []string{"networking.k8s.io/v1/NetworkPolicy/status"}, schema.GroupVersionKind{Kind: "NetworkPolicy", Group: "networking.k8s.io", Version: "v1"}, "status")
|
|
assert.Equal(t, match, true)
|
|
|
|
match = CheckKind(subresourceGVKToAPIResource, []string{"v1/Pod.status"}, schema.GroupVersionKind{Kind: "Pod", Group: "", Version: "v1"}, "status")
|
|
assert.Equal(t, match, true)
|
|
|
|
match = CheckKind(subresourceGVKToAPIResource, []string{"*/Pod.eviction"}, schema.GroupVersionKind{Kind: "Eviction", Group: "policy", Version: "v1"}, "eviction")
|
|
assert.Equal(t, match, true)
|
|
|
|
match = CheckKind(subresourceGVKToAPIResource, []string{"v1alpha1/Pod.eviction"}, schema.GroupVersionKind{Kind: "Eviction", Group: "policy", Version: "v1"}, "eviction")
|
|
assert.Equal(t, match, false)
|
|
}
|