github.com/iqoqo/nomad@v0.11.3-0.20200911112621-d7021c74d101/nomad/volumewatcher/interfaces_test.go (about)

     1  package volumewatcher
     2  
     3  import (
     4  	cstructs "github.com/hashicorp/nomad/client/structs"
     5  	"github.com/hashicorp/nomad/nomad/mock"
     6  	"github.com/hashicorp/nomad/nomad/state"
     7  	"github.com/hashicorp/nomad/nomad/structs"
     8  )
     9  
    10  // Create a client node with plugin info
    11  func testNode(node *structs.Node, plugin *structs.CSIPlugin, s *state.StateStore) *structs.Node {
    12  	if node != nil {
    13  		return node
    14  	}
    15  	node = mock.Node()
    16  	node.Attributes["nomad.version"] = "0.11.0" // client RPCs not supported on early version
    17  	node.CSINodePlugins = map[string]*structs.CSIInfo{
    18  		plugin.ID: {
    19  			PluginID:                 plugin.ID,
    20  			Healthy:                  true,
    21  			RequiresControllerPlugin: plugin.ControllerRequired,
    22  			NodeInfo:                 &structs.CSINodeInfo{},
    23  		},
    24  	}
    25  	if plugin.ControllerRequired {
    26  		node.CSIControllerPlugins = map[string]*structs.CSIInfo{
    27  			plugin.ID: {
    28  				PluginID:                 plugin.ID,
    29  				Healthy:                  true,
    30  				RequiresControllerPlugin: true,
    31  				ControllerInfo: &structs.CSIControllerInfo{
    32  					SupportsReadOnlyAttach:           true,
    33  					SupportsAttachDetach:             true,
    34  					SupportsListVolumes:              true,
    35  					SupportsListVolumesAttachedNodes: false,
    36  				},
    37  			},
    38  		}
    39  	} else {
    40  		node.CSIControllerPlugins = map[string]*structs.CSIInfo{}
    41  	}
    42  	s.UpsertNode(99, node)
    43  	return node
    44  }
    45  
    46  // Create a test volume with claim info
    47  func testVolume(vol *structs.CSIVolume, plugin *structs.CSIPlugin, alloc *structs.Allocation, nodeID string) *structs.CSIVolume {
    48  	if vol != nil {
    49  		return vol
    50  	}
    51  	vol = mock.CSIVolume(plugin)
    52  	vol.ControllerRequired = plugin.ControllerRequired
    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  			Mode:         structs.CSIVolumeClaimRead,
    60  			State:        structs.CSIVolumeClaimStateTaken,
    61  		},
    62  	}
    63  	return vol
    64  }
    65  
    66  // COMPAT(1.0): the claim fields were added after 0.11.1; this
    67  // mock and the associated test cases can be removed for 1.0
    68  func testOldVolume(vol *structs.CSIVolume, plugin *structs.CSIPlugin, alloc *structs.Allocation, nodeID string) *structs.CSIVolume {
    69  	if vol != nil {
    70  		return vol
    71  	}
    72  	vol = mock.CSIVolume(plugin)
    73  	vol.ControllerRequired = plugin.ControllerRequired
    74  
    75  	vol.ReadAllocs = map[string]*structs.Allocation{alloc.ID: alloc}
    76  	return vol
    77  }
    78  
    79  type MockRPCServer struct {
    80  	state *state.StateStore
    81  
    82  	// mock responses for ClientCSI.NodeDetachVolume
    83  	nextCSINodeDetachResponse *cstructs.ClientCSINodeDetachVolumeResponse
    84  	nextCSINodeDetachError    error
    85  	countCSINodeDetachVolume  int
    86  
    87  	// mock responses for ClientCSI.ControllerDetachVolume
    88  	nextCSIControllerDetachVolumeResponse *cstructs.ClientCSIControllerDetachVolumeResponse
    89  	nextCSIControllerDetachError          error
    90  	countCSIControllerDetachVolume        int
    91  
    92  	countUpdateClaims       int
    93  	countUpsertVolumeClaims int
    94  }
    95  
    96  func (srv *MockRPCServer) ControllerDetachVolume(args *cstructs.ClientCSIControllerDetachVolumeRequest, reply *cstructs.ClientCSIControllerDetachVolumeResponse) error {
    97  	reply = srv.nextCSIControllerDetachVolumeResponse
    98  	srv.countCSIControllerDetachVolume++
    99  	return srv.nextCSIControllerDetachError
   100  }
   101  
   102  func (srv *MockRPCServer) NodeDetachVolume(args *cstructs.ClientCSINodeDetachVolumeRequest, reply *cstructs.ClientCSINodeDetachVolumeResponse) error {
   103  	reply = srv.nextCSINodeDetachResponse
   104  	srv.countCSINodeDetachVolume++
   105  	return srv.nextCSINodeDetachError
   106  
   107  }
   108  
   109  func (srv *MockRPCServer) UpsertVolumeClaims(*structs.CSIVolumeClaimBatchRequest) (uint64, error) {
   110  	srv.countUpsertVolumeClaims++
   111  	return 0, nil
   112  }
   113  
   114  func (srv *MockRPCServer) State() *state.StateStore { return srv.state }
   115  
   116  func (srv *MockRPCServer) UpdateClaims(claims []structs.CSIVolumeClaimRequest) (uint64, error) {
   117  	srv.countUpdateClaims++
   118  	return 0, nil
   119  }
   120  
   121  type MockBatchingRPCServer struct {
   122  	MockRPCServer
   123  	volumeUpdateBatcher *VolumeUpdateBatcher
   124  }
   125  
   126  func (srv *MockBatchingRPCServer) UpdateClaims(claims []structs.CSIVolumeClaimRequest) (uint64, error) {
   127  	srv.countUpdateClaims++
   128  	return srv.volumeUpdateBatcher.CreateUpdate(claims).Results()
   129  }
   130  
   131  type MockStatefulRPCServer struct {
   132  	MockRPCServer
   133  	volumeUpdateBatcher *VolumeUpdateBatcher
   134  }
   135  
   136  func (srv *MockStatefulRPCServer) UpsertVolumeClaims(batch *structs.CSIVolumeClaimBatchRequest) (uint64, error) {
   137  	srv.countUpsertVolumeClaims++
   138  	index, _ := srv.state.LatestIndex()
   139  	for _, req := range batch.Claims {
   140  		index++
   141  		err := srv.state.CSIVolumeClaim(index, req.RequestNamespace(),
   142  			req.VolumeID, req.ToClaim())
   143  		if err != nil {
   144  			return 0, err
   145  		}
   146  	}
   147  	return index, nil
   148  }