|
1 | 1 | package nri |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "os" |
4 | 5 | "path/filepath" |
5 | 6 | "testing" |
6 | 7 |
|
7 | 8 | "github.com/containerd/nri/pkg/api" |
| 9 | + "github.com/moby/moby/api/types/mount" |
8 | 10 | "github.com/moby/moby/client" |
9 | 11 | "github.com/moby/moby/v2/integration/internal/container" |
10 | 12 | "github.com/moby/moby/v2/internal/testutil" |
@@ -68,3 +70,123 @@ func TestNRIContainerCreateEnvVarMod(t *testing.T) { |
68 | 70 | }) |
69 | 71 | } |
70 | 72 | } |
| 73 | + |
| 74 | +func TestNRIContainerCreateAddMount(t *testing.T) { |
| 75 | + skip.If(t, testEnv.IsRemoteDaemon, "cannot run daemon when remote daemon") |
| 76 | + skip.If(t, testEnv.DaemonInfo.OSType == "windows", "cannot start a separate daemon with NRI enabled on Windows") |
| 77 | + skip.If(t, testEnv.IsRootless) |
| 78 | + |
| 79 | + ctx := testutil.StartSpan(baseContext, t) |
| 80 | + |
| 81 | + sockPath := filepath.Join(t.TempDir(), "nri.sock") |
| 82 | + |
| 83 | + d := daemon.New(t) |
| 84 | + d.StartWithBusybox(ctx, t, |
| 85 | + "--nri-opts=enable=true,socket-path="+sockPath, |
| 86 | + "--iptables=false", "--ip6tables=false", |
| 87 | + ) |
| 88 | + defer d.Stop(t) |
| 89 | + c := d.NewClientT(t) |
| 90 | + |
| 91 | + // Create and populate a directory for containers to mount. |
| 92 | + dirToMount := t.TempDir() |
| 93 | + if err := os.WriteFile(filepath.Join(dirToMount, "testfile.txt"), []byte("hello\n"), 0o644); err != nil { |
| 94 | + assert.NilError(t, err) |
| 95 | + } |
| 96 | + const ( |
| 97 | + mountPoint = "/mountpoint" |
| 98 | + ctrTestFile = "/mountpoint/testfile.txt" |
| 99 | + exitOk = 0 |
| 100 | + exitFail = 1 |
| 101 | + ) |
| 102 | + |
| 103 | + // Create and populate a volume. |
| 104 | + const volName = "nri-test-volume" |
| 105 | + _, err := c.VolumeCreate(ctx, client.VolumeCreateOptions{Name: volName}) |
| 106 | + assert.NilError(t, err) |
| 107 | + defer func() { |
| 108 | + _, _ = c.VolumeRemove(ctx, volName, client.VolumeRemoveOptions{Force: true}) |
| 109 | + }() |
| 110 | + // Populate the volume with a test file. |
| 111 | + _ = container.Run(ctx, t, c, |
| 112 | + container.WithAutoRemove, |
| 113 | + container.WithMount(mount.Mount{Type: "volume", Source: volName, Target: mountPoint}), |
| 114 | + container.WithCmd("sh", "-c", "echo hello > "+ctrTestFile), |
| 115 | + ) |
| 116 | + |
| 117 | + tests := []struct { |
| 118 | + name string |
| 119 | + ctrCreateAdj *api.ContainerAdjustment |
| 120 | + |
| 121 | + expMountRead int |
| 122 | + expMountWrite int |
| 123 | + }{ |
| 124 | + { |
| 125 | + name: "mount/bind/ro", |
| 126 | + ctrCreateAdj: &api.ContainerAdjustment{Mounts: []*api.Mount{{ |
| 127 | + Type: "bind", |
| 128 | + Source: dirToMount, |
| 129 | + Destination: mountPoint, |
| 130 | + Options: []string{"ro"}, |
| 131 | + }}}, |
| 132 | + expMountRead: exitOk, |
| 133 | + expMountWrite: exitFail, |
| 134 | + }, |
| 135 | + { |
| 136 | + name: "mount/bind/rw", |
| 137 | + ctrCreateAdj: &api.ContainerAdjustment{Mounts: []*api.Mount{{ |
| 138 | + Type: "bind", |
| 139 | + Source: dirToMount, |
| 140 | + Destination: mountPoint, |
| 141 | + }}}, |
| 142 | + expMountRead: exitOk, |
| 143 | + expMountWrite: exitOk, |
| 144 | + }, |
| 145 | + { |
| 146 | + name: "mount/volume/ro", |
| 147 | + ctrCreateAdj: &api.ContainerAdjustment{Mounts: []*api.Mount{{ |
| 148 | + Type: "volume", |
| 149 | + Source: volName, |
| 150 | + Destination: mountPoint, |
| 151 | + Options: []string{"ro"}, |
| 152 | + }}}, |
| 153 | + expMountRead: exitOk, |
| 154 | + expMountWrite: exitFail, |
| 155 | + }, |
| 156 | + { |
| 157 | + name: "mount/volume/rw", |
| 158 | + ctrCreateAdj: &api.ContainerAdjustment{Mounts: []*api.Mount{{ |
| 159 | + Type: "volume", |
| 160 | + Source: volName, |
| 161 | + Destination: mountPoint, |
| 162 | + }}}, |
| 163 | + expMountRead: exitOk, |
| 164 | + expMountWrite: exitOk, |
| 165 | + }, |
| 166 | + } |
| 167 | + |
| 168 | + for _, tc := range tests { |
| 169 | + t.Run(tc.name, func(t *testing.T) { |
| 170 | + stopPlugin := startBuiltinPlugin(ctx, t, builtinPluginConfig{ |
| 171 | + pluginName: "nri-test-plugin", |
| 172 | + pluginIdx: "00", |
| 173 | + sockPath: sockPath, |
| 174 | + ctrCreateAdj: tc.ctrCreateAdj, |
| 175 | + }) |
| 176 | + defer stopPlugin() |
| 177 | + |
| 178 | + ctrId := container.Run(ctx, t, c) |
| 179 | + defer func() { _, _ = c.ContainerRemove(ctx, ctrId, client.ContainerRemoveOptions{Force: true}) }() |
| 180 | + |
| 181 | + res, err := container.Exec(ctx, c, ctrId, []string{"cat", ctrTestFile}) |
| 182 | + if assert.Check(t, err) { |
| 183 | + assert.Check(t, is.Equal(res.ExitCode, tc.expMountRead)) |
| 184 | + assert.Check(t, is.Equal(res.Stdout(), "hello\n")) |
| 185 | + } |
| 186 | + res, err = container.Exec(ctx, c, ctrId, []string{"touch", ctrTestFile}) |
| 187 | + if assert.Check(t, err) { |
| 188 | + assert.Check(t, is.Equal(res.ExitCode, tc.expMountWrite)) |
| 189 | + } |
| 190 | + }) |
| 191 | + } |
| 192 | +} |
0 commit comments