Repository: containerd/containerd
File: integration/image_volume_linux_test.go (Go)

Code before:

			podCtx.createContainer("running-1",
				alpineImage,
				criruntime.ContainerState_CONTAINER_RUNNING,
				WithCommand("sleep", "1d"),
				WithImageVolumeMount(alpineImage, "", "/alpine-2"))

			mpInfo1, err := mount.Lookup(targetVolumeMount)
			require.NoError(t, err)
			require.Equal(t, targetVolumeMount, mpInfo1.Mountpoint)

			podCtx.createContainer("running-2",
				alpineImage,
				criruntime.ContainerState_CONTAINER_RUNNING,
				WithCommand("sleep", "1d"),
				WithImageVolumeMount(alpineImage, "", "/alpine-2"))

			mpInfo2, err := mount.Lookup(targetVolumeMount)
			require.NoError(t, err)

			require.Equal(t, mpInfo1, mpInfo2, "should not mount twice")
		})
	}
}

func TestImageVolumeWithUserNamespace(t *testing.T) {
	// Check if user namespace and idmap are supported
	if !supportsUserNS() {
		t.Skip("user namespace not supported")
	}

	// Check if pidfd is supported
	if !sys.SupportsPidFD() {
		t.Skip("pidfd not supported")
	}

	containerID := uint32(0)
	hostID := uint32(65536)
	size := uint32(65536)

	containerImage := images.Get(images.Alpine)
	imageVolumeImage := images.Get(images.Pause)

	podLogDir := t.TempDir()
	podOpts := []PodSandboxOpts{
		WithPodLogDirectory(podLogDir),
		WithPodUserNs(containerID, hostID, size),
	}
	podCtx := newPodTCtx(t, runtimeService, t.Name(), "image-volume-userns", podOpts...)
	defer podCtx.stop(true)


Review comment (bug):
Can you add an integration test for this functionality? We have some here: `integration/image_volume_test.go`.  I'm not sure if it will be simple to add to `TestImageVolumeBasic()`, but if it's not simple/nice, you can create a new test function there.

We really need to verify that a pod can be started with an image config. As we say before, this test can pass but a pod fails to start. Let's just have an integration test that tests this flow more closely to what happens in a kubernetes pod.

Code after:

			podCtx.createContainer("running-1",
				alpineImage,
				criruntime.ContainerState_CONTAINER_RUNNING,
				WithCommand("sleep", "1d"),
				WithImageVolumeMount(alpineImage, "", "/alpine-2"))

			mpInfo1, err := mount.Lookup(targetVolumeMount)
			require.NoError(t, err)
			require.Equal(t, targetVolumeMount, mpInfo1.Mountpoint)

			podCtx.createContainer("running-2",
				alpineImage,
				criruntime.ContainerState_CONTAINER_RUNNING,
				WithCommand("sleep", "1d"),
				WithImageVolumeMount(alpineImage, "", "/alpine-2"))

			mpInfo2, err := mount.Lookup(targetVolumeMount)
			require.NoError(t, err)

			require.Equal(t, mpInfo1, mpInfo2, "should not mount twice")
		})
	}
}

func TestImageVolumeWithUserNamespace(t *testing.T) {
	// Check if user namespace and idmap are supported
	if !supportsUserNS() {
		t.Skip("user namespace not supported")
	}

	// Check if pidfd is supported
	if !sys.SupportsPidFD() {
		t.Skip("pidfd not supported")
	}

	if !supportsIDMap(defaultRoot) {
		t.Skipf("idmap mounts not supported on: %s", defaultRoot)
	}

	containerID := uint32(0)
	hostID := uint32(65536)
	size := uint32(65536)

	containerImage := images.Get(images.Alpine)
	imageVolumeImage := images.Get(images.Pause)

	podLogDir := t.TempDir()
	podOpts := []PodSandboxOpts{
		WithPodLogDirectory(podLogDir),
		WithPodUserNs(containerID, hostID, size),