Skip to content

Commit

Permalink
Merge pull request #671 from microsoft/katiewasnothere/plan9hostpath
Browse files Browse the repository at this point in the history
Change parsing of hostpath string for plan9 file mounting
  • Loading branch information
jterry75 authored Aug 20, 2019
2 parents ff1cc0b + 421312b commit 9e92188
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 1 deletion.
3 changes: 2 additions & 1 deletion internal/hcsoci/resources_lcow.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"fmt"
"os"
"path"
"path/filepath"
"strconv"
"strings"

Expand Down Expand Up @@ -110,7 +111,7 @@ func allocateLinuxResources(ctx context.Context, coi *createOptionsInternal, res
// Map the containing directory in, but restrict the share to a single
// file.
var fileName string
hostPath, fileName = path.Split(hostPath)
hostPath, fileName = filepath.Split(hostPath)
allowedNames = append(allowedNames, fileName)
restrictAccess = true
uvmPathForFile = path.Join(uvmPathForShare, fileName)
Expand Down
78 changes: 78 additions & 0 deletions test/cri-containerd/createcontainer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ package cri_containerd
import (
"context"
"fmt"
"io/ioutil"
"os"
"testing"

runtime "k8s.io/kubernetes/pkg/kubelet/apis/cri/runtime/v1alpha2"
Expand Down Expand Up @@ -793,3 +795,79 @@ func Test_CreateContainer_CPUShares_LCOW(t *testing.T) {
}
runCreateContainerTest(t, lcowRuntimeHandler, request)
}

func Test_CreateContainer_File_Hostpath_LCOW(t *testing.T) {
pullRequiredLcowImages(t, []string{imageLcowK8sPause, imageLcowAlpine})

tempFile, err := ioutil.TempFile("", "test")

if err != nil {
t.Fatalf("Failed to create temp file: %s", err)
}

tempFile.Close()

defer func() {
if err := os.Remove(tempFile.Name()); err != nil {
t.Fatalf("Failed to remove temp file: %s", err)
}
}()

containerFilePath := "/foo/test.txt"

request := &runtime.CreateContainerRequest{
Config: &runtime.ContainerConfig{
Metadata: &runtime.ContainerMetadata{
Name: t.Name() + "-Container",
},
Mounts: []*runtime.Mount{
{
HostPath: tempFile.Name(),
ContainerPath: containerFilePath,
},
},
Image: &runtime.ImageSpec{
Image: imageLcowAlpine,
},
Command: []string{
"top",
},
Linux: &runtime.LinuxContainerConfig{},
},
}
runCreateContainerTest(t, lcowRuntimeHandler, request)
}

func Test_CreateContainer_Dir_Hostpath_LCOW(t *testing.T) {
pullRequiredLcowImages(t, []string{imageLcowK8sPause, imageLcowAlpine})

tempDir, err := ioutil.TempDir("", "")

if err != nil {
t.Fatalf("Failed to create temp dir: %s", err)
}

containerFilePath := "/foo"

request := &runtime.CreateContainerRequest{
Config: &runtime.ContainerConfig{
Metadata: &runtime.ContainerMetadata{
Name: t.Name() + "-Container",
},
Mounts: []*runtime.Mount{
{
HostPath: tempDir,
ContainerPath: containerFilePath,
},
},
Image: &runtime.ImageSpec{
Image: imageLcowAlpine,
},
Command: []string{
"top",
},
Linux: &runtime.LinuxContainerConfig{},
},
}
runCreateContainerTest(t, lcowRuntimeHandler, request)
}

0 comments on commit 9e92188

Please sign in to comment.