2020-04-30 13:13:00 +00:00
|
|
|
package volumewatcher
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/hashicorp/nomad/nomad/mock"
|
|
|
|
"github.com/hashicorp/nomad/nomad/state"
|
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Create a client node with plugin info
|
2020-08-06 18:31:18 +00:00
|
|
|
func testNode(plugin *structs.CSIPlugin, s *state.StateStore) *structs.Node {
|
|
|
|
node := mock.Node()
|
2020-04-30 13:13:00 +00:00
|
|
|
node.Attributes["nomad.version"] = "0.11.0" // client RPCs not supported on early version
|
|
|
|
node.CSINodePlugins = map[string]*structs.CSIInfo{
|
|
|
|
plugin.ID: {
|
|
|
|
PluginID: plugin.ID,
|
|
|
|
Healthy: true,
|
|
|
|
RequiresControllerPlugin: plugin.ControllerRequired,
|
|
|
|
NodeInfo: &structs.CSINodeInfo{},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
if plugin.ControllerRequired {
|
|
|
|
node.CSIControllerPlugins = map[string]*structs.CSIInfo{
|
|
|
|
plugin.ID: {
|
|
|
|
PluginID: plugin.ID,
|
|
|
|
Healthy: true,
|
|
|
|
RequiresControllerPlugin: true,
|
|
|
|
ControllerInfo: &structs.CSIControllerInfo{
|
|
|
|
SupportsReadOnlyAttach: true,
|
|
|
|
SupportsAttachDetach: true,
|
|
|
|
SupportsListVolumes: true,
|
|
|
|
SupportsListVolumesAttachedNodes: false,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
node.CSIControllerPlugins = map[string]*structs.CSIInfo{}
|
|
|
|
}
|
2020-10-19 13:30:15 +00:00
|
|
|
s.UpsertNode(structs.MsgTypeTestSetup, 99, node)
|
2020-04-30 13:13:00 +00:00
|
|
|
return node
|
|
|
|
}
|
|
|
|
|
2022-02-23 14:51:20 +00:00
|
|
|
// Create a test volume with existing claim info
|
2020-08-06 18:31:18 +00:00
|
|
|
func testVolume(plugin *structs.CSIPlugin, alloc *structs.Allocation, nodeID string) *structs.CSIVolume {
|
|
|
|
vol := mock.CSIVolume(plugin)
|
2020-04-30 13:13:00 +00:00
|
|
|
vol.ControllerRequired = plugin.ControllerRequired
|
|
|
|
|
2022-02-23 14:51:20 +00:00
|
|
|
// these modes were set by the previous claim
|
|
|
|
vol.AccessMode = structs.CSIVolumeAccessModeMultiNodeReader
|
|
|
|
vol.AttachmentMode = structs.CSIVolumeAttachmentModeFilesystem
|
|
|
|
|
2020-04-30 13:13:00 +00:00
|
|
|
vol.ReadAllocs = map[string]*structs.Allocation{alloc.ID: alloc}
|
|
|
|
vol.ReadClaims = map[string]*structs.CSIVolumeClaim{
|
|
|
|
alloc.ID: {
|
2022-02-23 14:51:20 +00:00
|
|
|
AllocationID: alloc.ID,
|
|
|
|
NodeID: nodeID,
|
|
|
|
AccessMode: structs.CSIVolumeAccessModeMultiNodeReader,
|
|
|
|
AttachmentMode: structs.CSIVolumeAttachmentModeFilesystem,
|
|
|
|
Mode: structs.CSIVolumeClaimRead,
|
|
|
|
State: structs.CSIVolumeClaimStateTaken,
|
2020-04-30 13:13:00 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
return vol
|
|
|
|
}
|
|
|
|
|
|
|
|
type MockRPCServer struct {
|
|
|
|
state *state.StateStore
|
|
|
|
|
2020-08-06 18:31:18 +00:00
|
|
|
nextCSIUnpublishResponse *structs.CSIVolumeUnpublishResponse
|
|
|
|
nextCSIUnpublishError error
|
|
|
|
countCSIUnpublish int
|
2020-04-30 13:13:00 +00:00
|
|
|
}
|
|
|
|
|
2020-08-06 18:31:18 +00:00
|
|
|
func (srv *MockRPCServer) Unpublish(args *structs.CSIVolumeUnpublishRequest, reply *structs.CSIVolumeUnpublishResponse) error {
|
|
|
|
reply = srv.nextCSIUnpublishResponse
|
|
|
|
srv.countCSIUnpublish++
|
|
|
|
return srv.nextCSIUnpublishError
|
2020-04-30 13:13:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (srv *MockRPCServer) State() *state.StateStore { return srv.state }
|
|
|
|
|
|
|
|
type MockBatchingRPCServer struct {
|
|
|
|
MockRPCServer
|
|
|
|
}
|
|
|
|
|
|
|
|
type MockStatefulRPCServer struct {
|
|
|
|
MockRPCServer
|
|
|
|
}
|