2017-05-09 18:35:25 -04:00
|
|
|
package container
|
|
|
|
|
|
|
|
import (
|
2017-08-31 17:07:16 -04:00
|
|
|
"context"
|
2022-06-27 11:16:44 -04:00
|
|
|
"errors"
|
2018-03-06 05:15:18 -05:00
|
|
|
"fmt"
|
2017-08-31 17:07:16 -04:00
|
|
|
"io"
|
2017-05-09 18:35:25 -04:00
|
|
|
"os"
|
2017-08-02 20:31:32 -04:00
|
|
|
"runtime"
|
2018-12-13 16:19:46 -05:00
|
|
|
"sort"
|
2017-08-31 17:07:16 -04:00
|
|
|
"strings"
|
2017-05-09 18:35:25 -04:00
|
|
|
"testing"
|
|
|
|
|
2022-06-27 11:16:44 -04:00
|
|
|
"github.com/docker/cli/cli"
|
2018-12-13 16:19:46 -05:00
|
|
|
"github.com/docker/cli/cli/config/configfile"
|
2017-08-31 17:07:16 -04:00
|
|
|
"github.com/docker/cli/internal/test"
|
2018-03-06 05:15:18 -05:00
|
|
|
"github.com/docker/cli/internal/test/notary"
|
2017-08-31 17:07:16 -04:00
|
|
|
"github.com/docker/docker/api/types"
|
|
|
|
"github.com/docker/docker/api/types/container"
|
|
|
|
"github.com/docker/docker/api/types/network"
|
2017-12-21 16:27:57 -05:00
|
|
|
"github.com/google/go-cmp/cmp"
|
2020-05-27 14:32:22 -04:00
|
|
|
specs "github.com/opencontainers/image-spec/specs-go/v1"
|
2022-06-27 11:16:44 -04:00
|
|
|
"github.com/spf13/pflag"
|
2020-02-22 12:12:14 -05:00
|
|
|
"gotest.tools/v3/assert"
|
|
|
|
is "gotest.tools/v3/assert/cmp"
|
|
|
|
"gotest.tools/v3/fs"
|
|
|
|
"gotest.tools/v3/golden"
|
2017-05-09 18:35:25 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestCIDFileNoOPWithNoFilename(t *testing.T) {
|
|
|
|
file, err := newCIDFile("")
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.NilError(t, err)
|
2017-12-21 16:27:57 -05:00
|
|
|
assert.DeepEqual(t, &cidFile{}, file, cmp.AllowUnexported(cidFile{}))
|
2017-05-09 18:35:25 -04:00
|
|
|
|
2018-03-06 15:13:00 -05:00
|
|
|
assert.NilError(t, file.Write("id"))
|
|
|
|
assert.NilError(t, file.Close())
|
2017-05-09 18:35:25 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewCIDFileWhenFileAlreadyExists(t *testing.T) {
|
|
|
|
tempfile := fs.NewFile(t, "test-cid-file")
|
|
|
|
defer tempfile.Remove()
|
|
|
|
|
|
|
|
_, err := newCIDFile(tempfile.Path())
|
linting: fix incorrectly formatted errors (revive)
cli/compose/interpolation/interpolation.go:102:4: error-strings: error strings should not be capitalized or end with punctuation or a newline (revive)
"invalid interpolation format for %s: %#v. You may need to escape any $ with another $.",
^
cli/command/stack/loader/loader.go:30:30: error-strings: error strings should not be capitalized or end with punctuation or a newline (revive)
return nil, errors.Errorf("Compose file contains unsupported options:\n\n%s\n",
^
cli/command/formatter/formatter.go:76:30: error-strings: error strings should not be capitalized or end with punctuation or a newline (revive)
return tmpl, errors.Errorf("Template parsing error: %v\n", err)
^
cli/command/formatter/formatter.go:97:24: error-strings: error strings should not be capitalized or end with punctuation or a newline (revive)
return errors.Errorf("Template parsing error: %v\n", err)
^
cli/command/image/build.go:257:25: error-strings: error strings should not be capitalized or end with punctuation or a newline (revive)
return errors.Errorf("error checking context: '%s'.", err)
^
cli/command/volume/create.go:35:27: error-strings: error strings should not be capitalized or end with punctuation or a newline (revive)
return errors.Errorf("Conflicting options: either specify --name or provide positional arg, not both\n")
^
cli/command/container/create.go:160:24: error-strings: error strings should not be capitalized or end with punctuation or a newline (revive)
return errors.Errorf("failed to remove the CID file '%s': %s \n", cid.path, err)
^
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-03-27 15:13:03 -04:00
|
|
|
assert.ErrorContains(t, err, "container ID file found")
|
2017-05-09 18:35:25 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestCIDFileCloseWithNoWrite(t *testing.T) {
|
|
|
|
tempdir := fs.NewDir(t, "test-cid-file")
|
|
|
|
defer tempdir.Remove()
|
|
|
|
|
|
|
|
path := tempdir.Join("cidfile")
|
|
|
|
file, err := newCIDFile(path)
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Check(t, is.Equal(file.path, path))
|
2017-05-09 18:35:25 -04:00
|
|
|
|
2018-03-06 15:13:00 -05:00
|
|
|
assert.NilError(t, file.Close())
|
2017-05-09 18:35:25 -04:00
|
|
|
_, err = os.Stat(path)
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.Check(t, os.IsNotExist(err))
|
2017-05-09 18:35:25 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestCIDFileCloseWithWrite(t *testing.T) {
|
|
|
|
tempdir := fs.NewDir(t, "test-cid-file")
|
|
|
|
defer tempdir.Remove()
|
|
|
|
|
|
|
|
path := tempdir.Join("cidfile")
|
|
|
|
file, err := newCIDFile(path)
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.NilError(t, err)
|
2017-05-09 18:35:25 -04:00
|
|
|
|
|
|
|
content := "id"
|
2018-03-06 15:13:00 -05:00
|
|
|
assert.NilError(t, file.Write(content))
|
2017-05-09 18:35:25 -04:00
|
|
|
|
2022-02-25 07:05:59 -05:00
|
|
|
actual, err := os.ReadFile(path)
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Check(t, is.Equal(content, string(actual)))
|
2017-05-09 18:35:25 -04:00
|
|
|
|
2018-03-06 15:13:00 -05:00
|
|
|
assert.NilError(t, file.Close())
|
2017-05-09 18:35:25 -04:00
|
|
|
_, err = os.Stat(path)
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.NilError(t, err)
|
2017-05-09 18:35:25 -04:00
|
|
|
}
|
2017-08-31 17:07:16 -04:00
|
|
|
|
2019-03-07 07:34:26 -05:00
|
|
|
func TestCreateContainerImagePullPolicy(t *testing.T) {
|
2017-08-31 17:07:16 -04:00
|
|
|
imageName := "does-not-exist-locally"
|
|
|
|
containerID := "abcdef"
|
|
|
|
config := &containerConfig{
|
|
|
|
Config: &container.Config{
|
|
|
|
Image: imageName,
|
|
|
|
},
|
|
|
|
HostConfig: &container.HostConfig{},
|
|
|
|
}
|
|
|
|
|
2019-03-07 07:34:26 -05:00
|
|
|
cases := []struct {
|
|
|
|
PullPolicy string
|
|
|
|
ExpectedPulls int
|
2022-04-29 13:26:50 -04:00
|
|
|
ExpectedBody container.CreateResponse
|
2019-03-07 07:34:26 -05:00
|
|
|
ExpectedErrMsg string
|
|
|
|
ResponseCounter int
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
PullPolicy: PullImageMissing,
|
|
|
|
ExpectedPulls: 1,
|
2022-04-29 13:26:50 -04:00
|
|
|
ExpectedBody: container.CreateResponse{ID: containerID},
|
2019-03-07 07:34:26 -05:00
|
|
|
}, {
|
|
|
|
PullPolicy: PullImageAlways,
|
|
|
|
ExpectedPulls: 1,
|
2022-04-29 13:26:50 -04:00
|
|
|
ExpectedBody: container.CreateResponse{ID: containerID},
|
2019-03-07 07:34:26 -05:00
|
|
|
ResponseCounter: 1, // This lets us return a container on the first pull
|
|
|
|
}, {
|
|
|
|
PullPolicy: PullImageNever,
|
|
|
|
ExpectedPulls: 0,
|
|
|
|
ExpectedErrMsg: "error fake not found",
|
2019-02-16 18:41:56 -05:00
|
|
|
},
|
|
|
|
}
|
2019-03-07 07:34:26 -05:00
|
|
|
for _, c := range cases {
|
2019-10-29 09:41:38 -04:00
|
|
|
c := c
|
2019-03-07 07:34:26 -05:00
|
|
|
pullCounter := 0
|
|
|
|
|
|
|
|
client := &fakeClient{
|
|
|
|
createContainerFunc: func(
|
|
|
|
config *container.Config,
|
|
|
|
hostConfig *container.HostConfig,
|
|
|
|
networkingConfig *network.NetworkingConfig,
|
2020-05-27 14:32:22 -04:00
|
|
|
platform *specs.Platform,
|
2019-03-07 07:34:26 -05:00
|
|
|
containerName string,
|
2022-04-29 13:26:50 -04:00
|
|
|
) (container.CreateResponse, error) {
|
2019-03-07 07:34:26 -05:00
|
|
|
defer func() { c.ResponseCounter++ }()
|
|
|
|
switch c.ResponseCounter {
|
|
|
|
case 0:
|
2022-04-29 13:26:50 -04:00
|
|
|
return container.CreateResponse{}, fakeNotFound{}
|
2019-03-07 07:34:26 -05:00
|
|
|
default:
|
2022-04-29 13:26:50 -04:00
|
|
|
return container.CreateResponse{ID: containerID}, nil
|
2019-03-07 07:34:26 -05:00
|
|
|
}
|
|
|
|
},
|
|
|
|
imageCreateFunc: func(parentReference string, options types.ImageCreateOptions) (io.ReadCloser, error) {
|
|
|
|
defer func() { pullCounter++ }()
|
2022-02-25 07:05:59 -05:00
|
|
|
return io.NopCloser(strings.NewReader("")), nil
|
2019-03-07 07:34:26 -05:00
|
|
|
},
|
|
|
|
infoFunc: func() (types.Info, error) {
|
2021-04-30 03:31:41 -04:00
|
|
|
return types.Info{IndexServerAddress: "https://indexserver.example.com"}, nil
|
2019-03-07 07:34:26 -05:00
|
|
|
},
|
|
|
|
}
|
|
|
|
cli := test.NewFakeCli(client)
|
2019-03-01 07:00:25 -05:00
|
|
|
body, err := createContainer(context.Background(), cli, config, &createOptions{
|
|
|
|
name: "name",
|
|
|
|
platform: runtime.GOOS,
|
|
|
|
untrusted: true,
|
2019-03-07 07:34:26 -05:00
|
|
|
pull: c.PullPolicy,
|
2019-03-01 07:00:25 -05:00
|
|
|
})
|
|
|
|
|
2019-03-07 07:34:26 -05:00
|
|
|
if c.ExpectedErrMsg != "" {
|
|
|
|
assert.ErrorContains(t, err, c.ExpectedErrMsg)
|
|
|
|
} else {
|
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Check(t, is.DeepEqual(c.ExpectedBody, *body))
|
|
|
|
}
|
2018-11-05 08:12:22 -05:00
|
|
|
|
2019-03-07 07:34:26 -05:00
|
|
|
assert.Check(t, is.Equal(c.ExpectedPulls, pullCounter))
|
|
|
|
}
|
|
|
|
}
|
2022-06-27 11:16:44 -04:00
|
|
|
|
|
|
|
func TestCreateContainerImagePullPolicyInvalid(t *testing.T) {
|
|
|
|
cases := []struct {
|
|
|
|
PullPolicy string
|
|
|
|
ExpectedErrMsg string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
PullPolicy: "busybox:latest",
|
|
|
|
ExpectedErrMsg: `invalid pull option: 'busybox:latest': must be one of "always", "missing" or "never"`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
PullPolicy: "--network=foo",
|
|
|
|
ExpectedErrMsg: `invalid pull option: '--network=foo': must be one of "always", "missing" or "never"`,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tc := range cases {
|
|
|
|
tc := tc
|
|
|
|
t.Run(tc.PullPolicy, func(t *testing.T) {
|
|
|
|
dockerCli := test.NewFakeCli(&fakeClient{})
|
|
|
|
err := runCreate(
|
|
|
|
dockerCli,
|
|
|
|
&pflag.FlagSet{},
|
|
|
|
&createOptions{pull: tc.PullPolicy},
|
|
|
|
&containerOptions{},
|
|
|
|
)
|
|
|
|
|
|
|
|
statusErr := cli.StatusError{}
|
|
|
|
assert.Check(t, errors.As(err, &statusErr))
|
|
|
|
assert.Equal(t, statusErr.StatusCode, 125)
|
|
|
|
assert.Check(t, is.Contains(dockerCli.ErrBuffer().String(), tc.ExpectedErrMsg))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-06 05:15:18 -05:00
|
|
|
func TestNewCreateCommandWithContentTrustErrors(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
args []string
|
|
|
|
expectedError string
|
|
|
|
notaryFunc test.NotaryClientFuncType
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "offline-notary-server",
|
|
|
|
notaryFunc: notary.GetOfflineNotaryRepository,
|
|
|
|
expectedError: "client is offline",
|
|
|
|
args: []string{"image:tag"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "uninitialized-notary-server",
|
|
|
|
notaryFunc: notary.GetUninitializedNotaryRepository,
|
|
|
|
expectedError: "remote trust data does not exist",
|
|
|
|
args: []string{"image:tag"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "empty-notary-server",
|
|
|
|
notaryFunc: notary.GetEmptyTargetsNotaryRepository,
|
|
|
|
expectedError: "No valid trust data for tag",
|
|
|
|
args: []string{"image:tag"},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tc := range testCases {
|
2019-10-29 09:41:38 -04:00
|
|
|
tc := tc
|
2018-03-06 05:15:18 -05:00
|
|
|
cli := test.NewFakeCli(&fakeClient{
|
|
|
|
createContainerFunc: func(config *container.Config,
|
|
|
|
hostConfig *container.HostConfig,
|
|
|
|
networkingConfig *network.NetworkingConfig,
|
2020-05-27 14:32:22 -04:00
|
|
|
platform *specs.Platform,
|
2018-03-06 05:15:18 -05:00
|
|
|
containerName string,
|
2022-04-29 13:26:50 -04:00
|
|
|
) (container.CreateResponse, error) {
|
|
|
|
return container.CreateResponse{}, fmt.Errorf("shouldn't try to pull image")
|
2018-03-06 05:15:18 -05:00
|
|
|
},
|
|
|
|
}, test.EnableContentTrust)
|
|
|
|
cli.SetNotaryClient(tc.notaryFunc)
|
|
|
|
cmd := NewCreateCommand(cli)
|
2022-02-25 07:05:59 -05:00
|
|
|
cmd.SetOut(io.Discard)
|
2018-03-06 05:15:18 -05:00
|
|
|
cmd.SetArgs(tc.args)
|
|
|
|
err := cmd.Execute()
|
|
|
|
assert.ErrorContains(t, err, tc.expectedError)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-13 06:59:35 -05:00
|
|
|
func TestNewCreateCommandWithWarnings(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
args []string
|
|
|
|
warning bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "container-create-without-oom-kill-disable",
|
|
|
|
args: []string{"image:tag"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "container-create-oom-kill-disable-false",
|
|
|
|
args: []string{"--oom-kill-disable=false", "image:tag"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "container-create-oom-kill-without-memory-limit",
|
|
|
|
args: []string{"--oom-kill-disable", "image:tag"},
|
|
|
|
warning: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "container-create-oom-kill-true-without-memory-limit",
|
|
|
|
args: []string{"--oom-kill-disable=true", "image:tag"},
|
|
|
|
warning: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "container-create-oom-kill-true-with-memory-limit",
|
|
|
|
args: []string{"--oom-kill-disable=true", "--memory=100M", "image:tag"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "container-create-localhost-dns",
|
|
|
|
args: []string{"--dns=127.0.0.11", "image:tag"},
|
|
|
|
warning: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "container-create-localhost-dns-ipv6",
|
|
|
|
args: []string{"--dns=::1", "image:tag"},
|
|
|
|
warning: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tc := range testCases {
|
2019-10-29 09:41:38 -04:00
|
|
|
tc := tc
|
2018-12-13 06:59:35 -05:00
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
cli := test.NewFakeCli(&fakeClient{
|
|
|
|
createContainerFunc: func(config *container.Config,
|
|
|
|
hostConfig *container.HostConfig,
|
|
|
|
networkingConfig *network.NetworkingConfig,
|
2020-05-27 14:32:22 -04:00
|
|
|
platform *specs.Platform,
|
2018-12-13 06:59:35 -05:00
|
|
|
containerName string,
|
2022-04-29 13:26:50 -04:00
|
|
|
) (container.CreateResponse, error) {
|
|
|
|
return container.CreateResponse{}, nil
|
2018-12-13 06:59:35 -05:00
|
|
|
},
|
|
|
|
})
|
|
|
|
cmd := NewCreateCommand(cli)
|
2022-02-25 07:05:59 -05:00
|
|
|
cmd.SetOut(io.Discard)
|
2018-12-13 06:59:35 -05:00
|
|
|
cmd.SetArgs(tc.args)
|
|
|
|
err := cmd.Execute()
|
|
|
|
assert.NilError(t, err)
|
|
|
|
if tc.warning {
|
|
|
|
golden.Assert(t, cli.ErrBuffer().String(), tc.name+".golden")
|
|
|
|
} else {
|
|
|
|
assert.Equal(t, cli.ErrBuffer().String(), "")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-13 16:19:46 -05:00
|
|
|
func TestCreateContainerWithProxyConfig(t *testing.T) {
|
|
|
|
expected := []string{
|
|
|
|
"HTTP_PROXY=httpProxy",
|
|
|
|
"http_proxy=httpProxy",
|
|
|
|
"HTTPS_PROXY=httpsProxy",
|
|
|
|
"https_proxy=httpsProxy",
|
|
|
|
"NO_PROXY=noProxy",
|
|
|
|
"no_proxy=noProxy",
|
|
|
|
"FTP_PROXY=ftpProxy",
|
|
|
|
"ftp_proxy=ftpProxy",
|
2021-04-30 05:53:53 -04:00
|
|
|
"ALL_PROXY=allProxy",
|
|
|
|
"all_proxy=allProxy",
|
2018-12-13 16:19:46 -05:00
|
|
|
}
|
|
|
|
sort.Strings(expected)
|
|
|
|
|
|
|
|
cli := test.NewFakeCli(&fakeClient{
|
|
|
|
createContainerFunc: func(config *container.Config,
|
|
|
|
hostConfig *container.HostConfig,
|
|
|
|
networkingConfig *network.NetworkingConfig,
|
2020-05-27 14:32:22 -04:00
|
|
|
platform *specs.Platform,
|
2018-12-13 16:19:46 -05:00
|
|
|
containerName string,
|
2022-04-29 13:26:50 -04:00
|
|
|
) (container.CreateResponse, error) {
|
2018-12-13 16:19:46 -05:00
|
|
|
sort.Strings(config.Env)
|
|
|
|
assert.DeepEqual(t, config.Env, expected)
|
2022-04-29 13:26:50 -04:00
|
|
|
return container.CreateResponse{}, nil
|
2018-12-13 16:19:46 -05:00
|
|
|
},
|
|
|
|
})
|
|
|
|
cli.SetConfigFile(&configfile.ConfigFile{
|
|
|
|
Proxies: map[string]configfile.ProxyConfig{
|
|
|
|
"default": {
|
|
|
|
HTTPProxy: "httpProxy",
|
|
|
|
HTTPSProxy: "httpsProxy",
|
|
|
|
NoProxy: "noProxy",
|
|
|
|
FTPProxy: "ftpProxy",
|
2021-04-30 05:53:53 -04:00
|
|
|
AllProxy: "allProxy",
|
2018-12-13 16:19:46 -05:00
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
cmd := NewCreateCommand(cli)
|
2022-02-25 07:05:59 -05:00
|
|
|
cmd.SetOut(io.Discard)
|
2018-12-13 16:19:46 -05:00
|
|
|
cmd.SetArgs([]string{"image:tag"})
|
|
|
|
err := cmd.Execute()
|
|
|
|
assert.NilError(t, err)
|
|
|
|
}
|
|
|
|
|
2017-08-31 17:07:16 -04:00
|
|
|
type fakeNotFound struct{}
|
|
|
|
|
|
|
|
func (f fakeNotFound) NotFound() bool { return true }
|
|
|
|
func (f fakeNotFound) Error() string { return "error fake not found" }
|