|
| 1 | +package driver |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "github.com/container-storage-interface/spec/lib/go/csi" |
| 7 | + "github.com/sirupsen/logrus" |
| 8 | + "strings" |
| 9 | + "testing" |
| 10 | +) |
| 11 | + |
| 12 | +type TestPodVolume struct { |
| 13 | + ClaimName string |
| 14 | + SizeGB int |
| 15 | + StorageClass string |
| 16 | + LuksKey string |
| 17 | +} |
| 18 | + |
| 19 | +type TestPodDescriptor struct { |
| 20 | + Kind string |
| 21 | + Name string |
| 22 | + Volumes []TestPodVolume |
| 23 | +} |
| 24 | + |
| 25 | +type DiskInfo struct { |
| 26 | + PVCName string `json:"pvcName"` |
| 27 | + DeviceName string `json:"deviceName"` |
| 28 | + DeviceSize int `json:"deviceSize"` |
| 29 | + Filesystem string `json:"filesystem"` |
| 30 | + FilesystemUUID string `json:"filesystemUUID"` |
| 31 | + FilesystemSize int `json:"filesystemSize"` |
| 32 | + DeviceSource string `json:"deviceSource"` |
| 33 | + Luks string `json:"luks,omitempty"` |
| 34 | + Cipher string `json:"cipher,omitempty"` |
| 35 | + Keysize int `json:"keysize,omitempty"` |
| 36 | +} |
| 37 | + |
| 38 | +// creates a kubernetes pod from the given TestPodDescriptor |
| 39 | +func TestCreateLuksVolume(t *testing.T) { |
| 40 | + tests := []struct { |
| 41 | + name string |
| 42 | + listVolumesErr error |
| 43 | + getSnapshotErr error |
| 44 | + }{ |
| 45 | + { |
| 46 | + name: "listing volumes failing", |
| 47 | + listVolumesErr: errors.New("failed to list volumes"), |
| 48 | + }, |
| 49 | + { |
| 50 | + name: "fetching snapshot failing", |
| 51 | + getSnapshotErr: errors.New("failed to get snapshot"), |
| 52 | + }, |
| 53 | + } |
| 54 | + |
| 55 | + for _, test := range tests { |
| 56 | + t.Run(test.name, func(t *testing.T) { |
| 57 | + d := &Driver{ |
| 58 | + storage: &fakeStorageDriver{ |
| 59 | + listVolumesErr: test.listVolumesErr, |
| 60 | + }, |
| 61 | + snapshots: &fakeSnapshotsDriver{ |
| 62 | + getSnapshotErr: test.getSnapshotErr, |
| 63 | + }, |
| 64 | + log: logrus.New().WithField("test_enabed", true), |
| 65 | + } |
| 66 | + |
| 67 | + _, err := d.CreateVolume(context.Background(), &csi.CreateVolumeRequest{ |
| 68 | + Name: "name", |
| 69 | + Parameters: map[string]string{ |
| 70 | + LuksCipherAttribute: "cipher", |
| 71 | + LuksEncryptedAttribute: "true", |
| 72 | + LuksKeyAttribute: "key", |
| 73 | + LuksKeySizeAttribute: "23234", |
| 74 | + }, |
| 75 | + VolumeCapabilities: []*csi.VolumeCapability{ |
| 76 | + &csi.VolumeCapability{ |
| 77 | + AccessType: &csi.VolumeCapability_Mount{ |
| 78 | + Mount: &csi.VolumeCapability_MountVolume{}, |
| 79 | + }, |
| 80 | + AccessMode: &csi.VolumeCapability_AccessMode{ |
| 81 | + Mode: csi.VolumeCapability_AccessMode_SINGLE_NODE_WRITER, |
| 82 | + }, |
| 83 | + }, |
| 84 | + }, |
| 85 | + VolumeContentSource: &csi.VolumeContentSource{ |
| 86 | + Type: &csi.VolumeContentSource_Snapshot{ |
| 87 | + Snapshot: &csi.VolumeContentSource_SnapshotSource{ |
| 88 | + SnapshotId: "snapshotId", |
| 89 | + }, |
| 90 | + }, |
| 91 | + }, |
| 92 | + }) |
| 93 | + |
| 94 | + var wantErr error |
| 95 | + switch { |
| 96 | + case test.listVolumesErr != nil: |
| 97 | + wantErr = test.listVolumesErr |
| 98 | + case test.getSnapshotErr != nil: |
| 99 | + wantErr = test.getSnapshotErr |
| 100 | + } |
| 101 | + |
| 102 | + if wantErr == nil && err != nil { |
| 103 | + t.Errorf("got error %q, want none", err) |
| 104 | + } |
| 105 | + if wantErr != nil && !strings.Contains(err.Error(), wantErr.Error()) { |
| 106 | + t.Errorf("want error %q to include %q", err, wantErr) |
| 107 | + } |
| 108 | + }) |
| 109 | + } |
| 110 | +} |
0 commit comments