2018-02-09 13:37:55 -05:00
|
|
|
package container
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2019-05-14 16:01:12 -04:00
|
|
|
"runtime"
|
2018-02-09 13:37:55 -05:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/docker/docker/api/types"
|
|
|
|
"github.com/docker/docker/api/types/container"
|
|
|
|
"github.com/docker/docker/api/types/network"
|
|
|
|
"github.com/docker/docker/client"
|
2020-03-19 16:54:48 -04:00
|
|
|
specs "github.com/opencontainers/image-spec/specs-go/v1"
|
2020-02-07 08:39:24 -05:00
|
|
|
"gotest.tools/v3/assert"
|
2018-02-09 13:37:55 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
// TestContainerConfig holds container configuration struct that
|
|
|
|
// are used in api calls.
|
|
|
|
type TestContainerConfig struct {
|
|
|
|
Name string
|
|
|
|
Config *container.Config
|
|
|
|
HostConfig *container.HostConfig
|
|
|
|
NetworkingConfig *network.NetworkingConfig
|
2020-03-19 16:54:48 -04:00
|
|
|
Platform *specs.Platform
|
2018-02-09 13:37:55 -05:00
|
|
|
}
|
|
|
|
|
2019-03-14 23:44:18 -04:00
|
|
|
// create creates a container with the specified options
|
2019-06-06 07:00:37 -04:00
|
|
|
func create(ctx context.Context, t *testing.T, client client.APIClient, ops ...func(*TestContainerConfig)) (container.ContainerCreateCreatedBody, error) {
|
2018-02-09 13:37:55 -05:00
|
|
|
t.Helper()
|
2019-05-14 16:01:12 -04:00
|
|
|
cmd := []string{"top"}
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
cmd = []string{"sleep", "240"}
|
|
|
|
}
|
2018-02-09 13:37:55 -05:00
|
|
|
config := &TestContainerConfig{
|
|
|
|
Config: &container.Config{
|
|
|
|
Image: "busybox",
|
2019-05-14 16:01:12 -04:00
|
|
|
Cmd: cmd,
|
2018-02-09 13:37:55 -05:00
|
|
|
},
|
|
|
|
HostConfig: &container.HostConfig{},
|
|
|
|
NetworkingConfig: &network.NetworkingConfig{},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, op := range ops {
|
|
|
|
op(config)
|
|
|
|
}
|
|
|
|
|
2020-03-19 16:54:48 -04:00
|
|
|
return client.ContainerCreate(ctx, config.Config, config.HostConfig, config.NetworkingConfig, config.Platform, config.Name)
|
2019-03-14 23:44:18 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create creates a container with the specified options, asserting that there was no error
|
2019-06-06 07:00:37 -04:00
|
|
|
func Create(ctx context.Context, t *testing.T, client client.APIClient, ops ...func(*TestContainerConfig)) string {
|
|
|
|
c, err := create(ctx, t, client, ops...)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2018-02-09 13:37:55 -05:00
|
|
|
|
|
|
|
return c.ID
|
|
|
|
}
|
|
|
|
|
2019-03-14 23:44:18 -04:00
|
|
|
// CreateExpectingErr creates a container, expecting an error with the specified message
|
2019-06-06 07:00:37 -04:00
|
|
|
func CreateExpectingErr(ctx context.Context, t *testing.T, client client.APIClient, errMsg string, ops ...func(*TestContainerConfig)) {
|
|
|
|
_, err := create(ctx, t, client, ops...)
|
2019-03-14 23:44:18 -04:00
|
|
|
assert.ErrorContains(t, err, errMsg)
|
|
|
|
}
|
|
|
|
|
2018-02-09 13:37:55 -05:00
|
|
|
// Run creates and start a container with the specified options
|
2019-06-06 07:15:31 -04:00
|
|
|
func Run(ctx context.Context, t *testing.T, client client.APIClient, ops ...func(*TestContainerConfig)) string {
|
2018-02-09 13:37:55 -05:00
|
|
|
t.Helper()
|
2019-06-06 07:00:37 -04:00
|
|
|
id := Create(ctx, t, client, ops...)
|
2018-02-09 13:37:55 -05:00
|
|
|
|
|
|
|
err := client.ContainerStart(ctx, id, types.ContainerStartOptions{})
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2018-02-09 13:37:55 -05:00
|
|
|
|
|
|
|
return id
|
|
|
|
}
|