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 f2621e4
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,6 @@ var _ = Describe("{{ .Resource.Kind }} controller", func() {
Context("{{ .Resource.Kind }} controller test", func() {
const {{ .Resource.Kind }}Name = "test-{{ lower .Resource.Kind }}"
ctx := context.Background()
namespace := &corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: {{ .Resource.Kind }}Name,
Expand All @@ -111,29 +108,20 @@ var _ = Describe("{{ .Resource.Kind }} controller", func() {
typeNamespaceName := types.NamespacedName{Name: {{ .Resource.Kind }}Name, Namespace: {{ .Resource.Kind }}Name}
BeforeEach(func() {
By("Creating the Namespace to perform the tests")
err := k8sClient.Create(ctx, namespace);
Expect(err).To(Not(HaveOccurred()))
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 }})
err := k8sClient.Get(context.TODO, 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 @@ -150,14 +138,14 @@ var _ = Describe("{{ .Resource.Kind }} controller", func() {
},
}
err = k8sClient.Create(ctx, {{ lower .Resource.Kind }})
err = k8sClient.Create(context.TODO, {{ lower .Resource.Kind }})
Expect(err).To(Not(HaveOccurred()))
}
By("Checking if the custom resource was successfully created")
Eventually(func() error {
found := &{{ .Resource.ImportAlias }}.{{ .Resource.Kind }}{}
return k8sClient.Get(ctx, typeNamespaceName, found)
return k8sClient.Get(context.TODO, typeNamespaceName, found)
}, time.Minute, time.Second).Should(Succeed())
By("Reconciling the custom resource created")
Expand All @@ -166,15 +154,15 @@ var _ = Describe("{{ .Resource.Kind }} controller", func() {
Scheme: k8sClient.Scheme(),
}
_, err = {{ lower .Resource.Kind }}Reconciler.Reconcile(ctx, reconcile.Request{
_, err = {{ lower .Resource.Kind }}Reconciler.Reconcile(context.TODO, reconcile.Request{
NamespacedName: typeNamespaceName,
})
Expect(err).To(Not(HaveOccurred()))
By("Checking if Deployment was successfully created in the reconciliation")
Eventually(func() error {
found := &appsv1.Deployment{}
return k8sClient.Get(ctx, typeNamespaceName, found)
return k8sClient.Get(context.TODO, typeNamespaceName, found)
}, time.Minute, time.Second).Should(Succeed())
By("Checking the latest Status Condition added to the {{ .Resource.Kind }} instance")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,6 @@ var _ = Describe("Busybox controller", func() {
Context("Busybox controller test", func() {

const BusyboxName = "test-busybox"

ctx := context.Background()

namespace := &corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: BusyboxName,
Expand All @@ -51,29 +48,20 @@ var _ = Describe("Busybox controller", func() {
typeNamespaceName := types.NamespacedName{Name: BusyboxName, Namespace: BusyboxName}

BeforeEach(func() {
By("Creating the Namespace to perform the tests")
err := k8sClient.Create(ctx, namespace)
Expect(err).To(Not(HaveOccurred()))

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)
err := k8sClient.Get(context.TODO, 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 @@ -87,14 +75,14 @@ var _ = Describe("Busybox controller", func() {
},
}

err = k8sClient.Create(ctx, busybox)
err = k8sClient.Create(context.TODO, busybox)
Expect(err).To(Not(HaveOccurred()))
}

By("Checking if the custom resource was successfully created")
Eventually(func() error {
found := &examplecomv1alpha1.Busybox{}
return k8sClient.Get(ctx, typeNamespaceName, found)
return k8sClient.Get(context.TODO, typeNamespaceName, found)
}, time.Minute, time.Second).Should(Succeed())

By("Reconciling the custom resource created")
Expand All @@ -103,15 +91,15 @@ var _ = Describe("Busybox controller", func() {
Scheme: k8sClient.Scheme(),
}

_, err = busyboxReconciler.Reconcile(ctx, reconcile.Request{
_, err = busyboxReconciler.Reconcile(context.TODO, reconcile.Request{
NamespacedName: typeNamespaceName,
})
Expect(err).To(Not(HaveOccurred()))

By("Checking if Deployment was successfully created in the reconciliation")
Eventually(func() error {
found := &appsv1.Deployment{}
return k8sClient.Get(ctx, typeNamespaceName, found)
return k8sClient.Get(context.TODO, typeNamespaceName, found)
}, time.Minute, time.Second).Should(Succeed())

By("Checking the latest Status Condition added to the Busybox instance")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,6 @@ var _ = Describe("Memcached controller", func() {
Context("Memcached controller test", func() {

const MemcachedName = "test-memcached"

ctx := context.Background()

namespace := &corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: MemcachedName,
Expand All @@ -51,29 +48,20 @@ var _ = Describe("Memcached controller", func() {
typeNamespaceName := types.NamespacedName{Name: MemcachedName, Namespace: MemcachedName}

BeforeEach(func() {
By("Creating the Namespace to perform the tests")
err := k8sClient.Create(ctx, namespace)
Expect(err).To(Not(HaveOccurred()))

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)
err := k8sClient.Get(context.TODO, 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 @@ -88,14 +76,14 @@ var _ = Describe("Memcached controller", func() {
},
}

err = k8sClient.Create(ctx, memcached)
err = k8sClient.Create(context.TODO, memcached)
Expect(err).To(Not(HaveOccurred()))
}

By("Checking if the custom resource was successfully created")
Eventually(func() error {
found := &examplecomv1alpha1.Memcached{}
return k8sClient.Get(ctx, typeNamespaceName, found)
return k8sClient.Get(context.TODO, typeNamespaceName, found)
}, time.Minute, time.Second).Should(Succeed())

By("Reconciling the custom resource created")
Expand All @@ -104,15 +92,15 @@ var _ = Describe("Memcached controller", func() {
Scheme: k8sClient.Scheme(),
}

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

By("Checking if Deployment was successfully created in the reconciliation")
Eventually(func() error {
found := &appsv1.Deployment{}
return k8sClient.Get(ctx, typeNamespaceName, found)
return k8sClient.Get(context.TODO, typeNamespaceName, found)
}, time.Minute, time.Second).Should(Succeed())

By("Checking the latest Status Condition added to the Memcached instance")
Expand Down

0 comments on commit f2621e4

Please sign in to comment.