k6

package module
v0.30.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 8, 2024 License: MIT Imports: 12 Imported by: 0

Documentation

Index

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 WithRemoteTestScript added in v0.30.0

func WithRemoteTestScript(d DownloadableFile) testcontainers.CustomizeRequestOption

WithRemoteTestScript takes a RemoteTestFileDescription and copies to container

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

func WithTestScriptReader added in v0.30.0

func WithTestScriptReader(reader io.Reader, scriptBaseName string) testcontainers.CustomizeRequestOption

WithTestScriptReader copies files into the Container using the Reader API The script base name is not a path, neither absolute or relative and should be just the file name of the script

Types

type DownloadableFile added in v0.30.0

type DownloadableFile struct {
	Uri         url.URL
	DownloadDir string
	User        string
	Password    string
}

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

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL