[Feature]: Provide mechanism for cleaning up build cache in k6 module #1740
Labels
feature
New functionality or new behaviors on the existing one
hacktoberfest
Pull Requests accepted for Hacktoberfest.
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 theWithCache
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:
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: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, theWithCache
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 toTestContainers
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
The text was updated successfully, but these errors were encountered: