Skip to content

Commit a06d98c

Browse files
authored
Merge pull request #2830 from camilamacedo86/tests
✨ (deploy.image/v1-alpha): improve controller-test reconciliation
2 parents e116b54 + 59cf8f2 commit a06d98c

File tree

5 files changed

+332
-124
lines changed

5 files changed

+332
-124
lines changed

pkg/plugins/golang/deploy-image/v1alpha1/scaffolds/internal/templates/controllers/controller-test.go

Lines changed: 68 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,18 @@ const controllerTestTemplate = `{{ .Boilerplate }}
6060
package {{ if and .MultiGroup .Resource.Group }}{{ .Resource.PackageName }}{{ else }}controllers{{ end }}
6161
6262
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"
6471
"k8s.io/apimachinery/pkg/api/errors"
6572
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
6673
"k8s.io/apimachinery/pkg/types"
67-
68-
"time"
69-
"context"
70-
74+
"sigs.k8s.io/controller-runtime/pkg/reconcile"
7175
. "github.com/onsi/ginkgo"
7276
. "github.com/onsi/gomega"
7377
@@ -77,30 +81,50 @@ import (
7781
)
7882
7983
var _ = Describe("{{ .Resource.Kind }} controller", func() {
84+
Context("{{ .Resource.Kind }} controller test", func() {
8085
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 }}"
8687
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()))
90103
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() {
91118
By("Creating the custom resource for the Kind {{ .Resource.Kind }}")
92119
{{ 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 }})
94121
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
96124
{{ lower .Resource.Kind }} := &{{ .Resource.ImportAlias }}.{{ .Resource.Kind }}{
97-
TypeMeta: metav1.TypeMeta{
98-
APIVersion: "{{ .Resource.Group }}.{{ .Resource.Domain }}/{{ .Resource.Version }}",
99-
Kind: "{{ .Resource.Kind }}",
100-
},
101125
ObjectMeta: metav1.ObjectMeta{
102126
Name: {{ .Resource.Kind }}Name,
103-
Namespace: {{ .Resource.Kind }}Namespace,
127+
Namespace: namespace.Name,
104128
},
105129
Spec: {{ .Resource.ImportAlias }}.{{ .Resource.Kind }}Spec{
106130
Size: 1,
@@ -109,24 +133,44 @@ var _ = Describe("{{ .Resource.Kind }} controller", func() {
109133
{{- end }}
110134
},
111135
}
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+
113137
err = k8sClient.Create(ctx, {{ lower .Resource.Kind }})
114138
if err != nil {
115139
Expect(err).To(Not(HaveOccurred()))
116140
}
117141
}
118142
119-
By("Checking with {{ .Resource.Kind }} Kind exist")
143+
By("Checking if the custom resource was successfully crated")
120144
Eventually(func() error {
121145
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)
123168
if err != nil {
124169
return err
125170
}
126171
return nil
127172
}, time.Minute, time.Second).Should(Succeed())
128173
})
129174
})
130-
131175
})
132176
`

testdata/project-v3-with-deploy-image/controllers/busybox_controller_test.go

Lines changed: 66 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -17,68 +17,109 @@ limitations under the License.
1717
package controllers
1818

1919
import (
20-
"fmt"
21-
22-
"k8s.io/apimachinery/pkg/api/errors"
23-
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
24-
"k8s.io/apimachinery/pkg/types"
25-
2620
"context"
21+
"os"
2722
"time"
2823

2924
. "github.com/onsi/ginkgo"
3025
. "github.com/onsi/gomega"
26+
appsv1 "k8s.io/api/apps/v1"
27+
corev1 "k8s.io/api/core/v1"
28+
"k8s.io/apimachinery/pkg/api/errors"
29+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
30+
"k8s.io/apimachinery/pkg/types"
31+
"sigs.k8s.io/controller-runtime/pkg/reconcile"
3132

3233
examplecomv1alpha1 "sigs.k8s.io/kubebuilder/testdata/project-v3-with-deploy-image/api/v1alpha1"
3334
)
3435

