Skip to content

Commit

Permalink
Auto-cleanup of k6 build cache (#1788)
Browse files Browse the repository at this point in the history
* Auto-cleanup of k6 build cache

Signed-off-by: Pablo Chacin <pablochacin@gmail.com>

* Address review comments

Signed-off-by: Pablo Chacin <pablochacin@gmail.com>

---------

Signed-off-by: Pablo Chacin <pablochacin@gmail.com>
  • Loading branch information
pablochacin committed Oct 26, 2023
1 parent e52cd03 commit b9398b9
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 9 deletions.
10 changes: 6 additions & 4 deletions docs/modules/k6.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions modules/k6/examples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
)
Expand Down
25 changes: 21 additions & 4 deletions modules/k6/k6.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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",
}
Expand Down
2 changes: 1 addition & 1 deletion modules/k6/k6_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down

0 comments on commit b9398b9

Please sign in to comment.