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

K6 module #1721

Merged
merged 18 commits into from
Oct 9, 2023
Merged
Show file tree
Hide file tree
Changes from 11 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
7 changes: 7 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,13 @@ updates:
day: sunday
open-pull-requests-limit: 3
rebase-strategy: disabled
- package-ecosystem: gomod
directory: /modules/k6
schedule:
interval: monthly
day: sunday
open-pull-requests-limit: 3
rebase-strategy: disabled
- package-ecosystem: gomod
directory: /modules/kafka
schedule:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ jobs:
matrix:
go-version: [1.20.x, 1.x]
platform: [ubuntu-latest, macos-latest]
module: [artemis, clickhouse, compose, couchbase, elasticsearch, gcloud, k3s, kafka, localstack, mariadb, mongodb, mysql, nats, neo4j, postgres, pulsar, rabbitmq, redis, redpanda, vault]
module: [artemis, clickhouse, compose, couchbase, elasticsearch, gcloud, k3s, k6, kafka, localstack, mariadb, mongodb, mysql, nats, neo4j, postgres, pulsar, rabbitmq, redis, redpanda, vault]
uses: ./.github/workflows/ci-test-go.yml
with:
go-version: ${{ matrix.go-version }}
Expand Down
4 changes: 4 additions & 0 deletions .vscode/.testcontainers-go.code-workspace
mdelapenya marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@
"name": "module / k3s",
"path": "../modules/k3s"
},
{
"name": "module / k6",
"path": "../modules/k6"
},
{
"name": "module / kafka",
"path": "../modules/kafka"
Expand Down
85 changes: 85 additions & 0 deletions docs/modules/k6.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
# K6

Not available until the next release of testcontainers-go <a href="https:/testcontainers/testcontainers-go"><span class="tc-version">:material-tag: main</span></a>

## Introduction

The Testcontainers module for K6.


### Using k6 extensions

This module takes advantage of [k6x](https:/szkiba/k6x) to dynamically build a `k6` binary with all the [k6 extensions](https://k6.io/docs/extensions/get-started/explore/) required by the test script.

## Adding this module to your project dependencies

Please run the following command to add the K6 module to your Go dependencies:

```
go get github.com/testcontainers/testcontainers-go/modules/k6
```

## Usage example

<!--codeinclude-->
[Creating a K6 container](../../modules/k6/examples_test.go) inside_block:runK6Container
<!--/codeinclude-->

## Module reference

The K6 module exposes one entrypoint function to run the K6 container, and this function receives two parameters:

```golang
func RunContainer(ctx context.Context, opts ...testcontainers.ContainerCustomizer) (*K6Container, error)
```

- `context.Context`, the Go context.
- `testcontainers.ContainerCustomizer`, a variadic argument for passing options.

### Container Options

When starting the K6 container, you can pass options in a variadic way to configure it.

#### WithTestScript

Use the `WithTestScript` option to specify the test script to run. The path to the script must be an absolute path. This option mounts the script file in the container and pass it to k6's `run` command.

```golang
k6.RunContainer(ctx, k6.WithTestScript("/tests/test.js"))
```

#### WithCmdOptions

Use `WithCmdOptions` to pass a variadic list of strings as [options](https://k6.io/docs/using-k6/k6-options/reference/) to the k6 run command

```golang
k6.RunContainer(ctx, WithCmdOptions("--vus=10", "--duration=30s"), k6.WithTestScript("/tests/test.js"))
```

#### WithCache

Use `WithCache` option to pass a local directory to be used as a [cache for building the k6 binary](https:/szkiba/k6x#cache) inside the `k6` container.
This option can improve considerably the execution time of tests.

!!!tip
In order to use this option, you must override the default user used by the `k6` container with an user that has write permission on the cache directory. Use the `AsUser` option to set this user.

```golang
k6.RunContainer(ctx, WithCache("/tmp/.cache"), AsUser(os.Getuid(), os.Getgid()), k6.WithTestScript("/tests/test.js"))
```

#### AsUser

The `AsUser` sets the user id and group id to be used when running the `k6` container.

```golang
k6.RunContainer(ctx, AsUser(os.Getuid(), os.Getgid()), k6.WithTestScript("/tests/test.js"))
```

#### SetEvVar
pablochacin marked this conversation as resolved.
Show resolved Hide resolved

`WithEnvVar` sets an [environment variable](https://k6.io/docs/using-k6/environment-variables/) for the test script

```golang
k6.RunContainer(ctx, k6.SetEnvVar("URL","test.k6.io"), k6.WithTestScript("/tests/test.js"))
```
1 change: 1 addition & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ nav:
- modules/elasticsearch.md
- modules/gcloud.md
- modules/k3s.md
- modules/k6.md
- modules/kafka.md
- modules/localstack.md
- modules/mariadb.md
Expand Down
5 changes: 5 additions & 0 deletions modules/k6/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
include ../../commons-test.mk

.PHONY: test
test:
$(MAKE) test-k6
73 changes: 73 additions & 0 deletions modules/k6/examples_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package k6_test

import (
"context"
"fmt"
"path/filepath"

"github.com/testcontainers/testcontainers-go"
"github.com/testcontainers/testcontainers-go/modules/k6"
"github.com/testcontainers/testcontainers-go/wait"
)

func ExampleRunContainer() {
// runK6Container {
ctx := context.Background()

gcr := testcontainers.GenericContainerRequest{
mdelapenya marked this conversation as resolved.
Show resolved Hide resolved
ProviderType: testcontainers.ProviderDocker,
ContainerRequest: testcontainers.ContainerRequest{
Image: "kennethreitz/httpbin",
mdelapenya marked this conversation as resolved.
Show resolved Hide resolved
ExposedPorts: []string{
"80",
},
WaitingFor: wait.ForExposedPort(),
},
Started: true,
}
httpbin, err := testcontainers.GenericContainer(ctx, gcr)
if err != nil {
panic(fmt.Errorf("failed to create httpbin container %v", err))

Check failure on line 30 in modules/k6/examples_test.go

View workflow job for this annotation

GitHub Actions / test-modules (1.20.x, ubuntu-latest, k6) / modules/k6/ubuntu-latest/1.20.x

non-wrapping format verb for fmt.Errorf. Use `%w` to format errors (errorlint)
}

defer func() {
if err := httpbin.Terminate(ctx); err != nil {
panic(fmt.Errorf("failed to terminate container: %s", err))

Check failure on line 35 in modules/k6/examples_test.go

View workflow job for this annotation

GitHub Actions / test-modules (1.20.x, ubuntu-latest, k6) / modules/k6/ubuntu-latest/1.20.x

non-wrapping format verb for fmt.Errorf. Use `%w` to format errors (errorlint)
}
}()

httpbinIP, err := httpbin.ContainerIP(ctx)
if err != nil {
panic(fmt.Errorf("failed to get httpbin IP:\n%v", err))

Check failure on line 41 in modules/k6/examples_test.go

View workflow job for this annotation

GitHub Actions / test-modules (1.20.x, ubuntu-latest, k6) / modules/k6/ubuntu-latest/1.20.x

non-wrapping format verb for fmt.Errorf. Use `%w` to format errors (errorlint)
}

absPath, err := filepath.Abs(filepath.Join("scripts", "httpbin.js"))
if err != nil {
panic(fmt.Errorf("failed to get path to test script: %s", err))
}

k6, err := k6.RunContainer(
ctx,
k6.WithTestScript(absPath),
mdelapenya marked this conversation as resolved.
Show resolved Hide resolved
k6.WithEnvVar("HTTPBIN", httpbinIP),
)
if err != nil {
panic(fmt.Errorf("failed to start k6 container: %s", err))
}

defer func() {
if err := k6.Terminate(ctx); err != nil {
panic(fmt.Errorf("failed to terminate container: %s", err))
}
}()

// assert the result of the test
state, err := k6.State(ctx)
if err != nil {
panic(err)
}
if state.ExitCode != 0 {
mdelapenya marked this conversation as resolved.
Show resolved Hide resolved
panic("k6 test failed")
}
//}
}
51 changes: 51 additions & 0 deletions modules/k6/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
module github.com/testcontainers/testcontainers-go/modules/k6

go 1.20

require github.com/testcontainers/testcontainers-go v0.25.0

require (
dario.cat/mergo v1.0.0 // indirect
github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1 // indirect
github.com/Microsoft/go-winio v0.6.1 // indirect
github.com/Microsoft/hcsshim v0.11.0 // indirect
github.com/cenkalti/backoff/v4 v4.2.1 // indirect
github.com/containerd/containerd v1.7.6 // indirect
github.com/cpuguy83/dockercfg v0.3.1 // indirect
github.com/docker/distribution v2.8.2+incompatible // indirect
github.com/docker/docker v24.0.6+incompatible // indirect
github.com/docker/go-connections v0.4.0 // indirect
github.com/docker/go-units v0.5.0 // indirect
github.com/go-ole/go-ole v1.2.6 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/uuid v1.3.1 // indirect
github.com/klauspost/compress v1.16.0 // indirect
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/moby/patternmatcher v0.5.0 // indirect
github.com/moby/sys/sequential v0.5.0 // indirect
github.com/moby/term v0.5.0 // indirect
github.com/morikuni/aec v1.0.0 // indirect
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/opencontainers/image-spec v1.1.0-rc4 // indirect
github.com/opencontainers/runc v1.1.5 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c // indirect
github.com/shirou/gopsutil/v3 v3.23.9 // indirect
github.com/shoenig/go-m1cpu v0.1.6 // indirect
github.com/sirupsen/logrus v1.9.0 // indirect
github.com/tklauser/go-sysconf v0.3.12 // indirect
github.com/tklauser/numcpus v0.6.1 // indirect
github.com/yusufpapurcu/wmi v1.2.3 // indirect
golang.org/x/exp v0.0.0-20230510235704-dd950f8aeaea // indirect
golang.org/x/mod v0.9.0 // indirect
golang.org/x/net v0.15.0 // indirect
golang.org/x/sys v0.12.0 // indirect
golang.org/x/tools v0.7.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20230525234030-28d5490b6b19 // indirect
google.golang.org/grpc v1.57.0 // indirect
google.golang.org/protobuf v1.30.0 // indirect
)

replace github.com/testcontainers/testcontainers-go => ../..
Loading
Loading