2018-02-09 13:37:55 -05:00
|
|
|
package container
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"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"
|
2018-06-11 09:32:11 -04:00
|
|
|
"gotest.tools/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
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create creates a container with the specified options
|
2018-07-09 13:51:13 -04:00
|
|
|
// nolint: golint
|
2018-02-09 13:37:55 -05:00
|
|
|
func Create(t *testing.T, ctx context.Context, client client.APIClient, ops ...func(*TestContainerConfig)) string { // nolint: golint
|
|
|
|
t.Helper()
|
|
|
|
config := &TestContainerConfig{
|
|
|
|
Config: &container.Config{
|
|
|
|
Image: "busybox",
|
|
|
|
Cmd: []string{"top"},
|
|
|
|
},
|
|
|
|
HostConfig: &container.HostConfig{},
|
|
|
|
NetworkingConfig: &network.NetworkingConfig{},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, op := range ops {
|
|
|
|
op(config)
|
|
|
|
}
|
|
|
|
|
|
|
|
c, err := client.ContainerCreate(ctx, config.Config, config.HostConfig, config.NetworkingConfig, config.Name)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2018-02-09 13:37:55 -05:00
|
|
|
|
|
|
|
return c.ID
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run creates and start a container with the specified options
|
2018-07-09 13:51:13 -04:00
|
|
|
// nolint: golint
|
2018-02-09 13:37:55 -05:00
|
|
|
func Run(t *testing.T, ctx context.Context, client client.APIClient, ops ...func(*TestContainerConfig)) string { // nolint: golint
|
|
|
|
t.Helper()
|
|
|
|
id := Create(t, ctx, client, ops...)
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|