2020-01-28 12:19:56 +00:00
|
|
|
package csimanager
|
|
|
|
|
|
|
|
import (
|
2020-01-29 12:20:41 +00:00
|
|
|
"context"
|
|
|
|
"errors"
|
2020-01-28 12:19:56 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"runtime"
|
|
|
|
"testing"
|
|
|
|
|
2020-05-21 17:56:50 +00:00
|
|
|
"github.com/hashicorp/nomad/helper/mount"
|
2020-01-28 12:19:56 +00:00
|
|
|
"github.com/hashicorp/nomad/helper/testlog"
|
2020-03-31 21:13:52 +00:00
|
|
|
"github.com/hashicorp/nomad/nomad/mock"
|
2020-01-28 12:19:56 +00:00
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
2020-03-23 17:55:26 +00:00
|
|
|
"github.com/hashicorp/nomad/plugins/csi"
|
2020-01-28 12:19:56 +00:00
|
|
|
csifake "github.com/hashicorp/nomad/plugins/csi/fake"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func tmpDir(t testing.TB) string {
|
|
|
|
t.Helper()
|
|
|
|
dir, err := ioutil.TempDir("", "nomad")
|
|
|
|
require.NoError(t, err)
|
|
|
|
return dir
|
|
|
|
}
|
|
|
|
|
2020-05-21 17:56:50 +00:00
|
|
|
func checkMountSupport() bool {
|
|
|
|
path, err := os.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
m := mount.New()
|
|
|
|
_, err = m.IsNotAMountPoint(path)
|
|
|
|
return err == nil
|
|
|
|
}
|
|
|
|
|
2020-01-28 12:19:56 +00:00
|
|
|
func TestVolumeManager_ensureStagingDir(t *testing.T) {
|
2020-05-21 17:56:50 +00:00
|
|
|
if !checkMountSupport() {
|
|
|
|
t.Skip("mount point detection not supported for this platform")
|
|
|
|
}
|
2020-01-28 12:19:56 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
cases := []struct {
|
|
|
|
Name string
|
|
|
|
Volume *structs.CSIVolume
|
2020-02-17 11:10:12 +00:00
|
|
|
UsageOptions *UsageOptions
|
2020-01-28 12:19:56 +00:00
|
|
|
CreateDirAheadOfTime bool
|
|
|
|
MountDirAheadOfTime bool
|
|
|
|
|
|
|
|
ExpectedErr error
|
|
|
|
ExpectedMountState bool
|
|
|
|
}{
|
|
|
|
{
|
2020-02-17 11:10:12 +00:00
|
|
|
Name: "Creates a directory when one does not exist",
|
|
|
|
Volume: &structs.CSIVolume{ID: "foo"},
|
|
|
|
UsageOptions: &UsageOptions{},
|
2020-01-28 12:19:56 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "Does not fail because of a pre-existing directory",
|
|
|
|
Volume: &structs.CSIVolume{ID: "foo"},
|
2020-02-17 11:10:12 +00:00
|
|
|
UsageOptions: &UsageOptions{},
|
2020-01-28 12:19:56 +00:00
|
|
|
CreateDirAheadOfTime: true,
|
|
|
|
},
|
|
|
|
{
|
2020-02-17 11:10:12 +00:00
|
|
|
Name: "Returns negative mount info",
|
|
|
|
UsageOptions: &UsageOptions{},
|
|
|
|
Volume: &structs.CSIVolume{ID: "foo"},
|
2020-01-28 12:19:56 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "Returns positive mount info",
|
|
|
|
Volume: &structs.CSIVolume{ID: "foo"},
|
2020-02-17 11:10:12 +00:00
|
|
|
UsageOptions: &UsageOptions{},
|
2020-01-28 12:19:56 +00:00
|
|
|
CreateDirAheadOfTime: true,
|
|
|
|
MountDirAheadOfTime: true,
|
|
|
|
ExpectedMountState: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range cases {
|
|
|
|
t.Run(tc.Name, func(t *testing.T) {
|
|
|
|
// Step 1: Validate that the test case makes sense
|
|
|
|
if !tc.CreateDirAheadOfTime && tc.MountDirAheadOfTime {
|
|
|
|
require.Fail(t, "Cannot Mount without creating a dir")
|
|
|
|
}
|
|
|
|
|
|
|
|
if tc.MountDirAheadOfTime {
|
|
|
|
// We can enable these tests by either mounting a fake device on linux
|
|
|
|
// e.g shipping a small ext4 image file and using that as a loopback
|
|
|
|
// device, but there's no convenient way to implement this.
|
|
|
|
t.Skip("TODO: Skipped because we don't detect bind mounts")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Step 2: Test Setup
|
|
|
|
tmpPath := tmpDir(t)
|
|
|
|
defer os.RemoveAll(tmpPath)
|
|
|
|
|
|
|
|
csiFake := &csifake.Client{}
|
2020-03-31 21:13:52 +00:00
|
|
|
eventer := func(e *structs.NodeEvent) {}
|
|
|
|
manager := newVolumeManager(testlog.HCLogger(t), eventer, csiFake, tmpPath, tmpPath, true)
|
2020-04-02 20:04:56 +00:00
|
|
|
expectedStagingPath := manager.stagingDirForVolume(tmpPath, tc.Volume.ID, tc.UsageOptions)
|
2020-01-28 12:19:56 +00:00
|
|
|
|
|
|
|
if tc.CreateDirAheadOfTime {
|
|
|
|
err := os.MkdirAll(expectedStagingPath, 0700)
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Step 3: Now we can do some testing
|
|
|
|
|
2020-02-17 11:10:12 +00:00
|
|
|
path, detectedMount, testErr := manager.ensureStagingDir(tc.Volume, tc.UsageOptions)
|
2020-01-28 12:19:56 +00:00
|
|
|
if tc.ExpectedErr != nil {
|
|
|
|
require.EqualError(t, testErr, tc.ExpectedErr.Error())
|
|
|
|
return // We don't perform extra validation if an error was detected.
|
|
|
|
}
|
|
|
|
|
|
|
|
require.NoError(t, testErr)
|
|
|
|
require.Equal(t, tc.ExpectedMountState, detectedMount)
|
|
|
|
|
|
|
|
// If the ensureStagingDir call had to create a directory itself, then here
|
|
|
|
// we validate that the directory exists and its permissions
|
|
|
|
if !tc.CreateDirAheadOfTime {
|
|
|
|
file, err := os.Lstat(path)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.True(t, file.IsDir())
|
|
|
|
|
|
|
|
// TODO: Figure out a windows equivalent of this test
|
|
|
|
if runtime.GOOS != "windows" {
|
|
|
|
require.Equal(t, os.FileMode(0700), file.Mode().Perm())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2020-01-29 12:20:41 +00:00
|
|
|
|
|
|
|
func TestVolumeManager_stageVolume(t *testing.T) {
|
2020-05-21 17:56:50 +00:00
|
|
|
if !checkMountSupport() {
|
|
|
|
t.Skip("mount point detection not supported for this platform")
|
|
|
|
}
|
2020-01-29 12:20:41 +00:00
|
|
|
t.Parallel()
|
2020-05-21 17:56:50 +00:00
|
|
|
|
2020-01-29 12:20:41 +00:00
|
|
|
cases := []struct {
|
2020-02-17 11:10:12 +00:00
|
|
|
Name string
|
|
|
|
Volume *structs.CSIVolume
|
|
|
|
UsageOptions *UsageOptions
|
|
|
|
PluginErr error
|
|
|
|
ExpectedErr error
|
2020-01-29 12:20:41 +00:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
Name: "Returns an error when an invalid AttachmentMode is provided",
|
|
|
|
Volume: &structs.CSIVolume{
|
|
|
|
ID: "foo",
|
|
|
|
AttachmentMode: "nonsense",
|
|
|
|
},
|
2020-02-17 11:10:12 +00:00
|
|
|
UsageOptions: &UsageOptions{},
|
|
|
|
ExpectedErr: errors.New("Unknown volume attachment mode: nonsense"),
|
2020-01-29 12:20:41 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "Returns an error when an invalid AccessMode is provided",
|
|
|
|
Volume: &structs.CSIVolume{
|
|
|
|
ID: "foo",
|
|
|
|
AttachmentMode: structs.CSIVolumeAttachmentModeBlockDevice,
|
|
|
|
AccessMode: "nonsense",
|
|
|
|
},
|
2020-02-17 11:10:12 +00:00
|
|
|
UsageOptions: &UsageOptions{},
|
|
|
|
ExpectedErr: errors.New("Unknown volume access mode: nonsense"),
|
2020-01-29 12:20:41 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "Returns an error when the plugin returns an error",
|
|
|
|
Volume: &structs.CSIVolume{
|
|
|
|
ID: "foo",
|
|
|
|
AttachmentMode: structs.CSIVolumeAttachmentModeBlockDevice,
|
|
|
|
AccessMode: structs.CSIVolumeAccessModeMultiNodeMultiWriter,
|
|
|
|
},
|
2020-02-17 11:10:12 +00:00
|
|
|
UsageOptions: &UsageOptions{},
|
|
|
|
PluginErr: errors.New("Some Unknown Error"),
|
|
|
|
ExpectedErr: errors.New("Some Unknown Error"),
|
2020-01-29 12:20:41 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "Happy Path",
|
|
|
|
Volume: &structs.CSIVolume{
|
|
|
|
ID: "foo",
|
|
|
|
AttachmentMode: structs.CSIVolumeAttachmentModeBlockDevice,
|
|
|
|
AccessMode: structs.CSIVolumeAccessModeMultiNodeMultiWriter,
|
|
|
|
},
|
2020-02-17 11:10:12 +00:00
|
|
|
UsageOptions: &UsageOptions{},
|
|
|
|
PluginErr: nil,
|
|
|
|
ExpectedErr: nil,
|
2020-01-29 12:20:41 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range cases {
|
|
|
|
t.Run(tc.Name, func(t *testing.T) {
|
|
|
|
tmpPath := tmpDir(t)
|
|
|
|
defer os.RemoveAll(tmpPath)
|
|
|
|
|
|
|
|
csiFake := &csifake.Client{}
|
|
|
|
csiFake.NextNodeStageVolumeErr = tc.PluginErr
|
|
|
|
|
2020-03-31 21:13:52 +00:00
|
|
|
eventer := func(e *structs.NodeEvent) {}
|
|
|
|
manager := newVolumeManager(testlog.HCLogger(t), eventer, csiFake, tmpPath, tmpPath, true)
|
2020-01-29 12:20:41 +00:00
|
|
|
ctx := context.Background()
|
|
|
|
|
2020-02-17 12:57:25 +00:00
|
|
|
err := manager.stageVolume(ctx, tc.Volume, tc.UsageOptions, nil)
|
2020-01-29 12:20:41 +00:00
|
|
|
|
|
|
|
if tc.ExpectedErr != nil {
|
|
|
|
require.EqualError(t, err, tc.ExpectedErr.Error())
|
|
|
|
} else {
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2020-01-31 11:11:40 +00:00
|
|
|
|
|
|
|
func TestVolumeManager_unstageVolume(t *testing.T) {
|
2020-05-21 17:56:50 +00:00
|
|
|
if !checkMountSupport() {
|
|
|
|
t.Skip("mount point detection not supported for this platform")
|
|
|
|
}
|
2020-01-31 11:11:40 +00:00
|
|
|
t.Parallel()
|
2020-05-21 17:56:50 +00:00
|
|
|
|
2020-01-31 11:11:40 +00:00
|
|
|
cases := []struct {
|
|
|
|
Name string
|
|
|
|
Volume *structs.CSIVolume
|
2020-02-17 11:10:12 +00:00
|
|
|
UsageOptions *UsageOptions
|
2020-01-31 11:11:40 +00:00
|
|
|
PluginErr error
|
|
|
|
ExpectedErr error
|
|
|
|
ExpectedCSICallCount int64
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
Name: "Returns an error when the plugin returns an error",
|
|
|
|
Volume: &structs.CSIVolume{
|
|
|
|
ID: "foo",
|
|
|
|
},
|
2020-02-17 11:10:12 +00:00
|
|
|
UsageOptions: &UsageOptions{},
|
2020-01-31 11:11:40 +00:00
|
|
|
PluginErr: errors.New("Some Unknown Error"),
|
|
|
|
ExpectedErr: errors.New("Some Unknown Error"),
|
|
|
|
ExpectedCSICallCount: 1,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "Happy Path",
|
|
|
|
Volume: &structs.CSIVolume{
|
|
|
|
ID: "foo",
|
|
|
|
},
|
2020-02-17 11:10:12 +00:00
|
|
|
UsageOptions: &UsageOptions{},
|
2020-01-31 11:11:40 +00:00
|
|
|
PluginErr: nil,
|
|
|
|
ExpectedErr: nil,
|
|
|
|
ExpectedCSICallCount: 1,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range cases {
|
|
|
|
t.Run(tc.Name, func(t *testing.T) {
|
|
|
|
tmpPath := tmpDir(t)
|
|
|
|
defer os.RemoveAll(tmpPath)
|
|
|
|
|
|
|
|
csiFake := &csifake.Client{}
|
|
|
|
csiFake.NextNodeUnstageVolumeErr = tc.PluginErr
|
|
|
|
|
2020-03-31 21:13:52 +00:00
|
|
|
eventer := func(e *structs.NodeEvent) {}
|
|
|
|
manager := newVolumeManager(testlog.HCLogger(t), eventer, csiFake, tmpPath, tmpPath, true)
|
2020-01-31 11:11:40 +00:00
|
|
|
ctx := context.Background()
|
|
|
|
|
2020-04-04 15:03:44 +00:00
|
|
|
err := manager.unstageVolume(ctx,
|
|
|
|
tc.Volume.ID, tc.Volume.RemoteID(), tc.UsageOptions)
|
2020-01-31 11:11:40 +00:00
|
|
|
|
|
|
|
if tc.ExpectedErr != nil {
|
|
|
|
require.EqualError(t, err, tc.ExpectedErr.Error())
|
|
|
|
} else {
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
require.Equal(t, tc.ExpectedCSICallCount, csiFake.NodeUnstageVolumeCallCount)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2020-01-31 13:45:48 +00:00
|
|
|
|
|
|
|
func TestVolumeManager_publishVolume(t *testing.T) {
|
2020-05-21 17:56:50 +00:00
|
|
|
if !checkMountSupport() {
|
|
|
|
t.Skip("mount point detection not supported for this platform")
|
|
|
|
}
|
2020-01-31 13:45:48 +00:00
|
|
|
t.Parallel()
|
2020-05-21 17:56:50 +00:00
|
|
|
|
2020-01-31 13:45:48 +00:00
|
|
|
cases := []struct {
|
2020-03-23 17:55:26 +00:00
|
|
|
Name string
|
|
|
|
Allocation *structs.Allocation
|
|
|
|
Volume *structs.CSIVolume
|
|
|
|
UsageOptions *UsageOptions
|
|
|
|
PluginErr error
|
|
|
|
ExpectedErr error
|
|
|
|
ExpectedCSICallCount int64
|
|
|
|
ExpectedVolumeCapability *csi.VolumeCapability
|
2020-01-31 13:45:48 +00:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
Name: "Returns an error when the plugin returns an error",
|
|
|
|
Allocation: structs.MockAlloc(),
|
|
|
|
Volume: &structs.CSIVolume{
|
|
|
|
ID: "foo",
|
|
|
|
AttachmentMode: structs.CSIVolumeAttachmentModeBlockDevice,
|
|
|
|
AccessMode: structs.CSIVolumeAccessModeMultiNodeMultiWriter,
|
|
|
|
},
|
2020-02-17 11:10:12 +00:00
|
|
|
UsageOptions: &UsageOptions{},
|
2020-01-31 13:45:48 +00:00
|
|
|
PluginErr: errors.New("Some Unknown Error"),
|
|
|
|
ExpectedErr: errors.New("Some Unknown Error"),
|
|
|
|
ExpectedCSICallCount: 1,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "Happy Path",
|
|
|
|
Allocation: structs.MockAlloc(),
|
|
|
|
Volume: &structs.CSIVolume{
|
|
|
|
ID: "foo",
|
|
|
|
AttachmentMode: structs.CSIVolumeAttachmentModeBlockDevice,
|
|
|
|
AccessMode: structs.CSIVolumeAccessModeMultiNodeMultiWriter,
|
|
|
|
},
|
2020-02-17 11:10:12 +00:00
|
|
|
UsageOptions: &UsageOptions{},
|
2020-01-31 13:45:48 +00:00
|
|
|
PluginErr: nil,
|
|
|
|
ExpectedErr: nil,
|
|
|
|
ExpectedCSICallCount: 1,
|
|
|
|
},
|
2020-03-23 17:55:26 +00:00
|
|
|
{
|
|
|
|
Name: "Mount options in the volume",
|
|
|
|
Allocation: structs.MockAlloc(),
|
|
|
|
Volume: &structs.CSIVolume{
|
|
|
|
ID: "foo",
|
|
|
|
AttachmentMode: structs.CSIVolumeAttachmentModeFilesystem,
|
|
|
|
AccessMode: structs.CSIVolumeAccessModeMultiNodeMultiWriter,
|
|
|
|
MountOptions: &structs.CSIMountOptions{
|
|
|
|
MountFlags: []string{"ro"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
UsageOptions: &UsageOptions{},
|
|
|
|
PluginErr: nil,
|
|
|
|
ExpectedErr: nil,
|
|
|
|
ExpectedCSICallCount: 1,
|
|
|
|
ExpectedVolumeCapability: &csi.VolumeCapability{
|
|
|
|
AccessType: csi.VolumeAccessTypeMount,
|
|
|
|
AccessMode: csi.VolumeAccessModeMultiNodeMultiWriter,
|
|
|
|
MountVolume: &structs.CSIMountOptions{
|
|
|
|
MountFlags: []string{"ro"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "Mount options override in the request",
|
|
|
|
Allocation: structs.MockAlloc(),
|
|
|
|
Volume: &structs.CSIVolume{
|
|
|
|
ID: "foo",
|
|
|
|
AttachmentMode: structs.CSIVolumeAttachmentModeFilesystem,
|
|
|
|
AccessMode: structs.CSIVolumeAccessModeMultiNodeMultiWriter,
|
|
|
|
MountOptions: &structs.CSIMountOptions{
|
|
|
|
MountFlags: []string{"ro"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
UsageOptions: &UsageOptions{
|
|
|
|
MountOptions: &structs.CSIMountOptions{
|
|
|
|
MountFlags: []string{"rw"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
PluginErr: nil,
|
|
|
|
ExpectedErr: nil,
|
|
|
|
ExpectedCSICallCount: 1,
|
|
|
|
ExpectedVolumeCapability: &csi.VolumeCapability{
|
|
|
|
AccessType: csi.VolumeAccessTypeMount,
|
|
|
|
AccessMode: csi.VolumeAccessModeMultiNodeMultiWriter,
|
|
|
|
MountVolume: &structs.CSIMountOptions{
|
|
|
|
MountFlags: []string{"rw"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2020-01-31 13:45:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range cases {
|
|
|
|
t.Run(tc.Name, func(t *testing.T) {
|
|
|
|
tmpPath := tmpDir(t)
|
|
|
|
defer os.RemoveAll(tmpPath)
|
|
|
|
|
|
|
|
csiFake := &csifake.Client{}
|
|
|
|
csiFake.NextNodePublishVolumeErr = tc.PluginErr
|
|
|
|
|
2020-03-31 21:13:52 +00:00
|
|
|
eventer := func(e *structs.NodeEvent) {}
|
|
|
|
manager := newVolumeManager(testlog.HCLogger(t), eventer, csiFake, tmpPath, tmpPath, true)
|
2020-01-31 13:45:48 +00:00
|
|
|
ctx := context.Background()
|
|
|
|
|
2020-02-17 12:57:25 +00:00
|
|
|
_, err := manager.publishVolume(ctx, tc.Volume, tc.Allocation, tc.UsageOptions, nil)
|
2020-01-31 13:45:48 +00:00
|
|
|
|
|
|
|
if tc.ExpectedErr != nil {
|
|
|
|
require.EqualError(t, err, tc.ExpectedErr.Error())
|
|
|
|
} else {
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
require.Equal(t, tc.ExpectedCSICallCount, csiFake.NodePublishVolumeCallCount)
|
2020-03-23 17:55:26 +00:00
|
|
|
|
|
|
|
if tc.ExpectedVolumeCapability != nil {
|
|
|
|
require.Equal(t, tc.ExpectedVolumeCapability, csiFake.PrevVolumeCapability)
|
|
|
|
}
|
|
|
|
|
2020-01-31 13:45:48 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2020-02-14 12:34:41 +00:00
|
|
|
|
|
|
|
func TestVolumeManager_unpublishVolume(t *testing.T) {
|
2020-05-21 17:56:50 +00:00
|
|
|
if !checkMountSupport() {
|
|
|
|
t.Skip("mount point detection not supported for this platform")
|
|
|
|
}
|
2020-02-14 12:34:41 +00:00
|
|
|
t.Parallel()
|
2020-05-21 17:56:50 +00:00
|
|
|
|
2020-02-14 12:34:41 +00:00
|
|
|
cases := []struct {
|
|
|
|
Name string
|
|
|
|
Allocation *structs.Allocation
|
|
|
|
Volume *structs.CSIVolume
|
2020-02-17 11:10:12 +00:00
|
|
|
UsageOptions *UsageOptions
|
2020-02-14 12:34:41 +00:00
|
|
|
PluginErr error
|
|
|
|
ExpectedErr error
|
|
|
|
ExpectedCSICallCount int64
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
Name: "Returns an error when the plugin returns an error",
|
|
|
|
Allocation: structs.MockAlloc(),
|
|
|
|
Volume: &structs.CSIVolume{
|
|
|
|
ID: "foo",
|
|
|
|
},
|
2020-02-17 11:10:12 +00:00
|
|
|
UsageOptions: &UsageOptions{},
|
2020-02-14 12:34:41 +00:00
|
|
|
PluginErr: errors.New("Some Unknown Error"),
|
|
|
|
ExpectedErr: errors.New("Some Unknown Error"),
|
|
|
|
ExpectedCSICallCount: 1,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "Happy Path",
|
|
|
|
Allocation: structs.MockAlloc(),
|
|
|
|
Volume: &structs.CSIVolume{
|
|
|
|
ID: "foo",
|
|
|
|
},
|
2020-02-17 11:10:12 +00:00
|
|
|
UsageOptions: &UsageOptions{},
|
2020-02-14 12:34:41 +00:00
|
|
|
PluginErr: nil,
|
|
|
|
ExpectedErr: nil,
|
|
|
|
ExpectedCSICallCount: 1,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range cases {
|
|
|
|
t.Run(tc.Name, func(t *testing.T) {
|
|
|
|
tmpPath := tmpDir(t)
|
|
|
|
defer os.RemoveAll(tmpPath)
|
|
|
|
|
|
|
|
csiFake := &csifake.Client{}
|
|
|
|
csiFake.NextNodeUnpublishVolumeErr = tc.PluginErr
|
|
|
|
|
2020-03-31 21:13:52 +00:00
|
|
|
eventer := func(e *structs.NodeEvent) {}
|
|
|
|
manager := newVolumeManager(testlog.HCLogger(t), eventer, csiFake, tmpPath, tmpPath, true)
|
2020-02-14 12:34:41 +00:00
|
|
|
ctx := context.Background()
|
|
|
|
|
2020-04-04 15:03:44 +00:00
|
|
|
err := manager.unpublishVolume(ctx,
|
|
|
|
tc.Volume.ID, tc.Volume.RemoteID(), tc.Allocation.ID, tc.UsageOptions)
|
2020-02-14 12:34:41 +00:00
|
|
|
|
|
|
|
if tc.ExpectedErr != nil {
|
|
|
|
require.EqualError(t, err, tc.ExpectedErr.Error())
|
|
|
|
} else {
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
require.Equal(t, tc.ExpectedCSICallCount, csiFake.NodeUnpublishVolumeCallCount)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2020-03-31 21:13:52 +00:00
|
|
|
|
|
|
|
func TestVolumeManager_MountVolumeEvents(t *testing.T) {
|
2020-05-21 17:56:50 +00:00
|
|
|
if !checkMountSupport() {
|
|
|
|
t.Skip("mount point detection not supported for this platform")
|
|
|
|
}
|
2020-03-31 21:13:52 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
tmpPath := tmpDir(t)
|
|
|
|
defer os.RemoveAll(tmpPath)
|
|
|
|
|
|
|
|
csiFake := &csifake.Client{}
|
|
|
|
|
|
|
|
var events []*structs.NodeEvent
|
|
|
|
eventer := func(e *structs.NodeEvent) {
|
|
|
|
events = append(events, e)
|
|
|
|
}
|
|
|
|
|
|
|
|
manager := newVolumeManager(testlog.HCLogger(t), eventer, csiFake, tmpPath, tmpPath, true)
|
|
|
|
ctx := context.Background()
|
|
|
|
vol := &structs.CSIVolume{
|
|
|
|
ID: "vol",
|
|
|
|
Namespace: "ns",
|
|
|
|
AccessMode: structs.CSIVolumeAccessModeMultiNodeMultiWriter,
|
|
|
|
}
|
|
|
|
alloc := mock.Alloc()
|
|
|
|
usage := &UsageOptions{}
|
|
|
|
pubCtx := map[string]string{}
|
|
|
|
|
|
|
|
_, err := manager.MountVolume(ctx, vol, alloc, usage, pubCtx)
|
|
|
|
require.Error(t, err, "Unknown volume attachment mode: ")
|
|
|
|
require.Equal(t, 1, len(events))
|
|
|
|
e := events[0]
|
|
|
|
require.Equal(t, "Mount volume", e.Message)
|
|
|
|
require.Equal(t, "Storage", e.Subsystem)
|
|
|
|
require.Equal(t, "vol", e.Details["volume_id"])
|
|
|
|
require.Equal(t, "false", e.Details["success"])
|
|
|
|
require.Equal(t, "Unknown volume attachment mode: ", e.Details["error"])
|
|
|
|
events = events[1:]
|
|
|
|
|
|
|
|
vol.AttachmentMode = structs.CSIVolumeAttachmentModeFilesystem
|
|
|
|
_, err = manager.MountVolume(ctx, vol, alloc, usage, pubCtx)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
require.Equal(t, 1, len(events))
|
|
|
|
e = events[0]
|
|
|
|
require.Equal(t, "Mount volume", e.Message)
|
|
|
|
require.Equal(t, "Storage", e.Subsystem)
|
|
|
|
require.Equal(t, "vol", e.Details["volume_id"])
|
|
|
|
require.Equal(t, "true", e.Details["success"])
|
|
|
|
events = events[1:]
|
|
|
|
|
2020-04-04 15:03:44 +00:00
|
|
|
err = manager.UnmountVolume(ctx, vol.ID, vol.RemoteID(), alloc.ID, usage)
|
2020-03-31 21:13:52 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
require.Equal(t, 1, len(events))
|
|
|
|
e = events[0]
|
|
|
|
require.Equal(t, "Unmount volume", e.Message)
|
|
|
|
require.Equal(t, "Storage", e.Subsystem)
|
|
|
|
require.Equal(t, "vol", e.Details["volume_id"])
|
|
|
|
require.Equal(t, "true", e.Details["success"])
|
|
|
|
}
|