diff --git a/docs/modules/k6.md b/docs/modules/k6.md index 60e739f4b1..ccdfbce8dc 100644 --- a/docs/modules/k6.md +++ b/docs/modules/k6.md @@ -62,13 +62,15 @@ k6.RunContainer(ctx, k6.SetEnvVar("URL","test.k6.io"), k6.WithTestScript("/tests #### WithCache -Use `WithCache` passes a volume to be used as a [cache for building the k6 binary](https://github.com/szkiba/k6x#cache) inside the `k6` container. -This option improves considerably the execution time of test suites that creates multiple `k6` test containers. -If the volume does not exits, it is created. The test is responsible for cleaning up this volume when no longer needed. +Use `WithCache` sets a volume to be used as [cache for building the k6 binary](https://github.com/szkiba/k6x#cache) inside the `k6` container. +This option improves considerably the execution time of test suites that creates multiple `k6` test containers. +By default, a new volume is created and automatically removed when the test session ends. + +This is convenient for example for CI/CD environments. In other cases, such as local testing, it can be convenient to reuse the same cache volume across test sessions.In this cases, the TC_K6_BUILD_CACHE environment variables can used to provide the name of a volume to be used and kept across test sessions. If this volume does not exist, it will be created. ```golang -k6.RunContainer(ctx, WithCache("cache"), k6.WithTestScript("/tests/test.js")) +k6.RunContainer(ctx, WithCache(), k6.WithTestScript("/tests/test.js")) ``` #### WithCmdOptions diff --git a/modules/k6/examples_test.go b/modules/k6/examples_test.go index 8ad313931e..69e79fb6af 100644 --- a/modules/k6/examples_test.go +++ b/modules/k6/examples_test.go @@ -55,6 +55,7 @@ func ExampleRunContainer() { // run the httpbin.js test scripts passing the IP address the httpbin container k6, err := k6.RunContainer( ctx, + k6.WithCache(), k6.WithTestScript(absPath), k6.SetEnvVar("HTTPBIN", httpbinIP), ) diff --git a/modules/k6/k6.go b/modules/k6/k6.go index ee26c433a0..82976b7ea3 100644 --- a/modules/k6/k6.go +++ b/modules/k6/k6.go @@ -3,8 +3,11 @@ package k6 import ( "context" "fmt" + "os" "path/filepath" + "github.com/docker/docker/api/types/mount" + "github.com/testcontainers/testcontainers-go" "github.com/testcontainers/testcontainers-go/wait" ) @@ -49,13 +52,27 @@ func SetEnvVar(variable string, value string) testcontainers.CustomizeRequestOpt } } -// WithCache uses the given volume as a cache for building the k6 binary. -// If the volume does not exists, it is created. -func WithCache(cache string) testcontainers.CustomizeRequestOption { +// WithCache sets a volume as a cache for building the k6 binary +// If a volume name is provided in the TC_K6_BUILD_CACHE, this volume is used and it will +// persist across test sessions. +// If no value is provided, a volume is created and automatically deleted when the test session ends. +func WithCache() testcontainers.CustomizeRequestOption { + var volOptions *mount.VolumeOptions + + cacheVol := os.Getenv("TC_K6_BUILD_CACHE") + // if no volume is provided, create one and ensure add labels for garbage collection + if cacheVol == "" { + cacheVol = fmt.Sprintf("k6-cache-%s", testcontainers.SessionID()) + volOptions = &mount.VolumeOptions{ + Labels: testcontainers.GenericLabels(), + } + } + return func(req *testcontainers.GenericContainerRequest) { mount := testcontainers.ContainerMount{ Source: testcontainers.DockerVolumeMountSource{ - Name: cache, + Name: cacheVol, + VolumeOptions: volOptions, }, Target: "/cache", } diff --git a/modules/k6/k6_test.go b/modules/k6/k6_test.go index 4fc6b45c51..02e8433c70 100644 --- a/modules/k6/k6_test.go +++ b/modules/k6/k6_test.go @@ -34,7 +34,7 @@ func TestK6(t *testing.T) { t.Fatal(err) } - container, err := RunContainer(ctx, WithTestScript(absPath)) + container, err := RunContainer(ctx, WithCache(), WithTestScript(absPath)) if err != nil { t.Fatal(err) }