Skip to content

Commit 0b6855d

Browse files
committed
Test list type handlings that have a slice of pointers as items field
1 parent f024e45 commit 0b6855d

File tree

3 files changed

+82
-2
lines changed

3 files changed

+82
-2
lines changed

pkg/controller/controller_integration_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,11 @@ var _ = Describe("controller", func() {
167167
Expect(err).NotTo(HaveOccurred())
168168
Expect(<-reconciled).To(Equal(expectedReconcileRequest))
169169

170+
By("Listing a type with a slice of pointers as items field")
171+
err = cm.GetClient().
172+
List(context.Background(), &UnconventionalListTypeList{})
173+
Expect(err).NotTo(HaveOccurred())
174+
170175
close(done)
171176
}, 5)
172177
})

pkg/controller/controller_suite_test.go

Lines changed: 66 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,19 @@ import (
2121

2222
. "github.com/onsi/ginkgo"
2323
. "github.com/onsi/gomega"
24+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
25+
"k8s.io/apimachinery/pkg/runtime"
26+
"k8s.io/apimachinery/pkg/runtime/schema"
2427
"k8s.io/client-go/kubernetes"
28+
"k8s.io/client-go/kubernetes/scheme"
2529
"k8s.io/client-go/rest"
30+
2631
"sigs.k8s.io/controller-runtime/pkg/envtest"
2732
"sigs.k8s.io/controller-runtime/pkg/envtest/printer"
2833
logf "sigs.k8s.io/controller-runtime/pkg/log"
2934
"sigs.k8s.io/controller-runtime/pkg/log/zap"
3035
"sigs.k8s.io/controller-runtime/pkg/metrics"
36+
crscheme "sigs.k8s.io/controller-runtime/pkg/scheme"
3137
)
3238

3339
func TestSource(t *testing.T) {
@@ -42,9 +48,17 @@ var clientset *kubernetes.Clientset
4248
var _ = BeforeSuite(func(done Done) {
4349
logf.SetLogger(zap.LoggerTo(GinkgoWriter, true))
4450

45-
testenv = &envtest.Environment{}
51+
err := (&crscheme.Builder{
52+
GroupVersion: schema.GroupVersion{Group: "chaosapps.metamagical.io", Version: "v1"},
53+
}).
54+
Register(&UnconventionalListType{}, &UnconventionalListTypeList{}).
55+
AddToScheme(scheme.Scheme)
56+
Expect(err).To(BeNil())
57+
58+
testenv = &envtest.Environment{
59+
CRDDirectoryPaths: []string{"testdata/crds"},
60+
}
4661

47-
var err error
4862
cfg, err = testenv.Start()
4963
Expect(err).NotTo(HaveOccurred())
5064

@@ -63,3 +77,53 @@ var _ = AfterSuite(func() {
6377
// Put the DefaultBindAddress back
6478
metrics.DefaultBindAddress = ":8080"
6579
})
80+
81+
var _ runtime.Object = &UnconventionalListType{}
82+
var _ runtime.Object = &UnconventionalListTypeList{}
83+
84+
// UnconventionalListType is used to test CRDs with List types that
85+
// have a slice of pointers rather than a slice of literals.
86+
type UnconventionalListType struct {
87+
metav1.TypeMeta `json:",inline"`
88+
metav1.ObjectMeta `json:"metadata,omitempty"`
89+
Spec string `json:"spec,omitempty"`
90+
}
91+
92+
// DeepCopyObject implements runtime.Object
93+
// Handwritten for simplicity.
94+
func (u *UnconventionalListType) DeepCopyObject() runtime.Object {
95+
return u.DeepCopy()
96+
}
97+
98+
func (u *UnconventionalListType) DeepCopy() *UnconventionalListType {
99+
return &UnconventionalListType{
100+
TypeMeta: u.TypeMeta,
101+
ObjectMeta: *u.ObjectMeta.DeepCopy(),
102+
Spec: u.Spec,
103+
}
104+
}
105+
106+
// UnconventionalListTypeList is used to test CRDs with List types that
107+
// have a slice of pointers rather than a slice of literals.
108+
type UnconventionalListTypeList struct {
109+
metav1.TypeMeta `json:",inline"`
110+
metav1.ListMeta `json:"metadata,omitempty"`
111+
Items []*UnconventionalListType `json:"items"`
112+
}
113+
114+
// DeepCopyObject implements runtime.Object
115+
// Handwritten for simplicity.
116+
func (u *UnconventionalListTypeList) DeepCopyObject() runtime.Object {
117+
return u.DeepCopy()
118+
}
119+
120+
func (u *UnconventionalListTypeList) DeepCopy() *UnconventionalListTypeList {
121+
out := &UnconventionalListTypeList{
122+
TypeMeta: u.TypeMeta,
123+
ListMeta: *u.ListMeta.DeepCopy(),
124+
}
125+
for _, item := range u.Items {
126+
out.Items = append(out.Items, item.DeepCopy())
127+
}
128+
return out
129+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
apiVersion: apiextensions.k8s.io/v1beta1
2+
kind: CustomResourceDefinition
3+
metadata:
4+
name: unconventionallisttypes.chaosapps.metamagical.io
5+
spec:
6+
group: chaosapps.metamagical.io
7+
names:
8+
kind: UnconventionalListType
9+
plural: unconventionallisttypes
10+
scope: Namespaced
11+
version: "v1"

0 commit comments

Comments
 (0)