-
Notifications
You must be signed in to change notification settings - Fork 166
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
fix: don't run starter job if TestRun/K6 is paused #458
Open
LCaparelli
wants to merge
1
commit into
grafana:main
Choose a base branch
from
LCaparelli:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
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
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,125 @@ | ||
package controllers | ||
|
||
import ( | ||
"context" | ||
|
||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
batchv1 "k8s.io/api/batch/v1" | ||
k8sErrors "k8s.io/apimachinery/pkg/api/errors" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
|
||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/types" | ||
ctrl "sigs.k8s.io/controller-runtime" | ||
|
||
"github.com/grafana/k6-operator/api/v1alpha1" | ||
) | ||
|
||
var testRunSuiteReconciler *TestRunReconciler | ||
|
||
var _ = Describe("TestRun", func() { | ||
ctx := context.Background() | ||
|
||
testRun := &v1alpha1.TestRun{} | ||
starterJob := &batchv1.Job{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Namespace: "default", | ||
Name: "some-test-starter", | ||
}, | ||
} | ||
|
||
BeforeEach(func() { | ||
testRun = &v1alpha1.TestRun{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Labels: map[string]string{k6CrLabelName: "some-test"}, | ||
Name: "some-test", | ||
Namespace: "default", | ||
}, | ||
} | ||
}) | ||
|
||
AfterEach(func() { | ||
Expect(k8sClient.Delete(ctx, testRun)).Error().ToNot(HaveOccurred()) | ||
err := k8sClient.Delete(ctx, starterJob) | ||
Expect(client.IgnoreNotFound(err)).Error().ToNot(HaveOccurred()) | ||
}) | ||
|
||
When("Reconciling a TestRun that is in 'created' stage and spec.paused is set to 'true'", func() { | ||
It("should prevent the starter job from running", func() { | ||
|
||
testRun.Spec.Paused = "true" | ||
Expect(k8sClient.Create(ctx, testRun)).Error().ToNot(HaveOccurred()) | ||
testRun.Status.Stage = "created" | ||
Expect(k8sClient.Status().Update(ctx, testRun)).Error().ToNot(HaveOccurred()) | ||
|
||
By("returning no error and no requeue when reconciled", func() { | ||
result, err := testRunSuiteReconciler.Reconcile(context.Background(), ctrl.Request{ | ||
NamespacedName: types.NamespacedName{ | ||
Namespace: "default", | ||
Name: "some-test", | ||
}, | ||
}) | ||
Expect(err).ToNot(HaveOccurred()) | ||
Expect(result).To(BeZero()) | ||
}) | ||
|
||
By("not having started the jobs", func() { | ||
err := k8sClient.Get(ctx, client.ObjectKeyFromObject(starterJob), &batchv1.Job{}) | ||
Expect(k8sErrors.IsNotFound(err)).To(BeTrue()) | ||
}) | ||
}) | ||
}) | ||
|
||
When("Reconciling a TestRun that is in 'created' stage and spec.paused isn't set", func() { | ||
It("should prevent the starter job from running", func() { | ||
|
||
testRun.Spec.Paused = "" | ||
Expect(k8sClient.Create(ctx, testRun)).Error().ToNot(HaveOccurred()) | ||
testRun.Status.Stage = "created" | ||
Expect(k8sClient.Status().Update(ctx, testRun)).Error().ToNot(HaveOccurred()) | ||
|
||
By("returning no error and no requeue when reconciled", func() { | ||
result, err := testRunSuiteReconciler.Reconcile(context.Background(), ctrl.Request{ | ||
NamespacedName: types.NamespacedName{ | ||
Namespace: "default", | ||
Name: "some-test", | ||
}, | ||
}) | ||
Expect(err).ToNot(HaveOccurred()) | ||
Expect(result).To(BeZero()) | ||
}) | ||
|
||
By("not having started the jobs", func() { | ||
err := k8sClient.Get(ctx, client.ObjectKeyFromObject(starterJob), &batchv1.Job{}) | ||
Expect(k8sErrors.IsNotFound(err)).To(BeTrue()) | ||
}) | ||
}) | ||
}) | ||
|
||
When("Reconciling a TestRun that is in 'created' stage and spec.paused is set to 'false'", func() { | ||
It("should create the starter job", func() { | ||
|
||
testRun.Spec.Paused = "false" | ||
Expect(k8sClient.Create(ctx, testRun)).Error().ToNot(HaveOccurred()) | ||
testRun.Status.Stage = "created" | ||
Expect(k8sClient.Status().Update(ctx, testRun)).Error().ToNot(HaveOccurred()) | ||
|
||
By("returning no error and no requeue when reconciled", func() { | ||
// we don't care about the result itself for what this test asserts, just the error | ||
_, err := testRunSuiteReconciler.Reconcile(context.Background(), ctrl.Request{ | ||
NamespacedName: types.NamespacedName{ | ||
Namespace: "default", | ||
Name: "some-test", | ||
}, | ||
}) | ||
Expect(err).ToNot(HaveOccurred()) | ||
}) | ||
|
||
By("having started the jobs", func() { | ||
err := k8sClient.Get(ctx, client.ObjectKeyFromObject(starterJob), &batchv1.Job{}) | ||
Expect(err).NotTo(HaveOccurred()) | ||
}) | ||
}) | ||
}) | ||
}) |
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
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.
There is a weird behavior here, where if the string passed by the user in is not a valid boolean, it's interpreted as
false
byParseBool
. I'm just copying the existing behavior from where I extracted this logic, but let me know if we should instead returntrue
, our default if uninformed.I'd argue validation should be placed elsewhere, perhaps as an enum, but I think it'd be best to just make this a bool and avoid all of these shenanigans #455 :-)