Documentation ¶
Overview ¶
Package exec provides a k6 extension allowing users to execute shell commands from k6 scripts.
The package includes support for custom k6 metrics to track various aspects of command execution such as duration, stdout/stderr bytes, and failure rate. The metrics are exposed to k6 and will appear in the summary at the end of a k6 test execution.
The exec package introduces a new global object 'Cmd' in the k6 JavaScript context, which can be used to construct commands. Each 'Cmd' object has an 'Arg' method for adding command-line arguments, an 'Env' method for setting environment variables, and an 'Exec' method for executing the command and returning a promise that resolves with the command's result.
The 'Cmd' object's 'Exec' method runs the command in a non-blocking manner and returns a promise, making it compatible with the k6 event loop.
Command executions are done within the context of the Virtual User (VU) that called the 'Exec' method, and the command will be interrupted if the VU context is cancelled.
Note: The current implementation of the exec package should be considered experimental and potentially unsafe. It allows scripts to execute arbitrary commands on the system running k6, which could be a security risk if k6 is used to run untrusted scripts.
Example usage:
``` import exec from 'k6/x/exec';
export default async function() { let cmd = new exec.Cmd("echo").Arg("Hello, World!"); const result = await cmd.Exec(); console.log(result.stdout); // Output: "Hello, World!" }
```
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Command ¶
type Command struct { Name string // contains filtered or unexported fields }
Command represents a command to be executed.
type CommandResult ¶
type CommandResult struct { ExitCode int `js:"exitCode"` Stdout string `js:"stdout"` Stderr string `js:"stderr"` }
CommandResult holds the result of a command execution.
type CustomMetrics ¶
type CustomMetrics struct { ExecCommandDuration *metrics.Metric ExecCommandsTotal *metrics.Metric ExecCommandStdoutBytesTotal *metrics.Metric ExecCommandStderrBytesTotal *metrics.Metric ExecCommandFailedRate *metrics.Metric }
CustomMetrics are the custom k6 metrics used by xk6-browser.
func RegisterCustomMetrics ¶
func RegisterCustomMetrics(registry *metrics.Registry) *CustomMetrics
RegisterCustomMetrics creates and registers our custom metrics with the k6 VU Registry and returns our internal struct pointer.
type ModuleInstance ¶
type ModuleInstance struct { *Command Metrics *CustomMetrics // contains filtered or unexported fields }
ModuleInstance represents an instance of the JS module.
func (*ModuleInstance) Exports ¶
func (mi *ModuleInstance) Exports() modules.Exports
Exports implements the modules.Instance interface and returns the exports of the JS module.
func (*ModuleInstance) NewCmd ¶
func (mi *ModuleInstance) NewCmd(call goja.ConstructorCall) *goja.Object
NewCmd is the JS constructor for the Cmd object.
type RootModule ¶
type RootModule struct{}
RootModule is the global module instance that will create Client instances for each VU.
func (*RootModule) NewModuleInstance ¶
func (*RootModule) NewModuleInstance(vu modules.VU) modules.Instance
NewModuleInstance implements the modules.Module interface and returns a new instance for each VU.