Skip to content

Commit

Permalink
Make mount and staging dir creation flexible
Browse files Browse the repository at this point in the history
This change adds an option to attach callback functions to CSI sanity
for mount target directory creation. This could be used to customize the
target and staging mount path creation.

Also, adds `targetPath` and `stagingPath` fields to `SanityContext` to
store the actual target and staging paths, derived from the sanity
config.

Adds an e2e test section to test the above setup.
  • Loading branch information
darkowlzz committed Jan 27, 2019
1 parent 0caac94 commit 92fd1a2
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 20 deletions.
46 changes: 46 additions & 0 deletions hack/_apitest2/api_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package apitest2

import (
"fmt"
"os"
"path"
"testing"

"github.com/kubernetes-csi/csi-test/pkg/sanity"
)

func TestMyDriverWithCustomTargetPaths(t *testing.T) {
// tmpPath could be a CO specific directory under which all the target dirs
// are created. For k8s, it could be /var/lib/kubelet/pods under which the
// mount directories could be created.
tmpPath := path.Join(os.TempDir(), "csi")
config := &sanity.Config{
TargetPath: "foo/target/mount",
StagingPath: "foo/staging/mount",
Address: "/tmp/e2e-csi-sanity.sock",
CreateTargetDir: func(targetPath string) (string, error) {
targetPath = path.Join(tmpPath, targetPath)
return targetPath, createTargetDir(targetPath)
},
CreateStagingDir: func(targetPath string) (string, error) {
targetPath = path.Join(tmpPath, targetPath)
return targetPath, createTargetDir(targetPath)
},
}

sanity.Test(t, config)
}

func createTargetDir(targetPath string) error {
fileInfo, err := os.Stat(targetPath)
if err != nil && os.IsNotExist(err) {
return os.MkdirAll(targetPath, 0755)
} else if err != nil {
return err
}
if !fileInfo.IsDir() {
return fmt.Errorf("Target location %s is not a directory", targetPath)
}

return nil
}
15 changes: 15 additions & 0 deletions hack/e2e.sh
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,18 @@ runTestAPI()
fi
}

runTestAPIWithCustomTargetPaths()
{
CSI_ENDPOINT=$1 ./bin/mock &
local pid=$!

GOCACHE=off go test -v ./hack/_apitest2/api_test.go; ret=$?

if [ $ret -ne 0 ] ; then
exit $ret
fi
}

make

cd cmd/csi-sanity
Expand All @@ -69,4 +81,7 @@ rm -f $UDS
runTestAPI "${UDS}"
rm -f $UDS

runTestAPIWithCustomTargetPaths "${UDS}"
rm -rf $UDS

