package dockerutil
import "gvisor.dev/gvisor/pkg/test/dockerutil"
Package dockerutil is a collection of utility functions.
Package dockerutil provides utility functions for GPU tests.
Index ¶
- Constants
- Variables
- func EnsureDockerExperimentalEnabled()
- func EnsureSupportedDockerVersion()
- func IsGVisorRuntime(ctx context.Context, t *testing.T) (bool, error)
- func NumGPU() int
- func PrintDockerConfig()
- func Runtime() string
- func RuntimeArgs() ([]string, error)
- func RuntimePath() (string, error)
- func Save(logger testutil.Logger, image string, w io.Writer) error
- func UsingSystemdCgroup() (bool, error)
- type Container
- func MakeContainer(ctx context.Context, logger testutil.Logger) *Container
- func MakeContainerWithRuntime(ctx context.Context, logger testutil.Logger, suffix string) *Container
- func MakeNativeContainer(ctx context.Context, logger testutil.Logger) *Container
- func (c *Container) Checkpoint(ctx context.Context, name string) error
- func (c *Container) CheckpointResume(ctx context.Context, name string) error
- func (c *Container) CleanUp(ctx context.Context)
- func (c *Container) ConfigsFrom(ctx context.Context, r RunOpts, args ...string) (*container.Config, *container.HostConfig, *network.NetworkingConfig, error)
- func (c *Container) CopyFiles(opts *RunOpts, target string, sources ...string)
- func (c *Container) Create(ctx context.Context, r RunOpts, args ...string) error
- func (c *Container) CreateFrom(ctx context.Context, profileImage string, conf *container.Config, hostconf *container.HostConfig, netconf *network.NetworkingConfig) error
- func (c *Container) Exec(ctx context.Context, opts ExecOpts, args ...string) (string, error)
- func (c *Container) ExecProcess(ctx context.Context, opts ExecOpts, args ...string) (Process, error)
- func (c *Container) FindIP(ctx context.Context, ipv6 bool) (net.IP, error)
- func (c *Container) FindPort(ctx context.Context, sandboxPort int) (int, error)
- func (c *Container) ID() string
- func (c *Container) Kill(ctx context.Context) error
- func (c *Container) Logs(ctx context.Context) (string, error)
- func (c *Container) MakeLink(target string) string
- func (c *Container) OutputStreams(ctx context.Context) (string, string, error)
- func (c *Container) Pause(ctx context.Context) error
- func (c *Container) Remove(ctx context.Context) error
- func (c *Container) Restore(ctx context.Context, name string) error
- func (c *Container) RestoreInTest(ctx context.Context, t *testing.T, name string)
- func (c *Container) RootDirectory() (string, error)
- func (c *Container) Run(ctx context.Context, r RunOpts, args ...string) (string, error)
- func (c *Container) SandboxPid(ctx context.Context) (int, error)
- func (c *Container) Spawn(ctx context.Context, r RunOpts, args ...string) error
- func (c *Container) SpawnProcess(ctx context.Context, r RunOpts, args ...string) (Process, error)
- func (c *Container) Start(ctx context.Context) error
- func (c *Container) Stats(ctx context.Context) (*container.StatsResponse, error)
- func (c *Container) Status(ctx context.Context) (types.ContainerState, error)
- func (c *Container) Stop(ctx context.Context) error
- func (c *Container) Unpause(ctx context.Context) error
- func (c *Container) Wait(ctx context.Context) error
- func (c *Container) WaitForOutput(ctx context.Context, pattern string, timeout time.Duration) (string, error)
- func (c *Container) WaitForOutputSubmatch(ctx context.Context, pattern string, timeout time.Duration) ([]string, error)
- func (c *Container) WaitTimeout(ctx context.Context, timeout time.Duration) error
- type ContainerPool
- func NewContainerPool(containers []*Container) *ContainerPool
- func (cp *ContainerPool) CleanUp(ctx context.Context)
- func (cp *ContainerPool) Get(ctx context.Context) (*Container, func(), error)
- func (cp *ContainerPool) GetExclusive(ctx context.Context) (*Container, func(), error)
- func (cp *ContainerPool) SetContainerLabel(c *Container, label string)
- func (cp *ContainerPool) String() string
- func (cp *ContainerPool) Utilization() float64
- type CudaVersion
- func MaxSuportedCUDAVersion(ctx context.Context, t *testing.T) (*CudaVersion, error)
- func MustParseCudaVersion(version string) *CudaVersion
- func (c *CudaVersion) IsAtLeast(other *CudaVersion) bool
- func (c *CudaVersion) String() string
- type ExecError
- type ExecOpts
- type Network
- func NewNetwork(ctx context.Context, logger testutil.Logger) *Network
- func (n *Network) Cleanup(ctx context.Context) error
- func (n *Network) Connect(ctx context.Context, container *Container, ipv4, ipv6 string) error
- func (n *Network) Create(ctx context.Context) error
- func (n *Network) Inspect(ctx context.Context) (network.Inspect, error)
- type Process
- func (p *Process) ExitCode(ctx context.Context) (int, error)
- func (p *Process) IsRunning(ctx context.Context) (bool, error)
- func (p *Process) Logs() (string, error)
- func (p *Process) Read() (string, string, error)
- func (p *Process) WaitExitStatus(ctx context.Context) (int, error)
- func (p *Process) Write(timeout time.Duration, buf []byte) (int, error)
- type RunOpts
- type SniffGPUOpts
Constants ¶
const ( // AllGPUCapabilitiesEnv is the environment variable that enables all NVIDIA // GPU capabilities within a container. AllGPUCapabilitiesEnv = "NVIDIA_DRIVER_CAPABILITIES=all" // DefaultGPUCapabilities are the driver capabilities enabled by default. DefaultGPUCapabilities = "compute,utility" )
Variables ¶
ErrNoIP indicates that no IP address is available.
Functions ¶
func EnsureDockerExperimentalEnabled ¶
func EnsureDockerExperimentalEnabled()
EnsureDockerExperimentalEnabled ensures that Docker has experimental features enabled.
func EnsureSupportedDockerVersion ¶
func EnsureSupportedDockerVersion()
EnsureSupportedDockerVersion checks if correct docker is installed.
This logs directly to stderr, as it is typically called from a Main wrapper.
func IsGVisorRuntime ¶
IsGVisorRuntime returns whether the default container runtime used by `dockerutil` is gVisor-based or not.
func NumGPU ¶
func NumGPU() int
NumGPU crudely estimates the number of NVIDIA GPUs on the host.
func PrintDockerConfig ¶
func PrintDockerConfig()
PrintDockerConfig prints the whole Docker configuration file to the log.
func Runtime ¶
func Runtime() string
Runtime returns the value of the flag runtime.
func RuntimeArgs ¶
RuntimeArgs returns the arguments for the current runtime.
func RuntimePath ¶
RuntimePath returns the binary path for the current runtime.
func Save ¶
Save exports a container image to the given Writer.
Note that the writer should be actively consuming the output, otherwise it is not guaranteed that the Save will make any progress and the call may stall indefinitely.
This is called by criutil in order to import imports.
func UsingSystemdCgroup ¶
UsingSystemdCgroup returns true if the docker configuration has the native.cgroupdriver=systemd option set in "exec-opts", or if the system is using cgroupv2, in which case systemd is the default driver.
Types ¶
type Container ¶
type Container struct { Name string // contains filtered or unexported fields }
Container represents a Docker Container allowing user to configure and control as one would with the 'docker' client. Container is backed by the official golang docker API. See: https://pkg.go.dev/github.com/docker/docker.
func MakeContainer ¶
MakeContainer constructs a suitable Container object.
The runtime used is determined by the runtime flag.
Containers will check flags for profiling requests.
func MakeContainerWithRuntime ¶
func MakeContainerWithRuntime(ctx context.Context, logger testutil.Logger, suffix string) *Container
MakeContainerWithRuntime is like MakeContainer, but allows for a runtime to be specified by suffix.
func MakeNativeContainer ¶
MakeNativeContainer constructs a suitable Container object.
The runtime used will be the system default.
Native containers aren't profiled.
func (*Container) Checkpoint ¶
Checkpoint is analogous to 'docker checkpoint'.
func (*Container) CheckpointResume ¶
CheckpointResume is analogous to 'docker checkpoint'.
func (*Container) CleanUp ¶
CleanUp kills and deletes the container (best effort).
func (*Container) ConfigsFrom ¶
func (c *Container) ConfigsFrom(ctx context.Context, r RunOpts, args ...string) (*container.Config, *container.HostConfig, *network.NetworkingConfig, error)
ConfigsFrom returns container configs from RunOpts and args. The caller should call 'CreateFrom' and Start.
func (*Container) CopyFiles ¶
CopyFiles copies in and mounts the given files. They are always ReadOnly.
func (*Container) Create ¶
Create is analogous to 'docker create'.
func (*Container) CreateFrom ¶
func (c *Container) CreateFrom(ctx context.Context, profileImage string, conf *container.Config, hostconf *container.HostConfig, netconf *network.NetworkingConfig) error
CreateFrom creates a container from the given configs.
func (*Container) Exec ¶
Exec creates a process inside the container. If the process exits with a non-zero error code, the error will be of type `ExecError`.
func (*Container) ExecProcess ¶
func (c *Container) ExecProcess(ctx context.Context, opts ExecOpts, args ...string) (Process, error)
ExecProcess creates a process inside the container and returns a process struct for the caller to use.
func (*Container) FindIP ¶
FindIP returns the IP address of the container.
func (*Container) FindPort ¶
FindPort returns the host port that is mapped to 'sandboxPort'.
func (*Container) ID ¶
ID returns the container id.
func (*Container) Kill ¶
Kill kills the container.
func (*Container) Logs ¶
Logs is analogous 'docker logs'.
func (*Container) MakeLink ¶
MakeLink formats a link to add to a RunOpts.
func (*Container) OutputStreams ¶
OutputStreams gets the container's stdout and stderr streams separately.
func (*Container) Pause ¶
Pause is analogous to 'docker pause'.
func (*Container) Remove ¶
Remove is analogous to 'docker rm'.
func (*Container) Restore ¶
Restore is analogous to 'docker start --checkpoint [name]'.
func (*Container) RestoreInTest ¶
RestoreInTest is the same as Restore, except that it handles known issues while testing.
func (*Container) RootDirectory ¶
RootDirectory returns an educated guess about the container's root directory.
func (*Container) Run ¶
Run is analogous to 'docker run'.
func (*Container) SandboxPid ¶
SandboxPid returns the container's pid.
func (*Container) Spawn ¶
Spawn is analogous to 'docker run -d'.
func (*Container) SpawnProcess ¶
SpawnProcess is analogous to 'docker run -it'. It returns a process which represents the root process.
func (*Container) Start ¶
Start is analogous to 'docker start'.
func (*Container) Stats ¶
Stats returns a snapshot of container stats similar to `docker stats`.
func (*Container) Status ¶
Status inspects the container returns its status.
func (*Container) Stop ¶
Stop is analogous to 'docker stop'.
func (*Container) Unpause ¶
Unpause is analogous to 'docker unpause'.
func (*Container) Wait ¶
Wait waits for the container to exit.
func (*Container) WaitForOutput ¶
func (c *Container) WaitForOutput(ctx context.Context, pattern string, timeout time.Duration) (string, error)
WaitForOutput searches container logs for pattern and returns or timesout.
func (*Container) WaitForOutputSubmatch ¶
func (c *Container) WaitForOutputSubmatch(ctx context.Context, pattern string, timeout time.Duration) ([]string, error)
WaitForOutputSubmatch searches container logs for the given pattern or times out. It returns any regexp submatches as well.
func (*Container) WaitTimeout ¶
WaitTimeout waits for the container to exit with a timeout.
type ContainerPool ¶
type ContainerPool struct {
// contains filtered or unexported fields
}
ContainerPool represents a pool of reusable containers. Callers may request a container from the pool, and must release it back when they are done with it.
This is useful for large tests which can `exec` individual test cases inside the same set of reusable containers, to avoid the cost of creating and destroying containers for each test.
It also supports reserving the whole pool ("exclusive"), which locks out all other callers from getting any other container from the pool. This is useful for tests where running in parallel may induce unexpected errors which running serially would not cause. This allows the test to first try to run in parallel, and then re-run failing tests exclusively to make sure their failure is not due to parallel execution.
func NewContainerPool ¶
func NewContainerPool(containers []*Container) *ContainerPool
NewContainerPool returns a new ContainerPool holding the given set of containers.
func (*ContainerPool) CleanUp ¶
func (cp *ContainerPool) CleanUp(ctx context.Context)
CleanUp waits for all containers to be back into the pool, and cleans up each container as soon as it gets back in the pool.
func (*ContainerPool) Get ¶
func (cp *ContainerPool) Get(ctx context.Context) (*Container, func(), error)
Get returns a free container and a function to release it back to the pool.
func (*ContainerPool) GetExclusive ¶
func (cp *ContainerPool) GetExclusive(ctx context.Context) (*Container, func(), error)
GetExclusive ensures all pooled containers are in the pool, reserves all of them, and returns one of them, along with a function to release them all back to the pool.
func (*ContainerPool) SetContainerLabel ¶
func (cp *ContainerPool) SetContainerLabel(c *Container, label string)
SetContainerLabel sets the label for a container. This is printed in `cp.String`, and wiped every time the container changes state. It is useful for the ContainerPool user to track what each container is doing.
func (*ContainerPool) String ¶
func (cp *ContainerPool) String() string
String returns a string representation of the pool. It includes the state of each container, and their user-set label.
func (*ContainerPool) Utilization ¶
func (cp *ContainerPool) Utilization() float64
Utilization returns the utilization of the pool. This is the ratio of the cumulative duration containers have been in a productive state (reserved) divided by the maximum potential productive container-duration since the first container was reserved.
type CudaVersion ¶
CudaVersion represents a cuda version.
func MaxSuportedCUDAVersion ¶
MaxSuportedCUDAVersion returns the maximum supported by the host machine.
func MustParseCudaVersion ¶
func MustParseCudaVersion(version string) *CudaVersion
MustParseCudaVersion returns a new CudaVersion from a string.
func (*CudaVersion) IsAtLeast ¶
func (c *CudaVersion) IsAtLeast(other *CudaVersion) bool
IsAtLeast returns true if the cuda version is at least as new as the other cuda version.
func (*CudaVersion) String ¶
func (c *CudaVersion) String() string
type ExecError ¶
type ExecError struct { ExitStatus int }
ExecError is returned when a process terminated with a non-zero exit status. It implements `error`.
func (*ExecError) Error ¶
Error implements `error.Error`.
type ExecOpts ¶
type ExecOpts struct { // Env are additional environment variables. Env []string // Privileged enables privileged mode. Privileged bool // User is the user to use. User string // Enables Tty and stdin for the created process. UseTTY bool // WorkDir is the working directory of the process. WorkDir string // NoWrap, if true, indicates that no command-line wrapping may be performed // on the command line being exec'd. Otherwise, settings are inherited from // the container. NoWrap bool }
ExecOpts holds arguments for Exec calls.
type Network ¶
Network is a docker network.
func NewNetwork ¶
NewNetwork sets up the struct for a Docker network. Names of networks will be unique.
func (*Network) Cleanup ¶
Cleanup cleans up the docker network.
func (*Network) Connect ¶
Connect is analogous to 'docker network connect' with the arguments provided.
func (*Network) Create ¶
Create is analogous to 'docker network create'.
func (*Network) Inspect ¶
Inspect returns this network's info.
type Process ¶
type Process struct {
// contains filtered or unexported fields
}
Process represents a containerized process.
func (*Process) ExitCode ¶
ExitCode returns the process's exit code.
func (*Process) IsRunning ¶
IsRunning checks if the process is running.
func (*Process) Logs ¶
Logs returns combined stdout/stderr from the process.
func (*Process) Read ¶
Read returns process's stdout and stderr.
func (*Process) WaitExitStatus ¶
WaitExitStatus until process completes and returns exit status.
func (*Process) Write ¶
Write writes buf to the process's stdin.
type RunOpts ¶
type RunOpts struct { // Image is the image relative to images/. This will be mangled // appropriately, to ensure that only first-party images are used. Image string // Memory is the memory limit in bytes. Memory int // Cpus in which to allow execution. ("0", "1", "0-2"). CpusetCpus string // Ports are the ports to be allocated. Ports []int // WorkDir sets the working directory. WorkDir string // ReadOnly sets the read-only flag. ReadOnly bool // Env are additional environment variables. Env []string // User is the user to use. User string // Optional argv to override the ENTRYPOINT specified in the image. Entrypoint []string // Privileged enables privileged mode. Privileged bool // Sets network mode for the container. See container.NetworkMode for types. Several options will // not work w/ gVisor. For example, you can't set the "sandbox" network option for gVisor using // this handle. NetworkMode string // CapAdd are the extra set of capabilities to add. CapAdd []string // CapDrop are the extra set of capabilities to drop. CapDrop []string // Mounts is the list of directories/files to be mounted inside the container. Mounts []mount.Mount // Links is the list of containers to be connected to the container. Links []string // DeviceRequests are device requests on the container itself. DeviceRequests []container.DeviceRequest Devices []container.DeviceMapping // SecurityOpts are security options to set on the container. SecurityOpts []string // contains filtered or unexported fields }
RunOpts are options for running a container.
func GPURunOpts ¶
func GPURunOpts(sniffGPUOpts SniffGPUOpts) (RunOpts, error)
GPURunOpts returns Docker run options with GPU support enabled.
type SniffGPUOpts ¶
type SniffGPUOpts struct { // If set, explains why the sniffer should be disabled for this test. // If unset or empty, the sniffer is enabled. DisableSnifferReason string // If true, the test will not fail even when the workload calls incompatible // ioctls. Useful for debugging. // TODO(b/340955577): Should be converted to a flag and removed from this // struct once all GPU tests have no incompatible ioctls. AllowIncompatibleIoctl bool // The set of GPU capabilities exposed to the container. // If unset, defaults to `DefaultGPUCapabilities`. Capabilities string // contains filtered or unexported fields }
SniffGPUOpts dictates options to sniffer GPU workloads.
func (*SniffGPUOpts) GPUCapabilities ¶
func (sgo *SniffGPUOpts) GPUCapabilities() string
GPUCapabilities returns the set of GPU capabilities meant to be exposed to the container.
Source Files ¶
container.go dockerutil.go exec.go gpu.go network.go profile.go
- Version
- v0.0.0-20250603074042-45108140e4ad (latest)
- Published
- Jun 3, 2025
- Platform
- linux/amd64
- Imports
- 33 packages
- Last checked
- 13 hours ago –
Tools for package owners.