mirror of
https://github.com/kemko/nomad.git
synced 2026-01-06 18:35:44 +03:00
test: Refactor mock CSI manager (#18554)
and MockCSIManager to support the call counting that csi_hook_test expects instead of implementing csimanager interfaces in two separate places: * client/allocrunner/csi_hook_test * client/csi_endpoint_test they can both use the same mocks defined in client/pluginmanager/csimanager/ alongside the actual implementations of them. also refactor TestCSINode_DetachVolume to use use it like Node_ExpandVolume so we can also test the happy path there
This commit is contained in:
@@ -5,10 +5,12 @@ package csimanager
|
||||
|
||||
import (
|
||||
"context"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/hashicorp/nomad/client/pluginmanager"
|
||||
nstructs "github.com/hashicorp/nomad/nomad/structs"
|
||||
"github.com/hashicorp/nomad/plugins/csi"
|
||||
"github.com/hashicorp/nomad/testutil"
|
||||
)
|
||||
|
||||
var _ Manager = &MockCSIManager{}
|
||||
@@ -29,6 +31,9 @@ func (m *MockCSIManager) WaitForPlugin(_ context.Context, pluginType, pluginID s
|
||||
}
|
||||
|
||||
func (m *MockCSIManager) ManagerForPlugin(_ context.Context, pluginID string) (VolumeManager, error) {
|
||||
if m.VM == nil {
|
||||
m.VM = &MockVolumeManager{}
|
||||
}
|
||||
return m.VM, m.NextManagerForPluginErr
|
||||
}
|
||||
|
||||
@@ -39,20 +44,68 @@ func (m *MockCSIManager) Shutdown() {
|
||||
var _ VolumeManager = &MockVolumeManager{}
|
||||
|
||||
type MockVolumeManager struct {
|
||||
CallCounter *testutil.CallCounter
|
||||
|
||||
Mounts map[string]bool // lazy set
|
||||
|
||||
NextMountVolumeErr error
|
||||
NextUnmountVolumeErr error
|
||||
|
||||
NextExpandVolumeErr error
|
||||
LastExpandVolumeCall *MockExpandVolumeCall
|
||||
}
|
||||
|
||||
func (m *MockVolumeManager) mountName(volID, allocID string, usageOpts *UsageOptions) string {
|
||||
return filepath.Join("test-alloc-dir", allocID, volID, usageOpts.ToFS())
|
||||
}
|
||||
|
||||
func (m *MockVolumeManager) MountVolume(_ context.Context, vol *nstructs.CSIVolume, alloc *nstructs.Allocation, usageOpts *UsageOptions, publishContext map[string]string) (*MountInfo, error) {
|
||||
panic("implement me")
|
||||
if m.CallCounter != nil {
|
||||
m.CallCounter.Inc("MountVolume")
|
||||
}
|
||||
|
||||
if m.NextMountVolumeErr != nil {
|
||||
err := m.NextMountVolumeErr
|
||||
m.NextMountVolumeErr = nil // reset it
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// "mount" it
|
||||
if m.Mounts == nil {
|
||||
m.Mounts = make(map[string]bool)
|
||||
}
|
||||
source := m.mountName(vol.ID, alloc.ID, usageOpts)
|
||||
m.Mounts[source] = true
|
||||
|
||||
return &MountInfo{
|
||||
Source: source,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (m *MockVolumeManager) UnmountVolume(_ context.Context, volID, remoteID, allocID string, usageOpts *UsageOptions) error {
|
||||
panic("implement me")
|
||||
if m.CallCounter != nil {
|
||||
m.CallCounter.Inc("UnmountVolume")
|
||||
}
|
||||
|
||||
if m.NextUnmountVolumeErr != nil {
|
||||
err := m.NextUnmountVolumeErr
|
||||
m.NextUnmountVolumeErr = nil // reset it
|
||||
return err
|
||||
}
|
||||
|
||||
// "unmount" it
|
||||
delete(m.Mounts, m.mountName(volID, allocID, usageOpts))
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *MockVolumeManager) HasMount(_ context.Context, mountInfo *MountInfo) (bool, error) {
|
||||
panic("implement me")
|
||||
if m.CallCounter != nil {
|
||||
m.CallCounter.Inc("HasMount")
|
||||
}
|
||||
if m.Mounts == nil {
|
||||
return false, nil
|
||||
}
|
||||
return m.Mounts[mountInfo.Source], nil
|
||||
}
|
||||
|
||||
func (m *MockVolumeManager) ExpandVolume(_ context.Context, volID, remoteID, allocID string, usageOpts *UsageOptions, capacity *csi.CapacityRange) (int64, error) {
|
||||
|
||||
Reference in New Issue
Block a user