2019-12-16 13:19:59 +00:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/hashicorp/nomad/client/dynamicplugins"
|
|
|
|
"github.com/hashicorp/nomad/client/structs"
|
2020-01-09 16:14:07 +00:00
|
|
|
nstructs "github.com/hashicorp/nomad/nomad/structs"
|
2019-12-16 13:19:59 +00:00
|
|
|
"github.com/hashicorp/nomad/plugins/csi"
|
|
|
|
"github.com/hashicorp/nomad/plugins/csi/fake"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
var fakePlugin = &dynamicplugins.PluginInfo{
|
|
|
|
Name: "test-plugin",
|
|
|
|
Type: "csi-controller",
|
|
|
|
ConnectionInfo: &dynamicplugins.PluginConnectionInfo{},
|
|
|
|
}
|
|
|
|
|
2020-02-21 09:48:19 +00:00
|
|
|
func TestCSIController_AttachVolume(t *testing.T) {
|
2019-12-16 13:19:59 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
cases := []struct {
|
|
|
|
Name string
|
|
|
|
ClientSetupFunc func(*fake.Client)
|
2019-12-17 11:37:33 +00:00
|
|
|
Request *structs.ClientCSIControllerAttachVolumeRequest
|
2019-12-16 13:19:59 +00:00
|
|
|
ExpectedErr error
|
2019-12-17 11:37:33 +00:00
|
|
|
ExpectedResponse *structs.ClientCSIControllerAttachVolumeResponse
|
2019-12-16 13:19:59 +00:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
Name: "returns plugin not found errors",
|
2019-12-17 11:37:33 +00:00
|
|
|
Request: &structs.ClientCSIControllerAttachVolumeRequest{
|
2019-12-16 13:19:59 +00:00
|
|
|
PluginName: "some-garbage",
|
|
|
|
},
|
|
|
|
ExpectedErr: errors.New("plugin some-garbage for type csi-controller not found"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "validates volumeid is not empty",
|
2019-12-17 11:37:33 +00:00
|
|
|
Request: &structs.ClientCSIControllerAttachVolumeRequest{
|
2019-12-16 13:19:59 +00:00
|
|
|
PluginName: fakePlugin.Name,
|
|
|
|
},
|
|
|
|
ExpectedErr: errors.New("VolumeID is required"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "validates nodeid is not empty",
|
2019-12-17 11:37:33 +00:00
|
|
|
Request: &structs.ClientCSIControllerAttachVolumeRequest{
|
2019-12-16 13:19:59 +00:00
|
|
|
PluginName: fakePlugin.Name,
|
|
|
|
VolumeID: "1234-4321-1234-4321",
|
|
|
|
},
|
|
|
|
ExpectedErr: errors.New("NodeID is required"),
|
|
|
|
},
|
2019-12-17 11:37:33 +00:00
|
|
|
{
|
|
|
|
Name: "validates AccessMode",
|
|
|
|
Request: &structs.ClientCSIControllerAttachVolumeRequest{
|
|
|
|
PluginName: fakePlugin.Name,
|
|
|
|
VolumeID: "1234-4321-1234-4321",
|
|
|
|
NodeID: "abcde",
|
2020-01-09 16:14:07 +00:00
|
|
|
AccessMode: nstructs.CSIVolumeAccessMode("foo"),
|
2019-12-17 11:37:33 +00:00
|
|
|
},
|
|
|
|
ExpectedErr: errors.New("Unknown access mode: foo"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "validates attachmentmode is not empty",
|
|
|
|
Request: &structs.ClientCSIControllerAttachVolumeRequest{
|
|
|
|
PluginName: fakePlugin.Name,
|
|
|
|
VolumeID: "1234-4321-1234-4321",
|
|
|
|
NodeID: "abcde",
|
2020-01-09 16:14:07 +00:00
|
|
|
AccessMode: nstructs.CSIVolumeAccessModeMultiNodeReader,
|
|
|
|
AttachmentMode: nstructs.CSIVolumeAttachmentMode("bar"),
|
2019-12-17 11:37:33 +00:00
|
|
|
},
|
|
|
|
ExpectedErr: errors.New("Unknown attachment mode: bar"),
|
|
|
|
},
|
2019-12-16 13:19:59 +00:00
|
|
|
{
|
|
|
|
Name: "returns transitive errors",
|
|
|
|
ClientSetupFunc: func(fc *fake.Client) {
|
|
|
|
fc.NextControllerPublishVolumeErr = errors.New("hello")
|
|
|
|
},
|
2019-12-17 11:37:33 +00:00
|
|
|
Request: &structs.ClientCSIControllerAttachVolumeRequest{
|
|
|
|
PluginName: fakePlugin.Name,
|
|
|
|
VolumeID: "1234-4321-1234-4321",
|
|
|
|
NodeID: "abcde",
|
2020-01-09 16:14:07 +00:00
|
|
|
AccessMode: nstructs.CSIVolumeAccessModeSingleNodeWriter,
|
|
|
|
AttachmentMode: nstructs.CSIVolumeAttachmentModeFilesystem,
|
2019-12-16 13:19:59 +00:00
|
|
|
},
|
|
|
|
ExpectedErr: errors.New("hello"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "handles nil PublishContext",
|
|
|
|
ClientSetupFunc: func(fc *fake.Client) {
|
|
|
|
fc.NextControllerPublishVolumeResponse = &csi.ControllerPublishVolumeResponse{}
|
|
|
|
},
|
2019-12-17 11:37:33 +00:00
|
|
|
Request: &structs.ClientCSIControllerAttachVolumeRequest{
|
2019-12-17 11:38:07 +00:00
|
|
|
PluginName: fakePlugin.Name,
|
|
|
|
VolumeID: "1234-4321-1234-4321",
|
|
|
|
NodeID: "abcde",
|
2020-01-09 16:14:07 +00:00
|
|
|
AccessMode: nstructs.CSIVolumeAccessModeSingleNodeWriter,
|
|
|
|
AttachmentMode: nstructs.CSIVolumeAttachmentModeFilesystem,
|
2019-12-16 13:19:59 +00:00
|
|
|
},
|
2019-12-17 11:37:33 +00:00
|
|
|
ExpectedResponse: &structs.ClientCSIControllerAttachVolumeResponse{},
|
2019-12-16 13:19:59 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "handles non-nil PublishContext",
|
|
|
|
ClientSetupFunc: func(fc *fake.Client) {
|
|
|
|
fc.NextControllerPublishVolumeResponse = &csi.ControllerPublishVolumeResponse{
|
|
|
|
PublishContext: map[string]string{"foo": "bar"},
|
|
|
|
}
|
|
|
|
},
|
2019-12-17 11:37:33 +00:00
|
|
|
Request: &structs.ClientCSIControllerAttachVolumeRequest{
|
2019-12-17 11:38:07 +00:00
|
|
|
PluginName: fakePlugin.Name,
|
|
|
|
VolumeID: "1234-4321-1234-4321",
|
|
|
|
NodeID: "abcde",
|
2020-01-09 16:14:07 +00:00
|
|
|
AccessMode: nstructs.CSIVolumeAccessModeSingleNodeWriter,
|
|
|
|
AttachmentMode: nstructs.CSIVolumeAttachmentModeFilesystem,
|
2019-12-16 13:19:59 +00:00
|
|
|
},
|
2019-12-17 11:37:33 +00:00
|
|
|
ExpectedResponse: &structs.ClientCSIControllerAttachVolumeResponse{
|
2019-12-16 13:19:59 +00:00
|
|
|
PublishContext: map[string]string{"foo": "bar"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range cases {
|
|
|
|
t.Run(tc.Name, func(t *testing.T) {
|
|
|
|
require := require.New(t)
|
|
|
|
client, cleanup := TestClient(t, nil)
|
|
|
|
defer cleanup()
|
|
|
|
|
|
|
|
fakeClient := &fake.Client{}
|
|
|
|
if tc.ClientSetupFunc != nil {
|
|
|
|
tc.ClientSetupFunc(fakeClient)
|
|
|
|
}
|
|
|
|
|
|
|
|
dispenserFunc := func(*dynamicplugins.PluginInfo) (interface{}, error) {
|
|
|
|
return fakeClient, nil
|
|
|
|
}
|
|
|
|
client.dynamicRegistry.StubDispenserForType(dynamicplugins.PluginTypeCSIController, dispenserFunc)
|
|
|
|
|
|
|
|
err := client.dynamicRegistry.RegisterPlugin(fakePlugin)
|
|
|
|
require.Nil(err)
|
|
|
|
|
2019-12-17 11:37:33 +00:00
|
|
|
var resp structs.ClientCSIControllerAttachVolumeResponse
|
2020-02-21 09:48:19 +00:00
|
|
|
err = client.ClientRPC("CSIController.AttachVolume", tc.Request, &resp)
|
2019-12-16 13:19:59 +00:00
|
|
|
require.Equal(tc.ExpectedErr, err)
|
|
|
|
if tc.ExpectedResponse != nil {
|
|
|
|
require.Equal(tc.ExpectedResponse, &resp)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2020-02-18 16:08:38 +00:00
|
|
|
|
2020-02-21 09:48:19 +00:00
|
|
|
func TestCSIController_ValidateVolume(t *testing.T) {
|
2020-02-18 16:08:38 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
cases := []struct {
|
|
|
|
Name string
|
|
|
|
ClientSetupFunc func(*fake.Client)
|
|
|
|
Request *structs.ClientCSIControllerValidateVolumeRequest
|
|
|
|
ExpectedErr error
|
|
|
|
ExpectedResponse *structs.ClientCSIControllerValidateVolumeResponse
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
Name: "validates volumeid is not empty",
|
|
|
|
Request: &structs.ClientCSIControllerValidateVolumeRequest{
|
|
|
|
PluginID: fakePlugin.Name,
|
|
|
|
},
|
|
|
|
ExpectedErr: errors.New("VolumeID is required"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "returns plugin not found errors",
|
|
|
|
Request: &structs.ClientCSIControllerValidateVolumeRequest{
|
|
|
|
PluginID: "some-garbage",
|
|
|
|
VolumeID: "foo",
|
|
|
|
},
|
|
|
|
ExpectedErr: errors.New("plugin some-garbage for type csi-controller not found"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "validates attachmentmode",
|
|
|
|
Request: &structs.ClientCSIControllerValidateVolumeRequest{
|
|
|
|
PluginID: fakePlugin.Name,
|
|
|
|
VolumeID: "1234-4321-1234-4321",
|
|
|
|
AttachmentMode: nstructs.CSIVolumeAttachmentMode("bar"),
|
|
|
|
AccessMode: nstructs.CSIVolumeAccessModeMultiNodeReader,
|
|
|
|
},
|
|
|
|
ExpectedErr: errors.New("Unknown volume attachment mode: bar"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "validates AccessMode",
|
|
|
|
Request: &structs.ClientCSIControllerValidateVolumeRequest{
|
|
|
|
PluginID: fakePlugin.Name,
|
|
|
|
VolumeID: "1234-4321-1234-4321",
|
|
|
|
AttachmentMode: nstructs.CSIVolumeAttachmentModeFilesystem,
|
|
|
|
AccessMode: nstructs.CSIVolumeAccessMode("foo"),
|
|
|
|
},
|
|
|
|
ExpectedErr: errors.New("Unknown volume access mode: foo"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "returns transitive errors",
|
|
|
|
ClientSetupFunc: func(fc *fake.Client) {
|
|
|
|
fc.NextControllerValidateVolumeErr = errors.New("hello")
|
|
|
|
},
|
|
|
|
Request: &structs.ClientCSIControllerValidateVolumeRequest{
|
|
|
|
PluginID: fakePlugin.Name,
|
|
|
|
VolumeID: "1234-4321-1234-4321",
|
|
|
|
AccessMode: nstructs.CSIVolumeAccessModeSingleNodeWriter,
|
|
|
|
AttachmentMode: nstructs.CSIVolumeAttachmentModeFilesystem,
|
|
|
|
},
|
|
|
|
ExpectedErr: errors.New("hello"),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range cases {
|
|
|
|
t.Run(tc.Name, func(t *testing.T) {
|
|
|
|
require := require.New(t)
|
|
|
|
client, cleanup := TestClient(t, nil)
|
|
|
|
defer cleanup()
|
|
|
|
|
|
|
|
fakeClient := &fake.Client{}
|
|
|
|
if tc.ClientSetupFunc != nil {
|
|
|
|
tc.ClientSetupFunc(fakeClient)
|
|
|
|
}
|
|
|
|
|
|
|
|
dispenserFunc := func(*dynamicplugins.PluginInfo) (interface{}, error) {
|
|
|
|
return fakeClient, nil
|
|
|
|
}
|
|
|
|
client.dynamicRegistry.StubDispenserForType(dynamicplugins.PluginTypeCSIController, dispenserFunc)
|
|
|
|
|
|
|
|
err := client.dynamicRegistry.RegisterPlugin(fakePlugin)
|
|
|
|
require.Nil(err)
|
|
|
|
|
|
|
|
var resp structs.ClientCSIControllerValidateVolumeResponse
|
2020-02-21 09:48:19 +00:00
|
|
|
err = client.ClientRPC("CSIController.ValidateVolume", tc.Request, &resp)
|
2020-02-18 16:08:38 +00:00
|
|
|
require.Equal(tc.ExpectedErr, err)
|
|
|
|
if tc.ExpectedResponse != nil {
|
|
|
|
require.Equal(tc.ExpectedResponse, &resp)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|