Documentation
¶
Overview ¶
Package conex provides easy to use Docker Integration with Testing.
Index ¶
- Constants
- Variables
- func Logf(t testing.TB, plugin string, f string, args ...any)
- func Main(m *testing.M, opts ...Option)
- func Require(images ...func() string)
- func Run(m *testing.M, opts ...Option) int
- type Cmd
- type Config
- type Container
- type DockerRunner
- type Manager
- type NativeRunner
- type Option
- type Runner
- type RunnerConfig
- type RunnerType
- type TartRunner
Constants ¶
const ( // ConexNetworkName is the name of the Docker network used for conex containers. ConexNetworkName = "conex" // ConexRunnerEnv is the environment variable that indicates we're running inside a conex container. ConexRunnerEnv = "CONEX_INSIDE_DOCKER" )
Variables ¶
var ( // FailReturnCode is used as status code when conex fails to setup during Run. // This does not override the return value of testing.M.Run, only when conex // fails to even testing.M.Run. // // Deprecated: Use OptReturnCode instead. FailReturnCode = 255 // PullImages dictates whether the Manager should attempt to pull images // on run or simply ensure they exist. // // Deprecated: Use OptPullImages instead. PullImages = true // BuildImages dictates whether the Manager should attempt to build images // on run or simply ensure they exist. // // Deprecated: Use OptBuildImages instead. BuildImages = true // GoImage is the Docker image used to run tests inside a container when // using the Docker runner. This should be a Go image that matches your // Go version. Set this before calling Run() if you need a specific version. // Example: "golang:1.21-alpine" // // Deprecated: Use OptGoImage instead. GoImage = "golang:1.22" )
var ErrPortWaitTimedOut = errors.New("wait timeout")
ErrPortWaitTimedOut is returned when Container.Wait reaches maxWait before the port accepts connections.
Functions ¶
func Logf ¶ added in v0.1.1
Logf logs directly to stdout to avoid the test file and line number prefix, providing a cleaner output format. It can be used by plugins to log uniformly.
func Main ¶ added in v0.0.7
Main is a helper that wraps Run in os.Exit, intended to be called from TestMain.
Types ¶
type Cmd ¶ added in v0.0.6
type Cmd struct {
// Path is the path of the command to run.
Path string
// Args holds command line arguments, including the command as Args[0].
Args []string
// Env specifies the environment of the process.
Env []string
// Dir specifies the working directory of the command.
Dir string
// Stdin specifies the process's standard input.
Stdin io.Reader
// Stdout and Stderr specify the process's standard output and error.
Stdout io.Writer
Stderr io.Writer
// contains filtered or unexported fields
}
Cmd represents an external command being prepared or run. It has similar fields and methods to os/exec.Cmd.
func (*Cmd) CombinedOutput ¶ added in v0.0.6
CombinedOutput runs the command and returns its combined standard output and standard error.
type Config ¶
type Config struct {
Image string // Name of the image as it was passed by the operator (e.g. could be symbolic)
Env []string // List of environment variable to set in the container
Entrypoint []string // Entrypoint to run in the container
Cmd []string // Command to run when starting the container
Hostname string // Hostname
Domainname string // Domainname
User string // User that will run the command(s) inside the container, also support user:group
Expose []string // Ports to expose, supports the docker command line style syntax proto/port or just port which defaults to tcp
Privileged bool // Run the container in privileged mode
Binds []string // Volume binds (e.g. "/host/path:/container/path")
}
Config contains the configuration data about a container.
type Container ¶
type Container interface {
ID() string
Name() string
Image() string
Address() string
// Drop stops and removes the container. It is automatically called
// via t.Cleanup, but can be called manually. Calling it multiple
// times is a no-op.
Drop()
Wait(port string, timeout time.Duration) error // Wait for the port to respond to tcp/udp.
// Exec creates a command to run inside the container.
Exec(cmd ...string) *Cmd
Logs(stdout io.Writer, stderr io.Writer) error
}
Container is a simple interface to a docker container.
type DockerRunner ¶ added in v0.0.2
type DockerRunner struct {
// contains filtered or unexported fields
}
DockerRunner runs tests inside a Docker container on the same network as other conex containers. This allows conex to work on systems where container IPs are not directly accessible (e.g., Docker for Mac).
func NewDockerRunner ¶ added in v0.0.2
func NewDockerRunner(config *RunnerConfig) *DockerRunner
NewDockerRunner creates a new Docker runner.
func (*DockerRunner) Box ¶ added in v0.0.2
Box creates a container on the conex network and returns a Container that uses the container name for connections.
func (*DockerRunner) Run ¶ added in v0.0.2
func (r *DockerRunner) Run(m *testing.M) int
Run executes the tests. If we're already inside a Docker container (detected by environment variable), it just runs the tests. Otherwise, it creates a container, mounts the current directory, and runs the tests inside it.
type Manager ¶
type Manager interface {
Run(m *testing.M, images ...string) int
Box(t testing.TB, config *Config) Container
}
Manager is the conex container manager.
type NativeRunner ¶ added in v0.0.2
type NativeRunner struct {
// contains filtered or unexported fields
}
NativeRunner runs tests on the host machine and connects to containers via their IP addresses. This requires native Docker (not Docker for Mac).
func NewNativeRunner ¶ added in v0.0.2
func NewNativeRunner(config *RunnerConfig) *NativeRunner
NewNativeRunner creates a new native runner.
type Option ¶ added in v0.0.4
type Option func(conf *managerConfig)
func OptBuildImages ¶ added in v0.0.4
func OptGoImage ¶ added in v0.0.4
func OptPullImages ¶ added in v0.0.4
func OptRequireImage ¶ added in v0.0.4
func OptReturnCode ¶ added in v0.0.4
func OptRunnerType ¶ added in v0.0.5
func OptRunnerType(runner RunnerType) Option
OptRunnerType allows setting the RunnerType explicitly.
type Runner ¶ added in v0.0.2
type Runner interface {
// Run executes the test suite. The runner is responsible for setting up
// any necessary environment and executing m.Run().
Run(m *testing.M) int
// Box creates a container and returns a Container interface.
// The implementation determines how the container is accessed (direct IP vs network alias).
Box(t testing.TB, conf *Config, name string) Container
}
Runner is an abstraction that allows running tests either natively on the host or inside a Docker container. This enables conex to work on systems where container IPs are not directly accessible from the host (e.g., Docker for Mac).
type RunnerConfig ¶ added in v0.0.2
type RunnerConfig struct {
Client client.APIClient
Name string // prefix for container names
PullImages bool
Images []string
RetCode int
Counter *counter
GoImage string // Go image for running tests in Docker runner
}
RunnerConfig holds configuration for creating a runner.
type RunnerType ¶ added in v0.0.2
type RunnerType string
RunnerType specifies which runner implementation to use.
const ( // RunnerNative runs tests on the host with direct container IP access. // This is the default and requires native Docker. RunnerNative RunnerType = "native" // RunnerDocker runs containers on a shared network, allowing tests to // work on systems where container IPs are not accessible from the host // (e.g., Docker for Mac, Docker Machine). RunnerDocker RunnerType = "docker" )
const ( // RunnerTart runs VMs using Tart virtualization. // Container IPs are directly accessible from the host. RunnerTart RunnerType = "tart" )
type TartRunner ¶ added in v0.0.3
type TartRunner struct {
// contains filtered or unexported fields
}
TartRunner runs tests on the host machine and manages Tart VMs as containers. VMs are cloned from base images and accessed via their direct IP addresses.
func NewTartRunner ¶ added in v0.0.3
func NewTartRunner(config *RunnerConfig) *TartRunner
NewTartRunner creates a new tart runner.