Skip to content

Commit b9398b9

Browse files
authored
Auto-cleanup of k6 build cache (#1788)
* Auto-cleanup of k6 build cache Signed-off-by: Pablo Chacin <[email protected]> * Address review comments Signed-off-by: Pablo Chacin <[email protected]> --------- Signed-off-by: Pablo Chacin <[email protected]>
1 parent e52cd03 commit b9398b9

File tree

4 files changed

+29
-9
lines changed

4 files changed

+29
-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_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.
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: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@ package k6
33
import (
44
"context"
55
"fmt"
6+
"os"
67
"path/filepath"
78

9+
"github.com/docker/docker/api/types/mount"
10+
811
"github.com/testcontainers/testcontainers-go"
912
"github.com/testcontainers/testcontainers-go/wait"
1013
)
@@ -49,13 +52,27 @@ func SetEnvVar(variable string, value string) testcontainers.CustomizeRequestOpt
4952
}
5053
}
5154

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 {
55+
// WithCache sets a volume as a cache for building the k6 binary
56+
// If a volume name is provided in the TC_K6_BUILD_CACHE, this volume is used and it will
57+
// persist across test sessions.
58+
// If no value is provided, a volume is created and automatically deleted when the test session ends.
59+
func WithCache() testcontainers.CustomizeRequestOption {
60+
var volOptions *mount.VolumeOptions
61+
62+
cacheVol := os.Getenv("TC_K6_BUILD_CACHE")
63+
// if no volume is provided, create one and ensure add labels for garbage collection
64+
if cacheVol == "" {
65+
cacheVol = fmt.Sprintf("k6-cache-%s", testcontainers.SessionID())
66+
volOptions = &mount.VolumeOptions{
67+
Labels: testcontainers.GenericLabels(),
68+
}
69+
}
70+
5571
return func(req *testcontainers.GenericContainerRequest) {
5672
mount := testcontainers.ContainerMount{
5773
Source: testcontainers.DockerVolumeMountSource{
58-
Name: cache,
74+
Name: cacheVol,
75+
VolumeOptions: volOptions,
5976
},
6077
Target: "/cache",
6178
}

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)