Documentation
¶
Overview ¶
Package conex provides easy to use Docker Integration with Testing.
Index ¶
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. FailReturnCode = 255 // PullImages dictates whatever the Manager should attempt to pull the images // on run or simply ensure they exist. // Note: Pulling images may result into updates. PullImages = 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" GoImage = "golang:1.22" )
var ErrPortWaitTimedOut = errors.New("wait timeout")
ErrPortWaitTimedOut is returned when Container.Wait reaches maxWait before the port accepts connections.
Functions ¶
Types ¶
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
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()
Wait(port string, timeout time.Duration) error // Wait for the port to respond to tcp/udp.
}
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 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 *docker.Client
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.