Skip to content

Commit

Permalink
kubernetes/unstructured: add more unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
iawia002 committed Sep 8, 2023
1 parent 6316719 commit 44ea4b8
Showing 1 changed file with 93 additions and 0 deletions.
93 changes: 93 additions & 0 deletions kubernetes/unstructured/unstructured_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,101 @@ package unstructured

import (
"testing"

corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func TestConvertToUnstructured(t *testing.T) {
tests := []struct {
name string
obj interface{}
isErr bool
}{
{
name: "obj test",
obj: &corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: "test",
},
},
isErr: false,
},
{
name: "list test",
obj: &corev1.NamespaceList{
Items: []corev1.Namespace{
{
ObjectMeta: metav1.ObjectMeta{
Name: "test",
},
},
},
},
isErr: false,
},
{
name: "error test",
obj: []byte(`
apiVersion: v1
kind: Namespace
metadata:
name: test
`),
isErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if _, err := ConvertToUnstructured(tt.obj); tt.isErr != (err != nil) {
t.Errorf("%s ConvertToUnstructured() unexpected error: %v", tt.name, err)
}
})
}
}

func TestConvertToTyped(t *testing.T) {
tests := []struct {
name string
obj interface{}
typedObj interface{}
isErr bool
}{
{
name: "obj test",
obj: &corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: "test",
},
},
typedObj: &corev1.Namespace{},
isErr: false,
},
{
name: "list test",
obj: &corev1.NamespaceList{
Items: []corev1.Namespace{
{
ObjectMeta: metav1.ObjectMeta{
Name: "test",
},
},
},
},
typedObj: &corev1.NamespaceList{},
isErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
unstructuredObj, _ := ConvertToUnstructured(tt.obj)
if err := ConvertToTyped(unstructuredObj, tt.typedObj); tt.isErr != (err != nil) {
t.Errorf("%s ConvertToTyped() unexpected error: %v", tt.name, err)
}
})
}
}

func TestYAMLToUnstructured(t *testing.T) {
tests := []struct {
name string
Expand Down

0 comments on commit 44ea4b8

Please sign in to comment.