github.com/anth0d/nomad@v0.0.0-20221214183521-ae3a0a2cad06/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 NextControllerCreateVolumeResponse *csi.ControllerCreateVolumeResponse 54 NextControllerCreateVolumeErr error 55 ControllerCreateVolumeCallCount int64 56 57 NextControllerDeleteVolumeErr error 58 ControllerDeleteVolumeCallCount int64 59 60 NextControllerListVolumesResponse *csi.ControllerListVolumesResponse 61 NextControllerListVolumesErr error 62 ControllerListVolumesCallCount int64 63 64 NextControllerValidateVolumeErr error 65 ControllerValidateVolumeCallCount int64 66 67 NextControllerCreateSnapshotResponse *csi.ControllerCreateSnapshotResponse 68 NextControllerCreateSnapshotErr error 69 ControllerCreateSnapshotCallCount int64 70 71 NextControllerDeleteSnapshotErr error 72 ControllerDeleteSnapshotCallCount int64 73 74 NextControllerListSnapshotsResponse *csi.ControllerListSnapshotsResponse 75 NextControllerListSnapshotsErr error 76 ControllerListSnapshotsCallCount int64 77 78 NextNodeGetCapabilitiesResponse *csi.NodeCapabilitySet 79 NextNodeGetCapabilitiesErr error 80 NodeGetCapabilitiesCallCount int64 81 82 NextNodeGetInfoResponse *csi.NodeGetInfoResponse 83 NextNodeGetInfoErr error 84 NodeGetInfoCallCount int64 85 86 NextNodeStageVolumeErr error 87 NodeStageVolumeCallCount int64 88 89 NextNodeUnstageVolumeErr error 90 NodeUnstageVolumeCallCount int64 91 92 PrevVolumeCapability *csi.VolumeCapability 93 NextNodePublishVolumeErr error 94 NodePublishVolumeCallCount int64 95 96 NextNodeUnpublishVolumeErr error 97 NodeUnpublishVolumeCallCount int64 98 } 99 100 // PluginInfo describes the type and version of a plugin. 101 func (c *Client) PluginInfo() (*base.PluginInfoResponse, error) { 102 c.Mu.Lock() 103 defer c.Mu.Unlock() 104 105 c.PluginInfoCallCount++ 106 107 return c.NextPluginInfoResponse, c.NextPluginInfoErr 108 } 109 110 // ConfigSchema returns the schema for parsing the plugins configuration. 111 func (c *Client) ConfigSchema() (*hclspec.Spec, error) { 112 return nil, errors.New("Unsupported") 113 } 114 115 // SetConfig is used to set the configuration by passing a MessagePack 116 // encoding of it. 117 func (c *Client) SetConfig(a *base.Config) error { 118 return errors.New("Unsupported") 119 } 120 121 // PluginProbe is used to verify that the plugin is in a healthy state 122 func (c *Client) PluginProbe(ctx context.Context) (bool, error) { 123 c.Mu.Lock() 124 defer c.Mu.Unlock() 125 126 c.PluginProbeCallCount++ 127 128 return c.NextPluginProbeResponse, c.NextPluginProbeErr 129 } 130 131 // PluginGetInfo is used to return semantic data about the plugin. 132 // Response: 133 // - string: name, the name of the plugin in domain notation format. 134 func (c *Client) PluginGetInfo(ctx context.Context) (string, string, error) { 135 c.Mu.Lock() 136 defer c.Mu.Unlock() 137 138 c.PluginGetInfoCallCount++ 139 140 return c.NextPluginGetInfoNameResponse, c.NextPluginGetInfoVersionResponse, c.NextPluginGetInfoErr 141 } 142 143 // PluginGetCapabilities is used to return the available capabilities from the 144 // identity service. This currently only looks for the CONTROLLER_SERVICE and 145 // Accessible Topology Support 146 func (c *Client) PluginGetCapabilities(ctx context.Context) (*csi.PluginCapabilitySet, error) { 147 c.Mu.Lock() 148 defer c.Mu.Unlock() 149 150 c.PluginGetCapabilitiesCallCount++ 151 152 return c.NextPluginGetCapabilitiesResponse, c.NextPluginGetCapabilitiesErr 153 } 154 155 func (c *Client) ControllerGetCapabilities(ctx context.Context) (*csi.ControllerCapabilitySet, error) { 156 c.Mu.Lock() 157 defer c.Mu.Unlock() 158 159 c.ControllerGetCapabilitiesCallCount++ 160 161 return c.NextControllerGetCapabilitiesResponse, c.NextControllerGetCapabilitiesErr 162 } 163 164 // ControllerPublishVolume is used to attach a remote volume to a node 165 func (c *Client) ControllerPublishVolume(ctx context.Context, req *csi.ControllerPublishVolumeRequest, opts ...grpc.CallOption) (*csi.ControllerPublishVolumeResponse, error) { 166 c.Mu.Lock() 167 defer c.Mu.Unlock() 168 169 c.ControllerPublishVolumeCallCount++ 170 171 return c.NextControllerPublishVolumeResponse, c.NextControllerPublishVolumeErr 172 } 173 174 // ControllerUnpublishVolume is used to attach a remote volume to a node 175 func (c *Client) ControllerUnpublishVolume(ctx context.Context, req *csi.ControllerUnpublishVolumeRequest, opts ...grpc.CallOption) (*csi.ControllerUnpublishVolumeResponse, error) { 176 c.Mu.Lock() 177 defer c.Mu.Unlock() 178 179 c.ControllerUnpublishVolumeCallCount++ 180 181 return c.NextControllerUnpublishVolumeResponse, c.NextControllerUnpublishVolumeErr 182 } 183 184 func (c *Client) ControllerValidateCapabilities(ctx context.Context, req *csi.ControllerValidateVolumeRequest, opts ...grpc.CallOption) error { 185 c.Mu.Lock() 186 defer c.Mu.Unlock() 187 188 c.ControllerValidateVolumeCallCount++ 189 190 return c.NextControllerValidateVolumeErr 191 } 192 193 func (c *Client) ControllerCreateVolume(ctx context.Context, in *csi.ControllerCreateVolumeRequest, opts ...grpc.CallOption) (*csi.ControllerCreateVolumeResponse, error) { 194 c.Mu.Lock() 195 defer c.Mu.Unlock() 196 c.ControllerCreateVolumeCallCount++ 197 return c.NextControllerCreateVolumeResponse, c.NextControllerCreateVolumeErr 198 } 199 200 func (c *Client) ControllerDeleteVolume(ctx context.Context, req *csi.ControllerDeleteVolumeRequest, opts ...grpc.CallOption) error { 201 c.Mu.Lock() 202 defer c.Mu.Unlock() 203 c.ControllerDeleteVolumeCallCount++ 204 return c.NextControllerDeleteVolumeErr 205 } 206 207 func (c *Client) ControllerListVolumes(ctx context.Context, req *csi.ControllerListVolumesRequest, opts ...grpc.CallOption) (*csi.ControllerListVolumesResponse, error) { 208 c.Mu.Lock() 209 defer c.Mu.Unlock() 210 c.ControllerListVolumesCallCount++ 211 return c.NextControllerListVolumesResponse, c.NextControllerListVolumesErr 212 } 213 214 func (c *Client) ControllerCreateSnapshot(ctx context.Context, req *csi.ControllerCreateSnapshotRequest, opts ...grpc.CallOption) (*csi.ControllerCreateSnapshotResponse, error) { 215 c.Mu.Lock() 216 defer c.Mu.Unlock() 217 c.ControllerCreateSnapshotCallCount++ 218 return c.NextControllerCreateSnapshotResponse, c.NextControllerCreateSnapshotErr 219 } 220 221 func (c *Client) ControllerDeleteSnapshot(ctx context.Context, req *csi.ControllerDeleteSnapshotRequest, opts ...grpc.CallOption) error { 222 c.Mu.Lock() 223 defer c.Mu.Unlock() 224 c.ControllerDeleteSnapshotCallCount++ 225 return c.NextControllerDeleteSnapshotErr 226 } 227 228 func (c *Client) ControllerListSnapshots(ctx context.Context, req *csi.ControllerListSnapshotsRequest, opts ...grpc.CallOption) (*csi.ControllerListSnapshotsResponse, error) { 229 c.Mu.Lock() 230 defer c.Mu.Unlock() 231 c.ControllerListSnapshotsCallCount++ 232 return c.NextControllerListSnapshotsResponse, c.NextControllerListSnapshotsErr 233 } 234 235 func (c *Client) NodeGetCapabilities(ctx context.Context) (*csi.NodeCapabilitySet, error) { 236 c.Mu.Lock() 237 defer c.Mu.Unlock() 238 239 c.NodeGetCapabilitiesCallCount++ 240 241 return c.NextNodeGetCapabilitiesResponse, c.NextNodeGetCapabilitiesErr 242 } 243 244 // NodeGetInfo is used to return semantic data about the current node in 245 // respect to the SP. 246 func (c *Client) NodeGetInfo(ctx context.Context) (*csi.NodeGetInfoResponse, error) { 247 c.Mu.Lock() 248 defer c.Mu.Unlock() 249 250 c.NodeGetInfoCallCount++ 251 252 return c.NextNodeGetInfoResponse, c.NextNodeGetInfoErr 253 } 254 255 // NodeStageVolume is used when a plugin has the STAGE_UNSTAGE volume capability 256 // to prepare a volume for usage on a host. If err == nil, the response should 257 // be assumed to be successful. 258 func (c *Client) NodeStageVolume(ctx context.Context, req *csi.NodeStageVolumeRequest, opts ...grpc.CallOption) error { 259 c.Mu.Lock() 260 defer c.Mu.Unlock() 261 262 c.NodeStageVolumeCallCount++ 263 264 return c.NextNodeStageVolumeErr 265 } 266 267 // NodeUnstageVolume is used when a plugin has the STAGE_UNSTAGE volume capability 268 // to undo the work performed by NodeStageVolume. If a volume has been staged, 269 // this RPC must be called before freeing the volume. 270 // 271 // If err == nil, the response should be assumed to be successful. 272 func (c *Client) NodeUnstageVolume(ctx context.Context, volumeID string, stagingTargetPath string, opts ...grpc.CallOption) error { 273 c.Mu.Lock() 274 defer c.Mu.Unlock() 275 276 c.NodeUnstageVolumeCallCount++ 277 278 return c.NextNodeUnstageVolumeErr 279 } 280 281 func (c *Client) NodePublishVolume(ctx context.Context, req *csi.NodePublishVolumeRequest, opts ...grpc.CallOption) error { 282 c.Mu.Lock() 283 defer c.Mu.Unlock() 284 285 c.PrevVolumeCapability = req.VolumeCapability 286 c.NodePublishVolumeCallCount++ 287 288 return c.NextNodePublishVolumeErr 289 } 290 291 func (c *Client) NodeUnpublishVolume(ctx context.Context, volumeID, targetPath string, opts ...grpc.CallOption) error { 292 c.Mu.Lock() 293 defer c.Mu.Unlock() 294 295 c.NodeUnpublishVolumeCallCount++ 296 297 return c.NextNodeUnpublishVolumeErr 298 } 299 300 // Close the client and ensure any connections are cleaned up. 301 func (c *Client) Close() error { 302 303 c.NextPluginInfoResponse = nil 304 c.NextPluginInfoErr = fmt.Errorf("closed client") 305 306 c.NextPluginProbeResponse = false 307 c.NextPluginProbeErr = fmt.Errorf("closed client") 308 309 c.NextPluginGetInfoNameResponse = "" 310 c.NextPluginGetInfoVersionResponse = "" 311 c.NextPluginGetInfoErr = fmt.Errorf("closed client") 312 313 c.NextPluginGetCapabilitiesResponse = nil 314 c.NextPluginGetCapabilitiesErr = fmt.Errorf("closed client") 315 316 c.NextControllerGetCapabilitiesResponse = nil 317 c.NextControllerGetCapabilitiesErr = fmt.Errorf("closed client") 318 319 c.NextControllerPublishVolumeResponse = nil 320 c.NextControllerPublishVolumeErr = fmt.Errorf("closed client") 321 322 c.NextControllerUnpublishVolumeResponse = nil 323 c.NextControllerUnpublishVolumeErr = fmt.Errorf("closed client") 324 325 c.NextControllerValidateVolumeErr = fmt.Errorf("closed client") 326 327 c.NextNodeGetCapabilitiesResponse = nil 328 c.NextNodeGetCapabilitiesErr = fmt.Errorf("closed client") 329 330 c.NextNodeGetInfoResponse = nil 331 c.NextNodeGetInfoErr = fmt.Errorf("closed client") 332 333 c.NextNodeStageVolumeErr = fmt.Errorf("closed client") 334 335 c.NextNodeUnstageVolumeErr = fmt.Errorf("closed client") 336 337 c.NextNodePublishVolumeErr = fmt.Errorf("closed client") 338 339 c.NextNodeUnpublishVolumeErr = fmt.Errorf("closed client") 340 341 return nil 342 }