Skip to content

Commit 10c0fc4

Browse files
committed
NRI: add TestNRIContainerCreateAddMount
Signed-off-by: Rob Murray <[email protected]>
1 parent a30301b commit 10c0fc4

1 file changed

Lines changed: 122 additions & 0 deletions

File tree

integration/daemon/nri/nri_test.go

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
package nri
22

33
import (
4+
"os"
45
"path/filepath"
56
"testing"
67

78
"github.com/containerd/nri/pkg/api"
9+
"github.com/moby/moby/api/types/mount"
810
"github.com/moby/moby/client"
911
"github.com/moby/moby/v2/integration/internal/container"
1012
"github.com/moby/moby/v2/internal/testutil"
@@ -68,3 +70,123 @@ func TestNRIContainerCreateEnvVarMod(t *testing.T) {
6870
})
6971
}
7072
}
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

Comments
 (0)