github.com/hernad/nomad@v1.6.112/nomad/volumewatcher/interfaces_test.go (about)

     1  // Copyright (c) HashiCorp, Inc.
     2  // SPDX-License-Identifier: MPL-2.0
     3  
     4  package volumewatcher
     5  
     6  import (
     7  	"github.com/hernad/nomad/nomad/mock"
     8  	"github.com/hernad/nomad/nomad/state"
     9  	"github.com/hernad/nomad/nomad/structs"
    10  )
    11  
    12  // Create a client node with plugin info
    13  func testNode(plugin *structs.CSIPlugin, s *state.StateStore) *structs.Node {
    14  	node := mock.Node()
    15  	node.Attributes["nomad.version"] = "0.11.0" // client RPCs not supported on early version
    16  	node.CSINodePlugins = map[string]*structs.CSIInfo{
    17  		plugin.ID: {
    18  			PluginID:                 plugin.ID,
    19  			Healthy:                  true,
    20  			RequiresControllerPlugin: plugin.ControllerRequired,
    21  			NodeInfo:                 &structs.CSINodeInfo{},
    22  		},
    23  	}
    24  	if plugin.ControllerRequired {
    25  		node.CSIControllerPlugins = map[string]*structs.CSIInfo{
    26  			plugin.ID: {
    27  				PluginID:                 plugin.ID,
    28  				Healthy:                  true,
    29  				RequiresControllerPlugin: true,
    30  				ControllerInfo: &structs.CSIControllerInfo{
    31  					SupportsReadOnlyAttach:           true,
    32  					SupportsAttachDetach:             true,
    33  					SupportsListVolumes:              true,
    34  					SupportsListVolumesAttachedNodes: false,
    35  				},
    36  			},
    37  		}
    38  	} else {
    39  		node.CSIControllerPlugins = map[string]*structs.CSIInfo{}
    40  	}
    41  	s.UpsertNode(structs.MsgTypeTestSetup, 99, node)
    42  	return node
    43  }
    44  
    45  // Create a test volume with existing claim info
    46  func testVolume(plugin *structs.CSIPlugin, alloc *structs.Allocation, nodeID string) *structs.CSIVolume {
    47  	vol := mock.CSIVolume(plugin)
    48  	vol.ControllerRequired = plugin.ControllerRequired
    49  
    50  	// these modes were set by the previous claim
    51  	vol.AccessMode = structs.CSIVolumeAccessModeMultiNodeReader
    52  	vol.AttachmentMode = structs.CSIVolumeAttachmentModeFilesystem
    53  
    54  	vol.ReadAllocs = map[string]*structs.Allocation{alloc.ID: alloc}
    55  	vol.ReadClaims = map[string]*structs.CSIVolumeClaim{
    56  		alloc.ID: {
    57  			AllocationID:   alloc.ID,
    58  			NodeID:         nodeID,
    59  			AccessMode:     structs.CSIVolumeAccessModeMultiNodeReader,
    60  			AttachmentMode: structs.CSIVolumeAttachmentModeFilesystem,
    61  			Mode:           structs.CSIVolumeClaimRead,
    62  			State:          structs.CSIVolumeClaimStateTaken,
    63  		},
    64  	}
    65  	return vol
    66  }
    67  
    68  type MockRPCServer struct {
    69  	state *state.StateStore
    70  
    71  	nextCSIUnpublishResponse *structs.CSIVolumeUnpublishResponse
    72  	nextCSIUnpublishError    error
    73  	countCSIUnpublish        int
    74  }
    75  
    76  func (srv *MockRPCServer) Unpublish(args *structs.CSIVolumeUnpublishRequest, reply *structs.CSIVolumeUnpublishResponse) error {
    77  	reply = srv.nextCSIUnpublishResponse
    78  	srv.countCSIUnpublish++
    79  	return srv.nextCSIUnpublishError
    80  }
    81  
    82  func (srv *MockRPCServer) State() *state.StateStore { return srv.state }
    83  
    84  type MockBatchingRPCServer struct {
    85  	MockRPCServer
    86  }
    87  
    88  type MockStatefulRPCServer struct {
    89  	MockRPCServer
    90  }