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

docs: add default options to k6 module #1744

Merged
merged 6 commits into from
Oct 13, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
18 changes: 17 additions & 1 deletion docs/modules/k6.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ Not available until the next release of testcontainers-go <a href="https://githu

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.
Expand All @@ -22,6 +21,9 @@ go get github.com/testcontainers/testcontainers-go/modules/k6
## Usage example

<!--codeinclude-->
[Creating a httpbin application](../../modules/k6/examples_test.go) inside_block:runHTTPBin
[Obtain IP for the httpbin application](../../modules/k6/examples_test.go) inside_block:getHTTPBinIP
[k6 script for testing httpbin](../../modules/k6/scripts/httpbin.js)
[Creating a K6 container](../../modules/k6/examples_test.go) inside_block:runK6Container
<!--/codeinclude-->

Expand All @@ -40,6 +42,16 @@ func RunContainer(ctx context.Context, opts ...testcontainers.ContainerCustomize

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

#### Image

!!! warning
The K6 module uses a `k6x` image to build a `k6` binary with all the required extensions. Therefore, only the [szkiba/k6x](https://hub.docker.com/r/szkiba/k6x) image should be used with this module.

If you need to set a different K6 Docker image, you can use `testcontainers.WithImage` with a valid Docker image
for k6x. E.g. `testcontainers.WithImage("szkiba/k6x:v0.3.1")`.

{% include "../features/common_functional_options.md" %}

#### 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.
Expand Down Expand Up @@ -74,3 +86,7 @@ Use the `WithTestScript` option to specify the test script to run. The path to t
```golang
k6.RunContainer(ctx, k6.WithTestScript("/tests/test.js"))
```

### Container Methods

The K6 container does not expose any method.
6 changes: 5 additions & 1 deletion modules/k6/examples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
)

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

// create a container with the httpbin application that will be the target
Expand All @@ -37,17 +37,21 @@ func ExampleRunContainer() {
panic(fmt.Errorf("failed to terminate container: %w", err))
}
}()
// }

// getHTTPBinIP {
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))
}

// runK6Container {
// run the httpbin.js test scripts passing the IP address the httpbin container
k6, err := k6.RunContainer(
ctx,
Expand Down