Documentation
¶
Index ¶
- func SetEnvVar(variable string, value string) testcontainers.CustomizeRequestOption
- func WithCache() testcontainers.CustomizeRequestOption
- func WithCmdOptions(options ...string) testcontainers.CustomizeRequestOption
- func WithTestScript(scriptPath string) testcontainers.CustomizeRequestOption
- type K6Container
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func SetEnvVar ¶
func SetEnvVar(variable string, value string) testcontainers.CustomizeRequestOption
SetEnvVar adds a '--env' command-line flag to the k6 command in the container for setting an environment variable for the test script.
func WithCache ¶
func WithCache() testcontainers.CustomizeRequestOption
WithCache sets a volume as a cache for building the k6 binary If a volume name is provided in the TC_K6_BUILD_CACHE, this volume is used and it will persist across test sessions. If no value is provided, a volume is created and automatically deleted when the test session ends.
func WithCmdOptions ¶
func WithCmdOptions(options ...string) testcontainers.CustomizeRequestOption
WithCmdOptions pass the given options to the k6 run command
func WithTestScript ¶
func WithTestScript(scriptPath string) testcontainers.CustomizeRequestOption
WithTestScript mounts the given script into the ./test directory in the container and passes it to k6 as the test to run. The path to the script must be an absolute path
Types ¶
type K6Container ¶
type K6Container struct {
testcontainers.Container
}
K6Container represents the K6 container type used in the module
func RunContainer ¶
func RunContainer(ctx context.Context, opts ...testcontainers.ContainerCustomizer) (*K6Container, error)
RunContainer creates an instance of the K6 container type
Example ¶
// runHTTPBin {
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 {
log.Fatalf("failed to start container: %s", err)
}
defer func() {
if err := httpbin.Terminate(ctx); err != nil {
log.Fatalf("failed to terminate container: %s", err)
}
}()
// }
// getHTTPBinIP {
httpbinIP, err := httpbin.ContainerIP(ctx)
if err != nil {
log.Fatalf("failed to get container IP: %s", err) // nolint:gocritic
}
// }
absPath, err := filepath.Abs(filepath.Join("scripts", "httpbin.js"))
if err != nil {
log.Fatalf("failed to get absolute path to test script: %s", err)
}
// runK6Container {
// run the httpbin.js test scripts passing the IP address the httpbin container
k6, err := k6.RunContainer(
ctx,
k6.WithCache(),
k6.WithTestScript(absPath),
k6.SetEnvVar("HTTPBIN", httpbinIP),
)
if err != nil {
log.Fatalf("failed to start container: %s", err)
}
defer func() {
if err := k6.Terminate(ctx); err != nil {
log.Fatalf("failed to terminate container: %s", err)
}
}()
//}
// assert the result of the test
state, err := k6.State(ctx)
if err != nil {
log.Fatalf("failed to get container state: %s", err)
}
fmt.Println(state.ExitCode)
Output: 0