Skip to content

Commit

Permalink
K6 module (#1721)
Browse files Browse the repository at this point in the history
* Add modulegen generated code for k6 module

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

* Initial implementation

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

* Address review comments

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

* Remove k6 examples

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

* fix: lint

* Use k6 return code in tests

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

* Use k6 return code to check test result

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

* Add WithCache option

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

* Fix context in tests

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

* Add SetEnvVar option

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

* Add complete usage example

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

* Address lint issues

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

* Reorganize options by name in documentation

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

* Rename option to SetEnvVar

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

* Adress review comments

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

* Copy test script to container

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

* Address review comments

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

* Fix executable example

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

---------

Signed-off-by: Pablo Chacin <pablochacin@gmail.com>
Co-authored-by: Manuel de la Peña <mdelapenya@gmail.com>
  • Loading branch information
pablochacin and mdelapenya committed Oct 9, 2023
1 parent 3079ce0 commit d2fe780
Show file tree
Hide file tree
Showing 15 changed files with 576 additions and 2 deletions.
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
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
76 changes: 76 additions & 0 deletions docs/modules/k6.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
# K6

Not available until the next release of testcontainers-go <a href="https://github.com/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://github.com/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.

#### SetEnvVar

`SetEnvVar` sets an [environment variable](https://k6.io/docs/using-k6/environment-variables/) for the test script using the '--env' command-line flag in the k6 command in the container.

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

#### 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.


```golang
k6.RunContainer(ctx, WithCache("cache"), 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"))
```

#### WithTestScript

Use the `WithTestScript` option to specify the test script to run. The path to the script must be an absolute path. This option copies the script file to the container and pass it to k6's `run` command. At least one `WithTestScript` option must be specified.

```golang
k6.RunContainer(ctx, 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
76 changes: 76 additions & 0 deletions modules/k6/examples_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
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()

// create a container with the httpbin application that will be the target
// for the test script that runs in the k6 container
gcr := testcontainers.GenericContainerRequest{
ProviderType: testcontainers.ProviderDocker,
ContainerRequest: testcontainers.ContainerRequest{
Image: "kennethreitz/httpbin",
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 %w", err))
}

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

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

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

// run the httpbin.js test scripts passing the IP address the httpbin container
k6, err := k6.RunContainer(
ctx,
k6.WithTestScript(absPath),
k6.SetEnvVar("HTTPBIN", httpbinIP),
)
if err != nil {
panic(fmt.Errorf("failed to start k6 container: %w", err))
}

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

// assert the result of the test
state, err := k6.State(ctx)
if err != nil {
panic(err)
}

fmt.Println(state.ExitCode)
// Output: 0
}
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 => ../..

0 comments on commit d2fe780

Please sign in to comment.