-
Notifications
You must be signed in to change notification settings - Fork 6
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 ?
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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?There was a problem hiding this comment.
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.