Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Auto-cleanup of k6 build cache #1788

Merged
merged 4 commits into from
Oct 26, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
27 changes: 23 additions & 4 deletions modules/k6/k6.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,14 @@ 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/internal/testcontainersdocker"
"github.com/testcontainers/testcontainers-go/internal/testcontainerssession"
"github.com/testcontainers/testcontainers-go/wait"
)

Expand Down Expand Up @@ -49,13 +54,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.
mdelapenya marked this conversation as resolved.
Show resolved Hide resolved
// If no value is provided, a volume is created and removed when the test session ends.
func WithCache() testcontainers.CustomizeRequestOption {
var volOptions *mount.VolumeOptions

cacheVol := os.Getenv("TC_K6_BUILD_CACHE")
mdelapenya marked this conversation as resolved.
Show resolved Hide resolved
// if no volume is provided, create one and ensure add labels for garbage collection
if cacheVol == "" {
sessionID := testcontainerssession.SessionID()
cacheVol = fmt.Sprintf("k6-cache-%s", sessionID)
volOptions = &mount.VolumeOptions{
Labels: testcontainersdocker.DefaultLabels(sessionID),
mdelapenya marked this conversation as resolved.
Show resolved Hide resolved
}
}

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