mirror of
https://github.com/kyverno/kyverno.git
synced 2025-03-10 01:46:55 +00:00
* refactor: move util func in sub packages 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> * Update pkg/utils/kube/crd.go Signed-off-by: shuting <shutting06@gmail.com> Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com> Signed-off-by: shuting <shutting06@gmail.com> Co-authored-by: shuting <shutting06@gmail.com>
79 lines
3 KiB
Go
79 lines
3 KiB
Go
package utils
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"gotest.tools/assert"
|
|
)
|
|
|
|
func Test_ConvertResource(t *testing.T) {
|
|
testCases := []struct {
|
|
name string
|
|
raw string
|
|
group, version, kind string
|
|
namespace string
|
|
expectedNamespace string
|
|
}{
|
|
{
|
|
name: "test-namespaced-resource-secret-with-namespace",
|
|
raw: `{"apiVersion": "v1","data": {"password": "YXNkO2xma2o4OTJsIC1uCg=="},"kind": "Secret","metadata": {"name": "my-secret","namespace": "test"},"type": "Opaque"}`,
|
|
group: "",
|
|
version: "v1",
|
|
kind: "Secret",
|
|
namespace: "mynamespace",
|
|
expectedNamespace: "mynamespace",
|
|
},
|
|
{
|
|
name: "test-namespaced-resource-secret-without-namespace",
|
|
raw: `{"apiVersion": "v1","data": {"password": "YXNkO2xma2o4OTJsIC1uCg=="},"kind": "Secret","metadata": {"name": "my-secret"},"type": "Opaque"}`,
|
|
group: "",
|
|
version: "v1",
|
|
kind: "Secret",
|
|
namespace: "mynamespace",
|
|
expectedNamespace: "mynamespace",
|
|
},
|
|
{
|
|
name: "test-cluster-resource-namespace-with-namespace",
|
|
raw: `{"apiVersion": "v1","kind": "Namespace","metadata": {"name": "my-namespace","namespace": "oldnamespace"},"type": "Opaque"}`,
|
|
group: "",
|
|
version: "v1",
|
|
kind: "Namespace",
|
|
namespace: "newnamespace",
|
|
expectedNamespace: "",
|
|
},
|
|
{
|
|
name: "test-cluster-resource-namespace-without-namespace",
|
|
raw: `{"apiVersion": "v1","kind": "Namespace","metadata": {"name": "my-namespace"},"type": "Opaque"}`,
|
|
group: "",
|
|
version: "v1",
|
|
kind: "Namespace",
|
|
namespace: "newnamespace",
|
|
expectedNamespace: "",
|
|
},
|
|
{
|
|
name: "test-cluster-resource-cluster-role-with-namespace",
|
|
raw: `{"apiVersion": "rbac.authorization.k8s.io/v1","kind": "ClusterRole","metadata": {"name": "my-cluster-role","namespace":"test"},"rules": [{"apiGroups": ["*"],"resources": ["namespaces"],"verbs": ["watch"]}]}`,
|
|
group: "rbac.authorization.k8s.io",
|
|
version: "v1",
|
|
kind: "ClusterRole",
|
|
namespace: "",
|
|
expectedNamespace: "",
|
|
},
|
|
{
|
|
name: "test-cluster-resource-cluster-role-without-namespace",
|
|
raw: `{"apiVersion": "rbac.authorization.k8s.io/v1","kind": "ClusterRole","metadata": {"name": "my-cluster-role"},"rules": [{"apiGroups": ["*"],"resources": ["namespaces"],"verbs": ["watch"]}]}`,
|
|
group: "rbac.authorization.k8s.io",
|
|
version: "v1",
|
|
kind: "ClusterRole",
|
|
namespace: "",
|
|
expectedNamespace: "",
|
|
},
|
|
}
|
|
|
|
for _, test := range testCases {
|
|
resource, err := ConvertResource([]byte(test.raw), test.group, test.version, test.kind, test.namespace)
|
|
assert.NilError(t, err)
|
|
assert.Assert(t, resource.GetNamespace() == test.expectedNamespace)
|
|
break
|
|
}
|
|
}
|