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 2 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
8 changes: 8 additions & 0 deletions docs/modules/k6.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,12 @@ The `AsUser` sets the user id and group id to be used when running the `k6` cont

```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"))
```
51 changes: 42 additions & 9 deletions modules/k6/examples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,36 +5,69 @@ import (
"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()

absPath, err := filepath.Abs("./scripts/pass.js")
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(err)
panic(fmt.Errorf("failed to create httpbin container %v", err))
}

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

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

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 := container.Terminate(ctx); err != nil {
panic(err)
if err := k6.Terminate(ctx); err != nil {
panic(fmt.Errorf("failed to terminate container: %s", err))
}
}()

// assert the result of the test
state, err := container.State(ctx)
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(fmt.Errorf("test failed with exit code %d", state.ExitCode))
panic("k6 test failed")
}
// }
//}
}
7 changes: 7 additions & 0 deletions modules/k6/k6.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ func WithCmdOptions(options ...string) testcontainers.CustomizeRequestOption {
}
}

// WithEnvVar sets an environment variable for the test script
func WithEnvVar(variable string, value string) testcontainers.CustomizeRequestOption {
return func(req *testcontainers.GenericContainerRequest) {
req.Cmd = append(req.Cmd, "--env", fmt.Sprintf("%s=%s", variable, value))
}
}

// WithCache uses the given directory as a cache directory building the k6 binary.
// The path to the directory must be an absolute path
// Note: The container must run using an user that
Expand Down
10 changes: 10 additions & 0 deletions modules/k6/scripts/httpbin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { check } from 'k6';
import http from 'k6/http';

export default function () {
const res = http.get(`http://${__ENV.HTTPBIN}/status/200`);

check(res, {
'is status 200': (r) => r.status === 200,
});
}