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