Documentation
¶
Index ¶
- func SetEnvVar(variable string, value string) testcontainers.CustomizeRequestOption
- func WithCache() testcontainers.CustomizeRequestOption
- func WithCmdOptions(options ...string) testcontainers.CustomizeRequestOptiondeprecated
- func WithRemoteTestScript(d DownloadableFile) testcontainers.CustomizeRequestOption
- func WithTestScript(scriptPath string) testcontainers.CustomizeRequestOption
- func WithTestScriptReader(reader io.Reader, scriptBaseName string) testcontainers.CustomizeRequestOption
- type DownloadableFile
- 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
deprecated
func WithCmdOptions(options ...string) testcontainers.CustomizeRequestOption
Deprecated: use testcontainers.WithCmdArgs instead 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 nor relative and should be just the file name of the script
Types ¶
type DownloadableFile ¶ added in v0.30.0
type K6Container ¶
type K6Container struct {
testcontainers.Container
}
K6Container represents the K6 container type used in the module
func Run ¶ added in v0.32.0
func Run(ctx context.Context, img string, opts ...testcontainers.ContainerCustomizer) (*K6Container, error)
Run 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
httpbin, err := testcontainers.Run(ctx, "kennethreitz/httpbin", testcontainers.WithExposedPorts("80/tcp"), testcontainers.WithWaitStrategy(wait.ForExposedPort()))
defer func() {
if err := testcontainers.TerminateContainer(httpbin); err != nil {
log.Printf("failed to terminate container: %s", err)
}
}()
if err != nil {
log.Printf("failed to start container: %s", err)
return
}
// }
// getHTTPBinIP {
httpbinIP, err := httpbin.ContainerIP(ctx)
if err != nil {
log.Printf("failed to get container IP: %s", err)
return
}
// }
absPath, err := filepath.Abs(filepath.Join("scripts", "httpbin.js"))
if err != nil {
log.Printf("failed to get absolute path to test script: %s", err)
return
}
// runK6Container {
// run the httpbin.js test scripts passing the IP address the httpbin container
k6, err := k6.Run(
ctx,
"szkiba/k6x:v0.3.1",
k6.WithCache(),
k6.WithTestScript(absPath),
k6.SetEnvVar("HTTPBIN", httpbinIP),
)
defer func() {
cacheMount, err := k6.CacheMount(ctx)
if err != nil {
log.Printf("failed to determine cache mount: %s", err)
}
var options []testcontainers.TerminateOption
if cacheMount != "" {
options = append(options, testcontainers.RemoveVolumes(cacheMount))
}
if err = testcontainers.TerminateContainer(k6, options...); err != nil {
log.Printf("failed to terminate container: %s", err)
}
}()
if err != nil {
log.Printf("failed to start container: %s", err)
return
}
//}
// assert the result of the test
state, err := k6.State(ctx)
if err != nil {
log.Printf("failed to get container state: %s", err)
return
}
fmt.Println(state.ExitCode)
Output: 0
func RunContainer
deprecated
func RunContainer(ctx context.Context, opts ...testcontainers.ContainerCustomizer) (*K6Container, error)
Deprecated: use Run instead RunContainer creates an instance of the K6 container type
func (*K6Container) CacheMount ¶ added in v0.34.0
func (k *K6Container) CacheMount(ctx context.Context) (string, error)
CacheMount returns the name of volume used as a cache or an empty string if no cache was found.