Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ (deploy.image/v1-alpha): improve controller-test reconciliation #2830

Merged
merged 1 commit into from
Jul 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,18 @@ const controllerTestTemplate = `{{ .Boilerplate }}
package {{ if and .MultiGroup .Resource.Group }}{{ .Resource.PackageName }}{{ else }}controllers{{ end }}

import (
"fmt"
"context"
"os"
"time"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"

"time"
"context"

"sigs.k8s.io/controller-runtime/pkg/reconcile"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"

Expand All @@ -77,30 +81,50 @@ import (
)

var _ = Describe("{{ .Resource.Kind }} controller", func() {
Context("{{ .Resource.Kind }} controller test", func() {

// Define utility constants for object names and testing timeouts/durations and intervals.
const (
{{ .Resource.Kind }}Name = "test-{{ lower .Resource.Kind }}"
{{ .Resource.Kind }}Namespace = "default"
)
const {{ .Resource.Kind }}Name = "test-{{ lower .Resource.Kind }}"

Context("{{ .Resource.Kind }} controller test", func() {
It("should create successfully the custom resource for the {{ .Resource.Kind }}", func() {
ctx := context.Background()
ctx := context.Background()

namespace := &corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: {{ .Resource.Kind }}Name,
Namespace: {{ .Resource.Kind }}Name,
},
}

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() {
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, types.NamespacedName{Name: {{ .Resource.Kind }}Name, Namespace: {{ .Resource.Kind }}Namespace}, {{ lower .Resource.Kind }})
err := k8sClient.Get(ctx, typeNamespaceName, {{ lower .Resource.Kind }})
if err != nil && errors.IsNotFound(err) {
// Define a new custom resource
// Let's mock our custom resource at the same way that we would
// apply on the cluster the manifest under config/samples
{{ lower .Resource.Kind }} := &{{ .Resource.ImportAlias }}.{{ .Resource.Kind }}{
TypeMeta: metav1.TypeMeta{
APIVersion: "{{ .Resource.Group }}.{{ .Resource.Domain }}/{{ .Resource.Version }}",
Kind: "{{ .Resource.Kind }}",
},
ObjectMeta: metav1.ObjectMeta{
Name: {{ .Resource.Kind }}Name,
Namespace: {{ .Resource.Kind }}Namespace,
Namespace: namespace.Name,
},
Spec: {{ .Resource.ImportAlias }}.{{ .Resource.Kind }}Spec{
Size: 1,
Expand All @@ -109,24 +133,44 @@ var _ = Describe("{{ .Resource.Kind }} controller", func() {
{{- end }}
},
}
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))

err = k8sClient.Create(ctx, {{ lower .Resource.Kind }})
if err != nil {
Expect(err).To(Not(HaveOccurred()))
}
}

By("Checking with {{ .Resource.Kind }} Kind exist")
By("Checking if the custom resource was successfully crated")
Eventually(func() error {
found := &{{ .Resource.ImportAlias }}.{{ .Resource.Kind }}{}
err = k8sClient.Get(ctx, types.NamespacedName{Name: {{ .Resource.Kind }}Name, Namespace: {{ .Resource.Kind }}Namespace}, found)
err = k8sClient.Get(ctx, typeNamespaceName, found)
if err != nil {
return err
}
return nil
}, time.Minute, time.Second).Should(Succeed())

By("Reconciling the custom resource created")
{{ lower .Resource.Kind }}Reconciler := &{{ .Resource.Kind }}Reconciler{
Client: k8sClient,
Scheme: k8sClient.Scheme(),
}

_, err = {{ lower .Resource.Kind }}Reconciler.Reconcile(ctx, reconcile.Request{
NamespacedName: typeNamespaceName,
})
Expect(err).To(Not(HaveOccurred()))

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

})
`
Original file line number Diff line number Diff line change
Expand Up @@ -17,68 +17,109 @@ limitations under the License.
package controllers

import (
"fmt"

"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"

"context"
"os"
"time"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/reconcile"

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

var _ = Describe("Busybox controller", func() {
Context("Busybox controller test", func() {

// Define utility constants for object names and testing timeouts/durations and intervals.
const (
BusyboxName = "test-busybox"
BusyboxNamespace = "default"
)
const BusyboxName = "test-busybox"

Context("Busybox controller test", func() {
It("should create successfully the custom resource for the Busybox", func() {
ctx := context.Background()
ctx := context.Background()

namespace := &corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: BusyboxName,
Namespace: BusyboxName,
},
}

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() {
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, types.NamespacedName{Name: BusyboxName, Namespace: BusyboxNamespace}, busybox)
err := k8sClient.Get(ctx, typeNamespaceName, busybox)
if err != nil && errors.IsNotFound(err) {
// Define a new custom resource
// Let's mock our custom resource at the same way that we would
// apply on the cluster the manifest under config/samples
busybox := &examplecomv1alpha1.Busybox{
TypeMeta: metav1.TypeMeta{
APIVersion: "example.com.testproject.org/v1alpha1",
Kind: "Busybox",
},
ObjectMeta: metav1.ObjectMeta{
Name: BusyboxName,
Namespace: BusyboxNamespace,
Namespace: namespace.Name,
},
Spec: examplecomv1alpha1.BusyboxSpec{
Size: 1,
},
}
fmt.Fprintf(GinkgoWriter, fmt.Sprintf("Creating a new custom resource in the namespace: %s with the name %s\n", busybox.Namespace, busybox.Name))

err = k8sClient.Create(ctx, busybox)
if err != nil {
Expect(err).To(Not(HaveOccurred()))
}
}

By("Checking with Busybox Kind exist")
By("Checking if the custom resource was successfully crated")
Eventually(func() error {
found := &examplecomv1alpha1.Busybox{}
err = k8sClient.Get(ctx, types.NamespacedName{Name: BusyboxName, Namespace: BusyboxNamespace}, found)
err = k8sClient.Get(ctx, typeNamespaceName, found)
if err != nil {
return err
}
return nil
}, time.Minute, time.Second).Should(Succeed())

By("Reconciling the custom resource created")
busyboxReconciler := &BusyboxReconciler{
Client: k8sClient,
Scheme: k8sClient.Scheme(),
}

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

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

})
Loading