From 2abc806c81641b71dbbead13e18d09a371e3b78a Mon Sep 17 00:00:00 2001 From: Paul Meyer <49727155+katexochen@users.noreply.github.com> Date: Tue, 9 Jan 2024 12:38:16 +0100 Subject: [PATCH] kubeapi: add unit tests Signed-off-by: Paul Meyer <49727155+katexochen@users.noreply.github.com> --- internal/kubeapi/kubeapi.go | 7 + internal/kubeapi/kubeapi_test.go | 225 +++++++++++++++++++++++++++++++ 2 files changed, 232 insertions(+) create mode 100644 internal/kubeapi/kubeapi_test.go diff --git a/internal/kubeapi/kubeapi.go b/internal/kubeapi/kubeapi.go index 2ffbb571b0..f70bcbeb8d 100644 --- a/internal/kubeapi/kubeapi.go +++ b/internal/kubeapi/kubeapi.go @@ -65,6 +65,13 @@ func UnmarshalK8SResources(data []byte) ([]any, error) { return nil, err } result = append(result, replicaSet) + case "DaemonSet": + var daemonSet appsv1.DaemonSet + err := runtime.DefaultUnstructuredConverter.FromUnstructured(obj.UnstructuredContent(), &daemonSet) + if err != nil { + return nil, err + } + result = append(result, daemonSet) } } return result, nil diff --git a/internal/kubeapi/kubeapi_test.go b/internal/kubeapi/kubeapi_test.go new file mode 100644 index 0000000000..92914f67c9 --- /dev/null +++ b/internal/kubeapi/kubeapi_test.go @@ -0,0 +1,225 @@ +package kubeapi + +import ( + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestUnmarshalK8SResource(t *testing.T) { + testCases := map[string]struct { + resources string + wantErr bool + wantTypes []any + }{ + "namespace ignored": { + wantTypes: []any{}, + resources: ` +apiVersion: v1 +kind: Namespace +metadata: + name: test +`, + }, + "pod": { + wantTypes: []any{Pod{}}, + resources: ` +apiVersion: v1 +kind: Pod +metadata: + name: test +`, + }, + "deployment, ignored service, daemonset": { + wantTypes: []any{Deployment{}, DaemonSet{}}, + resources: ` +apiVersion: apps/v1 +kind: Deployment +metadata: + name: test +--- +apiVersion: v1 +kind: Service +metadata: + name: test +--- +apiVersion: apps/v1 +kind: DaemonSet +metadata: + name: test +`, + }, + "statefulset, replicaset": { + wantTypes: []any{StatefulSet{}, ReplicaSet{}}, + resources: ` +apiVersion: apps/v1 +kind: StatefulSet +metadata: + name: test +--- +apiVersion: apps/v1 +kind: ReplicaSet +metadata: + name: test +`, + }, + } + + for name, tc := range testCases { + t.Run(name, func(t *testing.T) { + assert := assert.New(t) + require := require.New(t) + + got, err := UnmarshalK8SResources([]byte(tc.resources)) + + if tc.wantErr { + assert.Error(err) + return + } + assert.NoError(err) + + require.Len(got, len(tc.wantTypes)) + for i, g := range got { + assert.IsType(tc.wantTypes[i], g) + } + }) + } +} + +func TestSplitYAML(t *testing.T) { + testCases := map[string]struct { + resources string + wantSplit []string + wantErr bool + }{ + "empty": { + resources: "", + wantSplit: []string{}, + }, + "single resource": { + resources: `apiVersion: v1 +kind: Namespace +metadata: + name: test1 +`, + wantSplit: []string{ + `apiVersion: v1 +kind: Namespace +metadata: + name: test1 +`, + }, + }, + "single resource with doc seperator": { + resources: ` +--- +apiVersion: v1 +kind: Namespace +metadata: + name: test1 +`, + wantSplit: []string{ + `apiVersion: v1 +kind: Namespace +metadata: + name: test1 +`, + }, + }, + "2 documents": { + resources: `--- +apiVersion: v1 +kind: Namespace +metadata: + name: test1 +--- +apiVersion: v1 +kind: Namespace +metadata: + name: test2 +`, + wantSplit: []string{ + `apiVersion: v1 +kind: Namespace +metadata: + name: test1 +`, + `apiVersion: v1 +kind: Namespace +metadata: + name: test2 +`, + }, + }, + "3 documents": { + resources: `--- +apiVersion: v1 +kind: Namespace +metadata: + name: test1 +--- +apiVersion: v1 +kind: Namespace +metadata: + name: test2 +--- +apiVersion: v1 +kind: Namespace +metadata: + name: test3 +`, + wantSplit: []string{ + `apiVersion: v1 +kind: Namespace +metadata: + name: test1 +`, + `apiVersion: v1 +kind: Namespace +metadata: + name: test2 +`, + `apiVersion: v1 +kind: Namespace +metadata: + name: test3 +`, + }, + }, + "2 document invalid": { + resources: `--- +apiVersion: v1 +kind: Namespace +metadata: + name: test1 +--- +apiVersion +`, + wantErr: true, + }, + } + + for name, tc := range testCases { + t.Run(name, func(t *testing.T) { + assert := assert.New(t) + + gotSplit, err := splitYAML([]byte(tc.resources)) + + if tc.wantErr { + assert.Error(err) + return + } + assert.NoError(err) + assert.Equal(tc.wantSplit, bytesToStrings(gotSplit)) + }) + } +} + +func bytesToStrings(b [][]byte) []string { + s := make([]string, len(b)) + for i, bb := range b { + s[i] = string(bb) + } + return s +}