Documentation ¶
Overview ¶
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 (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, ...) 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) 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) (*types.StatsJSON, 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 (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 ExecError
- type ExecOpts
- type Network
- 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 ( // AllGPUCapabilities is the environment variable that enables all NVIDIA // GPU capabilities within a container. AllGPUCapabilities = "NVIDIA_DRIVER_CAPABILITIES=all" // DefaultGPUCapabilities is the environment variable that enables default // NVIDIA GPU capabilities within a container. DefaultGPUCapabilities = "NVIDIA_DRIVER_CAPABILITIES=compute,utility" )
Variables ¶
var ErrNoIP = errors.New("no IP available")
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 PrintDockerConfig ¶
func PrintDockerConfig()
PrintDockerConfig prints the whole Docker configuration file to the log.
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) 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) 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) OutputStreams ¶
OutputStreams gets the container's stdout and stderr streams separately.
func (*Container) RootDirectory ¶
RootDirectory returns an educated guess about the container's root directory.
func (*Container) SandboxPid ¶
SandboxPid returns the container's pid.
func (*Container) SpawnProcess ¶
SpawnProcess is analogous to 'docker run -it'. It returns a process which represents the root process.
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.
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 ExecError ¶
type ExecError struct {
ExitStatus int
}
ExecError is returned when a process terminated with a non-zero exit status. It implements `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) Connect ¶
Connect is analogous to 'docker network connect' with the arguments provided.
type Process ¶
type Process struct {
// contains filtered or unexported fields
}
Process represents a containerized process.
func (*Process) WaitExitStatus ¶
WaitExitStatus until process completes and returns exit status.
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.