1
0
Fork 0
mirror of https://github.com/kyverno/kyverno.git synced 2024-12-14 11:57:48 +00:00

test: added test for pkg/utils/policy/marshal.go (#9583)

* test: added test for pkg/utils/policy/marshal.go

Signed-off-by: sivasathyaseeelan <dnsiva.sathyaseelan.chy21@iitbhu.ac.in>

* test: added test for pkg/utils/policy/marshal.go

Signed-off-by: sivasathyaseeelan <dnsiva.sathyaseelan.chy21@iitbhu.ac.in>

---------

Signed-off-by: sivasathyaseeelan <dnsiva.sathyaseelan.chy21@iitbhu.ac.in>
This commit is contained in:
D N Siva Sathyaseelan 2024-02-01 04:30:22 +05:30 committed by GitHub
parent 231e7a681e
commit f267d19761
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -0,0 +1,96 @@
package policy
import (
"encoding/json"
"reflect"
"testing"
kyvernov1 "github.com/kyverno/kyverno/api/kyverno/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/yaml"
)
func TestToJson(t *testing.T) {
tests := []struct {
name string
policy kyvernov1.PolicyInterface
}{
{
name: "Valid ClusterPolicy",
policy: &kyvernov1.ClusterPolicy{
ObjectMeta: metav1.ObjectMeta{Name: "test-policy"},
TypeMeta: metav1.TypeMeta{Kind: "APIv1"},
},
},
{
name: "Valid Policy",
policy: &kyvernov1.Policy{
ObjectMeta: metav1.ObjectMeta{Name: "test-namespace-policy", Namespace: "test-namespace"},
},
},
{
name: "Nil Policy",
policy: nil,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
result, err := ToJson(test.policy)
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
expectedResult, err := json.Marshal(test.policy)
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
if !reflect.DeepEqual(result, expectedResult) {
t.Errorf("Expected Json %s, got %s", expectedResult, result)
}
})
}
}
func TestToYaml(t *testing.T) {
tests := []struct {
name string
policy kyvernov1.PolicyInterface
}{
{
name: "Valid ClusterPolicy",
policy: &kyvernov1.ClusterPolicy{
ObjectMeta: metav1.ObjectMeta{Name: "test-policy"},
},
},
{
name: "Valid Policy",
policy: &kyvernov1.Policy{
ObjectMeta: metav1.ObjectMeta{Name: "test-namespace-policy", Namespace: "test-namespace"},
},
},
{
name: "Nil Policy",
policy: nil,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
result, err := ToYaml(test.policy)
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
jsonBytes, err := ToJson(test.policy)
if err != nil {
if !reflect.DeepEqual(result, nil) {
t.Errorf("Expected Json %+v, got %s", nil, result)
}
}
expectedResult, err := yaml.JSONToYAML(jsonBytes)
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
if !reflect.DeepEqual(result, expectedResult) {
t.Errorf("Expected Json %s, got %s", expectedResult, result)
}
})
}
}