github.com/rohankumardubey/nomad@v0.11.8/plugins/csi/fake/client.go (about)

     1  // fake is a package that includes fake implementations of public interfaces
     2  // from the CSI package for testing.
     3  package fake
     4  
     5  import (
     6  	"context"
     7  	"errors"
     8  	"fmt"
     9  	"sync"
    10  
    11  	"github.com/hashicorp/nomad/nomad/structs"
    12  	"github.com/hashicorp/nomad/plugins/base"
    13  	"github.com/hashicorp/nomad/plugins/csi"
    14  	"github.com/hashicorp/nomad/plugins/shared/hclspec"
    15  	"google.golang.org/grpc"
    16  )
    17  
    18  var _ csi.CSIPlugin = &Client{}
    19  
    20  // Client is a mock implementation of the csi.CSIPlugin interface for use in testing
    21  // external components
    22  type Client struct {
    23  	Mu sync.RWMutex
    24  
    25  	NextPluginInfoResponse *base.PluginInfoResponse
    26  	NextPluginInfoErr      error
    27  	PluginInfoCallCount    int64
    28  
    29  	NextPluginProbeResponse bool
    30  	NextPluginProbeErr      error
    31  	PluginProbeCallCount    int64
    32  
    33  	NextPluginGetInfoNameResponse    string
    34  	NextPluginGetInfoVersionResponse string
    35  	NextPluginGetInfoErr             error
    36  	PluginGetInfoCallCount           int64
    37  
    38  	NextPluginGetCapabilitiesResponse *csi.PluginCapabilitySet
    39  	NextPluginGetCapabilitiesErr      error
    40  	PluginGetCapabilitiesCallCount    int64
    41  
    42  	NextControllerGetCapabilitiesResponse *csi.ControllerCapabilitySet
    43  	NextControllerGetCapabilitiesErr      error
    44  	ControllerGetCapabilitiesCallCount    int64
    45  
    46  	NextControllerPublishVolumeResponse *csi.ControllerPublishVolumeResponse
    47  	NextControllerPublishVolumeErr      error
    48  	ControllerPublishVolumeCallCount    int64
    49  
    50  	NextControllerUnpublishVolumeResponse *csi.ControllerUnpublishVolumeResponse
    51  	NextControllerUnpublishVolumeErr      error
    52  	ControllerUnpublishVolumeCallCount    int64
    53  
    54  	NextControllerValidateVolumeErr   error
    55  	ControllerValidateVolumeCallCount int64
    56  
    57  	NextNodeGetCapabilitiesResponse *csi.NodeCapabilitySet
    58  	NextNodeGetCapabilitiesErr      error
    59  	NodeGetCapabilitiesCallCount    int64
    60  
    61  	NextNodeGetInfoResponse *csi.NodeGetInfoResponse
    62  	NextNodeGetInfoErr      error
    63  	NodeGetInfoCallCount    int64
    64  
    65  	NextNodeStageVolumeErr   error
    66  	NodeStageVolumeCallCount int64
    67  
    68  	NextNodeUnstageVolumeErr   error
    69  	NodeUnstageVolumeCallCount int64
    70  
    71  	PrevVolumeCapability       *csi.VolumeCapability
    72  	NextNodePublishVolumeErr   error
    73  	NodePublishVolumeCallCount int64
    74  
    75  	NextNodeUnpublishVolumeErr   error
    76  	NodeUnpublishVolumeCallCount int64
    77  }
    78  
    79  // PluginInfo describes the type and version of a plugin.
    80  func (c *Client) PluginInfo() (*base.PluginInfoResponse, error) {
    81  	c.Mu.Lock()
    82  	defer c.Mu.Unlock()
    83  
    84  	c.PluginInfoCallCount++
    85  
    86  	return c.NextPluginInfoResponse, c.NextPluginInfoErr
    87  }
    88  
    89  // ConfigSchema returns the schema for parsing the plugins configuration.
    90  func (c *Client) ConfigSchema() (*hclspec.Spec, error) {
    91  	return nil, errors.New("Unsupported")
    92  }
    93  
    94  // SetConfig is used to set the configuration by passing a MessagePack
    95  // encoding of it.
    96  func (c *Client) SetConfig(a *base.Config) error {
    97  	return errors.New("Unsupported")
    98  }
    99  
   100  // PluginProbe is used to verify that the plugin is in a healthy state
   101  func (c *Client) PluginProbe(ctx context.Context) (bool, error) {
   102  	c.Mu.Lock()
   103  	defer c.Mu.Unlock()
   104  
   105  	c.PluginProbeCallCount++
   106  
   107  	return c.NextPluginProbeResponse, c.NextPluginProbeErr
   108  }
   109  
   110  // PluginGetInfo is used to return semantic data about the plugin.
   111  // Response:
   112  //  - string: name, the name of the plugin in domain notation format.
   113  func (c *Client) PluginGetInfo(ctx context.Context) (string, string, error) {
   114  	c.Mu.Lock()
   115  	defer c.Mu.Unlock()
   116  
   117  	c.PluginGetInfoCallCount++
   118  
   119  	return c.NextPluginGetInfoNameResponse, c.NextPluginGetInfoVersionResponse, c.NextPluginGetInfoErr
   120  }
   121  
   122  // PluginGetCapabilities is used to return the available capabilities from the
   123  // identity service. This currently only looks for the CONTROLLER_SERVICE and
   124  // Accessible Topology Support
   125  func (c *Client) PluginGetCapabilities(ctx context.Context) (*csi.PluginCapabilitySet, error) {
   126  	c.Mu.Lock()
   127  	defer c.Mu.Unlock()
   128  
   129  	c.PluginGetCapabilitiesCallCount++
   130  
   131  	return c.NextPluginGetCapabilitiesResponse, c.NextPluginGetCapabilitiesErr
   132  }
   133  
   134  func (c *Client) ControllerGetCapabilities(ctx context.Context) (*csi.ControllerCapabilitySet, error) {
   135  	c.Mu.Lock()
   136  	defer c.Mu.Unlock()
   137  
   138  	c.ControllerGetCapabilitiesCallCount++
   139  
   140  	return c.NextControllerGetCapabilitiesResponse, c.NextControllerGetCapabilitiesErr
   141  }
   142  
   143  // ControllerPublishVolume is used to attach a remote volume to a node
   144  func (c *Client) ControllerPublishVolume(ctx context.Context, req *csi.ControllerPublishVolumeRequest, opts ...grpc.CallOption) (*csi.ControllerPublishVolumeResponse, error) {
   145  	c.Mu.Lock()
   146  	defer c.Mu.Unlock()
   147  
   148  	c.ControllerPublishVolumeCallCount++
   149  
   150  	return c.NextControllerPublishVolumeResponse, c.NextControllerPublishVolumeErr
   151  }
   152  
   153  // ControllerUnpublishVolume is used to attach a remote volume to a node
   154  func (c *Client) ControllerUnpublishVolume(ctx context.Context, req *csi.ControllerUnpublishVolumeRequest, opts ...grpc.CallOption) (*csi.ControllerUnpublishVolumeResponse, error) {
   155  	c.Mu.Lock()
   156  	defer c.Mu.Unlock()
   157  
   158  	c.ControllerUnpublishVolumeCallCount++
   159  
   160  	return c.NextControllerUnpublishVolumeResponse, c.NextControllerUnpublishVolumeErr
   161  }
   162  
   163  func (c *Client) ControllerValidateCapabilities(ctx context.Context, req *csi.ControllerValidateVolumeRequest, opts ...grpc.CallOption) error {
   164  	c.Mu.Lock()
   165  	defer c.Mu.Unlock()
   166  
   167  	c.ControllerValidateVolumeCallCount++
   168  
   169  	return c.NextControllerValidateVolumeErr
   170  }
   171  
   172  func (c *Client) NodeGetCapabilities(ctx context.Context) (*csi.NodeCapabilitySet, error) {
   173  	c.Mu.Lock()
   174  	defer c.Mu.Unlock()
   175  
   176  	c.NodeGetCapabilitiesCallCount++
   177  
   178  	return c.NextNodeGetCapabilitiesResponse, c.NextNodeGetCapabilitiesErr
   179  }
   180  
   181  // NodeGetInfo is used to return semantic data about the current node in
   182  // respect to the SP.
   183  func (c *Client) NodeGetInfo(ctx context.Context) (*csi.NodeGetInfoResponse, error) {
   184  	c.Mu.Lock()
   185  	defer c.Mu.Unlock()
   186  
   187  	c.NodeGetInfoCallCount++
   188  
   189  	return c.NextNodeGetInfoResponse, c.NextNodeGetInfoErr
   190  }
   191  
   192  // NodeStageVolume is used when a plugin has the STAGE_UNSTAGE volume capability
   193  // to prepare a volume for usage on a host. If err == nil, the response should
   194  // be assumed to be successful.
   195  func (c *Client) NodeStageVolume(ctx context.Context, volumeID string, publishContext map[string]string, stagingTargetPath string, capabilities *csi.VolumeCapability, secrets structs.CSISecrets, opts ...grpc.CallOption) error {
   196  	c.Mu.Lock()
   197  	defer c.Mu.Unlock()
   198  
   199  	c.NodeStageVolumeCallCount++
   200  
   201  	return c.NextNodeStageVolumeErr
   202  }
   203  
   204  // NodeUnstageVolume is used when a plugin has the STAGE_UNSTAGE volume capability
   205  // to undo the work performed by NodeStageVolume. If a volume has been staged,
   206  // this RPC must be called before freeing the volume.
   207  //
   208  // If err == nil, the response should be assumed to be successful.
   209  func (c *Client) NodeUnstageVolume(ctx context.Context, volumeID string, stagingTargetPath string, opts ...grpc.CallOption) error {
   210  	c.Mu.Lock()
   211  	defer c.Mu.Unlock()
   212  
   213  	c.NodeUnstageVolumeCallCount++
   214  
   215  	return c.NextNodeUnstageVolumeErr
   216  }
   217  
   218  func (c *Client) NodePublishVolume(ctx context.Context, req *csi.NodePublishVolumeRequest, opts ...grpc.CallOption) error {
   219  	c.Mu.Lock()
   220  	defer c.Mu.Unlock()
   221  
   222  	c.PrevVolumeCapability = req.VolumeCapability
   223  	c.NodePublishVolumeCallCount++
   224  
   225  	return c.NextNodePublishVolumeErr
   226  }
   227  
   228  func (c *Client) NodeUnpublishVolume(ctx context.Context, volumeID, targetPath string, opts ...grpc.CallOption) error {
   229  	c.Mu.Lock()
   230  	defer c.Mu.Unlock()
   231  
   232  	c.NodeUnpublishVolumeCallCount++
   233  
   234  	return c.NextNodeUnpublishVolumeErr
   235  }
   236  
   237  // Shutdown the client and ensure any connections are cleaned up.
   238  func (c *Client) Close() error {
   239  
   240  	c.NextPluginInfoResponse = nil
   241  	c.NextPluginInfoErr = fmt.Errorf("closed client")
   242  
   243  	c.NextPluginProbeResponse = false
   244  	c.NextPluginProbeErr = fmt.Errorf("closed client")
   245  
   246  	c.NextPluginGetInfoNameResponse = ""
   247  	c.NextPluginGetInfoVersionResponse = ""
   248  	c.NextPluginGetInfoErr = fmt.Errorf("closed client")
   249  
   250  	c.NextPluginGetCapabilitiesResponse = nil
   251  	c.NextPluginGetCapabilitiesErr = fmt.Errorf("closed client")
   252  
   253  	c.NextControllerGetCapabilitiesResponse = nil
   254  	c.NextControllerGetCapabilitiesErr = fmt.Errorf("closed client")
   255  
   256  	c.NextControllerPublishVolumeResponse = nil
   257  	c.NextControllerPublishVolumeErr = fmt.Errorf("closed client")
   258  
   259  	c.NextControllerUnpublishVolumeResponse = nil
   260  	c.NextControllerUnpublishVolumeErr = fmt.Errorf("closed client")
   261  
   262  	c.NextControllerValidateVolumeErr = fmt.Errorf("closed client")
   263  
   264  	c.NextNodeGetCapabilitiesResponse = nil
   265  	c.NextNodeGetCapabilitiesErr = fmt.Errorf("closed client")
   266  
   267  	c.NextNodeGetInfoResponse = nil
   268  	c.NextNodeGetInfoErr = fmt.Errorf("closed client")
   269  
   270  	c.NextNodeStageVolumeErr = fmt.Errorf("closed client")
   271  
   272  	c.NextNodeUnstageVolumeErr = fmt.Errorf("closed client")
   273  
   274  	c.NextNodePublishVolumeErr = fmt.Errorf("closed client")
   275  
   276  	c.NextNodeUnpublishVolumeErr = fmt.Errorf("closed client")
   277  
   278  	return nil
   279  }