Skip to content

Commit ebeb2f2

Browse files
committed
Auto-cleanup of k6 build cache
Signed-off-by: Pablo Chacin <[email protected]>
1 parent ef189fb commit ebeb2f2

File tree

4 files changed

+30
-9
lines changed

4 files changed

+30
-9
lines changed

docs/modules/k6.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,15 @@ k6.RunContainer(ctx, k6.SetEnvVar("URL","test.k6.io"), k6.WithTestScript("/tests
6262

6363
#### WithCache
6464

65-
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.
66-
This option improves considerably the execution time of test suites that creates multiple `k6` test containers.
67-
If the volume does not exits, it is created. The test is responsible for cleaning up this volume when no longer needed.
65+
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.
66+
This option improves considerably the execution time of test suites that creates multiple `k6` test containers.
6867

68+
By default, a new volume is created and automatically removed when the test session ends.
69+
70+
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_CACHE_VOLUME 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.
6971

7072
```golang
71-
k6.RunContainer(ctx, WithCache("cache"), k6.WithTestScript("/tests/test.js"))
73+
k6.RunContainer(ctx, WithCache(), k6.WithTestScript("/tests/test.js"))
7274
```
7375

7476
#### WithCmdOptions

modules/k6/examples_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ func ExampleRunContainer() {
5555
// run the httpbin.js test scripts passing the IP address the httpbin container
5656
k6, err := k6.RunContainer(
5757
ctx,
58+
k6.WithCache(),
5859
k6.WithTestScript(absPath),
5960
k6.SetEnvVar("HTTPBIN", httpbinIP),
6061
)

modules/k6/k6.go

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,13 @@ package k6
33
import (
44
"context"
55
"fmt"
6+
"os"
67
"path/filepath"
78

9+
"github.com/docker/docker/api/types/mount"
810
"github.com/testcontainers/testcontainers-go"
11+
"github.com/testcontainers/testcontainers-go/internal/testcontainersdocker"
12+
"github.com/testcontainers/testcontainers-go/internal/testcontainerssession"
913
"github.com/testcontainers/testcontainers-go/wait"
1014
)
1115

@@ -49,13 +53,27 @@ func SetEnvVar(variable string, value string) testcontainers.CustomizeRequestOpt
4953
}
5054
}
5155

52-
// WithCache uses the given volume as a cache for building the k6 binary.
53-
// If the volume does not exists, it is created.
54-
func WithCache(cache string) testcontainers.CustomizeRequestOption {
56+
// WithCache sets a volume as a cache for building the k6 binary
57+
// If a volume name is provided in the TC_K6_CACHE_VOLUME, this volume is used.
58+
// If no value is provided, a volume is created and removed when the test session ends.
59+
func WithCache() testcontainers.CustomizeRequestOption {
60+
var volOptions *mount.VolumeOptions
61+
62+
cacheVol := os.Getenv("TC_K6_CACHE_VOLUME")
63+
// if no volume is provided, create one and ensure add labels for garbage collection
64+
if cacheVol == ""{
65+
sessionID := testcontainerssession.SessionID()
66+
cacheVol = fmt.Sprintf("k6-cache-%s", sessionID)
67+
volOptions = &mount.VolumeOptions{
68+
Labels: testcontainersdocker.DefaultLabels(sessionID),
69+
}
70+
}
71+
5572
return func(req *testcontainers.GenericContainerRequest) {
5673
mount := testcontainers.ContainerMount{
5774
Source: testcontainers.DockerVolumeMountSource{
58-
Name: cache,
75+
Name: cacheVol,
76+
VolumeOptions: volOptions,
5977
},
6078
Target: "/cache",
6179
}

modules/k6/k6_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ func TestK6(t *testing.T) {
3434
t.Fatal(err)
3535
}
3636

37-
container, err := RunContainer(ctx, WithTestScript(absPath))
37+
container, err := RunContainer(ctx, WithCache(), WithTestScript(absPath))
3838
if err != nil {
3939
t.Fatal(err)
4040
}

0 commit comments

Comments
 (0)