|
| 1 | +package cinder |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/container-storage-interface/spec/lib/go/csi" |
| 7 | + "github.com/kubernetes-csi/drivers/pkg/cinder/openstack" |
| 8 | + "github.com/stretchr/testify/assert" |
| 9 | + "github.com/stretchr/testify/mock" |
| 10 | +) |
| 11 | + |
| 12 | +var fakeCs *controllerServer |
| 13 | + |
| 14 | +// Init Controller Server |
| 15 | +func init() { |
| 16 | + if fakeCs == nil { |
| 17 | + d := NewDriver(fakeNodeID, fakeEndpoint) |
| 18 | + fakeCs = NewControllerServer(d) |
| 19 | + } |
| 20 | +} |
| 21 | + |
| 22 | +// Test CreateVolume |
| 23 | +func TestCreateVolume(t *testing.T) { |
| 24 | + |
| 25 | + // mock OpenStack |
| 26 | + osmock := new(openstack.OpenStackMock) |
| 27 | + // CreateVolume(name string, size int, vtype, availability string, tags *map[string]string) (string, string, error) |
| 28 | + osmock.On("CreateVolume", fakeVolName, mock.AnythingOfType("int"), fakeVolType, fakeAvailability, (*map[string]string)(nil)).Return(fakeVolID, fakeAvailability, nil) |
| 29 | + openstack.OsInstance = osmock |
| 30 | + |
| 31 | + // Init assert |
| 32 | + assert := assert.New(t) |
| 33 | + |
| 34 | + // Fake request |
| 35 | + fakeReq := &csi.CreateVolumeRequest{ |
| 36 | + Version: &version, |
| 37 | + Name: fakeVolName, |
| 38 | + VolumeCapabilities: nil, |
| 39 | + } |
| 40 | + |
| 41 | + // Invoke CreateVolume |
| 42 | + actualRes, err := fakeCs.CreateVolume(fakeCtx, fakeReq) |
| 43 | + if err != nil { |
| 44 | + t.Errorf("failed to CreateVolume: %v", err) |
| 45 | + } |
| 46 | + |
| 47 | + // Assert |
| 48 | + assert.NotNil(actualRes.VolumeInfo) |
| 49 | + |
| 50 | + assert.NotEqual(0, len(actualRes.VolumeInfo.Id), "Volume Id is nil") |
| 51 | + |
| 52 | + assert.Equal(fakeAvailability, actualRes.VolumeInfo.Attributes["availability"]) |
| 53 | +} |
| 54 | + |
| 55 | +// Test DeleteVolume |
| 56 | +func TestDeleteVolume(t *testing.T) { |
| 57 | + |
| 58 | + // mock OpenStack |
| 59 | + osmock := new(openstack.OpenStackMock) |
| 60 | + // DeleteVolume(volumeID string) error |
| 61 | + osmock.On("DeleteVolume", fakeVolID).Return(nil) |
| 62 | + openstack.OsInstance = osmock |
| 63 | + |
| 64 | + // Init assert |
| 65 | + assert := assert.New(t) |
| 66 | + |
| 67 | + // Fake request |
| 68 | + fakeReq := &csi.DeleteVolumeRequest{ |
| 69 | + Version: &version, |
| 70 | + VolumeId: fakeVolID, |
| 71 | + } |
| 72 | + |
| 73 | + // Expected Result |
| 74 | + expectedRes := &csi.DeleteVolumeResponse{} |
| 75 | + |
| 76 | + // Invoke DeleteVolume |
| 77 | + actualRes, err := fakeCs.DeleteVolume(fakeCtx, fakeReq) |
| 78 | + if err != nil { |
| 79 | + t.Errorf("failed to DeleteVolume: %v", err) |
| 80 | + } |
| 81 | + |
| 82 | + // Assert |
| 83 | + assert.Equal(expectedRes, actualRes) |
| 84 | +} |
| 85 | + |
| 86 | +// Test ControllerPublishVolume |
| 87 | +func TestControllerPublishVolume(t *testing.T) { |
| 88 | + |
| 89 | + // mock OpenStack |
| 90 | + osmock := new(openstack.OpenStackMock) |
| 91 | + // AttachVolume(instanceID, volumeID string) (string, error) |
| 92 | + osmock.On("AttachVolume", fakeNodeID, fakeVolID).Return(fakeVolID, nil) |
| 93 | + // WaitDiskAttached(instanceID string, volumeID string) error |
| 94 | + osmock.On("WaitDiskAttached", fakeNodeID, fakeVolID).Return(nil) |
| 95 | + // GetAttachmentDiskPath(instanceID, volumeID string) (string, error) |
| 96 | + osmock.On("GetAttachmentDiskPath", fakeNodeID, fakeVolID).Return(fakeDevicePath, nil) |
| 97 | + openstack.OsInstance = osmock |
| 98 | + |
| 99 | + // Init assert |
| 100 | + assert := assert.New(t) |
| 101 | + |
| 102 | + // Fake request |
| 103 | + fakeReq := &csi.ControllerPublishVolumeRequest{ |
| 104 | + Version: &version, |
| 105 | + VolumeId: fakeVolID, |
| 106 | + NodeId: fakeNodeID, |
| 107 | + VolumeCapability: nil, |
| 108 | + Readonly: false, |
| 109 | + } |
| 110 | + |
| 111 | + // Expected Result |
| 112 | + expectedRes := &csi.ControllerPublishVolumeResponse{ |
| 113 | + PublishVolumeInfo: map[string]string{ |
| 114 | + "DevicePath": fakeDevicePath, |
| 115 | + }, |
| 116 | + } |
| 117 | + |
| 118 | + // Invoke ControllerPublishVolume |
| 119 | + actualRes, err := fakeCs.ControllerPublishVolume(fakeCtx, fakeReq) |
| 120 | + if err != nil { |
| 121 | + t.Errorf("failed to ControllerPublishVolume: %v", err) |
| 122 | + } |
| 123 | + |
| 124 | + // Assert |
| 125 | + assert.Equal(expectedRes, actualRes) |
| 126 | +} |
| 127 | + |
| 128 | +// Test ControllerUnpublishVolume |
| 129 | +func TestControllerUnpublishVolume(t *testing.T) { |
| 130 | + |
| 131 | + // mock OpenStack |
| 132 | + osmock := new(openstack.OpenStackMock) |
| 133 | + // DetachVolume(instanceID, volumeID string) error |
| 134 | + osmock.On("DetachVolume", fakeNodeID, fakeVolID).Return(nil) |
| 135 | + // WaitDiskDetached(instanceID string, volumeID string) error |
| 136 | + osmock.On("WaitDiskDetached", fakeNodeID, fakeVolID).Return(nil) |
| 137 | + openstack.OsInstance = osmock |
| 138 | + |
| 139 | + // Init assert |
| 140 | + assert := assert.New(t) |
| 141 | + |
| 142 | + // Fake request |
| 143 | + fakeReq := &csi.ControllerUnpublishVolumeRequest{ |
| 144 | + Version: &version, |
| 145 | + VolumeId: fakeVolID, |
| 146 | + NodeId: fakeNodeID, |
| 147 | + } |
| 148 | + |
| 149 | + // Expected Result |
| 150 | + expectedRes := &csi.ControllerUnpublishVolumeResponse{} |
| 151 | + |
| 152 | + // Invoke ControllerUnpublishVolume |
| 153 | + actualRes, err := fakeCs.ControllerUnpublishVolume(fakeCtx, fakeReq) |
| 154 | + if err != nil { |
| 155 | + t.Errorf("failed to ControllerUnpublishVolume: %v", err) |
| 156 | + } |
| 157 | + |
| 158 | + // Assert |
| 159 | + assert.Equal(expectedRes, actualRes) |
| 160 | +} |
0 commit comments