Skip to content

Commit c8a2154

Browse files
committed
Add tests for object deserialization
Signed-off-by: Angel Misevski <[email protected]>
1 parent ddd3124 commit c8a2154

File tree

7 files changed

+197
-0
lines changed

7 files changed

+197
-0
lines changed
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
// Copyright (c) 2019-2022 Red Hat, Inc.
2+
// Licensed under the Apache License, Version 2.0 (the "License");
3+
// you may not use this file except in compliance with the License.
4+
// You may obtain a copy of the License at
5+
//
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
//
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
14+
package kubernetes
15+
16+
import (
17+
"fmt"
18+
"os"
19+
"testing"
20+
21+
dw "github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2"
22+
"github.com/devfile/devworkspace-operator/apis/controller/v1alpha1"
23+
"github.com/devfile/devworkspace-operator/pkg/provision/sync"
24+
"github.com/google/go-cmp/cmp"
25+
"github.com/stretchr/testify/assert"
26+
corev1 "k8s.io/api/core/v1"
27+
"k8s.io/apimachinery/pkg/runtime"
28+
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
29+
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
30+
"sigs.k8s.io/controller-runtime/pkg/client"
31+
"sigs.k8s.io/yaml"
32+
)
33+
34+
var (
35+
scheme = runtime.NewScheme()
36+
api = sync.ClusterAPI{
37+
Scheme: scheme,
38+
}
39+
)
40+
41+
func init() {
42+
utilruntime.Must(clientgoscheme.AddToScheme(scheme))
43+
utilruntime.Must(v1alpha1.AddToScheme(scheme))
44+
utilruntime.Must(dw.AddToScheme(scheme))
45+
}
46+
47+
func TestDeserializeObject(t *testing.T) {
48+
InitializeDeserializer(scheme)
49+
defer func() {
50+
decoder = nil
51+
}()
52+
tests := []struct {
53+
name string
54+
filePath string
55+
expectedObjStub client.Object
56+
expectedErrRegexp string
57+
}{
58+
{
59+
name: "Deserializes Pod",
60+
filePath: "testdata/k8s_objects/pod.yaml",
61+
expectedObjStub: &corev1.Pod{},
62+
},
63+
{
64+
name: "Deserializes Service",
65+
filePath: "testdata/k8s_objects/service.yaml",
66+
expectedObjStub: &corev1.Service{},
67+
},
68+
{
69+
name: "Deserializes ConfigMap",
70+
filePath: "testdata/k8s_objects/configmap.yaml",
71+
expectedObjStub: &corev1.ConfigMap{},
72+
},
73+
{
74+
name: "Kubernetes list",
75+
filePath: "testdata/k8s_objects/kubernetes-list.yaml",
76+
expectedErrRegexp: "objects of kind 'List' are unsupported",
77+
},
78+
{
79+
name: "Random yaml that is not a Kubernetes object",
80+
filePath: "testdata/k8s_objects/non-k8s-object.yaml",
81+
expectedErrRegexp: "Object 'Kind' is missing",
82+
},
83+
{
84+
name: "Object with unrecognized kind",
85+
filePath: "testdata/k8s_objects/unrecognized-kind.yaml",
86+
expectedErrRegexp: "no kind .* is registered for version .* in scheme",
87+
},
88+
}
89+
for _, tt := range tests {
90+
t.Run(fmt.Sprintf("%s (%s)", tt.name, tt.filePath), func(t *testing.T) {
91+
jsonBytes := readBytesFromFile(t, tt.filePath)
92+
actualObj, err := deserializeToObject(jsonBytes, api)
93+
if tt.expectedErrRegexp != "" {
94+
if !assert.Error(t, err, "Expect error to be returned") {
95+
return
96+
}
97+
assert.Regexp(t, tt.expectedErrRegexp, err.Error(), "Expect error to match pattern")
98+
} else {
99+
if !assert.NoError(t, err, "Expect no error to be returned") {
100+
return
101+
}
102+
err := yaml.Unmarshal(jsonBytes, tt.expectedObjStub)
103+
assert.NoError(t, err)
104+
assert.True(t, cmp.Equal(tt.expectedObjStub, actualObj), cmp.Diff(tt.expectedObjStub, actualObj))
105+
}
106+
})
107+
}
108+
}
109+
110+
func TestErrorIfDeserializerNotInitialized(t *testing.T) {
111+
_, err := deserializeToObject([]byte(""), api)
112+
assert.Error(t, err)
113+
assert.Equal(t, "kubernetes object deserializer is not initialized", err.Error())
114+
}
115+
116+
func readBytesFromFile(t *testing.T, filePath string) []byte {
117+
bytes, err := os.ReadFile(filePath)
118+
if err != nil {
119+
t.Fatal(err)
120+
}
121+
return bytes
122+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
apiVersion: v1
2+
kind: ConfigMap
3+
metadata:
4+
name: test-configmap
5+
data:
6+
key: value
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
apiVersion: v1
2+
kind: List
3+
metadata:
4+
resourceVersion: ""
5+
items:
6+
- apiVersion: v1
7+
kind: Pod
8+
metadata:
9+
name: test-pod
10+
labels:
11+
testLabel: testPod
12+
spec:
13+
containers:
14+
- name: test-container
15+
image: test-image
16+
resources:
17+
limits:
18+
memory: "128Mi"
19+
cpu: "500m"
20+
ports:
21+
- containerPort: 8080
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
random: ymal
2+
that: does
3+
not:
4+
satisfy:
5+
- typeMeta
6+
- objectMeta
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
apiVersion: v1
2+
kind: Pod
3+
metadata:
4+
name: test-pod
5+
labels:
6+
testLabel: testPod
7+
spec:
8+
containers:
9+
- name: test-container
10+
image: test-image
11+
resources:
12+
limits:
13+
memory: "128Mi"
14+
cpu: "500m"
15+
ports:
16+
- containerPort: 8080
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
apiVersion: v1
2+
kind: Service
3+
metadata:
4+
name: test-service
5+
spec:
6+
selector:
7+
test: test-app
8+
ports:
9+
- port: 8080
10+
targetPort: 8081
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
apiVersion: v1
2+
kind: Plod
3+
metadata:
4+
name: test-pod
5+
labels:
6+
testLabel: testPod
7+
spec:
8+
containers:
9+
- name: test-container
10+
image: test-image
11+
resources:
12+
limits:
13+
memory: "128Mi"
14+
cpu: "500m"
15+
ports:
16+
- containerPort: 8080

0 commit comments

Comments
 (0)