@@ -60,14 +60,18 @@ const controllerTestTemplate = `{{ .Boilerplate }}
60
60
package {{ if and .MultiGroup .Resource.Group }}{{ .Resource.PackageName }}{{ else }}controllers{{ end }}
61
61
62
62
import (
63
- "fmt"
63
+ "context"
64
+ "os"
65
+ "time"
66
+
67
+ . "github.com/onsi/ginkgo"
68
+ . "github.com/onsi/gomega"
69
+ appsv1 "k8s.io/api/apps/v1"
70
+ corev1 "k8s.io/api/core/v1"
64
71
"k8s.io/apimachinery/pkg/api/errors"
65
72
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
66
73
"k8s.io/apimachinery/pkg/types"
67
-
68
- "time"
69
- "context"
70
-
74
+ "sigs.k8s.io/controller-runtime/pkg/reconcile"
71
75
. "github.com/onsi/ginkgo"
72
76
. "github.com/onsi/gomega"
73
77
@@ -77,30 +81,50 @@ import (
77
81
)
78
82
79
83
var _ = Describe("{{ .Resource.Kind }} controller", func() {
84
+ Context("{{ .Resource.Kind }} controller test", func() {
80
85
81
- // Define utility constants for object names and testing timeouts/durations and intervals.
82
- const (
83
- {{ .Resource.Kind }}Name = "test-{{ lower .Resource.Kind }}"
84
- {{ .Resource.Kind }}Namespace = "default"
85
- )
86
+ const {{ .Resource.Kind }}Name = "test-{{ lower .Resource.Kind }}"
86
87
87
- Context("{{ .Resource.Kind }} controller test", func() {
88
- It("should create successfully the custom resource for the {{ .Resource.Kind }}", func() {
89
- ctx := context.Background()
88
+ ctx := context.Background()
89
+
90
+ namespace := &corev1.Namespace{
91
+ ObjectMeta: metav1.ObjectMeta{
92
+ Name: {{ .Resource.Kind }}Name,
93
+ Namespace: {{ .Resource.Kind }}Name,
94
+ },
95
+ }
96
+
97
+ typeNamespaceName := types.NamespacedName{Name: {{ .Resource.Kind }}Name, Namespace: {{ .Resource.Kind }}Name}
98
+
99
+ BeforeEach(func() {
100
+ By("Creating the Namespace to perform the tests")
101
+ err := k8sClient.Create(ctx, namespace);
102
+ Expect(err).To(Not(HaveOccurred()))
90
103
104
+ By("Setting the Image ENV VAR which stores the Operand image")
105
+ err= os.Setenv("{{ upper .Resource.Kind }}_IMAGE", "example.com/image:test")
106
+ Expect(err).To(Not(HaveOccurred()))
107
+ })
108
+
109
+ AfterEach(func() {
110
+ By("Deleting the Namespace to perform the tests")
111
+ _ = k8sClient.Delete(ctx, namespace);
112
+
113
+ By("Removing the Image ENV VAR which stores the Operand image")
114
+ _ = os.Unsetenv("{{ upper .Resource.Kind }}_IMAGE")
115
+ })
116
+
117
+ It("should successfully reconcile a custom resource for {{ .Resource.Kind }}", func() {
91
118
By("Creating the custom resource for the Kind {{ .Resource.Kind }}")
92
119
{{ lower .Resource.Kind }} := &{{ .Resource.ImportAlias }}.{{ .Resource.Kind }}{}
93
- err := k8sClient.Get(ctx, types.NamespacedName{Name: {{ .Resource.Kind }}Name, Namespace: {{ .Resource.Kind }}Namespace} , {{ lower .Resource.Kind }})
120
+ err := k8sClient.Get(ctx, typeNamespaceName , {{ lower .Resource.Kind }})
94
121
if err != nil && errors.IsNotFound(err) {
95
- // Define a new custom resource
122
+ // Let's mock our custom resource at the same way that we would
123
+ // apply on the cluster the manifest under config/samples
96
124
{{ lower .Resource.Kind }} := &{{ .Resource.ImportAlias }}.{{ .Resource.Kind }}{
97
- TypeMeta: metav1.TypeMeta{
98
- APIVersion: "{{ .Resource.Group }}.{{ .Resource.Domain }}/{{ .Resource.Version }}",
99
- Kind: "{{ .Resource.Kind }}",
100
- },
101
125
ObjectMeta: metav1.ObjectMeta{
102
126
Name: {{ .Resource.Kind }}Name,
103
- Namespace: {{ .Resource.Kind }}Namespace ,
127
+ Namespace: namespace.Name ,
104
128
},
105
129
Spec: {{ .Resource.ImportAlias }}.{{ .Resource.Kind }}Spec{
106
130
Size: 1,
@@ -109,24 +133,44 @@ var _ = Describe("{{ .Resource.Kind }} controller", func() {
109
133
{{- end }}
110
134
},
111
135
}
112
- fmt.Fprintf(GinkgoWriter, fmt.Sprintf("Creating a new custom resource in the namespace: %s with the name %s\n", {{ lower .Resource.Kind }}.Namespace, {{ lower .Resource.Kind }}.Name))
136
+
113
137
err = k8sClient.Create(ctx, {{ lower .Resource.Kind }})
114
138
if err != nil {
115
139
Expect(err).To(Not(HaveOccurred()))
116
140
}
117
141
}
118
142
119
- By("Checking with {{ .Resource.Kind }} Kind exist ")
143
+ By("Checking if the custom resource was successfully crated ")
120
144
Eventually(func() error {
121
145
found := &{{ .Resource.ImportAlias }}.{{ .Resource.Kind }}{}
122
- err = k8sClient.Get(ctx, types.NamespacedName{Name: {{ .Resource.Kind }}Name, Namespace: {{ .Resource.Kind }}Namespace}, found)
146
+ err = k8sClient.Get(ctx, typeNamespaceName, found)
147
+ if err != nil {
148
+ return err
149
+ }
150
+ return nil
151
+ }, time.Minute, time.Second).Should(Succeed())
152
+
153
+ By("Reconciling the custom resource created")
154
+ {{ lower .Resource.Kind }}Reconciler := &{{ .Resource.Kind }}Reconciler{
155
+ Client: k8sClient,
156
+ Scheme: k8sClient.Scheme(),
157
+ }
158
+
159
+ _, err = {{ lower .Resource.Kind }}Reconciler.Reconcile(ctx, reconcile.Request{
160
+ NamespacedName: typeNamespaceName,
161
+ })
162
+ Expect(err).To(Not(HaveOccurred()))
163
+
164
+ By("Checking if Deployment was successfully crated in the reconciliation")
165
+ Eventually(func() error {
166
+ found := &appsv1.Deployment{}
167
+ err = k8sClient.Get(ctx, typeNamespaceName, found)
123
168
if err != nil {
124
169
return err
125
170
}
126
171
return nil
127
172
}, time.Minute, time.Second).Should(Succeed())
128
173
})
129
174
})
130
-
131
175
})
132
176
`
0 commit comments