Skip to content

Commit

Permalink
Merge pull request #2661 from TinaMor/fix-windows-volume-mount
Browse files Browse the repository at this point in the history
Allow for binding a named pipe on Windows
  • Loading branch information
AkihiroSuda authored Dec 28, 2023
2 parents 30cbce6 + e4bf8b9 commit 406a6fb
Show file tree
Hide file tree
Showing 14 changed files with 1,328 additions and 83 deletions.
18 changes: 16 additions & 2 deletions cmd/nerdctl/container_run_mount_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,22 @@ func TestRunVolume(t *testing.T) {
func TestRunAnonymousVolume(t *testing.T) {
t.Parallel()
base := testutil.NewBase(t)
base.Cmd("run", "--rm", "-v", "/foo", testutil.AlpineImage,
"mountpoint", "-q", "/foo").AssertOK()
base.Cmd("run", "--rm", "-v", "/foo", testutil.AlpineImage).AssertOK()
base.Cmd("run", "--rm", "-v", "TestVolume2:/foo", testutil.AlpineImage).AssertOK()
base.Cmd("run", "--rm", "-v", "TestVolume", testutil.AlpineImage).AssertOK()

// Destination must be an absolute path not named volume
base.Cmd("run", "--rm", "-v", "TestVolume2:TestVolumes", testutil.AlpineImage).AssertFail()
}

func TestRunVolumeRelativePath(t *testing.T) {
t.Parallel()
base := testutil.NewBase(t)
base.Cmd("run", "--rm", "-v", "./foo:/mnt/foo", testutil.AlpineImage).AssertOK()
base.Cmd("run", "--rm", "-v", "./foo", testutil.AlpineImage).AssertOK()

// Destination must be an absolute path not a relative path
base.Cmd("run", "--rm", "-v", "./foo:./foo", testutil.AlpineImage).AssertFail()
}

func TestRunAnonymousVolumeWithTypeMountFlag(t *testing.T) {
Expand Down
214 changes: 214 additions & 0 deletions cmd/nerdctl/container_run_mount_windows_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,214 @@
/*
Copyright The containerd Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package main

import (
"fmt"
"os"
"testing"

"github.com/containerd/nerdctl/pkg/inspecttypes/dockercompat"
"github.com/containerd/nerdctl/pkg/testutil"
"gotest.tools/v3/assert"
)

func TestRunMountVolume(t *testing.T) {
t.Parallel()
base := testutil.NewBase(t)
tID := testutil.Identifier(t)
rwDir, err := os.MkdirTemp(t.TempDir(), "rw")
if err != nil {
t.Fatal(err)
}
roDir, err := os.MkdirTemp(t.TempDir(), "ro")
if err != nil {
t.Fatal(err)
}
rwVolName := tID + "-rw"
roVolName := tID + "-ro"
for _, v := range []string{rwVolName, roVolName} {
defer base.Cmd("volume", "rm", "-f", v).Run()
base.Cmd("volume", "create", v).AssertOK()
}

containerName := tID
defer base.Cmd("rm", "-f", containerName).AssertOK()
base.Cmd("run",
"-d",
"--name", containerName,
"-v", fmt.Sprintf("%s:C:/mnt1", rwDir),
"-v", fmt.Sprintf("%s:C:/mnt2:ro", roDir),
"-v", fmt.Sprintf("%s:C:/mnt3", rwVolName),
"-v", fmt.Sprintf("%s:C:/mnt4:ro", roVolName),
testutil.CommonImage,
"ping localhost -t",
).AssertOK()

base.Cmd("exec", containerName, "cmd", "/c", "echo -n str1 > C:/mnt1/file1").AssertOK()
base.Cmd("exec", containerName, "cmd", "/c", "echo -n str2 > C:/mnt2/file2").AssertFail()
base.Cmd("exec", containerName, "cmd", "/c", "echo -n str3 > C:/mnt3/file3").AssertOK()
base.Cmd("exec", containerName, "cmd", "/c", "echo -n str4 > C:/mnt4/file4").AssertFail()
base.Cmd("rm", "-f", containerName).AssertOK()

base.Cmd("run",
"--rm",
"-v", fmt.Sprintf("%s:C:/mnt1", rwDir),
"-v", fmt.Sprintf("%s:C:/mnt3", rwVolName),
testutil.CommonImage,
"cat", "C:/mnt1/file1", "C:/mnt3/file3",
).AssertOutContainsAll("str1", "str3")
base.Cmd("run",
"--rm",
"-v", fmt.Sprintf("%s:C:/mnt3/mnt1", rwDir),
"-v", fmt.Sprintf("%s:C:/mnt3", rwVolName),
testutil.CommonImage,
"cat", "C:/mnt3/mnt1/file1", "C:/mnt3/file3",
).AssertOutContainsAll("str1", "str3")
}

func TestRunMountVolumeInspect(t *testing.T) {
base := testutil.NewBase(t)
testContainer := testutil.Identifier(t)
testVolume := testutil.Identifier(t)

defer base.Cmd("volume", "rm", "-f", testVolume).Run()
base.Cmd("volume", "create", testVolume).AssertOK()
inspectVolume := base.InspectVolume(testVolume)
namedVolumeSource := inspectVolume.Mountpoint

base.Cmd(
"run", "-d", "--name", testContainer,
"-v", "C:/mnt1",
"-v", "C:/mnt2:C:/mnt2",
"-v", "\\\\.\\pipe\\containerd-containerd:\\\\.\\pipe\\containerd-containerd",
"-v", fmt.Sprintf("%s:C:/mnt3", testVolume),
testutil.CommonImage,
).AssertOK()

inspect := base.InspectContainer(testContainer)
// convert array to map to get by key of Destination
actual := make(map[string]dockercompat.MountPoint)
for i := range inspect.Mounts {
actual[inspect.Mounts[i].Destination] = inspect.Mounts[i]
}

expected := []struct {
dest string
mountPoint dockercompat.MountPoint
}{
// anonymous volume
{
dest: "C:\\mnt1",
mountPoint: dockercompat.MountPoint{
Type: "volume",
Source: "", // source of anonymous volume is a generated path, so here will not check it.
Destination: "C:\\mnt1",
},
},

// bind
{
dest: "C:\\mnt2",
mountPoint: dockercompat.MountPoint{
Type: "bind",
Source: "C:\\mnt2",
Destination: "C:\\mnt2",
},
},

// named pipe
{
dest: "\\\\.\\pipe\\containerd-containerd",
mountPoint: dockercompat.MountPoint{
Type: "npipe",
Source: "\\\\.\\pipe\\containerd-containerd",
Destination: "\\\\.\\pipe\\containerd-containerd",
},
},

// named volume
{
dest: "C:\\mnt3",
mountPoint: dockercompat.MountPoint{
Type: "volume",
Name: testVolume,
Source: namedVolumeSource,
Destination: "C:\\mnt3",
},
},
}

for i := range expected {
testCase := expected[i]
t.Logf("test volume[dest=%q]", testCase.dest)

mountPoint, ok := actual[testCase.dest]
assert.Assert(base.T, ok)

assert.Equal(base.T, testCase.mountPoint.Type, mountPoint.Type)
assert.Equal(base.T, testCase.mountPoint.Destination, mountPoint.Destination)

if testCase.mountPoint.Source == "" {
// for anonymous volumes, we want to make sure that the source is not the same as the destination
assert.Assert(base.T, mountPoint.Source != testCase.mountPoint.Destination)
} else {
assert.Equal(base.T, testCase.mountPoint.Source, mountPoint.Source)
}

if testCase.mountPoint.Name != "" {
assert.Equal(base.T, testCase.mountPoint.Name, mountPoint.Name)
}
}
}

func TestRunMountAnonymousVolume(t *testing.T) {
t.Parallel()
base := testutil.NewBase(t)
base.Cmd("run", "--rm", "-v", "TestVolume:C:/mnt", testutil.CommonImage).AssertOK()

// For docker-campatibility, Unrecognised volume spec: invalid volume specification: 'TestVolume'
base.Cmd("run", "--rm", "-v", "TestVolume", testutil.CommonImage).AssertFail()

// Destination must be an absolute path not named volume
base.Cmd("run", "--rm", "-v", "TestVolume2:TestVolumes", testutil.CommonImage).AssertFail()
}

func TestRunMountRelativePath(t *testing.T) {
t.Parallel()
base := testutil.NewBase(t)
base.Cmd("run", "--rm", "-v", "./mnt:C:/mnt1", testutil.CommonImage, "cmd").AssertOK()

// Destination cannot be a relative path
base.Cmd("run", "--rm", "-v", "./mnt", testutil.CommonImage).AssertFail()
base.Cmd("run", "--rm", "-v", "./mnt:./mnt1", testutil.CommonImage, "cmd").AssertFail()
}

func TestRunMountNamedPipeVolume(t *testing.T) {
t.Parallel()
base := testutil.NewBase(t)
base.Cmd("run", "--rm", "-v", `\\.\pipe\containerd-containerd`, testutil.CommonImage).AssertFail()
}

func TestRunMountVolumeSpec(t *testing.T) {
t.Parallel()
base := testutil.NewBase(t)
base.Cmd("run", "--rm", "-v", `InvalidPathC:\TestVolume:C:\Mount`, testutil.CommonImage).AssertFail()
base.Cmd("run", "--rm", "-v", `C:\TestVolume:C:\Mount:ro,rw:boot`, testutil.CommonImage).AssertFail()

// If -v is an empty string, it will be ignored
base.Cmd("run", "--rm", "-v", "", testutil.CommonImage).AssertOK()
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ require (
github.com/vishvananda/netlink v1.2.1-beta.2
github.com/vishvananda/netns v0.0.4
github.com/yuchanns/srslog v1.1.0
go.uber.org/mock v0.3.0
golang.org/x/crypto v0.17.0
golang.org/x/net v0.19.0
golang.org/x/sync v0.5.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,8 @@ go.opentelemetry.io/otel/metric v1.19.0/go.mod h1:L5rUsV9kM1IxCj1MmSdS+JQAcVm319
go.opentelemetry.io/otel/trace v1.19.0 h1:DFVQmlVbfVeOuBRrwdtaehRrWiL1JoVs9CPIQ1Dzxpg=
go.opentelemetry.io/otel/trace v1.19.0/go.mod h1:mfaSyvGyEJEI0nyV2I4qhNQnbBOUUmYZpYojqMnX2vo=
go.uber.org/goleak v1.1.12 h1:gZAh5/EyT/HQwlpkCy6wTpqfH9H8Lz8zbm3dZh+OyzA=
go.uber.org/mock v0.3.0 h1:3mUxI1No2/60yUYax92Pt8eNOEecx2D3lcXZh2NEZJo=
go.uber.org/mock v0.3.0/go.mod h1:a6FSlNadKUHUa9IP5Vyt1zh4fC7uAwxMutEAscFbkZc=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20190911031432-227b76d455e7/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
Expand Down
4 changes: 3 additions & 1 deletion pkg/cmd/container/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,9 @@ func Create(ctx context.Context, client *containerd.Client, args []string, netMa
// perform network setup/teardown in the main nerdctl executable.
if containerErr == nil && runtime.GOOS == "windows" {
netSetupErr = netManager.SetupNetworking(ctx, id)
log.G(ctx).WithError(netSetupErr).Warnf("networking setup error has occurred")
if netSetupErr != nil {
log.G(ctx).WithError(netSetupErr).Warnf("networking setup error has occurred")
}
}

if containerErr != nil || netSetupErr != nil {
Expand Down
Loading

0 comments on commit 406a6fb

Please sign in to comment.