Merge pull request #46209 from thaJeztah/testutils_WithPIDMode

integration/internal/container: add WithPIDMode option
This commit is contained in:
Sebastiaan van Stijn 2023-08-14 13:55:30 +02:00 committed by GitHub
commit 7b28bc51e6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 35 additions and 30 deletions

View file

@ -12,7 +12,7 @@ import (
"gotest.tools/v3/skip"
)
func TestPidHost(t *testing.T) {
func TestPIDModeHost(t *testing.T) {
skip.If(t, testEnv.DaemonInfo.OSType != "linux")
skip.If(t, testEnv.IsRemoteDaemon())
@ -23,9 +23,7 @@ func TestPidHost(t *testing.T) {
apiClient := testEnv.APIClient()
ctx := context.Background()
cID := container.Run(ctx, t, apiClient, func(c *container.TestContainerConfig) {
c.HostConfig.PidMode = "host"
})
cID := container.Run(ctx, t, apiClient, container.WithPIDMode("host"))
poll.WaitOn(t, container.IsInState(ctx, apiClient, cID, "running"), poll.WithDelay(100*time.Millisecond))
cPid := container.GetContainerNS(ctx, t, apiClient, cID, "pid")
assert.Assert(t, hostPid == cPid)

View file

@ -3,9 +3,9 @@ package container
import (
"strings"
containertypes "github.com/docker/docker/api/types/container"
mounttypes "github.com/docker/docker/api/types/mount"
networktypes "github.com/docker/docker/api/types/network"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/mount"
"github.com/docker/docker/api/types/network"
"github.com/docker/docker/api/types/strslice"
"github.com/docker/go-connections/nat"
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
@ -42,7 +42,7 @@ func WithCmd(cmds ...string) func(*TestContainerConfig) {
// WithNetworkMode sets the network mode of the container
func WithNetworkMode(mode string) func(*TestContainerConfig) {
return func(c *TestContainerConfig) {
c.HostConfig.NetworkMode = containertypes.NetworkMode(mode)
c.HostConfig.NetworkMode = container.NetworkMode(mode)
}
}
@ -71,7 +71,7 @@ func WithWorkingDir(dir string) func(*TestContainerConfig) {
}
// WithMount adds an mount
func WithMount(m mounttypes.Mount) func(*TestContainerConfig) {
func WithMount(m mount.Mount) func(*TestContainerConfig) {
return func(c *TestContainerConfig) {
c.HostConfig.Mounts = append(c.HostConfig.Mounts, m)
}
@ -115,34 +115,34 @@ func WithTmpfs(targetAndOpts string) func(config *TestContainerConfig) {
}
// WithIPv4 sets the specified ip for the specified network of the container
func WithIPv4(network, ip string) func(*TestContainerConfig) {
func WithIPv4(networkName, ip string) func(*TestContainerConfig) {
return func(c *TestContainerConfig) {
if c.NetworkingConfig.EndpointsConfig == nil {
c.NetworkingConfig.EndpointsConfig = map[string]*networktypes.EndpointSettings{}
c.NetworkingConfig.EndpointsConfig = map[string]*network.EndpointSettings{}
}
if v, ok := c.NetworkingConfig.EndpointsConfig[network]; !ok || v == nil {
c.NetworkingConfig.EndpointsConfig[network] = &networktypes.EndpointSettings{}
if v, ok := c.NetworkingConfig.EndpointsConfig[networkName]; !ok || v == nil {
c.NetworkingConfig.EndpointsConfig[networkName] = &network.EndpointSettings{}
}
if c.NetworkingConfig.EndpointsConfig[network].IPAMConfig == nil {
c.NetworkingConfig.EndpointsConfig[network].IPAMConfig = &networktypes.EndpointIPAMConfig{}
if c.NetworkingConfig.EndpointsConfig[networkName].IPAMConfig == nil {
c.NetworkingConfig.EndpointsConfig[networkName].IPAMConfig = &network.EndpointIPAMConfig{}
}
c.NetworkingConfig.EndpointsConfig[network].IPAMConfig.IPv4Address = ip
c.NetworkingConfig.EndpointsConfig[networkName].IPAMConfig.IPv4Address = ip
}
}
// WithIPv6 sets the specified ip6 for the specified network of the container
func WithIPv6(network, ip string) func(*TestContainerConfig) {
func WithIPv6(networkName, ip string) func(*TestContainerConfig) {
return func(c *TestContainerConfig) {
if c.NetworkingConfig.EndpointsConfig == nil {
c.NetworkingConfig.EndpointsConfig = map[string]*networktypes.EndpointSettings{}
c.NetworkingConfig.EndpointsConfig = map[string]*network.EndpointSettings{}
}
if v, ok := c.NetworkingConfig.EndpointsConfig[network]; !ok || v == nil {
c.NetworkingConfig.EndpointsConfig[network] = &networktypes.EndpointSettings{}
if v, ok := c.NetworkingConfig.EndpointsConfig[networkName]; !ok || v == nil {
c.NetworkingConfig.EndpointsConfig[networkName] = &network.EndpointSettings{}
}
if c.NetworkingConfig.EndpointsConfig[network].IPAMConfig == nil {
c.NetworkingConfig.EndpointsConfig[network].IPAMConfig = &networktypes.EndpointIPAMConfig{}
if c.NetworkingConfig.EndpointsConfig[networkName].IPAMConfig == nil {
c.NetworkingConfig.EndpointsConfig[networkName].IPAMConfig = &network.EndpointIPAMConfig{}
}
c.NetworkingConfig.EndpointsConfig[network].IPAMConfig.IPv6Address = ip
c.NetworkingConfig.EndpointsConfig[networkName].IPAMConfig.IPv6Address = ip
}
}
@ -162,7 +162,7 @@ func WithAutoRemove(c *TestContainerConfig) {
func WithPidsLimit(limit *int64) func(*TestContainerConfig) {
return func(c *TestContainerConfig) {
if c.HostConfig == nil {
c.HostConfig = &containertypes.HostConfig{}
c.HostConfig = &container.HostConfig{}
}
c.HostConfig.PidsLimit = limit
}
@ -186,7 +186,7 @@ func WithUser(user string) func(c *TestContainerConfig) {
func WithPrivileged(privileged bool) func(*TestContainerConfig) {
return func(c *TestContainerConfig) {
if c.HostConfig == nil {
c.HostConfig = &containertypes.HostConfig{}
c.HostConfig = &container.HostConfig{}
}
c.HostConfig.Privileged = privileged
}
@ -196,9 +196,9 @@ func WithPrivileged(privileged bool) func(*TestContainerConfig) {
func WithCgroupnsMode(mode string) func(*TestContainerConfig) {
return func(c *TestContainerConfig) {
if c.HostConfig == nil {
c.HostConfig = &containertypes.HostConfig{}
c.HostConfig = &container.HostConfig{}
}
c.HostConfig.CgroupnsMode = containertypes.CgroupnsMode(mode)
c.HostConfig.CgroupnsMode = container.CgroupnsMode(mode)
}
}
@ -220,12 +220,12 @@ func WithPlatform(p *ocispec.Platform) func(*TestContainerConfig) {
// WithWindowsDevice specifies a Windows Device, ala `--device` on the CLI
func WithWindowsDevice(device string) func(*TestContainerConfig) {
return func(c *TestContainerConfig) {
c.HostConfig.Devices = append(c.HostConfig.Devices, containertypes.DeviceMapping{PathOnHost: device})
c.HostConfig.Devices = append(c.HostConfig.Devices, container.DeviceMapping{PathOnHost: device})
}
}
// WithIsolation specifies the isolation technology to apply to the container
func WithIsolation(isolation containertypes.Isolation) func(*TestContainerConfig) {
func WithIsolation(isolation container.Isolation) func(*TestContainerConfig) {
return func(c *TestContainerConfig) {
c.HostConfig.Isolation = isolation
}
@ -248,7 +248,7 @@ func WithRuntime(name string) func(*TestContainerConfig) {
// WithCDIDevices sets the CDI devices to use to start the container
func WithCDIDevices(cdiDeviceNames ...string) func(*TestContainerConfig) {
return func(c *TestContainerConfig) {
request := containertypes.DeviceRequest{
request := container.DeviceRequest{
Driver: "cdi",
DeviceIDs: cdiDeviceNames,
}
@ -273,3 +273,10 @@ func WithSecurityOpt(opt string) func(*TestContainerConfig) {
c.HostConfig.SecurityOpt = append(c.HostConfig.SecurityOpt, opt)
}
}
// WithPIDMode sets the PID-mode for the container.
func WithPIDMode(mode container.PidMode) func(c *TestContainerConfig) {
return func(c *TestContainerConfig) {
c.HostConfig.PidMode = mode
}
}