Skip to content

Commit 044be6b

Browse files
committed
Add unit tests for sort.go
This commit add unit tests for sort.go
1 parent 4ba4cad commit 044be6b

File tree

2 files changed

+234
-0
lines changed

2 files changed

+234
-0
lines changed

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ require (
88
github.com/go-git/go-git/v5 v5.1.0
99
github.com/go-logr/logr v0.3.0
1010
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b
11+
github.com/google/go-cmp v0.5.2
1112
github.com/prometheus/client_golang v1.7.1
1213
github.com/stretchr/testify v1.6.1
1314
golang.org/x/crypto v0.0.0-20201002170205-7f63de1d35b0

pkg/patterns/declarative/sort_test.go

Lines changed: 233 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,233 @@
1+
/*
2+
Copyright 2019 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package declarative
18+
19+
import (
20+
"context"
21+
"testing"
22+
23+
"github.com/google/go-cmp/cmp"
24+
"github.com/google/go-cmp/cmp/cmpopts"
25+
26+
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
27+
"sigs.k8s.io/kubebuilder-declarative-pattern/pkg/patterns/declarative/pkg/manifest"
28+
)
29+
30+
func Test_Sort(t *testing.T) {
31+
crd1 := &unstructured.Unstructured{
32+
Object: map[string]interface{}{
33+
"apiVersion": "apiextensions.k8s.io/v1beta1",
34+
"kind": "CustomResourceDefinition",
35+
"metadata": map[string]interface{}{
36+
"name": "test-crd",
37+
"namespace": "kube-system",
38+
},
39+
},
40+
}
41+
crdObj1, _ := manifest.NewObject(crd1)
42+
43+
ns1 := &unstructured.Unstructured{
44+
Object: map[string]interface{}{
45+
"apiVersion": "v1",
46+
"kind": "Namespace",
47+
"metadata": map[string]interface{}{
48+
"name": "test-crd",
49+
"namespace": "kube-system",
50+
},
51+
},
52+
}
53+
nsObj1, _ := manifest.NewObject(ns1)
54+
55+
sa1 := &unstructured.Unstructured{
56+
Object: map[string]interface{}{
57+
"apiVersion": "v1",
58+
"kind": "ServiceAccount",
59+
"metadata": map[string]interface{}{
60+
"name": "foo-operator",
61+
"namespace": "kube-system",
62+
},
63+
},
64+
}
65+
saObj1, _ := manifest.NewObject(sa1)
66+
67+
sa2 := &unstructured.Unstructured{
68+
Object: map[string]interface{}{
69+
"apiVersion": "v1",
70+
"kind": "ServiceAccount",
71+
"metadata": map[string]interface{}{
72+
"name": "bar-operator",
73+
"namespace": "kube-system",
74+
},
75+
},
76+
}
77+
saObj2, _ := manifest.NewObject(sa2)
78+
79+
clusterRole1 := &unstructured.Unstructured{
80+
Object: map[string]interface{}{
81+
"apiVersion": "rbac.authorization.k8s.io/v1",
82+
"kind": "ClusterRole",
83+
"metadata": map[string]interface{}{
84+
"name": "test-clusterrole",
85+
"namespace": "kube-system",
86+
},
87+
},
88+
}
89+
clusterRoleObj1, _ := manifest.NewObject(clusterRole1)
90+
91+
clusterRoleBinding1 := &unstructured.Unstructured{
92+
Object: map[string]interface{}{
93+
"apiVersion": "rbac.authorization.k8s.io/v1",
94+
"kind": "ClusterRoleBinding",
95+
"metadata": map[string]interface{}{
96+
"name": "test-clusterrolebinding",
97+
"namespace": "kube-system",
98+
},
99+
},
100+
}
101+
clusterRoleBindingObj1, _ := manifest.NewObject(clusterRoleBinding1)
102+
103+
cm1 := &unstructured.Unstructured{
104+
Object: map[string]interface{}{
105+
"apiVersion": "v1",
106+
"kind": "ConfigMap",
107+
"metadata": map[string]interface{}{
108+
"name": "test-configmap",
109+
},
110+
},
111+
}
112+
cmObj1, _ := manifest.NewObject(cm1)
113+
114+
secret1 := &unstructured.Unstructured{
115+
Object: map[string]interface{}{
116+
"apiVersion": "v1",
117+
"kind": "Secrets", //TODO: current code set use Secrets not Secret..
118+
"metadata": map[string]interface{}{
119+
"name": "test-secret",
120+
},
121+
},
122+
}
123+
secretObj1, _ := manifest.NewObject(secret1)
124+
125+
dp1 := &unstructured.Unstructured{
126+
Object: map[string]interface{}{
127+
"apiVersion": "apps/v1",
128+
"kind": "Deployment",
129+
"metadata": map[string]interface{}{
130+
"name": "frontend111",
131+
},
132+
},
133+
}
134+
dpObj1, _ := manifest.NewObject(dp1)
135+
136+
dp2 := &unstructured.Unstructured{
137+
Object: map[string]interface{}{
138+
"apiVersion": "extensions/v1beta1",
139+
"kind": "Deployment",
140+
"metadata": map[string]interface{}{
141+
"name": "frontend222",
142+
},
143+
},
144+
}
145+
dpObj2, _ := manifest.NewObject(dp2)
146+
147+
hpa1 := &unstructured.Unstructured{
148+
Object: map[string]interface{}{
149+
"apiVersion": "autoscaling/v1",
150+
"kind": "HorizontalPodAutoscaler",
151+
"metadata": map[string]interface{}{
152+
"name": "test-autoscaler",
153+
},
154+
},
155+
}
156+
hpaObj1, _ := manifest.NewObject(hpa1)
157+
158+
svc1 := &unstructured.Unstructured{
159+
Object: map[string]interface{}{
160+
"apiVersion": "v1",
161+
"kind": "Service",
162+
"metadata": map[string]interface{}{
163+
"name": "test-service",
164+
},
165+
},
166+
}
167+
svcObj1, _ := manifest.NewObject(svc1)
168+
169+
pod1 := &unstructured.Unstructured{
170+
Object: map[string]interface{}{
171+
"apiVersion": "v1",
172+
"kind": "Pod",
173+
"metadata": map[string]interface{}{
174+
"name": "test-pod",
175+
},
176+
},
177+
}
178+
podObj1, _ := manifest.NewObject(pod1)
179+
180+
var testcases = []struct {
181+
name string
182+
input *manifest.Objects
183+
expected *manifest.Objects
184+
}{
185+
{
186+
name: "multiple objects",
187+
input: &manifest.Objects{
188+
Items: []*manifest.Object{
189+
saObj1,
190+
crdObj1,
191+
nsObj1,
192+
saObj2,
193+
dpObj1,
194+
dpObj2,
195+
clusterRoleObj1,
196+
hpaObj1,
197+
podObj1,
198+
svcObj1,
199+
clusterRoleBindingObj1,
200+
cmObj1,
201+
secretObj1,
202+
},
203+
},
204+
expected: &manifest.Objects{
205+
Items: []*manifest.Object{
206+
crdObj1,
207+
nsObj1,
208+
saObj2,
209+
saObj1,
210+
clusterRoleObj1,
211+
clusterRoleBindingObj1,
212+
cmObj1,
213+
secretObj1,
214+
podObj1,
215+
dpObj1,
216+
dpObj2,
217+
hpaObj1,
218+
svcObj1,
219+
},
220+
},
221+
},
222+
}
223+
for _, tc := range testcases {
224+
t.Run(tc.name, func(t *testing.T) {
225+
ctx := context.Background()
226+
tc.input.Sort(DefaultObjectOrder(ctx))
227+
if diff := cmp.Diff(tc.expected, tc.input, cmpopts.IgnoreUnexported(manifest.Object{})); diff != "" {
228+
t.Errorf("sort result mismatch (-want +got):\n%s", diff)
229+
}
230+
})
231+
}
232+
233+
}

0 commit comments

Comments
 (0)