Skip to content

Commit

Permalink
docs: add default options to k6 module (testcontainers#1744)
Browse files Browse the repository at this point in the history
* docs: include core options on k6 module

* docs: complete sections

* docs: split usage example by parts

* Apply suggestions from code review

Co-authored-by: pablochacin <[email protected]>

* docs: add warning about k6x image

* docs: simplify

Co-authored-by: pablochacin <[email protected]>

---------

Co-authored-by: pablochacin <[email protected]>
  • Loading branch information
mdelapenya and pablochacin authored Oct 13, 2023
1 parent 9fa3ed4 commit 7381f04
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
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

0 comments on commit 7381f04

Please sign in to comment.