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

[Feature]: Provide mechanism for cleaning up build cache in k6 module #1740

Closed
pablochacin opened this issue Oct 9, 2023 · 0 comments · Fixed by #1788
Closed

[Feature]: Provide mechanism for cleaning up build cache in k6 module #1740

pablochacin opened this issue Oct 9, 2023 · 0 comments · Fixed by #1788
Assignees
Labels
feature New functionality or new behaviors on the existing one hacktoberfest Pull Requests accepted for Hacktoberfest.

Comments

@pablochacin
Copy link
Contributor

Problem

The k6 module is built around the k6x project, which allows the dynamic builds of a k6 executable with all the k6 extensions required by a test.

In order to improve the speed of this build process when multiple tests are executed in a test suite, the k6 module's RunContainer method offers the WithCache option, which mounts a volume as a cache for the build process.

Notice that this cache only contains build artifacts and therefore keeping it across tests will not have side effects in the test (as would for example reusing a test database).

This cache volume is created automatically by docker if it doesn't exist, but it is the responsibility of the test to delete it when no longer required. This creates a usability problem as TestContainers presently does not offer any mechanism for managing docker volumes.

Solution

One alternative, suggested by @mdelapenya is to add a label to the cache volume with the test session ID and let the garbage collection process automatically delete it when no longer used.

A test session is defined as:

  • a single "go test" invocation (including flags)
  • a single "go test ./..." invocation (including flags)
  • the execution of a single test or a set of tests using the IDE

Notice that if the cache volume is created and deleted automatically, the k6 module can generate a name automatically based on the session ID. Therefore the WithCache option does not require any argument:

k6.RunContainer(ctx, WithCache(), k6.WithTestScript("/tests/test.js"))

There is, however, another scenario that is not properly covered by the solution described above.

In a local environment, the developer may want to keep the build cache across multiple test sessions. Remember that each execution of go test is a new test session.

Therefore the automatic cleanup of a test after each session will severely affect the execution time of the tests and the developer's productivity.

We consider that there should be a mechanism for overriding the default behavior of auto-cleanup of the build cache volume.

One possibility is to add an environment variable, such as k6_BUILD_CACHE_VOLUME. When this variable is set, the WithCache option will use the volume named in the variable as a build cache and will not add the session ID label, preventing the auto-cleanup at the end of the test session.

Benefit

The lifecycle of the cache volume will be aligned with the life cycle of the test session, allowing reuse in a test suite but preventing the cache volume from being leaked in a CI environment.

Also, using the k6_BUILD_CACHE_VOLUME the developer can reuse the test cache in their local environments and reuse.

Alternatives

Leave the WithCache option as it is presently and add to TestContainers an API for managing docker volumes, to allow the test code to delete the cache volume when no longer required.

Would you like to help contributing this feature?

Yes

@pablochacin pablochacin added the feature New functionality or new behaviors on the existing one label Oct 9, 2023
@mdelapenya mdelapenya added the hacktoberfest Pull Requests accepted for Hacktoberfest. label Oct 18, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New functionality or new behaviors on the existing one hacktoberfest Pull Requests accepted for Hacktoberfest.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants