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

add tests for main Perses controller #5

Merged
merged 1 commit into from
Jan 15, 2024
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,5 @@ Dockerfile.cross
*.swp
*.swo
*~
.vscode

4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,10 @@ vet: ## Run go vet against code.
test: manifests generate fmt vet envtest ## Run tests.
KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) --bin-dir $(LOCALBIN) -p path)" go test ./... -coverprofile cover.out

.PHONY: lint
lint: ## Run linting.
golangci-lint run

##@ Build

.PHONY: build
Expand Down
21 changes: 11 additions & 10 deletions controllers/configmap_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,33 +50,31 @@ func (r *PersesReconciler) reconcileConfigMap(ctx context.Context, req ctrl.Requ
if err != nil && apierrors.IsNotFound(err) {
cm, err := createPersesConfigMap(r, perses)
if err != nil {
cmlog.Error(err, "Failed to define new ConfigMap resource for perses")
cmlog.WithError(err).Error("Failed to define new ConfigMap resource for perses")

meta.SetStatusCondition(&perses.Status.Conditions, metav1.Condition{Type: common.TypeAvailablePerses,
Status: metav1.ConditionFalse, Reason: "Reconciling",
Message: fmt.Sprintf("Failed to create ConfigMap for the custom resource (%s): (%s)", perses.Name, err)})

if err := r.Status().Update(ctx, perses); err != nil {
cmlog.Error(err, "Failed to update perses status")
cmlog.WithError(err).Error("Failed to update perses status")
return subreconciler.RequeueWithError(err)
}

return subreconciler.RequeueWithError(err)
}

cmlog.Info("Creating a new ConfigMap",
"ConfigMap.Namespace", cm.Namespace, "ConfigMap.Name", cm.Name)
cmlog.Infof("Creating a new ConfigMap: ConfigMap.Namespace %s ConfigMap.Name %s", cm.Namespace, cm.Name)
if err = r.Create(ctx, cm); err != nil {
cmlog.Error(err, "Failed to create new ConfigMap",
"ConfigMap.Namespace", cm.Namespace, "ConfigMap.Name", cm.Name)
cmlog.WithError(err).Errorf("Failed to create new ConfigMap: ConfigMap.Namespace %s ConfigMap.Name %s", cm.Namespace, cm.Name)
return subreconciler.RequeueWithError(err)
}

return subreconciler.RequeueWithDelay(time.Minute)
}

if err != nil {
cmlog.Error(err, "Failed to get Deployment")
cmlog.WithError(err).Error("Failed to get ConfigMap")
return subreconciler.RequeueWithError(err)
}

Expand All @@ -85,13 +83,16 @@ func (r *PersesReconciler) reconcileConfigMap(ctx context.Context, req ctrl.Requ

func createPersesConfigMap(r *PersesReconciler, perses *v1alpha1.Perses) (*corev1.ConfigMap, error) {
configName := common.GetConfigName(perses.Name)
ls := common.LabelsForPerses(r.Config.PersesImage, configName, perses.Name)
ls, err := common.LabelsForPerses(r.Config.PersesImage, configName, perses.Name)

if err != nil {
return nil, err
}

persesConfig, err := yaml.Marshal(perses.Spec.Config)

if err != nil {
cmlog.Error(err, "Failed to marshal configmap data",
"ConfigMap.Namespace", perses.Namespace, "ConfigMap.Name", configName)
cmlog.WithError(err).Errorf("Failed to marshal configmap data: ConfigMap.Namespace %s ConfigMap.Name %s", perses.Namespace, configName)
return nil, err
}

Expand Down
5 changes: 4 additions & 1 deletion controllers/deployment_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,10 @@ func (r *PersesReconciler) createPersesDeployment(
perses *v1alpha1.Perses) (*appsv1.Deployment, error) {
configName := common.GetConfigName(perses.Name)

ls := common.LabelsForPerses(r.Config.PersesImage, perses.Name, perses.Name)
ls, err := common.LabelsForPerses(r.Config.PersesImage, perses.Name, perses.Name)
if err != nil {
return nil, err
}

// Get the Operand image
image, err := common.ImageForPerses(r.Config.PersesImage)
Expand Down
11 changes: 6 additions & 5 deletions controllers/perses_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package controllers
import (
"context"
"fmt"
"time"

"github.com/perses/perses-operator/api/v1alpha1"
common "github.com/perses/perses-operator/internal/perses/common"
Expand Down Expand Up @@ -109,7 +110,7 @@ func (r *PersesReconciler) setStatusToUnknown(ctx context.Context, req ctrl.Requ
meta.SetStatusCondition(&perses.Status.Conditions, metav1.Condition{Type: common.TypeAvailablePerses, Status: metav1.ConditionUnknown, Reason: "Reconciling", Message: "Starting reconciliation"})
if err := r.Status().Update(ctx, perses); err != nil {
log.WithError(err).Error("Failed to update Perses status")
return subreconciler.RequeueWithError(err)
return subreconciler.RequeueWithDelayAndError(time.Second*10, err)
}
}

Expand All @@ -132,12 +133,12 @@ func (r *PersesReconciler) addFinalizer(ctx context.Context, req ctrl.Request) (
log.Info("Adding Finalizer for Perses")
if ok := controllerutil.AddFinalizer(perses, common.PersesFinalizer); !ok {
log.Error("Failed to add finalizer into the custom resource")
return subreconciler.Requeue()
return subreconciler.RequeueWithDelay(time.Second * 10)
}

if err := r.Update(ctx, perses); err != nil {
log.WithError(err).Error("Failed to update custom resource to add finalizer")
return subreconciler.RequeueWithError(err)
return subreconciler.RequeueWithDelayAndError(time.Second*10, err)
}
}

Expand Down Expand Up @@ -167,7 +168,7 @@ func (r *PersesReconciler) handleDelete(ctx context.Context, req ctrl.Request) (

if err := r.Status().Update(ctx, perses); err != nil {
log.WithError(err).Error("Failed to update Perses status")
return subreconciler.RequeueWithError(err)
return subreconciler.RequeueWithDelayAndError(time.Second*10, err)
}

// Perform all operations required before remove the finalizer and allow
Expand All @@ -193,7 +194,7 @@ func (r *PersesReconciler) handleDelete(ctx context.Context, req ctrl.Request) (

if err := r.Status().Update(ctx, perses); err != nil {
log.WithError(err).Error("Failed to update Perses status")
return subreconciler.RequeueWithError(err)
return subreconciler.RequeueWithDelayAndError(time.Second*10, err)
}

log.Info("Removing Finalizer for Perses after successfully perform the operations")
Expand Down
202 changes: 202 additions & 0 deletions controllers/perses_controller_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,202 @@
package controllers

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

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
persesv1alpha1 "github.com/perses/perses-operator/api/v1alpha1"
common "github.com/perses/perses-operator/internal/perses/common"
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"
)

var _ = Describe("Perses controller", func() {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a bit torn about this approach.
Thinking if don't make a bit harder for other go developers get starting with tests.
wdyt @Nexucis ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah I forgot to comment about this. I'm thinking the same, it's more a php/javascript approach I believe.

The "Golang way" is more about creating a list of cases and then looping other it to run the tested function.
Like that for example: https://github.com/perses/perses/blob/main/pkg/model/api/v1/metadata_test.go#L24-L47

Or for more complicated use case that requires more struct, then you could have something like that:
https://github.com/perses/perses/blob/main/internal/cli/test/test.go

Which is used like that: https://github.com/perses/perses/blob/main/internal/cli/cmd/apply/apply_test.go

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thnx for your feedback, I followed the docs on testing operators. In which test are run using envtest controller runtime so the eventual state of the operator can be asserted, I believe the goal here is not to test expected outputs of a function as the reconcilers creates resources in a cluster as a side effect. Are there other approaches or suggestions @simonpasquier?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have no strong opinion on the topic. In fact all the operators I've been working on have developed their own home-made framework (based on the Go testing library) to run end-to-end tests. In my current experiences, the end-to-end tests require a running Kube cluster (typically Kind), they setup all required resources (e.g. CRDs, service accounts, roles, bindings, ...) then they run the operator binary and exercise various scenarios.

So I've got no direct experience with the envtest framework and I understand that it can feel a bit daunting at first but at the same time, it may simplify the setup and teardown of the test environment.

Context("Perses controller test", func() {
const PersesName = "test-perses"

ctx := context.Background()

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

typeNamespaceName := types.NamespacedName{Name: PersesName, Namespace: PersesName}
configMapNamespaceName := types.NamespacedName{Name: common.GetConfigName(PersesName), Namespace: PersesName}
persesImage := "perses-dev.io/perses:test"

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("PERSES_IMAGE", persesImage)
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("PERSES_IMAGE")
})

It("should successfully reconcile a custom resource for Perses", func() {
By("Creating the custom resource for the Kind Perses")
perses := &persesv1alpha1.Perses{}
err := k8sClient.Get(ctx, typeNamespaceName, perses)
if err != nil && errors.IsNotFound(err) {
perses := &persesv1alpha1.Perses{
ObjectMeta: metav1.ObjectMeta{
Name: PersesName,
Namespace: namespace.Name,
},
Spec: persesv1alpha1.PersesSpec{
Config: persesv1alpha1.PersesConfig{
Readonly: false,
},
ContainerPort: 8080,
},
}

err = k8sClient.Create(ctx, perses)
Expect(err).To(Not(HaveOccurred()))
}

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

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

// Errors might arise during reconciliation, but we are checking the final state of the resources
_, err = persesReconciler.Reconcile(ctx, reconcile.Request{
NamespacedName: typeNamespaceName,
})

By("Checking if Service was successfully created in the reconciliation")
Eventually(func() error {
found := &corev1.Service{}
err = k8sClient.Get(ctx, typeNamespaceName, found)

if err == nil {
if len(found.Spec.Ports) < 1 {
return fmt.Errorf("The number of ports used in the service is not the one defined in the custom resource")
}
if found.Spec.Ports[0].Port != 8080 {
return fmt.Errorf("The port used in the service is not the one defined in the custom resource")
}
if found.Spec.Ports[0].TargetPort.IntVal != 8080 {
return fmt.Errorf("The target port used in the service is not the one defined in the custom resource")
}
if found.Spec.Selector["app.kubernetes.io/instance"] != PersesName {
return fmt.Errorf("The selector used in the service is not the one defined in the custom resource")
}
}

return err
}, time.Minute, time.Second).Should(Succeed())

By("Checking if ConfigMap was successfully created in the reconciliation")
Eventually(func() error {
found := &corev1.ConfigMap{}
return k8sClient.Get(ctx, configMapNamespaceName, found)
}, time.Minute*3, time.Second).Should(Succeed())

By("Checking if Deployment was successfully created in the reconciliation")
Eventually(func() error {
found := &appsv1.Deployment{}
err = k8sClient.Get(ctx, typeNamespaceName, found)

if err == nil {
if len(found.Spec.Template.Spec.Containers) < 1 {
return fmt.Errorf("The number of containers used in the deployment is not the one expected")
}
if found.Spec.Template.Spec.Containers[0].Image != persesImage {
return fmt.Errorf("The image used in the deployment is not the one expected")
}
if len(found.Spec.Template.Spec.Containers[0].Ports) < 1 && found.Spec.Template.Spec.Containers[0].Ports[0].ContainerPort != 8080 {
return fmt.Errorf("The port used in the deployment is not the one defined in the custom resource")
}
if len(found.Spec.Template.Spec.Containers[0].Args) < 1 && found.Spec.Template.Spec.Containers[0].Args[0] != "--config=/etc/perses/config/config.yaml" {
return fmt.Errorf("The config path used in the deployment is not the one defined in the custom resource")
}
}

return err
}, time.Minute, time.Second).Should(Succeed())

By("Checking the latest Status Condition added to the Perses instance")
Eventually(func() error {
if perses.Status.Conditions != nil && len(perses.Status.Conditions) != 0 {
latestStatusCondition := perses.Status.Conditions[len(perses.Status.Conditions)-1]
expectedLatestStatusCondition := metav1.Condition{Type: common.TypeAvailablePerses,
Status: metav1.ConditionTrue, Reason: "Reconciling",
Message: fmt.Sprintf("Deployment for custom resource (%s) created successfully", perses.Name)}
if latestStatusCondition != expectedLatestStatusCondition {
return fmt.Errorf("The latest status condition added to the perses instance is not as expected")
}
}
return nil
}, time.Minute, time.Second).Should(Succeed())

persesToDelete := &persesv1alpha1.Perses{}
err = k8sClient.Get(ctx, typeNamespaceName, persesToDelete)
Expect(err).To(Not(HaveOccurred()))

By("Deleting the custom resource")
err = k8sClient.Delete(ctx, persesToDelete)
Expect(err).To(Not(HaveOccurred()))

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

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

By("Checking if ConfigMap was successfully deleted in the reconciliation")
Eventually(func() error {
found := &corev1.ConfigMap{}
return k8sClient.Get(ctx, configMapNamespaceName, found)
}, time.Minute, time.Second).Should(Succeed())

By("Checking the latest Status Condition added to the Perses instance")
Eventually(func() error {
if perses.Status.Conditions != nil && len(perses.Status.Conditions) != 0 {
latestStatusCondition := perses.Status.Conditions[len(perses.Status.Conditions)-1]
expectedLatestStatusCondition := metav1.Condition{Type: common.TypeAvailablePerses,
Status: metav1.ConditionTrue, Reason: "Finalizing",
Message: fmt.Sprintf("Finalizer operations for custom resource %s name were successfully accomplished", perses.Name)}
if latestStatusCondition != expectedLatestStatusCondition {
return fmt.Errorf("The latest status condition added to the perses instance is not as expected")
}
}
return nil
}, time.Minute, time.Second).Should(Succeed())
})
})
})
6 changes: 5 additions & 1 deletion controllers/service_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,11 @@ func (r *PersesReconciler) reconcileService(ctx context.Context, req ctrl.Reques

func (r *PersesReconciler) createPersesService(
perses *v1alpha1.Perses) (*corev1.Service, error) {
ls := common.LabelsForPerses(r.Config.PersesImage, perses.Name, perses.Name)
ls, err := common.LabelsForPerses(r.Config.PersesImage, perses.Name, perses.Name)

if err != nil {
return nil, err
}

ser := &corev1.Service{
ObjectMeta: metav1.ObjectMeta{
Expand Down
Loading
Loading