-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add tests for main perses controller
Signed-off-by: Gabriel Bernal <[email protected]>
- Loading branch information
Showing
12 changed files
with
402 additions
and
177 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,3 +24,5 @@ Dockerfile.cross | |
*.swp | ||
*.swo | ||
*~ | ||
.vscode | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() { | ||
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()) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.