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

disruptors: error out if there are no targets #267

Merged
merged 2 commits into from
Aug 30, 2023
Merged

Conversation

roobre
Copy link
Member

@roobre roobre commented Jul 31, 2023

Description

This PR adds logic to error out if a PodDisruptor or a ServiceDisruptor do not have any targets, which would make injection noop.

To avoid making the error too obscure, I've also added a bit of logic to render a PodSelector as a nicely formatted string.

As a side effect of making the PodDisruptor and ServiceDisruptor constructors return an error, the JS API tests (github.com/grafana/xk6-disruptor/pkg/api) would fail. To work around this, the setup code for the JS API tests now initizlize the K8s fake client with a service and a pod backing it.
Fixes #247.

Checklist:

  • I have performed a self-review of my code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • I have added tests that prove my fix is effective or that my feature works.
  • I have run linter locally (make lint) and all checks pass.
  • I have run tests locally (make test) and all tests pass.
  • I have run relevant e2e test locally (make e2e-xxx for agent, disruptors, kubernetes or cluster related changes)
  • Any dependent changes have been merged and published in downstream modules

pkg/disruptors/pod.go Outdated Show resolved Hide resolved
@pablochacin
Copy link
Collaborator

Unit tests for API fail because the test setup does not initialize the fake Kubernetes with any resource. It should create a service backed by a pod.

However, as the fake Kubernetes does not update the container's status, the disruptor constructor would still fail because the container never gets ready.

The test setup could create the Pod with the agent's ephemeral container already added. This is a valid initial configuration because the disruptor checks if the container exists before injecting it.

@roobre roobre force-pushed the error-no-targets branch 3 times, most recently from 09916a0 to 4a2435d Compare August 28, 2023 16:35
@roobre roobre marked this pull request as ready for review August 28, 2023 16:35
@roobre roobre changed the title wip: disruptors: error out if there are no targets disruptors: error out if there are no targets Aug 28, 2023
@roobre roobre marked this pull request as draft August 28, 2023 16:40
@roobre
Copy link
Member Author

roobre commented Aug 28, 2023

Marking this as draft again as I think this case can be covered by unit tests only. ServiceDisruptor already has one, I will add the missing test for PodDisruptor and remove the e2e tests.

@roobre roobre marked this pull request as ready for review August 29, 2023 12:37
@roobre
Copy link
Member Author

roobre commented Aug 29, 2023

I've removed the E2E tests checking for this case in 8bdaaf3 as I think the unit tests should give enough coverage for this use case.

@pablochacin LMK what you think about this, I'm fine with bringing them back if you think they're useful. Other than that, this should be ready to review.

Comment on lines 77 to 93
if len(p.Select.Labels) != 0 {
str += "including("
for k, v := range p.Select.Labels {
str += fmt.Sprintf("%s=%s, ", k, v)
}
str = strings.TrimSuffix(str, ", ")
str += "), "
}

if len(p.Exclude.Labels) != 0 {
str += "excluding("
for k, v := range p.Exclude.Labels {
str += fmt.Sprintf("%s=%s, ", k, v)
}
str = strings.TrimSuffix(str, ", ")
str += "), "
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

These two blocks seem identical except for the prefix ("excluding", "including"). Maybe it worth using a small function that returns the labels enclosed in the prefix or null is len if 0

Copy link
Member Author

@roobre roobre Aug 30, 2023

Choose a reason for hiding this comment

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

I didn't do this initially because I was afraid that the function declaration boilerplate was going to be larger than just duplicating, but after trying it I actaully think it does look nicer. LMK what think: 521437a

Copy link
Collaborator

Choose a reason for hiding this comment

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

Nice. Looks cleaner.

Copy link
Collaborator

@pablochacin pablochacin left a comment

Choose a reason for hiding this comment

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

LGTM. Just some minor comments/suggestions.

@roobre roobre force-pushed the error-no-targets branch 2 times, most recently from eab43c3 to 521437a Compare August 30, 2023 12:44
@roobre roobre merged commit 66e1404 into main Aug 30, 2023
5 of 6 checks passed
@roobre roobre deleted the error-no-targets branch August 30, 2023 14:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Warn user or fail test if disruptor has no targets
2 participants