exit 0
32 changes: 16 additions & 16 deletions pkg/sanity/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ var _ = DescribeSanity("Node Service", func(sc *SanityContext) {
csi.ControllerServiceCapability_RPC_PUBLISH_UNPUBLISH_VOLUME)
nodeStageSupported = isNodeCapabilitySupported(c, csi.NodeServiceCapability_RPC_STAGE_UNSTAGE_VOLUME)
if nodeStageSupported {
err := createMountTargetLocation(sc.Config.StagingPath)
err := createMountTargetLocation(sc.stagingPath)
Expect(err).NotTo(HaveOccurred())
}
nodeVolumeStatsSupported = isNodeCapabilitySupported(c, csi.NodeServiceCapability_RPC_GET_VOLUME_STATS)
Expand Down Expand Up @@ -191,7 +191,7 @@ var _ = DescribeSanity("Node Service", func(sc *SanityContext) {
context.Background(),
&csi.NodePublishVolumeRequest{
VolumeId: "id",
TargetPath: sc.Config.TargetPath,
TargetPath: sc.stagingPath,

This comment has been minimized.

Copy link
@okartau

okartau Feb 14, 2019

Contributor

path argument changes in this file seem mostly simple rename, but this one is semantic change from targetPath to stagingPath.
Intentional?

This comment has been minimized.

Copy link
@darkowlzz

darkowlzz Feb 18, 2019

Author Contributor

oops! Thanks for pointing that out. That's a mistake. I'll update it.

Secrets: sc.Secrets.NodePublishVolumeSecret,
},
)
Expand Down Expand Up @@ -248,7 +248,7 @@ var _ = DescribeSanity("Node Service", func(sc *SanityContext) {
_, err := c.NodeStageVolume(
context.Background(),
&csi.NodeStageVolumeRequest{
StagingTargetPath: sc.Config.StagingPath,
StagingTargetPath: sc.stagingPath,
VolumeCapability: &csi.VolumeCapability{
AccessType: &csi.VolumeCapability_Mount{
Mount: &csi.VolumeCapability_MountVolume{},
Expand Down Expand Up @@ -301,7 +301,7 @@ var _ = DescribeSanity("Node Service", func(sc *SanityContext) {
context.Background(),
&csi.NodeStageVolumeRequest{
VolumeId: "id",
StagingTargetPath: sc.Config.StagingPath,
StagingTargetPath: sc.stagingPath,
PublishContext: map[string]string{
"device": device,
},
Expand All @@ -328,7 +328,7 @@ var _ = DescribeSanity("Node Service", func(sc *SanityContext) {
_, err := c.NodeUnstageVolume(
context.Background(),
&csi.NodeUnstageVolumeRequest{
StagingTargetPath: sc.Config.StagingPath,
StagingTargetPath: sc.stagingPath,
})
Expect(err).To(HaveOccurred())

Expand Down Expand Up @@ -478,7 +478,7 @@ var _ = DescribeSanity("Node Service", func(sc *SanityContext) {
Mode: csi.VolumeCapability_AccessMode_SINGLE_NODE_WRITER,
},
},
StagingTargetPath: sc.Config.StagingPath,
StagingTargetPath: sc.stagingPath,
VolumeContext: vol.GetVolume().GetVolumeContext(),
PublishContext: conpubvol.GetPublishContext(),
Secrets: sc.Secrets.NodeStageVolumeSecret,
Expand All @@ -491,13 +491,13 @@ var _ = DescribeSanity("Node Service", func(sc *SanityContext) {
By("publishing the volume on a node")
var stagingPath string
if nodeStageSupported {
stagingPath = sc.Config.StagingPath
stagingPath = sc.stagingPath
}
nodepubvol, err := c.NodePublishVolume(
context.Background(),
&csi.NodePublishVolumeRequest{
VolumeId: vol.GetVolume().GetVolumeId(),
TargetPath: sc.Config.TargetPath,
TargetPath: sc.targetPath,
StagingTargetPath: stagingPath,
VolumeCapability: &csi.VolumeCapability{
AccessType: &csi.VolumeCapability_Mount{
Expand Down Expand Up @@ -536,7 +536,7 @@ var _ = DescribeSanity("Node Service", func(sc *SanityContext) {
context.Background(),
&csi.NodeUnpublishVolumeRequest{
VolumeId: vol.GetVolume().GetVolumeId(),
TargetPath: sc.Config.TargetPath,
TargetPath: sc.targetPath,
})
Expect(err).NotTo(HaveOccurred())
Expect(nodeunpubvol).NotTo(BeNil())
Expand All @@ -547,7 +547,7 @@ var _ = DescribeSanity("Node Service", func(sc *SanityContext) {
context.Background(),
&csi.NodeUnstageVolumeRequest{
VolumeId: vol.GetVolume().GetVolumeId(),
StagingTargetPath: sc.Config.StagingPath,
StagingTargetPath: sc.stagingPath,
},
)
Expect(err).NotTo(HaveOccurred())
Expand Down Expand Up @@ -661,7 +661,7 @@ var _ = DescribeSanity("Node Service", func(sc *SanityContext) {
Mode: csi.VolumeCapability_AccessMode_SINGLE_NODE_WRITER,
},
},
StagingTargetPath: sc.Config.StagingPath,
StagingTargetPath: sc.stagingPath,
VolumeContext: vol.GetVolume().GetVolumeContext(),
PublishContext: conpubvol.GetPublishContext(),
Secrets: sc.Secrets.NodeStageVolumeSecret,
Expand All @@ -674,13 +674,13 @@ var _ = DescribeSanity("Node Service", func(sc *SanityContext) {
By("publishing the volume on a node")
var stagingPath string
if nodeStageSupported {
stagingPath = sc.Config.StagingPath
stagingPath = sc.stagingPath
}
nodepubvol, err := c.NodePublishVolume(
context.Background(),
&csi.NodePublishVolumeRequest{
VolumeId: vol.GetVolume().GetVolumeId(),
TargetPath: sc.Config.TargetPath,
TargetPath: sc.targetPath,
StagingTargetPath: stagingPath,
VolumeCapability: &csi.VolumeCapability{
AccessType: &csi.VolumeCapability_Mount{
Expand All @@ -705,7 +705,7 @@ var _ = DescribeSanity("Node Service", func(sc *SanityContext) {
context.Background(),
&csi.NodeGetVolumeStatsRequest{
VolumeId: vol.GetVolume().GetVolumeId(),
VolumePath: sc.Config.TargetPath,
VolumePath: sc.targetPath,
},
)
Expect(err).ToNot(HaveOccurred())
Expand All @@ -718,7 +718,7 @@ var _ = DescribeSanity("Node Service", func(sc *SanityContext) {
context.Background(),
&csi.NodeUnpublishVolumeRequest{
VolumeId: vol.GetVolume().GetVolumeId(),
TargetPath: sc.Config.TargetPath,
TargetPath: sc.targetPath,
})
Expect(err).NotTo(HaveOccurred())
Expect(nodeunpubvol).NotTo(BeNil())
Expand All @@ -729,7 +729,7 @@ var _ = DescribeSanity("Node Service", func(sc *SanityContext) {
context.Background(),
&csi.NodeUnstageVolumeRequest{
VolumeId: vol.GetVolume().GetVolumeId(),
StagingTargetPath: sc.Config.StagingPath,
StagingTargetPath: sc.stagingPath,
},
)
Expect(err).NotTo(HaveOccurred())
Expand Down
36 changes: 32 additions & 4 deletions pkg/sanity/sanity.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,13 @@ type Config struct {
TestVolumeSize int64
TestVolumeParametersFile string
TestVolumeParameters map[string]string

// Callback functions to customize the creation of target and staging
// directories. Returns the new paths for mount and staging.
// If not defined, directories are created in the default way at TargetPath
// and StagingPath.
CreateTargetDir func(path string) (string, error)
CreateStagingDir func(path string) (string, error)
}

// SanityContext holds the variables that each test can depend on. It
Expand All @@ -65,6 +72,10 @@ type SanityContext struct {
Secrets *CSISecrets

connAddress string

// Target and staging paths derived from the sanity config.
targetPath string
stagingPath string
}

// Test will test the CSI driver at the specified address by
Expand Down Expand Up @@ -123,11 +134,28 @@ func (sc *SanityContext) setup() {
}

By("creating mount and staging directories")
err = createMountTargetLocation(sc.Config.TargetPath)
Expect(err).NotTo(HaveOccurred())
if len(sc.Config.StagingPath) > 0 {
err = createMountTargetLocation(sc.Config.StagingPath)
// If callback function for creating target dir is specified, use it.
if sc.Config.CreateTargetDir != nil {
newpath, err := sc.Config.CreateTargetDir(sc.Config.TargetPath)
Expect(err).NotTo(HaveOccurred())
sc.targetPath = newpath
} else {
err = createMountTargetLocation(sc.Config.TargetPath)
Expect(err).NotTo(HaveOccurred())
sc.targetPath = sc.Config.TargetPath
}

if len(sc.Config.StagingPath) > 0 {
// If callback function for creating staging dir is specified, use it.
if sc.Config.CreateStagingDir != nil {
newpath, err := sc.Config.CreateStagingDir(sc.Config.StagingPath)
Expect(err).NotTo(HaveOccurred())
sc.stagingPath = newpath
} else {
err = createMountTargetLocation(sc.Config.StagingPath)
Expect(err).NotTo(HaveOccurred())
sc.stagingPath = sc.Config.StagingPath
}
}
}

Expand Down

0 comments on commit 92fd1a2

Please sign in to comment.