3536
var _ = Describe("Busybox controller", func() {
37+
Context("Busybox controller test", func() {
3638

37-
// Define utility constants for object names and testing timeouts/durations and intervals.
38-
const (
39-
BusyboxName = "test-busybox"
40-
BusyboxNamespace = "default"
41-
)
39+
const BusyboxName = "test-busybox"
4240

43-
Context("Busybox controller test", func() {
44-
It("should create successfully the custom resource for the Busybox", func() {
45-
ctx := context.Background()
41+
ctx := context.Background()
42+
43+
namespace := &corev1.Namespace{
44+
ObjectMeta: metav1.ObjectMeta{
45+
Name: BusyboxName,
46+
Namespace: BusyboxName,
47+
},
48+
}
49+
50+
typeNamespaceName := types.NamespacedName{Name: BusyboxName, Namespace: BusyboxName}
51+
52+
BeforeEach(func() {
53+
By("Creating the Namespace to perform the tests")
54+
err := k8sClient.Create(ctx, namespace)
55+
Expect(err).To(Not(HaveOccurred()))
56+
57+
By("Setting the Image ENV VAR which stores the Operand image")
58+
err = os.Setenv("BUSYBOX_IMAGE", "example.com/image:test")
59+
Expect(err).To(Not(HaveOccurred()))
60+
})
61+
62+
AfterEach(func() {
63+
By("Deleting the Namespace to perform the tests")
64+
_ = k8sClient.Delete(ctx, namespace)
4665

66+
By("Removing the Image ENV VAR which stores the Operand image")
67+
_ = os.Unsetenv("BUSYBOX_IMAGE")
68+
})
69+
70+
It("should successfully reconcile a custom resource for Busybox", func() {
4771
By("Creating the custom resource for the Kind Busybox")
4872
busybox := &examplecomv1alpha1.Busybox{}
49-
err := k8sClient.Get(ctx, types.NamespacedName{Name: BusyboxName, Namespace: BusyboxNamespace}, busybox)
73+
err := k8sClient.Get(ctx, typeNamespaceName, busybox)
5074
if err != nil && errors.IsNotFound(err) {
51-
// Define a new custom resource
75+
// Let's mock our custom resource at the same way that we would
76+
// apply on the cluster the manifest under config/samples
5277
busybox := &examplecomv1alpha1.Busybox{
53-
TypeMeta: metav1.TypeMeta{
54-
APIVersion: "example.com.testproject.org/v1alpha1",
55-
Kind: "Busybox",
56-
},
5778
ObjectMeta: metav1.ObjectMeta{
5879
Name: BusyboxName,
59-
Namespace: BusyboxNamespace,
80+
Namespace: namespace.Name,
6081
},
6182
Spec: examplecomv1alpha1.BusyboxSpec{
6283
Size: 1,
6384
},
6485
}
65-
fmt.Fprintf(GinkgoWriter, fmt.Sprintf("Creating a new custom resource in the namespace: %s with the name %s\n", busybox.Namespace, busybox.Name))
86+
6687
err = k8sClient.Create(ctx, busybox)
6788
if err != nil {
6889
Expect(err).To(Not(HaveOccurred()))
6990
}
7091
}
7192

72-
By("Checking with Busybox Kind exist")
93+
By("Checking if the custom resource was successfully crated")
7394
Eventually(func() error {
7495
found := &examplecomv1alpha1.Busybox{}
75-
err = k8sClient.Get(ctx, types.NamespacedName{Name: BusyboxName, Namespace: BusyboxNamespace}, found)
96+
err = k8sClient.Get(ctx, typeNamespaceName, found)
97+
if err != nil {
98+
return err
99+
}
100+
return nil
101+
}, time.Minute, time.Second).Should(Succeed())
102+
103+
By("Reconciling the custom resource created")
104+
busyboxReconciler := &BusyboxReconciler{
105+
Client: k8sClient,
106+
Scheme: k8sClient.Scheme(),
107+
}
108+
109+
_, err = busyboxReconciler.Reconcile(ctx, reconcile.Request{
110+
NamespacedName: typeNamespaceName,
111+
})
112+
Expect(err).To(Not(HaveOccurred()))
113+
114+
By("Checking if Deployment was successfully crated in the reconciliation")
115+
Eventually(func() error {
116+
found := &appsv1.Deployment{}
117+
err = k8sClient.Get(ctx, typeNamespaceName, found)
76118
if err != nil {
77119
return err
78120
}
79121
return nil
80122
}, time.Minute, time.Second).Should(Succeed())
81123
})
82124
})
83-
84125
})

0 commit comments

Comments
 (0)