Skip to content

Commit f79da11

Browse files
committed
Add unit tests for sort.go
This commit add unit tests for sort.go
1 parent b43c259 commit f79da11

File tree

3 files changed

+232
-0
lines changed

3 files changed

+232
-0
lines changed

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ require (
99
github.com/go-logr/logr v0.3.0
1010
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b
1111
github.com/prometheus/client_golang v1.7.1
12+
github.com/stretchr/testify v1.6.1
1213
golang.org/x/crypto v0.0.0-20201002170205-7f63de1d35b0
1314
golang.org/x/tools v0.0.0-20200714190737-9048b464a08d
1415
k8s.io/api v0.20.1

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -475,9 +475,11 @@ github.com/mailru/easyjson v0.7.0 h1:aizVhC/NAAcKWb+5QsU1iNOZb4Yws5UO2I+aIprQITM
475475
github.com/mailru/easyjson v0.7.0/go.mod h1:KAzv3t3aY1NaHWoQz1+4F1ccyAH66Jk7yos7ldAVICs=
476476
github.com/matoous/godox v0.0.0-20190911065817-5d6d842e92eb/go.mod h1:1BELzlh859Sh1c6+90blK8lbYy0kwQf1bYlBhBysy1s=
477477
github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU=
478+
github.com/mattn/go-colorable v0.1.4 h1:snbPLB8fVfU9iwbbo30TPtbLRzwWu6aJS6Xh4eaaviA=
478479
github.com/mattn/go-colorable v0.1.4/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE=
479480
github.com/mattn/go-isatty v0.0.3/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4=
480481
github.com/mattn/go-isatty v0.0.4/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4=
482+
github.com/mattn/go-isatty v0.0.8 h1:HLtExJ+uU2HOZ+wI0Tt5DtUDrx8yhUqDcp7fYERX4CE=
481483
github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
482484
github.com/mattn/go-runewidth v0.0.2/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU=
483485
github.com/mattn/goveralls v0.0.2/go.mod h1:8d1ZMHsd7fW6IRPKQh46F2WRpyib5/X4FOpevwGNQEw=

pkg/patterns/declarative/sort_test.go

Lines changed: 229 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,229 @@
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/stretchr/testify/assert"
24+
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
25+
"sigs.k8s.io/kubebuilder-declarative-pattern/pkg/patterns/declarative/pkg/manifest"
26+
)
27+
28+
func Test_Sort(t *testing.T) {
29+
crd1 := &unstructured.Unstructured{
30+
Object: map[string]interface{}{
31+
"apiVersion": "apiextensions.k8s.io/v1beta1",
32+
"kind": "CustomResourceDefinition",
33+
"metadata": map[string]interface{}{
34+
"name": "test-crd",
35+
"namespace": "kube-system",
36+
},
37+
},
38+
}
39+
crdObj1, _ := manifest.NewObject(crd1)
40+
41+
ns1 := &unstructured.Unstructured{
42+
Object: map[string]interface{}{
43+
"apiVersion": "v1",
44+
"kind": "Namespace",
45+
"metadata": map[string]interface{}{
46+
"name": "test-crd",
47+
"namespace": "kube-system",
48+
},
49+
},
50+
}
51+
nsObj1, _ := manifest.NewObject(ns1)
52+
53+
sa1 := &unstructured.Unstructured{
54+
Object: map[string]interface{}{
55+
"apiVersion": "v1",
56+
"kind": "ServiceAccount",
57+
"metadata": map[string]interface{}{
58+
"name": "foo-operator",
59+
"namespace": "kube-system",
60+
},
61+
},
62+
}
63+
saObj1, _ := manifest.NewObject(sa1)
64+
65+
sa2 := &unstructured.Unstructured{
66+
Object: map[string]interface{}{
67+
"apiVersion": "v1",
68+
"kind": "ServiceAccount",
69+
"metadata": map[string]interface{}{
70+
"name": "bar-operator",
71+
"namespace": "kube-system",
72+
},
73+
},
74+
}
75+
saObj2, _ := manifest.NewObject(sa2)
76+
77+
clusterRole1 := &unstructured.Unstructured{
78+
Object: map[string]interface{}{
79+
"apiVersion": "rbac.authorization.k8s.io/v1",
80+
"kind": "ClusterRole",
81+
"metadata": map[string]interface{}{
82+
"name": "test-clusterrole",
83+
"namespace": "kube-system",
84+
},
85+
},
86+
}
87+
clusterRoleObj1, _ := manifest.NewObject(clusterRole1)
88+
89+
clusterRoleBinding1 := &unstructured.Unstructured{
90+
Object: map[string]interface{}{
91+
"apiVersion": "rbac.authorization.k8s.io/v1",
92+
"kind": "ClusterRoleBinding",
93+
"metadata": map[string]interface{}{
94+
"name": "test-clusterrolebinding",
95+
"namespace": "kube-system",
96+
},
97+
},
98+
}
99+
clusterRoleBindingObj1, _ := manifest.NewObject(clusterRoleBinding1)
100+
101+
cm1 := &unstructured.Unstructured{
102+
Object: map[string]interface{}{
103+
"apiVersion": "v1",
104+
"kind": "ConfigMap",
105+
"metadata": map[string]interface{}{
106+
"name": "test-configmap",
107+
},
108+
},
109+
}
110+
cmObj1, _ := manifest.NewObject(cm1)
111+
112+
secret1 := &unstructured.Unstructured{
113+
Object: map[string]interface{}{
114+
"apiVersion": "v1",
115+
"kind": "Secrets", //TODO: current code set use Secrets not Secret..
116+
"metadata": map[string]interface{}{
117+
"name": "test-secret",
118+
},
119+
},
120+
}
121+
secretObj1, _ := manifest.NewObject(secret1)
122+
123+
dp1 := &unstructured.Unstructured{
124+
Object: map[string]interface{}{
125+
"apiVersion": "apps/v1",
126+
"kind": "Deployment",
127+
"metadata": map[string]interface{}{
128+
"name": "frontend111",
129+
},
130+
},
131+
}
132+
dpObj1, _ := manifest.NewObject(dp1)
133+
134+
dp2 := &unstructured.Unstructured{
135+
Object: map[string]interface{}{
136+
"apiVersion": "extensions/v1beta1",
137+
"kind": "Deployment",
138+
"metadata": map[string]interface{}{
139+
"name": "frontend222",
140+
},
141+
},
142+
}
143+
dpObj2, _ := manifest.NewObject(dp2)
144+
145+
hpa1 := &unstructured.Unstructured{
146+
Object: map[string]interface{}{
147+
"apiVersion": "autoscaling/v1",
148+
"kind": "HorizontalPodAutoscaler",
149+
"metadata": map[string]interface{}{
150+
"name": "test-autoscaler",
151+
},
152+
},
153+
}
154+
hpaObj1, _ := manifest.NewObject(hpa1)
155+
156+
svc1 := &unstructured.Unstructured{
157+
Object: map[string]interface{}{
158+
"apiVersion": "v1",
159+
"kind": "Service",
160+
"metadata": map[string]interface{}{
161+
"name": "test-service",
162+
},
163+
},
164+
}
165+
svcObj1, _ := manifest.NewObject(svc1)
166+
167+
pod1 := &unstructured.Unstructured{
168+
Object: map[string]interface{}{
169+
"apiVersion": "v1",
170+
"kind": "Pod",
171+
"metadata": map[string]interface{}{
172+
"name": "test-pod",
173+
},
174+
},
175+
}
176+
podObj1, _ := manifest.NewObject(pod1)
177+
178+
var testcases = []struct {
179+
name string
180+
input *manifest.Objects
181+
expected *manifest.Objects
182+
}{
183+
{
184+
name: "multiple objects",
185+
input: &manifest.Objects{
186+
Items: []*manifest.Object{
187+
saObj1,
188+
crdObj1,
189+
nsObj1,
190+
saObj2,
191+
dpObj1,
192+
dpObj2,
193+
clusterRoleObj1,
194+
hpaObj1,
195+
podObj1,
196+
svcObj1,
197+
clusterRoleBindingObj1,
198+
cmObj1,
199+
secretObj1,
200+
},
201+
},
202+
expected: &manifest.Objects{
203+
Items: []*manifest.Object{
204+
crdObj1,
205+
nsObj1,
206+
saObj2,
207+
saObj1,
208+
clusterRoleObj1,
209+
clusterRoleBindingObj1,
210+
cmObj1,
211+
secretObj1,
212+
podObj1,
213+
dpObj1,
214+
dpObj2,
215+
hpaObj1,
216+
svcObj1,
217+
},
218+
},
219+
},
220+
}
221+
for _, tc := range testcases {
222+
t.Run(tc.name, func(t *testing.T) {
223+
ctx := context.Background()
224+
tc.input.Sort(DefaultObjectOrder(ctx))
225+
assert.Equal(t, tc.expected, tc.input)
226+
})
227+
}
228+
229+
}

0 commit comments

Comments
 (0)