123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515 |
- package container // import "github.com/docker/docker/integration/container"
- import (
- "context"
- "encoding/json"
- "fmt"
- "strconv"
- "testing"
- "time"
- "github.com/docker/docker/api/types"
- "github.com/docker/docker/api/types/container"
- "github.com/docker/docker/api/types/network"
- "github.com/docker/docker/api/types/versions"
- "github.com/docker/docker/client"
- "github.com/docker/docker/errdefs"
- ctr "github.com/docker/docker/integration/internal/container"
- "github.com/docker/docker/internal/test/request"
- "github.com/docker/docker/oci"
- "gotest.tools/assert"
- is "gotest.tools/assert/cmp"
- "gotest.tools/poll"
- "gotest.tools/skip"
- )
- func TestCreateFailsWhenIdentifierDoesNotExist(t *testing.T) {
- defer setupTest(t)()
- client := testEnv.APIClient()
- testCases := []struct {
- doc string
- image string
- expectedError string
- }{
- {
- doc: "image and tag",
- image: "test456:v1",
- expectedError: "No such image: test456:v1",
- },
- {
- doc: "image no tag",
- image: "test456",
- expectedError: "No such image: test456",
- },
- {
- doc: "digest",
- image: "sha256:0cb40641836c461bc97c793971d84d758371ed682042457523e4ae701efeaaaa",
- expectedError: "No such image: sha256:0cb40641836c461bc97c793971d84d758371ed682042457523e4ae701efeaaaa",
- },
- }
- for _, tc := range testCases {
- tc := tc
- t.Run(tc.doc, func(t *testing.T) {
- t.Parallel()
- _, err := client.ContainerCreate(context.Background(),
- &container.Config{Image: tc.image},
- &container.HostConfig{},
- &network.NetworkingConfig{},
- "",
- )
- assert.Check(t, is.ErrorContains(err, tc.expectedError))
- assert.Check(t, errdefs.IsNotFound(err))
- })
- }
- }
- func TestCreateWithInvalidEnv(t *testing.T) {
- defer setupTest(t)()
- client := testEnv.APIClient()
- testCases := []struct {
- env string
- expectedError string
- }{
- {
- env: "",
- expectedError: "invalid environment variable:",
- },
- {
- env: "=",
- expectedError: "invalid environment variable: =",
- },
- {
- env: "=foo",
- expectedError: "invalid environment variable: =foo",
- },
- }
- for index, tc := range testCases {
- tc := tc
- t.Run(strconv.Itoa(index), func(t *testing.T) {
- t.Parallel()
- _, err := client.ContainerCreate(context.Background(),
- &container.Config{
- Image: "busybox",
- Env: []string{tc.env},
- },
- &container.HostConfig{},
- &network.NetworkingConfig{},
- "",
- )
- assert.Check(t, is.ErrorContains(err, tc.expectedError))
- assert.Check(t, errdefs.IsInvalidParameter(err))
- })
- }
- }
- // Test case for #30166 (target was not validated)
- func TestCreateTmpfsMountsTarget(t *testing.T) {
- skip.If(t, testEnv.DaemonInfo.OSType == "windows")
- defer setupTest(t)()
- client := testEnv.APIClient()
- testCases := []struct {
- target string
- expectedError string
- }{
- {
- target: ".",
- expectedError: "mount path must be absolute",
- },
- {
- target: "foo",
- expectedError: "mount path must be absolute",
- },
- {
- target: "/",
- expectedError: "destination can't be '/'",
- },
- {
- target: "//",
- expectedError: "destination can't be '/'",
- },
- }
- for _, tc := range testCases {
- _, err := client.ContainerCreate(context.Background(),
- &container.Config{
- Image: "busybox",
- },
- &container.HostConfig{
- Tmpfs: map[string]string{tc.target: ""},
- },
- &network.NetworkingConfig{},
- "",
- )
- assert.Check(t, is.ErrorContains(err, tc.expectedError))
- assert.Check(t, errdefs.IsInvalidParameter(err))
- }
- }
- func TestCreateWithCustomMaskedPaths(t *testing.T) {
- skip.If(t, testEnv.DaemonInfo.OSType != "linux")
- defer setupTest(t)()
- client := testEnv.APIClient()
- ctx := context.Background()
- testCases := []struct {
- maskedPaths []string
- expected []string
- }{
- {
- maskedPaths: []string{},
- expected: []string{},
- },
- {
- maskedPaths: nil,
- expected: oci.DefaultSpec().Linux.MaskedPaths,
- },
- {
- maskedPaths: []string{"/proc/kcore", "/proc/keys"},
- expected: []string{"/proc/kcore", "/proc/keys"},
- },
- }
- checkInspect := func(t *testing.T, ctx context.Context, name string, expected []string) {
- _, b, err := client.ContainerInspectWithRaw(ctx, name, false)
- assert.NilError(t, err)
- var inspectJSON map[string]interface{}
- err = json.Unmarshal(b, &inspectJSON)
- assert.NilError(t, err)
- cfg, ok := inspectJSON["HostConfig"].(map[string]interface{})
- assert.Check(t, is.Equal(true, ok), name)
- maskedPaths, ok := cfg["MaskedPaths"].([]interface{})
- assert.Check(t, is.Equal(true, ok), name)
- mps := []string{}
- for _, mp := range maskedPaths {
- mps = append(mps, mp.(string))
- }
- assert.DeepEqual(t, expected, mps)
- }
- for i, tc := range testCases {
- name := fmt.Sprintf("create-masked-paths-%d", i)
- config := container.Config{
- Image: "busybox",
- Cmd: []string{"true"},
- }
- hc := container.HostConfig{}
- if tc.maskedPaths != nil {
- hc.MaskedPaths = tc.maskedPaths
- }
- // Create the container.
- c, err := client.ContainerCreate(context.Background(),
- &config,
- &hc,
- &network.NetworkingConfig{},
- name,
- )
- assert.NilError(t, err)
- checkInspect(t, ctx, name, tc.expected)
- // Start the container.
- err = client.ContainerStart(ctx, c.ID, types.ContainerStartOptions{})
- assert.NilError(t, err)
- poll.WaitOn(t, ctr.IsInState(ctx, client, c.ID, "exited"), poll.WithDelay(100*time.Millisecond))
- checkInspect(t, ctx, name, tc.expected)
- }
- }
- func TestCreateWithCapabilities(t *testing.T) {
- skip.If(t, testEnv.DaemonInfo.OSType == "windows", "FIXME: test should be able to run on LCOW")
- skip.If(t, versions.LessThan(testEnv.DaemonAPIVersion(), "1.40"), "Capabilities was added in API v1.40")
- defer setupTest(t)()
- ctx := context.Background()
- clientNew := request.NewAPIClient(t)
- clientOld := request.NewAPIClient(t, client.WithVersion("1.39"))
- testCases := []struct {
- doc string
- hostConfig container.HostConfig
- expected []string
- expectedError string
- oldClient bool
- }{
- {
- doc: "no capabilities",
- hostConfig: container.HostConfig{},
- },
- {
- doc: "empty capabilities",
- hostConfig: container.HostConfig{
- Capabilities: []string{},
- },
- expected: []string{},
- },
- {
- doc: "valid capabilities",
- hostConfig: container.HostConfig{
- Capabilities: []string{"CAP_NET_RAW", "CAP_SYS_CHROOT"},
- },
- expected: []string{"CAP_NET_RAW", "CAP_SYS_CHROOT"},
- },
- {
- doc: "invalid capabilities",
- hostConfig: container.HostConfig{
- Capabilities: []string{"NET_RAW"},
- },
- expectedError: `invalid Capabilities: unknown capability: "NET_RAW"`,
- },
- {
- doc: "duplicate capabilities",
- hostConfig: container.HostConfig{
- Capabilities: []string{"CAP_SYS_NICE", "CAP_SYS_NICE"},
- },
- expected: []string{"CAP_SYS_NICE", "CAP_SYS_NICE"},
- },
- {
- doc: "capabilities API v1.39",
- hostConfig: container.HostConfig{
- Capabilities: []string{"CAP_NET_RAW", "CAP_SYS_CHROOT"},
- },
- expected: nil,
- oldClient: true,
- },
- {
- doc: "empty capadd",
- hostConfig: container.HostConfig{
- Capabilities: []string{"CAP_NET_ADMIN"},
- CapAdd: []string{},
- },
- expected: []string{"CAP_NET_ADMIN"},
- },
- {
- doc: "empty capdrop",
- hostConfig: container.HostConfig{
- Capabilities: []string{"CAP_NET_ADMIN"},
- CapDrop: []string{},
- },
- expected: []string{"CAP_NET_ADMIN"},
- },
- {
- doc: "capadd capdrop",
- hostConfig: container.HostConfig{
- CapAdd: []string{"SYS_NICE", "CAP_SYS_NICE"},
- CapDrop: []string{"SYS_NICE", "CAP_SYS_NICE"},
- },
- },
- {
- doc: "conflict with capadd",
- hostConfig: container.HostConfig{
- Capabilities: []string{"CAP_NET_ADMIN"},
- CapAdd: []string{"SYS_NICE"},
- },
- expectedError: `conflicting options: Capabilities and CapAdd`,
- },
- {
- doc: "conflict with capdrop",
- hostConfig: container.HostConfig{
- Capabilities: []string{"CAP_NET_ADMIN"},
- CapDrop: []string{"NET_RAW"},
- },
- expectedError: `conflicting options: Capabilities and CapDrop`,
- },
- }
- for _, tc := range testCases {
- tc := tc
- t.Run(tc.doc, func(t *testing.T) {
- t.Parallel()
- client := clientNew
- if tc.oldClient {
- client = clientOld
- }
- c, err := client.ContainerCreate(context.Background(),
- &container.Config{Image: "busybox"},
- &tc.hostConfig,
- &network.NetworkingConfig{},
- "",
- )
- if tc.expectedError == "" {
- assert.NilError(t, err)
- ci, err := client.ContainerInspect(ctx, c.ID)
- assert.NilError(t, err)
- assert.Check(t, ci.HostConfig != nil)
- assert.DeepEqual(t, tc.expected, ci.HostConfig.Capabilities)
- } else {
- assert.ErrorContains(t, err, tc.expectedError)
- assert.Check(t, errdefs.IsInvalidParameter(err))
- }
- })
- }
- }
- func TestCreateWithCustomReadonlyPaths(t *testing.T) {
- skip.If(t, testEnv.DaemonInfo.OSType != "linux")
- defer setupTest(t)()
- client := testEnv.APIClient()
- ctx := context.Background()
- testCases := []struct {
- doc string
- readonlyPaths []string
- expected []string
- }{
- {
- readonlyPaths: []string{},
- expected: []string{},
- },
- {
- readonlyPaths: nil,
- expected: oci.DefaultSpec().Linux.ReadonlyPaths,
- },
- {
- readonlyPaths: []string{"/proc/asound", "/proc/bus"},
- expected: []string{"/proc/asound", "/proc/bus"},
- },
- }
- checkInspect := func(t *testing.T, ctx context.Context, name string, expected []string) {
- _, b, err := client.ContainerInspectWithRaw(ctx, name, false)
- assert.NilError(t, err)
- var inspectJSON map[string]interface{}
- err = json.Unmarshal(b, &inspectJSON)
- assert.NilError(t, err)
- cfg, ok := inspectJSON["HostConfig"].(map[string]interface{})
- assert.Check(t, is.Equal(true, ok), name)
- readonlyPaths, ok := cfg["ReadonlyPaths"].([]interface{})
- assert.Check(t, is.Equal(true, ok), name)
- rops := []string{}
- for _, rop := range readonlyPaths {
- rops = append(rops, rop.(string))
- }
- assert.DeepEqual(t, expected, rops)
- }
- for i, tc := range testCases {
- name := fmt.Sprintf("create-readonly-paths-%d", i)
- config := container.Config{
- Image: "busybox",
- Cmd: []string{"true"},
- }
- hc := container.HostConfig{}
- if tc.readonlyPaths != nil {
- hc.ReadonlyPaths = tc.readonlyPaths
- }
- // Create the container.
- c, err := client.ContainerCreate(context.Background(),
- &config,
- &hc,
- &network.NetworkingConfig{},
- name,
- )
- assert.NilError(t, err)
- checkInspect(t, ctx, name, tc.expected)
- // Start the container.
- err = client.ContainerStart(ctx, c.ID, types.ContainerStartOptions{})
- assert.NilError(t, err)
- poll.WaitOn(t, ctr.IsInState(ctx, client, c.ID, "exited"), poll.WithDelay(100*time.Millisecond))
- checkInspect(t, ctx, name, tc.expected)
- }
- }
- func TestCreateWithInvalidHealthcheckParams(t *testing.T) {
- defer setupTest(t)()
- client := testEnv.APIClient()
- ctx := context.Background()
- testCases := []struct {
- doc string
- interval time.Duration
- timeout time.Duration
- retries int
- startPeriod time.Duration
- expectedErr string
- }{
- {
- doc: "test invalid Interval in Healthcheck: less than 0s",
- interval: -10 * time.Millisecond,
- timeout: time.Second,
- retries: 1000,
- expectedErr: fmt.Sprintf("Interval in Healthcheck cannot be less than %s", container.MinimumDuration),
- },
- {
- doc: "test invalid Interval in Healthcheck: larger than 0s but less than 1ms",
- interval: 500 * time.Microsecond,
- timeout: time.Second,
- retries: 1000,
- expectedErr: fmt.Sprintf("Interval in Healthcheck cannot be less than %s", container.MinimumDuration),
- },
- {
- doc: "test invalid Timeout in Healthcheck: less than 1ms",
- interval: time.Second,
- timeout: -100 * time.Millisecond,
- retries: 1000,
- expectedErr: fmt.Sprintf("Timeout in Healthcheck cannot be less than %s", container.MinimumDuration),
- },
- {
- doc: "test invalid Retries in Healthcheck: less than 0",
- interval: time.Second,
- timeout: time.Second,
- retries: -10,
- expectedErr: "Retries in Healthcheck cannot be negative",
- },
- {
- doc: "test invalid StartPeriod in Healthcheck: not 0 and less than 1ms",
- interval: time.Second,
- timeout: time.Second,
- retries: 1000,
- startPeriod: 100 * time.Microsecond,
- expectedErr: fmt.Sprintf("StartPeriod in Healthcheck cannot be less than %s", container.MinimumDuration),
- },
- }
- for _, tc := range testCases {
- tc := tc
- t.Run(tc.doc, func(t *testing.T) {
- t.Parallel()
- cfg := container.Config{
- Image: "busybox",
- Healthcheck: &container.HealthConfig{
- Interval: tc.interval,
- Timeout: tc.timeout,
- Retries: tc.retries,
- },
- }
- if tc.startPeriod != 0 {
- cfg.Healthcheck.StartPeriod = tc.startPeriod
- }
- resp, err := client.ContainerCreate(ctx, &cfg, &container.HostConfig{}, nil, "")
- assert.Check(t, is.Equal(len(resp.Warnings), 0))
- if versions.LessThan(testEnv.DaemonAPIVersion(), "1.32") {
- assert.Check(t, errdefs.IsSystem(err))
- } else {
- assert.Check(t, errdefs.IsInvalidParameter(err))
- }
- assert.ErrorContains(t, err, tc.expectedErr)
- })
- }
- }
|