|
|
|
@@ -17,16 +17,20 @@
|
|
|
|
|
package integration
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"encoding/json"
|
|
|
|
|
"fmt"
|
|
|
|
|
"os"
|
|
|
|
|
"path/filepath"
|
|
|
|
|
goruntime "runtime"
|
|
|
|
|
"runtime"
|
|
|
|
|
"testing"
|
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
"github.com/containerd/containerd/integration/images"
|
|
|
|
|
specs "github.com/opencontainers/runtime-spec/specs-go"
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
v1 "k8s.io/cri-api/pkg/apis/runtime/v1"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
const (
|
|
|
|
@@ -36,6 +40,16 @@ const (
|
|
|
|
|
containerUserSID = "S-1-5-93-2-2"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type volumeFile struct {
|
|
|
|
|
fileName string
|
|
|
|
|
contents string
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type containerVolume struct {
|
|
|
|
|
containerPath string
|
|
|
|
|
files []volumeFile
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestVolumeCopyUp(t *testing.T) {
|
|
|
|
|
var (
|
|
|
|
|
testImage = images.Get(images.VolumeCopyUp)
|
|
|
|
@@ -59,23 +73,85 @@ func TestVolumeCopyUp(t *testing.T) {
|
|
|
|
|
t.Logf("Start the container")
|
|
|
|
|
require.NoError(t, runtimeService.StartContainer(cn))
|
|
|
|
|
|
|
|
|
|
// ghcr.io/containerd/volume-copy-up:2.1 contains a test_dir
|
|
|
|
|
// volume, which contains a test_file with content "test_content".
|
|
|
|
|
t.Logf("Check whether volume contains the test file")
|
|
|
|
|
stdout, stderr, err := runtimeService.ExecSync(cn, []string{
|
|
|
|
|
"cat",
|
|
|
|
|
"/test_dir/test_file",
|
|
|
|
|
}, execTimeout)
|
|
|
|
|
expectedVolumes := []containerVolume{
|
|
|
|
|
{
|
|
|
|
|
containerPath: "/test_dir",
|
|
|
|
|
files: []volumeFile{
|
|
|
|
|
{
|
|
|
|
|
fileName: "test_file",
|
|
|
|
|
contents: "test_content\n",
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
containerPath: "/:colon_prefixed",
|
|
|
|
|
files: []volumeFile{
|
|
|
|
|
{
|
|
|
|
|
fileName: "colon_prefixed_file",
|
|
|
|
|
contents: "test_content\n",
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
containerPath: "C:/weird_test_dir",
|
|
|
|
|
files: []volumeFile{
|
|
|
|
|
{
|
|
|
|
|
fileName: "weird_test_file",
|
|
|
|
|
contents: "test_content\n",
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
|
expectedVolumes = []containerVolume{
|
|
|
|
|
{
|
|
|
|
|
containerPath: "C:\\test_dir",
|
|
|
|
|
files: []volumeFile{
|
|
|
|
|
{
|
|
|
|
|
fileName: "test_file",
|
|
|
|
|
contents: "test_content\n",
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
containerPath: "D:",
|
|
|
|
|
files: []volumeFile{},
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
volumeMappings, err := getContainerBindVolumes(t, cn)
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
assert.Empty(t, stderr)
|
|
|
|
|
assert.Equal(t, "test_content\n", string(stdout))
|
|
|
|
|
|
|
|
|
|
t.Logf("Check host path of the volume")
|
|
|
|
|
volumePaths, err := getHostPathForVolumes(*criRoot, cn)
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
assert.Equal(t, len(volumePaths), 1, "expected exactly 1 volume")
|
|
|
|
|
for _, vol := range expectedVolumes {
|
|
|
|
|
_, ok := volumeMappings[vol.containerPath]
|
|
|
|
|
assert.Equalf(t, true, ok, "expected to find volume %s", vol.containerPath)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
testFilePath := filepath.Join(volumePaths[0], "test_file")
|
|
|
|
|
// ghcr.io/containerd/volume-copy-up:2.2 contains 3 volumes on Linux and 2 volumes on Windows.
|
|
|
|
|
// On linux, each of the volumes contains a single file, all with the same conrent. On Windows,
|
|
|
|
|
// non C volumes defined in the image start out as empty.
|
|
|
|
|
for _, vol := range expectedVolumes {
|
|
|
|
|
files, err := os.ReadDir(volumeMappings[vol.containerPath])
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
assert.Equal(t, len(vol.files), len(files))
|
|
|
|
|
|
|
|
|
|
for _, file := range vol.files {
|
|
|
|
|
t.Logf("Check whether volume %s contains the test file %s", vol.containerPath, file.fileName)
|
|
|
|
|
stdout, stderr, err := runtimeService.ExecSync(cn, []string{
|
|
|
|
|
"cat",
|
|
|
|
|
filepath.ToSlash(filepath.Join(vol.containerPath, file.fileName)),
|
|
|
|
|
}, execTimeout)
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
assert.Empty(t, stderr)
|
|
|
|
|
assert.Equal(t, file.contents, string(stdout))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
testFilePath := filepath.Join(volumeMappings[expectedVolumes[0].containerPath], expectedVolumes[0].files[0].fileName)
|
|
|
|
|
inContainerPath := filepath.Join(expectedVolumes[0].containerPath, expectedVolumes[0].files[0].fileName)
|
|
|
|
|
contents, err := os.ReadFile(testFilePath)
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
assert.Equal(t, "test_content\n", string(contents))
|
|
|
|
@@ -84,7 +160,7 @@ func TestVolumeCopyUp(t *testing.T) {
|
|
|
|
|
_, _, err = runtimeService.ExecSync(cn, []string{
|
|
|
|
|
"sh",
|
|
|
|
|
"-c",
|
|
|
|
|
"echo new_content > /test_dir/test_file",
|
|
|
|
|
fmt.Sprintf("echo new_content > %s", filepath.ToSlash(inContainerPath)),
|
|
|
|
|
}, execTimeout)
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
|
@@ -124,15 +200,17 @@ func TestVolumeOwnership(t *testing.T) {
|
|
|
|
|
// exist inside the container that returns the owner in the form of USERNAME:SID.
|
|
|
|
|
t.Logf("Check ownership of test directory inside container")
|
|
|
|
|
|
|
|
|
|
volumePath := "/test_dir"
|
|
|
|
|
cmd := []string{
|
|
|
|
|
"stat", "-c", "%u:%g", "/test_dir",
|
|
|
|
|
"stat", "-c", "%u:%g", volumePath,
|
|
|
|
|
}
|
|
|
|
|
expectedContainerOutput := "65534:65534\n"
|
|
|
|
|
expectedHostOutput := "65534:65534\n"
|
|
|
|
|
if goruntime.GOOS == "windows" {
|
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
|
volumePath = "C:\\volumes\\test_dir"
|
|
|
|
|
cmd = []string{
|
|
|
|
|
"C:\\bin\\get_owner.exe",
|
|
|
|
|
"C:\\volumes\\test_dir",
|
|
|
|
|
volumePath,
|
|
|
|
|
}
|
|
|
|
|
expectedContainerOutput = fmt.Sprintf("%s:%s", containerUserName, containerUserSID)
|
|
|
|
|
// The username is unknown on the host, but we can still get the SID.
|
|
|
|
@@ -144,34 +222,36 @@ func TestVolumeOwnership(t *testing.T) {
|
|
|
|
|
assert.Equal(t, expectedContainerOutput, string(stdout))
|
|
|
|
|
|
|
|
|
|
t.Logf("Check ownership of test directory on the host")
|
|
|
|
|
volumePaths, err := getHostPathForVolumes(*criRoot, cn)
|
|
|
|
|
volumePaths, err := getContainerBindVolumes(t, cn)
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
assert.Equal(t, len(volumePaths), 1, "expected exactly 1 volume")
|
|
|
|
|
|
|
|
|
|
output, err := getOwnership(volumePaths[0])
|
|
|
|
|
output, err := getOwnership(volumePaths[volumePath])
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
assert.Equal(t, expectedHostOutput, output)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func getHostPathForVolumes(criRoot, containerID string) ([]string, error) {
|
|
|
|
|
hostPath := filepath.Join(criRoot, "containers", containerID, "volumes")
|
|
|
|
|
if _, err := os.Stat(hostPath); err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
func getContainerBindVolumes(t *testing.T, containerID string) (map[string]string, error) {
|
|
|
|
|
client, err := RawRuntimeClient()
|
|
|
|
|
require.NoError(t, err, "failed to get raw grpc runtime service client")
|
|
|
|
|
request := &v1.ContainerStatusRequest{
|
|
|
|
|
ContainerId: containerID,
|
|
|
|
|
Verbose: true,
|
|
|
|
|
}
|
|
|
|
|
response, err := client.ContainerStatus(context.TODO(), request)
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
ret := make(map[string]string)
|
|
|
|
|
|
|
|
|
|
volumes, err := os.ReadDir(hostPath)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
mounts := struct {
|
|
|
|
|
RuntimeSpec struct {
|
|
|
|
|
Mounts []specs.Mount `json:"mounts"`
|
|
|
|
|
} `json:"runtimeSpec"`
|
|
|
|
|
}{}
|
|
|
|
|
|
|
|
|
|
info := response.Info["info"]
|
|
|
|
|
err = json.Unmarshal([]byte(info), &mounts)
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
for _, mount := range mounts.RuntimeSpec.Mounts {
|
|
|
|
|
ret[mount.Destination] = mount.Source
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if len(volumes) == 0 {
|
|
|
|
|
return []string{}, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
volumePaths := make([]string, len(volumes))
|
|
|
|
|
for idx, volume := range volumes {
|
|
|
|
|
volumePaths[idx] = filepath.Join(hostPath, volume.Name())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return volumePaths, nil
|
|
|
|
|
return ret, nil
|
|
|
|
|
}
|
|
|
|
|