|
| 1 | +/* |
| 2 | + Copyright The containerd Authors. |
| 3 | +
|
| 4 | + Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + you may not use this file except in compliance with the License. |
| 6 | + You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | + Unless required by applicable law or agreed to in writing, software |
| 11 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + See the License for the specific language governing permissions and |
| 14 | + limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package container |
| 18 | + |
| 19 | +import ( |
| 20 | + "fmt" |
| 21 | + "testing" |
| 22 | + |
| 23 | + "github.com/containerd/nerdctl/mod/tigron/expect" |
| 24 | + "github.com/containerd/nerdctl/mod/tigron/require" |
| 25 | + "github.com/containerd/nerdctl/mod/tigron/test" |
| 26 | + |
| 27 | + "github.com/containerd/nerdctl/v2/pkg/testutil" |
| 28 | + "github.com/containerd/nerdctl/v2/pkg/testutil/nerdtest" |
| 29 | +) |
| 30 | + |
| 31 | +// TestRunMountTypeImage verifies that `--mount type=image` mounts the source |
| 32 | +// image's filesystem into the container so its files are readable at the target. |
| 33 | +func TestRunMountTypeImage(t *testing.T) { |
| 34 | + testCase := nerdtest.Setup() |
| 35 | + |
| 36 | + testCase.Command = func(data test.Data, helpers test.Helpers) test.TestableCommand { |
| 37 | + return helpers.Command("run", "--rm", |
| 38 | + "--mount", fmt.Sprintf("type=image,source=%s,destination=/mnt/img", testutil.CommonImage), |
| 39 | + testutil.CommonImage, "cat", "/mnt/img/etc/os-release") |
| 40 | + } |
| 41 | + |
| 42 | + testCase.Expected = func(data test.Data, helpers test.Helpers) *test.Expected { |
| 43 | + return &test.Expected{ |
| 44 | + ExitCode: expect.ExitCodeSuccess, |
| 45 | + Output: expect.Contains("Alpine"), |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + testCase.Run(t) |
| 50 | +} |
| 51 | + |
| 52 | +// TestRunMountTypeImageMultipleDestinations verifies the same image can be |
| 53 | +// mounted at two destinations in one container. |
| 54 | +func TestRunMountTypeImageMultipleDestinations(t *testing.T) { |
| 55 | + testCase := nerdtest.Setup() |
| 56 | + // nerdctl-only: Docker keys an image mount by its source image and rejects |
| 57 | + // mounting the same image twice ("mount already exists with name"). |
| 58 | + testCase.Require = require.Not(nerdtest.Docker) |
| 59 | + |
| 60 | + testCase.Command = func(data test.Data, helpers test.Helpers) test.TestableCommand { |
| 61 | + return helpers.Command("run", "--rm", |
| 62 | + "--mount", fmt.Sprintf("type=image,source=%s,destination=/mnt/a", testutil.CommonImage), |
| 63 | + "--mount", fmt.Sprintf("type=image,source=%s,destination=/mnt/b", testutil.CommonImage), |
| 64 | + testutil.CommonImage, "cat", "/mnt/a/etc/os-release", "/mnt/b/etc/os-release") |
| 65 | + } |
| 66 | + |
| 67 | + testCase.Expected = func(data test.Data, helpers test.Helpers) *test.Expected { |
| 68 | + return &test.Expected{ |
| 69 | + ExitCode: expect.ExitCodeSuccess, |
| 70 | + Output: expect.Contains("Alpine"), |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + testCase.Run(t) |
| 75 | +} |
| 76 | + |
| 77 | +// TestRunMountTypeImageReadOnly verifies an image mount is read-only (writing |
| 78 | +// fails). nerdctl-only: Docker mounts images read-write by default. |
| 79 | +func TestRunMountTypeImageReadOnly(t *testing.T) { |
| 80 | + testCase := nerdtest.Setup() |
| 81 | + testCase.Require = require.Not(nerdtest.Docker) |
| 82 | + |
| 83 | + testCase.Command = func(data test.Data, helpers test.Helpers) test.TestableCommand { |
| 84 | + return helpers.Command("run", "--rm", |
| 85 | + "--mount", fmt.Sprintf("type=image,source=%s,destination=/mnt/img", testutil.CommonImage), |
| 86 | + testutil.CommonImage, "touch", "/mnt/img/should-fail") |
| 87 | + } |
| 88 | + |
| 89 | + testCase.Expected = func(data test.Data, helpers test.Helpers) *test.Expected { |
| 90 | + return &test.Expected{ |
| 91 | + ExitCode: expect.ExitCodeGenericFail, |
| 92 | + Errors: []error{fmt.Errorf("Read-only file system")}, |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + testCase.Run(t) |
| 97 | +} |
| 98 | + |
| 99 | +// TestRunMountTypeImageErrors verifies that an image mount missing its source, |
| 100 | +// or using the not-yet-supported subpath option, is rejected. subpath is |
| 101 | +// nerdctl-specific behaviour here, so the test is not run against Docker. |
| 102 | +func TestRunMountTypeImageErrors(t *testing.T) { |
| 103 | + testCase := nerdtest.Setup() |
| 104 | + testCase.Require = require.Not(nerdtest.Docker) |
| 105 | + |
| 106 | + testCase.SubTests = []*test.Case{ |
| 107 | + { |
| 108 | + Description: "missing source", |
| 109 | + Command: func(data test.Data, helpers test.Helpers) test.TestableCommand { |
| 110 | + return helpers.Command("run", "--rm", "--mount", "type=image,destination=/mnt/img", |
| 111 | + testutil.CommonImage, "true") |
| 112 | + }, |
| 113 | + Expected: func(data test.Data, helpers test.Helpers) *test.Expected { |
| 114 | + return &test.Expected{ |
| 115 | + ExitCode: expect.ExitCodeGenericFail, |
| 116 | + Errors: []error{fmt.Errorf("source")}, |
| 117 | + } |
| 118 | + }, |
| 119 | + }, |
| 120 | + { |
| 121 | + Description: "subpath not supported", |
| 122 | + Command: func(data test.Data, helpers test.Helpers) test.TestableCommand { |
| 123 | + return helpers.Command("run", "--rm", |
| 124 | + "--mount", fmt.Sprintf("type=image,source=%s,destination=/mnt/img,subpath=etc", testutil.CommonImage), |
| 125 | + testutil.CommonImage, "true") |
| 126 | + }, |
| 127 | + Expected: func(data test.Data, helpers test.Helpers) *test.Expected { |
| 128 | + return &test.Expected{ |
| 129 | + ExitCode: expect.ExitCodeGenericFail, |
| 130 | + Errors: []error{fmt.Errorf("subpath")}, |
| 131 | + } |
| 132 | + }, |
| 133 | + }, |
| 134 | + } |
| 135 | + |
| 136 | + testCase.Run(t) |
| 137 | +} |
0 commit comments