From 29467367de89632d4446238b7b6e2ef863060c3e Mon Sep 17 00:00:00 2001 From: Ely Deckers Date: Sun, 19 Jul 2020 12:58:57 +0200 Subject: [PATCH] Add create-volume-test for LUKS --- driver/luks_util.go | 2 +- driver/luks_util_test.go | 110 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 111 insertions(+), 1 deletion(-) create mode 100644 driver/luks_util_test.go diff --git a/driver/luks_util.go b/driver/luks_util.go index 896c95dd..5ba64ecd 100644 --- a/driver/luks_util.go +++ b/driver/luks_util.go @@ -33,7 +33,7 @@ const ( LuksEncryptedAttribute = DefaultDriverName + "/luks-encrypted" // LuksCipherAttribute is used to pass the information about the luks encryption - // cypher to `NodeStageVolume` + // cipher to `NodeStageVolume` LuksCipherAttribute = DefaultDriverName + "/luks-cipher" // LuksKeySizeAttribute is used to pass the information about the luks key size diff --git a/driver/luks_util_test.go b/driver/luks_util_test.go new file mode 100644 index 00000000..c43cbeb1 --- /dev/null +++ b/driver/luks_util_test.go @@ -0,0 +1,110 @@ +package driver + +import ( + "context" + "errors" + "github.com/container-storage-interface/spec/lib/go/csi" + "github.com/sirupsen/logrus" + "strings" + "testing" +) + +type TestPodVolume struct { + ClaimName string + SizeGB int + StorageClass string + LuksKey string +} + +type TestPodDescriptor struct { + Kind string + Name string + Volumes []TestPodVolume +} + +type DiskInfo struct { + PVCName string `json:"pvcName"` + DeviceName string `json:"deviceName"` + DeviceSize int `json:"deviceSize"` + Filesystem string `json:"filesystem"` + FilesystemUUID string `json:"filesystemUUID"` + FilesystemSize int `json:"filesystemSize"` + DeviceSource string `json:"deviceSource"` + Luks string `json:"luks,omitempty"` + Cipher string `json:"cipher,omitempty"` + Keysize int `json:"keysize,omitempty"` +} + +// creates a kubernetes pod from the given TestPodDescriptor +func TestCreateLuksVolume(t *testing.T) { + tests := []struct { + name string + listVolumesErr error + getSnapshotErr error + }{ + { + name: "listing volumes failing", + listVolumesErr: errors.New("failed to list volumes"), + }, + { + name: "fetching snapshot failing", + getSnapshotErr: errors.New("failed to get snapshot"), + }, + } + + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + d := &Driver{ + storage: &fakeStorageDriver{ + listVolumesErr: test.listVolumesErr, + }, + snapshots: &fakeSnapshotsDriver{ + getSnapshotErr: test.getSnapshotErr, + }, + log: logrus.New().WithField("test_enabed", true), + } + + _, err := d.CreateVolume(context.Background(), &csi.CreateVolumeRequest{ + Name: "name", + Parameters: map[string]string{ + LuksCipherAttribute: "cipher", + LuksEncryptedAttribute: "true", + LuksKeyAttribute: "key", + LuksKeySizeAttribute: "23234", + }, + VolumeCapabilities: []*csi.VolumeCapability{ + &csi.VolumeCapability{ + AccessType: &csi.VolumeCapability_Mount{ + Mount: &csi.VolumeCapability_MountVolume{}, + }, + AccessMode: &csi.VolumeCapability_AccessMode{ + Mode: csi.VolumeCapability_AccessMode_SINGLE_NODE_WRITER, + }, + }, + }, + VolumeContentSource: &csi.VolumeContentSource{ + Type: &csi.VolumeContentSource_Snapshot{ + Snapshot: &csi.VolumeContentSource_SnapshotSource{ + SnapshotId: "snapshotId", + }, + }, + }, + }) + + var wantErr error + switch { + case test.listVolumesErr != nil: + wantErr = test.listVolumesErr + case test.getSnapshotErr != nil: + wantErr = test.getSnapshotErr + } + + if wantErr == nil && err != nil { + t.Errorf("got error %q, want none", err) + } + if wantErr != nil && !strings.Contains(err.Error(), wantErr.Error()) { + t.Errorf("want error %q to include %q", err, wantErr) + } + }) + } +}