2023-04-10 15:36:59 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2022-01-07 20:23:47 +00:00
|
|
|
package allocrunner
|
|
|
|
|
|
|
|
import (
|
2022-04-02 00:24:02 +00:00
|
|
|
"errors"
|
2022-01-07 20:23:47 +00:00
|
|
|
"fmt"
|
|
|
|
"testing"
|
2022-01-28 13:30:31 +00:00
|
|
|
"time"
|
2022-01-07 20:23:47 +00:00
|
|
|
|
2022-03-15 12:42:43 +00:00
|
|
|
"github.com/hashicorp/nomad/ci"
|
2022-01-07 20:23:47 +00:00
|
|
|
"github.com/hashicorp/nomad/client/allocrunner/interfaces"
|
2023-07-10 17:20:15 +00:00
|
|
|
"github.com/hashicorp/nomad/client/allocrunner/state"
|
2022-01-07 20:23:47 +00:00
|
|
|
"github.com/hashicorp/nomad/client/pluginmanager/csimanager"
|
|
|
|
cstructs "github.com/hashicorp/nomad/client/structs"
|
2022-08-17 16:26:34 +00:00
|
|
|
"github.com/hashicorp/nomad/helper/pointer"
|
2022-01-07 20:23:47 +00:00
|
|
|
"github.com/hashicorp/nomad/helper/testlog"
|
|
|
|
"github.com/hashicorp/nomad/nomad/mock"
|
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
|
|
|
"github.com/hashicorp/nomad/plugins/drivers"
|
2023-09-21 22:16:14 +00:00
|
|
|
"github.com/hashicorp/nomad/testutil"
|
2023-07-10 17:20:15 +00:00
|
|
|
"github.com/shoenig/test/must"
|
2022-04-02 00:24:02 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2022-01-07 20:23:47 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var _ interfaces.RunnerPrerunHook = (*csiHook)(nil)
|
|
|
|
var _ interfaces.RunnerPostrunHook = (*csiHook)(nil)
|
|
|
|
|
|
|
|
func TestCSIHook(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2022-01-07 20:23:47 +00:00
|
|
|
|
|
|
|
alloc := mock.Alloc()
|
2023-07-10 17:20:15 +00:00
|
|
|
testMountSrc := fmt.Sprintf(
|
|
|
|
"test-alloc-dir/%s/testvolume0/ro-file-system-single-node-reader-only", alloc.ID)
|
2022-01-07 20:23:47 +00:00
|
|
|
logger := testlog.HCLogger(t)
|
|
|
|
|
|
|
|
testcases := []struct {
|
2023-07-10 17:20:15 +00:00
|
|
|
name string
|
|
|
|
volumeRequests map[string]*structs.VolumeRequest
|
|
|
|
startsUnschedulable bool
|
|
|
|
startsWithClaims bool
|
|
|
|
startsWithStubs map[string]*state.CSIVolumeStub
|
|
|
|
startsWithValidMounts bool
|
|
|
|
failsFirstUnmount bool
|
|
|
|
expectedClaimErr error
|
|
|
|
expectedMounts map[string]*csimanager.MountInfo
|
|
|
|
expectedCalls map[string]int
|
2022-01-07 20:23:47 +00:00
|
|
|
}{
|
|
|
|
|
|
|
|
{
|
|
|
|
name: "simple case",
|
|
|
|
volumeRequests: map[string]*structs.VolumeRequest{
|
|
|
|
"vol0": {
|
|
|
|
Name: "vol0",
|
|
|
|
Type: structs.VolumeTypeCSI,
|
|
|
|
Source: "testvolume0",
|
|
|
|
ReadOnly: true,
|
|
|
|
AccessMode: structs.CSIVolumeAccessModeSingleNodeReader,
|
|
|
|
AttachmentMode: structs.CSIVolumeAttachmentModeFilesystem,
|
|
|
|
MountOptions: &structs.CSIMountOptions{},
|
|
|
|
PerAlloc: false,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expectedMounts: map[string]*csimanager.MountInfo{
|
2023-07-10 17:20:15 +00:00
|
|
|
"vol0": &csimanager.MountInfo{Source: testMountSrc},
|
2022-01-07 20:23:47 +00:00
|
|
|
},
|
2023-07-10 17:20:15 +00:00
|
|
|
expectedCalls: map[string]int{
|
2023-09-21 22:16:14 +00:00
|
|
|
"claim": 1, "MountVolume": 1, "UnmountVolume": 1, "unpublish": 1},
|
2022-01-07 20:23:47 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
name: "per-alloc case",
|
|
|
|
volumeRequests: map[string]*structs.VolumeRequest{
|
|
|
|
"vol0": {
|
|
|
|
Name: "vol0",
|
|
|
|
Type: structs.VolumeTypeCSI,
|
|
|
|
Source: "testvolume0",
|
|
|
|
ReadOnly: true,
|
|
|
|
AccessMode: structs.CSIVolumeAccessModeSingleNodeReader,
|
|
|
|
AttachmentMode: structs.CSIVolumeAttachmentModeFilesystem,
|
|
|
|
MountOptions: &structs.CSIMountOptions{},
|
|
|
|
PerAlloc: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expectedMounts: map[string]*csimanager.MountInfo{
|
2023-07-10 17:20:15 +00:00
|
|
|
"vol0": &csimanager.MountInfo{Source: testMountSrc},
|
2022-01-07 20:23:47 +00:00
|
|
|
},
|
2023-07-10 17:20:15 +00:00
|
|
|
expectedCalls: map[string]int{
|
2023-09-21 22:16:14 +00:00
|
|
|
"claim": 1, "MountVolume": 1, "UnmountVolume": 1, "unpublish": 1},
|
2022-01-07 20:23:47 +00:00
|
|
|
},
|
|
|
|
|
2022-02-24 15:39:07 +00:00
|
|
|
{
|
|
|
|
name: "fatal error on claim",
|
|
|
|
volumeRequests: map[string]*structs.VolumeRequest{
|
|
|
|
"vol0": {
|
|
|
|
Name: "vol0",
|
|
|
|
Type: structs.VolumeTypeCSI,
|
|
|
|
Source: "testvolume0",
|
|
|
|
ReadOnly: true,
|
|
|
|
AccessMode: structs.CSIVolumeAccessModeSingleNodeReader,
|
|
|
|
AttachmentMode: structs.CSIVolumeAttachmentModeFilesystem,
|
|
|
|
MountOptions: &structs.CSIMountOptions{},
|
|
|
|
PerAlloc: false,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
startsUnschedulable: true,
|
|
|
|
expectedMounts: map[string]*csimanager.MountInfo{
|
2023-07-10 17:20:15 +00:00
|
|
|
"vol0": &csimanager.MountInfo{Source: testMountSrc},
|
2022-02-24 15:39:07 +00:00
|
|
|
},
|
2023-07-10 17:20:15 +00:00
|
|
|
expectedCalls: map[string]int{"claim": 1},
|
2022-02-24 15:39:07 +00:00
|
|
|
expectedClaimErr: errors.New(
|
2023-07-10 17:20:15 +00:00
|
|
|
"claiming volumes: could not claim volume testvolume0: volume is currently unschedulable"),
|
2022-02-24 15:39:07 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
name: "retryable error on claim",
|
|
|
|
volumeRequests: map[string]*structs.VolumeRequest{
|
|
|
|
"vol0": {
|
|
|
|
Name: "vol0",
|
|
|
|
Type: structs.VolumeTypeCSI,
|
|
|
|
Source: "testvolume0",
|
|
|
|
ReadOnly: true,
|
|
|
|
AccessMode: structs.CSIVolumeAccessModeSingleNodeReader,
|
|
|
|
AttachmentMode: structs.CSIVolumeAttachmentModeFilesystem,
|
|
|
|
MountOptions: &structs.CSIMountOptions{},
|
|
|
|
PerAlloc: false,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
startsWithClaims: true,
|
|
|
|
expectedMounts: map[string]*csimanager.MountInfo{
|
2023-07-10 17:20:15 +00:00
|
|
|
"vol0": &csimanager.MountInfo{Source: testMountSrc},
|
2022-02-24 15:39:07 +00:00
|
|
|
},
|
2023-07-10 17:20:15 +00:00
|
|
|
expectedCalls: map[string]int{
|
2023-09-21 22:16:14 +00:00
|
|
|
"claim": 2, "MountVolume": 1, "UnmountVolume": 1, "unpublish": 1},
|
2023-07-10 17:20:15 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "already mounted",
|
|
|
|
volumeRequests: map[string]*structs.VolumeRequest{
|
|
|
|
"vol0": {
|
|
|
|
Name: "vol0",
|
|
|
|
Type: structs.VolumeTypeCSI,
|
|
|
|
Source: "testvolume0",
|
|
|
|
ReadOnly: true,
|
|
|
|
AccessMode: structs.CSIVolumeAccessModeSingleNodeReader,
|
|
|
|
AttachmentMode: structs.CSIVolumeAttachmentModeFilesystem,
|
|
|
|
MountOptions: &structs.CSIMountOptions{},
|
|
|
|
PerAlloc: false,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
startsWithStubs: map[string]*state.CSIVolumeStub{"vol0": {
|
|
|
|
VolumeID: "vol0",
|
|
|
|
PluginID: "vol0-plugin",
|
|
|
|
ExternalNodeID: "i-example",
|
|
|
|
MountInfo: &csimanager.MountInfo{Source: testMountSrc},
|
|
|
|
}},
|
|
|
|
startsWithValidMounts: true,
|
|
|
|
expectedMounts: map[string]*csimanager.MountInfo{
|
|
|
|
"vol0": &csimanager.MountInfo{Source: testMountSrc},
|
|
|
|
},
|
2023-09-21 22:16:14 +00:00
|
|
|
expectedCalls: map[string]int{"HasMount": 1, "UnmountVolume": 1, "unpublish": 1},
|
2023-07-10 17:20:15 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "existing but invalid mounts",
|
|
|
|
volumeRequests: map[string]*structs.VolumeRequest{
|
|
|
|
"vol0": {
|
|
|
|
Name: "vol0",
|
|
|
|
Type: structs.VolumeTypeCSI,
|
|
|
|
Source: "testvolume0",
|
|
|
|
ReadOnly: true,
|
|
|
|
AccessMode: structs.CSIVolumeAccessModeSingleNodeReader,
|
|
|
|
AttachmentMode: structs.CSIVolumeAttachmentModeFilesystem,
|
|
|
|
MountOptions: &structs.CSIMountOptions{},
|
|
|
|
PerAlloc: false,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
startsWithStubs: map[string]*state.CSIVolumeStub{"vol0": {
|
|
|
|
VolumeID: "testvolume0",
|
|
|
|
PluginID: "vol0-plugin",
|
|
|
|
ExternalNodeID: "i-example",
|
|
|
|
MountInfo: &csimanager.MountInfo{Source: testMountSrc},
|
|
|
|
}},
|
|
|
|
startsWithValidMounts: false,
|
|
|
|
expectedMounts: map[string]*csimanager.MountInfo{
|
|
|
|
"vol0": &csimanager.MountInfo{Source: testMountSrc},
|
|
|
|
},
|
|
|
|
expectedCalls: map[string]int{
|
2023-09-21 22:16:14 +00:00
|
|
|
"HasMount": 1, "claim": 1, "MountVolume": 1, "UnmountVolume": 1, "unpublish": 1},
|
2022-02-24 15:39:07 +00:00
|
|
|
},
|
|
|
|
|
2023-07-10 17:20:15 +00:00
|
|
|
{
|
|
|
|
name: "retry on failed unmount",
|
|
|
|
volumeRequests: map[string]*structs.VolumeRequest{
|
|
|
|
"vol0": {
|
|
|
|
Name: "vol0",
|
|
|
|
Type: structs.VolumeTypeCSI,
|
|
|
|
Source: "testvolume0",
|
|
|
|
ReadOnly: true,
|
|
|
|
AccessMode: structs.CSIVolumeAccessModeSingleNodeReader,
|
|
|
|
AttachmentMode: structs.CSIVolumeAttachmentModeFilesystem,
|
|
|
|
MountOptions: &structs.CSIMountOptions{},
|
|
|
|
PerAlloc: false,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
failsFirstUnmount: true,
|
|
|
|
expectedMounts: map[string]*csimanager.MountInfo{
|
|
|
|
"vol0": &csimanager.MountInfo{Source: testMountSrc},
|
|
|
|
},
|
|
|
|
expectedCalls: map[string]int{
|
2023-09-21 22:16:14 +00:00
|
|
|
"claim": 1, "MountVolume": 1, "UnmountVolume": 2, "unpublish": 2},
|
2023-07-10 17:20:15 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
name: "should not run",
|
|
|
|
volumeRequests: map[string]*structs.VolumeRequest{},
|
|
|
|
},
|
2022-01-07 20:23:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for i := range testcases {
|
|
|
|
tc := testcases[i]
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
2023-07-10 17:20:15 +00:00
|
|
|
|
2022-01-07 20:23:47 +00:00
|
|
|
alloc.Job.TaskGroups[0].Volumes = tc.volumeRequests
|
|
|
|
|
2023-09-21 22:16:14 +00:00
|
|
|
callCounts := testutil.NewCallCounter()
|
|
|
|
vm := &csimanager.MockVolumeManager{
|
|
|
|
CallCounter: callCounts,
|
|
|
|
}
|
|
|
|
mgr := &csimanager.MockCSIManager{VM: vm}
|
2022-02-24 15:39:07 +00:00
|
|
|
rpcer := mockRPCer{
|
|
|
|
alloc: alloc,
|
|
|
|
callCounts: callCounts,
|
2022-08-17 16:26:34 +00:00
|
|
|
hasExistingClaim: pointer.Of(tc.startsWithClaims),
|
|
|
|
schedulable: pointer.Of(!tc.startsUnschedulable),
|
2022-02-24 15:39:07 +00:00
|
|
|
}
|
2022-01-07 20:23:47 +00:00
|
|
|
ar := mockAllocRunner{
|
|
|
|
res: &cstructs.AllocHookResources{},
|
|
|
|
caps: &drivers.Capabilities{
|
|
|
|
FSIsolation: drivers.FSIsolationChroot,
|
|
|
|
MountConfigs: drivers.MountConfigSupportAll,
|
|
|
|
},
|
2023-07-10 17:20:15 +00:00
|
|
|
stubs: tc.startsWithStubs,
|
2022-01-07 20:23:47 +00:00
|
|
|
}
|
2023-07-10 17:20:15 +00:00
|
|
|
|
2023-04-03 15:03:36 +00:00
|
|
|
hook := newCSIHook(alloc, logger, mgr, rpcer, ar, ar.res, "secret")
|
2022-02-24 15:39:07 +00:00
|
|
|
hook.minBackoffInterval = 1 * time.Millisecond
|
|
|
|
hook.maxBackoffInterval = 10 * time.Millisecond
|
|
|
|
hook.maxBackoffDuration = 500 * time.Millisecond
|
2022-01-28 13:30:31 +00:00
|
|
|
|
2023-07-10 17:20:15 +00:00
|
|
|
must.NotNil(t, hook)
|
2022-01-07 20:23:47 +00:00
|
|
|
|
2023-09-21 22:16:14 +00:00
|
|
|
if tc.startsWithValidMounts {
|
|
|
|
// TODO: this works, but it requires knowledge of how the mock works. would rather vm.MountVolume()
|
|
|
|
vm.Mounts = map[string]bool{
|
|
|
|
tc.expectedMounts["vol0"].Source: true,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if tc.failsFirstUnmount {
|
|
|
|
vm.NextUnmountVolumeErr = errors.New("bad first attempt")
|
|
|
|
}
|
|
|
|
|
2022-02-24 15:39:07 +00:00
|
|
|
if tc.expectedClaimErr != nil {
|
2023-07-10 17:20:15 +00:00
|
|
|
must.EqError(t, hook.Prerun(), tc.expectedClaimErr.Error())
|
2023-04-03 15:03:36 +00:00
|
|
|
mounts := ar.res.GetCSIMounts()
|
2023-07-10 17:20:15 +00:00
|
|
|
must.Nil(t, mounts)
|
2022-02-24 15:39:07 +00:00
|
|
|
} else {
|
2023-07-10 17:20:15 +00:00
|
|
|
must.NoError(t, hook.Prerun())
|
2023-04-03 15:03:36 +00:00
|
|
|
mounts := ar.res.GetCSIMounts()
|
2023-07-10 17:20:15 +00:00
|
|
|
must.MapEq(t, tc.expectedMounts, mounts,
|
|
|
|
must.Sprintf("got mounts: %v", mounts))
|
|
|
|
must.NoError(t, hook.Postrun())
|
2022-02-24 15:39:07 +00:00
|
|
|
}
|
2022-01-07 20:23:47 +00:00
|
|
|
|
2023-07-10 17:20:15 +00:00
|
|
|
if tc.failsFirstUnmount {
|
|
|
|
// retrying the unmount doesn't block Postrun, so give it time
|
|
|
|
// to run once more before checking the call counts to ensure
|
|
|
|
// this doesn't flake between 1 and 2 unmount/unpublish calls
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
}
|
|
|
|
|
2023-09-21 22:16:14 +00:00
|
|
|
counts := callCounts.Get()
|
2023-07-10 17:20:15 +00:00
|
|
|
must.MapEq(t, tc.expectedCalls, counts,
|
|
|
|
must.Sprintf("got calls: %v", counts))
|
2022-01-07 20:23:47 +00:00
|
|
|
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-07-10 17:20:15 +00:00
|
|
|
// TestCSIHook_Prerun_Validation tests that the validation of task capabilities
|
|
|
|
// in Prerun ensures at least one task supports CSI.
|
|
|
|
func TestCSIHook_Prerun_Validation(t *testing.T) {
|
2022-06-22 14:43:43 +00:00
|
|
|
ci.Parallel(t)
|
|
|
|
|
|
|
|
alloc := mock.Alloc()
|
|
|
|
logger := testlog.HCLogger(t)
|
|
|
|
volumeRequests := map[string]*structs.VolumeRequest{
|
|
|
|
"vol0": {
|
|
|
|
Name: "vol0",
|
|
|
|
Type: structs.VolumeTypeCSI,
|
|
|
|
Source: "testvolume0",
|
|
|
|
ReadOnly: true,
|
|
|
|
AccessMode: structs.CSIVolumeAccessModeSingleNodeReader,
|
|
|
|
AttachmentMode: structs.CSIVolumeAttachmentModeFilesystem,
|
|
|
|
MountOptions: &structs.CSIMountOptions{},
|
|
|
|
PerAlloc: false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
type testCase struct {
|
2023-07-10 17:20:15 +00:00
|
|
|
name string
|
|
|
|
caps *drivers.Capabilities
|
|
|
|
capFunc func() (*drivers.Capabilities, error)
|
|
|
|
expectedErr string
|
2022-06-22 14:43:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
testcases := []testCase{
|
|
|
|
{
|
|
|
|
name: "invalid - driver does not support CSI",
|
|
|
|
caps: &drivers.Capabilities{
|
|
|
|
MountConfigs: drivers.MountConfigSupportNone,
|
|
|
|
},
|
2023-07-10 17:20:15 +00:00
|
|
|
capFunc: nil,
|
|
|
|
expectedErr: "no task supports CSI",
|
2022-06-22 14:43:43 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
name: "invalid - driver error",
|
|
|
|
caps: &drivers.Capabilities{},
|
|
|
|
capFunc: func() (*drivers.Capabilities, error) {
|
|
|
|
return nil, errors.New("error thrown by driver")
|
|
|
|
},
|
2023-07-10 17:20:15 +00:00
|
|
|
expectedErr: "could not validate task driver capabilities: error thrown by driver",
|
2022-06-22 14:43:43 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
name: "valid - driver supports CSI",
|
|
|
|
caps: &drivers.Capabilities{
|
|
|
|
MountConfigs: drivers.MountConfigSupportAll,
|
|
|
|
},
|
2023-07-10 17:20:15 +00:00
|
|
|
capFunc: nil,
|
2022-06-22 14:43:43 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testcases {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
alloc.Job.TaskGroups[0].Volumes = volumeRequests
|
|
|
|
|
2023-09-21 22:16:14 +00:00
|
|
|
mgr := &csimanager.MockCSIManager{
|
|
|
|
VM: &csimanager.MockVolumeManager{},
|
|
|
|
}
|
2022-06-22 14:43:43 +00:00
|
|
|
rpcer := mockRPCer{
|
|
|
|
alloc: alloc,
|
2023-09-21 22:16:14 +00:00
|
|
|
callCounts: testutil.NewCallCounter(),
|
2022-08-17 16:26:34 +00:00
|
|
|
hasExistingClaim: pointer.Of(false),
|
|
|
|
schedulable: pointer.Of(true),
|
2022-06-22 14:43:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ar := mockAllocRunner{
|
|
|
|
res: &cstructs.AllocHookResources{},
|
|
|
|
caps: tc.caps,
|
|
|
|
capFunc: tc.capFunc,
|
|
|
|
}
|
|
|
|
|
2023-04-03 15:03:36 +00:00
|
|
|
hook := newCSIHook(alloc, logger, mgr, rpcer, ar, ar.res, "secret")
|
2022-06-22 14:43:43 +00:00
|
|
|
require.NotNil(t, hook)
|
|
|
|
|
2023-07-10 17:20:15 +00:00
|
|
|
if tc.expectedErr != "" {
|
|
|
|
require.EqualError(t, hook.Prerun(), tc.expectedErr)
|
2023-04-03 15:03:36 +00:00
|
|
|
mounts := ar.res.GetCSIMounts()
|
2022-06-22 14:43:43 +00:00
|
|
|
require.Nil(t, mounts)
|
|
|
|
} else {
|
|
|
|
require.NoError(t, hook.Prerun())
|
2023-04-03 15:03:36 +00:00
|
|
|
mounts := ar.res.GetCSIMounts()
|
2022-06-22 14:43:43 +00:00
|
|
|
require.NotNil(t, mounts)
|
|
|
|
require.NoError(t, hook.Postrun())
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-07 20:23:47 +00:00
|
|
|
// HELPERS AND MOCKS
|
|
|
|
|
|
|
|
type mockRPCer struct {
|
2022-02-24 15:39:07 +00:00
|
|
|
alloc *structs.Allocation
|
2023-09-21 22:16:14 +00:00
|
|
|
callCounts *testutil.CallCounter
|
2022-02-24 15:39:07 +00:00
|
|
|
hasExistingClaim *bool
|
|
|
|
schedulable *bool
|
2022-01-07 20:23:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// RPC mocks the server RPCs, acting as though any request succeeds
|
2023-07-10 17:20:15 +00:00
|
|
|
func (r mockRPCer) RPC(method string, args any, reply any) error {
|
2022-01-07 20:23:47 +00:00
|
|
|
switch method {
|
|
|
|
case "CSIVolume.Claim":
|
2023-09-21 22:16:14 +00:00
|
|
|
r.callCounts.Inc("claim")
|
2022-01-07 20:23:47 +00:00
|
|
|
req := args.(*structs.CSIVolumeClaimRequest)
|
2022-02-24 15:39:07 +00:00
|
|
|
vol := r.testVolume(req.VolumeID)
|
2022-01-07 20:23:47 +00:00
|
|
|
err := vol.Claim(req.ToClaim(), r.alloc)
|
2023-07-10 17:20:15 +00:00
|
|
|
|
|
|
|
// after the first claim attempt is made, reset the volume's claims as
|
|
|
|
// though it's been released from another node
|
|
|
|
*r.hasExistingClaim = false
|
|
|
|
*r.schedulable = true
|
|
|
|
|
2022-01-07 20:23:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
resp := reply.(*structs.CSIVolumeClaimResponse)
|
|
|
|
resp.PublishContext = map[string]string{}
|
|
|
|
resp.Volume = vol
|
|
|
|
resp.QueryMeta = structs.QueryMeta{}
|
2023-07-10 17:20:15 +00:00
|
|
|
|
2022-01-07 20:23:47 +00:00
|
|
|
case "CSIVolume.Unpublish":
|
2023-09-21 22:16:14 +00:00
|
|
|
r.callCounts.Inc("unpublish")
|
2022-01-07 20:23:47 +00:00
|
|
|
resp := reply.(*structs.CSIVolumeUnpublishResponse)
|
|
|
|
resp.QueryMeta = structs.QueryMeta{}
|
2023-07-10 17:20:15 +00:00
|
|
|
|
2022-01-07 20:23:47 +00:00
|
|
|
default:
|
|
|
|
return fmt.Errorf("unexpected method")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-07-10 17:20:15 +00:00
|
|
|
// testVolume is a helper that optionally starts as unschedulable / claimed, so
|
|
|
|
// that we can test retryable vs non-retryable failures
|
2022-02-24 15:39:07 +00:00
|
|
|
func (r mockRPCer) testVolume(id string) *structs.CSIVolume {
|
|
|
|
vol := structs.NewCSIVolume(id, 0)
|
|
|
|
vol.Schedulable = *r.schedulable
|
2023-07-10 17:20:15 +00:00
|
|
|
vol.PluginID = "plugin-" + id
|
2022-02-24 15:39:07 +00:00
|
|
|
vol.RequestedCapabilities = []*structs.CSIVolumeCapability{
|
|
|
|
{
|
|
|
|
AttachmentMode: structs.CSIVolumeAttachmentModeFilesystem,
|
|
|
|
AccessMode: structs.CSIVolumeAccessModeSingleNodeReader,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
AttachmentMode: structs.CSIVolumeAttachmentModeFilesystem,
|
|
|
|
AccessMode: structs.CSIVolumeAccessModeSingleNodeWriter,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
if *r.hasExistingClaim {
|
|
|
|
vol.AccessMode = structs.CSIVolumeAccessModeSingleNodeReader
|
|
|
|
vol.AttachmentMode = structs.CSIVolumeAttachmentModeFilesystem
|
|
|
|
vol.ReadClaims["another-alloc-id"] = &structs.CSIVolumeClaim{
|
|
|
|
AllocationID: "another-alloc-id",
|
|
|
|
NodeID: "another-node-id",
|
|
|
|
Mode: structs.CSIVolumeClaimRead,
|
|
|
|
AccessMode: structs.CSIVolumeAccessModeSingleNodeReader,
|
|
|
|
AttachmentMode: structs.CSIVolumeAttachmentModeFilesystem,
|
|
|
|
State: structs.CSIVolumeClaimStateTaken,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return vol
|
|
|
|
}
|
|
|
|
|
2022-01-07 20:23:47 +00:00
|
|
|
type mockAllocRunner struct {
|
2022-06-22 14:43:43 +00:00
|
|
|
res *cstructs.AllocHookResources
|
|
|
|
caps *drivers.Capabilities
|
|
|
|
capFunc func() (*drivers.Capabilities, error)
|
2023-07-10 17:20:15 +00:00
|
|
|
|
|
|
|
stubs map[string]*state.CSIVolumeStub
|
|
|
|
stubFunc func() (map[string]*state.CSIVolumeStub, error)
|
2022-01-07 20:23:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ar mockAllocRunner) GetTaskDriverCapabilities(taskName string) (*drivers.Capabilities, error) {
|
2022-06-22 14:43:43 +00:00
|
|
|
if ar.capFunc != nil {
|
|
|
|
return ar.capFunc()
|
|
|
|
}
|
2022-01-07 20:23:47 +00:00
|
|
|
return ar.caps, nil
|
|
|
|
}
|
2023-07-10 17:20:15 +00:00
|
|
|
|
|
|
|
func (ar mockAllocRunner) SetCSIVolumes(stubs map[string]*state.CSIVolumeStub) error {
|
|
|
|
ar.stubs = stubs
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ar mockAllocRunner) GetCSIVolumes() (map[string]*state.CSIVolumeStub, error) {
|
|
|
|
if ar.stubFunc != nil {
|
|
|
|
return ar.stubFunc()
|
|
|
|
}
|
|
|
|
return ar.stubs, nil
|
|
|
|
}
|