Skip to content

Commit

Permalink
🐛 (deployImage) fix controller tests implemented by removing unecessa…
Browse files Browse the repository at this point in the history
…ry code implementation
  • Loading branch information
camilamacedo86 committed Apr 23, 2023
1 parent 9ba9d57 commit 3af9cfa
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ var _ = Describe("{{ .Resource.Kind }} controller", func() {
}
typeNamespaceName := types.NamespacedName{Name: {{ .Resource.Kind }}Name, Namespace: {{ .Resource.Kind }}Name}
{{ lower .Resource.Kind }} := &{{ .Resource.ImportAlias }}.{{ .Resource.Kind }}{}
BeforeEach(func() {
By("Creating the Namespace to perform the tests")
Expand All @@ -118,22 +119,9 @@ var _ = Describe("{{ .Resource.Kind }} controller", func() {
By("Setting the Image ENV VAR which stores the Operand image")
err= os.Setenv("{{ upper .Resource.Kind }}_IMAGE", "example.com/image:test")
Expect(err).To(Not(HaveOccurred()))
})
AfterEach(func() {
// TODO(user): Attention if you improve this code by adding other context test you MUST
// be aware of the current delete namespace limitations. More info: https://book.kubebuilder.io/reference/envtest.html#testing-considerations
By("Deleting the Namespace to perform the tests")
_ = k8sClient.Delete(ctx, namespace);
By("Removing the Image ENV VAR which stores the Operand image")
_ = os.Unsetenv("{{ upper .Resource.Kind }}_IMAGE")
})
It("should successfully reconcile a custom resource for {{ .Resource.Kind }}", func() {
By("Creating the custom resource for the Kind {{ .Resource.Kind }}")
{{ lower .Resource.Kind }} := &{{ .Resource.ImportAlias }}.{{ .Resource.Kind }}{}
err := k8sClient.Get(ctx, typeNamespaceName, {{ lower .Resource.Kind }})
By("creating the custom resource for the Kind {{ .Resource.Kind }}")
err = k8sClient.Get(ctx, typeNamespaceName, {{ lower .Resource.Kind }})
if err != nil && errors.IsNotFound(err) {
// Let's mock our custom resource at the same way that we would
// apply on the cluster the manifest under config/samples
Expand All @@ -152,8 +140,30 @@ var _ = Describe("{{ .Resource.Kind }} controller", func() {
err = k8sClient.Create(ctx, {{ lower .Resource.Kind }})
Expect(err).To(Not(HaveOccurred()))
}
}
})
AfterEach(func() {
By("removing the custom resource for the Kind {{ .Resource.Kind }}")
found := &{{ .Resource.ImportAlias }}.{{ .Resource.Kind }}{}
err := k8sClient.Get(context.TODO(), typeNamespaceName, found)
Expect(err).To(Not(HaveOccurred()))
Eventually(func() error {
return k8sClient.Delete(context.TODO(), found)
}, 2*time.Minute, time.Second).Should(Succeed())
// TODO(user): Attention if you improve this code by adding other context test you MUST
// be aware of the current delete namespace limitations.
// More info: https://book.kubebuilder.io/reference/envtest.html#testing-considerations
By("Deleting the Namespace to perform the tests")
_ = k8sClient.Delete(ctx, namespace);
By("Removing the Image ENV VAR which stores the Operand image")
_ = os.Unsetenv("{{ upper .Resource.Kind }}_IMAGE")
})
It("should successfully reconcile a custom resource for {{ .Resource.Kind }}", func() {
By("Checking if the custom resource was successfully created")
Eventually(func() error {
found := &{{ .Resource.ImportAlias }}.{{ .Resource.Kind }}{}
Expand All @@ -166,7 +176,7 @@ var _ = Describe("{{ .Resource.Kind }} controller", func() {
Scheme: k8sClient.Scheme(),
}
_, err = {{ lower .Resource.Kind }}Reconciler.Reconcile(ctx, reconcile.Request{
_, err := {{ lower .Resource.Kind }}Reconciler.Reconcile(ctx, reconcile.Request{
NamespacedName: typeNamespaceName,
})
Expect(err).To(Not(HaveOccurred()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ var _ = Describe("Busybox controller", func() {
}

typeNamespaceName := types.NamespacedName{Name: BusyboxName, Namespace: BusyboxName}
busybox := &examplecomv1alpha1.Busybox{}

BeforeEach(func() {
By("Creating the Namespace to perform the tests")
Expand All @@ -58,22 +59,9 @@ var _ = Describe("Busybox controller", func() {
By("Setting the Image ENV VAR which stores the Operand image")
err = os.Setenv("BUSYBOX_IMAGE", "example.com/image:test")
Expect(err).To(Not(HaveOccurred()))
})

AfterEach(func() {
// TODO(user): Attention if you improve this code by adding other context test you MUST
// be aware of the current delete namespace limitations. More info: https://book.kubebuilder.io/reference/envtest.html#testing-considerations
By("Deleting the Namespace to perform the tests")
_ = k8sClient.Delete(ctx, namespace)

By("Removing the Image ENV VAR which stores the Operand image")
_ = os.Unsetenv("BUSYBOX_IMAGE")
})

It("should successfully reconcile a custom resource for Busybox", func() {
By("Creating the custom resource for the Kind Busybox")
busybox := &examplecomv1alpha1.Busybox{}
err := k8sClient.Get(ctx, typeNamespaceName, busybox)
By("creating the custom resource for the Kind Busybox")
err = k8sClient.Get(ctx, typeNamespaceName, busybox)
if err != nil && errors.IsNotFound(err) {
// Let's mock our custom resource at the same way that we would
// apply on the cluster the manifest under config/samples
Expand All @@ -90,7 +78,29 @@ var _ = Describe("Busybox controller", func() {
err = k8sClient.Create(ctx, busybox)
Expect(err).To(Not(HaveOccurred()))
}
})

AfterEach(func() {
By("removing the custom resource for the Kind Busybox")
found := &examplecomv1alpha1.Busybox{}
err := k8sClient.Get(context.TODO(), typeNamespaceName, found)
Expect(err).To(Not(HaveOccurred()))

Eventually(func() error {
return k8sClient.Delete(context.TODO(), found)
}, 2*time.Minute, time.Second).Should(Succeed())

// TODO(user): Attention if you improve this code by adding other context test you MUST
// be aware of the current delete namespace limitations.
// More info: https://book.kubebuilder.io/reference/envtest.html#testing-considerations
By("Deleting the Namespace to perform the tests")
_ = k8sClient.Delete(ctx, namespace)

By("Removing the Image ENV VAR which stores the Operand image")
_ = os.Unsetenv("BUSYBOX_IMAGE")
})

It("should successfully reconcile a custom resource for Busybox", func() {
By("Checking if the custom resource was successfully created")
Eventually(func() error {
found := &examplecomv1alpha1.Busybox{}
Expand All @@ -103,7 +113,7 @@ var _ = Describe("Busybox controller", func() {
Scheme: k8sClient.Scheme(),
}

_, err = busyboxReconciler.Reconcile(ctx, reconcile.Request{
_, err := busyboxReconciler.Reconcile(ctx, reconcile.Request{
NamespacedName: typeNamespaceName,
})
Expect(err).To(Not(HaveOccurred()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ var _ = Describe("Memcached controller", func() {
}

typeNamespaceName := types.NamespacedName{Name: MemcachedName, Namespace: MemcachedName}
memcached := &examplecomv1alpha1.Memcached{}

BeforeEach(func() {
By("Creating the Namespace to perform the tests")
Expand All @@ -58,22 +59,9 @@ var _ = Describe("Memcached controller", func() {
By("Setting the Image ENV VAR which stores the Operand image")
err = os.Setenv("MEMCACHED_IMAGE", "example.com/image:test")
Expect(err).To(Not(HaveOccurred()))
})

AfterEach(func() {
// TODO(user): Attention if you improve this code by adding other context test you MUST
// be aware of the current delete namespace limitations. More info: https://book.kubebuilder.io/reference/envtest.html#testing-considerations
By("Deleting the Namespace to perform the tests")
_ = k8sClient.Delete(ctx, namespace)

By("Removing the Image ENV VAR which stores the Operand image")
_ = os.Unsetenv("MEMCACHED_IMAGE")
})

It("should successfully reconcile a custom resource for Memcached", func() {
By("Creating the custom resource for the Kind Memcached")
memcached := &examplecomv1alpha1.Memcached{}
err := k8sClient.Get(ctx, typeNamespaceName, memcached)
By("creating the custom resource for the Kind Memcached")
err = k8sClient.Get(ctx, typeNamespaceName, memcached)
if err != nil && errors.IsNotFound(err) {
// Let's mock our custom resource at the same way that we would
// apply on the cluster the manifest under config/samples
Expand All @@ -91,7 +79,29 @@ var _ = Describe("Memcached controller", func() {
err = k8sClient.Create(ctx, memcached)
Expect(err).To(Not(HaveOccurred()))
}
})

AfterEach(func() {
By("removing the custom resource for the Kind Memcached")
found := &examplecomv1alpha1.Memcached{}
err := k8sClient.Get(context.TODO(), typeNamespaceName, found)
Expect(err).To(Not(HaveOccurred()))

Eventually(func() error {
return k8sClient.Delete(context.TODO(), found)
}, 2*time.Minute, time.Second).Should(Succeed())

// TODO(user): Attention if you improve this code by adding other context test you MUST
// be aware of the current delete namespace limitations.
// More info: https://book.kubebuilder.io/reference/envtest.html#testing-considerations
By("Deleting the Namespace to perform the tests")
_ = k8sClient.Delete(ctx, namespace)

By("Removing the Image ENV VAR which stores the Operand image")
_ = os.Unsetenv("MEMCACHED_IMAGE")
})

It("should successfully reconcile a custom resource for Memcached", func() {
By("Checking if the custom resource was successfully created")
Eventually(func() error {
found := &examplecomv1alpha1.Memcached{}
Expand All @@ -104,7 +114,7 @@ var _ = Describe("Memcached controller", func() {
Scheme: k8sClient.Scheme(),
}

_, err = memcachedReconciler.Reconcile(ctx, reconcile.Request{
_, err := memcachedReconciler.Reconcile(ctx, reconcile.Request{
NamespacedName: typeNamespaceName,
})
Expect(err).To(Not(HaveOccurred()))
Expand Down

0 comments on commit 3af9cfa

Please sign in to comment.