Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changes for ut #29

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 21 additions & 11 deletions .secrets.baseline
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"files": "go.sum|^.secrets.baseline$",
"lines": null
},
"generated_at": "2021-04-07T05:44:16Z",
"generated_at": "2021-05-18T04:54:46Z",
"plugins_used": [
{
"name": "AWSKeyDetector"
Expand Down Expand Up @@ -66,42 +66,52 @@
"results": {
"pkg/driver/controllerserver.go": [
{
"hashed_secret": "a6c9db5260d5ccc01b754ff92c4ead3a10894370",
"hashed_secret": "f75b33f87ffeacb3a4f793a09693e672e07449ff",
"is_secret": false,
"is_verified": false,
"line_number": 99,
"line_number": 95,
"type": "Secret Keyword",
"verified_result": null
},
{
"hashed_secret": "c7c6508b19455e3e8040e60e9833fbede92e5d8e",
"is_secret": false,
"is_verified": false,
"line_number": 203,
"type": "Secret Keyword",
"verified_result": null
}
],
"pkg/s3client/s3client.go": [
"pkg/driver/controllerserver_test.go": [
{
"hashed_secret": "d4c3d66fd0c38547a3c7a4c6bdc29c36911bc030",
"hashed_secret": "b60d121b438a380c343d5ec3c2037564b82ffef3",
"is_secret": false,
"is_verified": false,
"line_number": 55,
"line_number": 329,
"type": "Secret Keyword",
"verified_result": null
},
}
],
"pkg/s3client/s3client.go": [
{
"hashed_secret": "096aacba084fa9ee67efa56625495beaeba085a1",
"is_secret": false,
"is_verified": false,
"line_number": 121,
"line_number": 175,
"type": "Secret Keyword",
"verified_result": null
},
{
"hashed_secret": "08404fa087437e04fbf3f688ca71d6ed039d65af",
"hashed_secret": "ccba5450a9a31dd78751224042f7598b6ac2f04f",
"is_secret": false,
"is_verified": false,
"line_number": 133,
"line_number": 180,
"type": "Secret Keyword",
"verified_result": null
}
]
},
"version": "0.13.1+ibm.34.dss",
"version": "0.13.1+ibm.31.dss",
"word_list": {
"file": null,
"hash": null
Expand Down
4 changes: 2 additions & 2 deletions pkg/driver/controllerserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,10 +168,10 @@ func (cs *controllerServer) CreateVolume(ctx context.Context, req *csi.CreateVol
klog.Error("CreateVolume: Unable to access the bucket: %v", err)
return nil, status.Error(codes.PermissionDenied, fmt.Sprintf("Unable to access the bucket: %v", bucketName))
}
params["cos-endpoint"] = endPoint
/*params["cos-endpoint"] = endPoint
params["location-constraint"] = locationConstraint
params["bucket-name"] = bucketName
params["obj-path"] = secretMap["obj-path"]
params["obj-path"] = secretMap["obj-path"]*/

klog.Infof("create volume: %v", volumeID)

Expand Down
173 changes: 165 additions & 8 deletions pkg/driver/controllerserver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,30 @@
* limitations under the License.
*/

//Package driver ...
//Package driver ...
package driver

import (
providerError "github.com/IBM/ibmcloud-storage-volume-lib/lib/utils"
"github.com/IBM/ibmcloud-volume-interface/lib/provider"
fakeclient "github.com/IBM/satellite-object-storage-plugin/pkg/driver/fake/s3client"
"github.com/IBM/satellite-object-storage-plugin/pkg/s3client"
csi "github.com/container-storage-interface/spec/lib/go/csi"
"github.com/stretchr/testify/assert"
"golang.org/x/net/context"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"reflect"
"testing"
)

func getCustomControllerServer(csiDriver *s3Driver, factory s3client.ObjectStorageSessionFactory) *controllerServer {
return &controllerServer{
s3Driver: csiDriver,
newSession: factory,
}
}

var (
// Define "normal" parameters
volCaps = []*csi.VolumeCapability{
Expand Down Expand Up @@ -95,13 +105,19 @@ func TestCreateVolumeArguments(t *testing.T) {
Name: volName,
CapacityRange: stdCapRange,
VolumeCapabilities: stdVolCap,
Secrets: map[string]string{"access-key": "xxx",
"secret-key": "yyy",
"bucket-name": "test-bucket",
"location-constraint": "test-region",
"cos-endpoint": "test-endpoint",
},
},
expVol: &csi.Volume{
CapacityBytes: 20 * 1024 * 1024, // In byte
VolumeId: "testVolumeId",
},
libVolumeResponse: &provider.Volume{Capacity: &cap, Name: &volName, VolumeID: "testVolumeId", Iops: &iopsStr, Az: "myzone", Region: "myregion"},
expErrCode: codes.InvalidArgument,
expErrCode: codes.OK,
libVolumeError: nil,
},
{
Expand Down Expand Up @@ -152,16 +168,80 @@ func TestCreateVolumeArguments(t *testing.T) {
expErrCode: codes.OutOfRange,
libVolumeError: nil,
},
{
name: "Empty Secret Key",
req: &csi.CreateVolumeRequest{
Name: volName,
CapacityRange: stdCapRange,
VolumeCapabilities: stdVolCap,
Secrets: map[string]string{"access-key": "xxx",
"secret-key": "",
"bucket-name": "test-bucket",
"location-constraint": "test-region",
"cos-endpoint": "test-endpoint",
},
},
expVol: &csi.Volume{
CapacityBytes: 20 * 1024 * 1024, // In byte
VolumeId: "testVolumeId",
},
libVolumeResponse: &provider.Volume{Capacity: &cap, Name: &volName, VolumeID: "testVolumeId", Iops: &iopsStr, Az: "myzone", Region: "myregion"},
expErrCode: codes.InvalidArgument,
libVolumeError: nil,
},
{
name: "Empty Bucket Name",
req: &csi.CreateVolumeRequest{
Name: volName,
CapacityRange: stdCapRange,
VolumeCapabilities: stdVolCap,
Secrets: map[string]string{"access-key": "xxx",
"secret-key": "xxx",
"bucket-name": "",
"location-constraint": "test-region",
"cos-endpoint": "test-endpoint",
},
},
expVol: &csi.Volume{
CapacityBytes: 20 * 1024 * 1024, // In byte
VolumeId: "testVolumeId",
},
libVolumeResponse: &provider.Volume{Capacity: &cap, Name: &volName, VolumeID: "testVolumeId", Iops: &iopsStr, Az: "myzone", Region: "myregion"},
expErrCode: codes.InvalidArgument,
libVolumeError: nil,
},
{
name: "Empty Access Key",
req: &csi.CreateVolumeRequest{
Name: volName,
CapacityRange: stdCapRange,
VolumeCapabilities: stdVolCap,
Secrets: map[string]string{"access-key": "",
"secret-key": "xxx",
"bucket-name": "test-bucket",
"location-constraint": "test-region",
"cos-endpoint": "test-endpoint",
},
},
expVol: &csi.Volume{
CapacityBytes: 20 * 1024 * 1024, // In byte
VolumeId: "testVolumeId",
},
libVolumeResponse: &provider.Volume{Capacity: &cap, Name: &volName, VolumeID: "testVolumeId", Iops: &iopsStr, Az: "myzone", Region: "myregion"},
expErrCode: codes.InvalidArgument,
libVolumeError: nil,
},
}

// Run test cases
for _, tc := range testCases {
t.Logf("test case: %s", tc.name)
// Setup new driver each time so no interference
icDriver := inits3Driver(t)
cs := getCustomControllerServer(icDriver, &fakeclient.ObjectStorageSessionFactory{})

// Call CSI CreateVolume
resp, err := icDriver.cs.CreateVolume(context.Background(), tc.req)
resp, err := cs.CreateVolume(context.Background(), tc.req)
if err != nil {
//errorType := providerError.GetErrorType(err)
serverError, ok := status.FromError(err)
Expand Down Expand Up @@ -198,9 +278,15 @@ func TestDeleteVolume(t *testing.T) {
}{
{
name: "Success volume delete",
req: &csi.DeleteVolumeRequest{VolumeId: "testVolumeId"},
//expResponse: &csi.DeleteVolumeResponse{},
expResponse: nil,
req: &csi.DeleteVolumeRequest{VolumeId: "testVolumeId",
Secrets: map[string]string{"access-key": "xxx",
"secret-key": "xxx",
"bucket-name": "test-bucket",
"location-constraint": "test-region",
"cos-endpoint": "test-endpoint",
},
},
expResponse: &csi.DeleteVolumeResponse{},
expErrCode: codes.OK,
libVolumeResponse: nil,
libVolumeGetResponce: &provider.Volume{VolumeID: "testVolumeId", Az: "myzone", Region: "myregion"},
Expand All @@ -221,15 +307,47 @@ func TestDeleteVolume(t *testing.T) {
libVolumeResponse: providerError.Message{Code: "FailedToDeleteVolume", Description: "Volume deletion failed", Type: providerError.DeletionFailed},
libVolumeGetResponce: &provider.Volume{VolumeID: "testVolumeId", Az: "myzone", Region: "myregion"},
},
{
name: "Empty bucket name",
req: &csi.DeleteVolumeRequest{VolumeId: "testVolumeId",
Secrets: map[string]string{"access-key": "xxx",
"secret-key": "xxx",
"bucket-name": "",
"location-constraint": "test-region",
"cos-endpoint": "test-endpoint",
},
},
//expResponse: &csi.DeleteVolumeResponse{},
expResponse: nil,
expErrCode: codes.OK,
libVolumeResponse: nil,
libVolumeGetResponce: &provider.Volume{VolumeID: "testVolumeId", Az: "myzone", Region: "myregion"},
},
{
name: "Empty service instance id",
req: &csi.DeleteVolumeRequest{VolumeId: "testVolumeId",
Secrets: map[string]string{"api-key": "xxx",
"s-id": "",
"bucket-name": "",
"location-constraint": "test-region",
"cos-endpoint": "test-endpoint",
},
},
//expResponse: &csi.DeleteVolumeResponse{},
expResponse: nil,
expErrCode: codes.InvalidArgument,
libVolumeResponse: nil,
libVolumeGetResponce: &provider.Volume{VolumeID: "testVolumeId", Az: "myzone", Region: "myregion"},
},
}

// Run test cases
for _, tc := range testCases {
t.Logf("test case: %s", tc.name)
// Setup new driver each time so no interference
icDriver := inits3Driver(t)

response, err := icDriver.cs.DeleteVolume(context.Background(), tc.req)
cs := getCustomControllerServer(icDriver, &fakeclient.ObjectStorageSessionFactory{})
response, err := cs.DeleteVolume(context.Background(), tc.req)
if tc.expErrCode != codes.OK {
t.Logf("Error code")
assert.NotNil(t, err)
Expand Down Expand Up @@ -537,3 +655,42 @@ func TestGetCapacity(t *testing.T) {
}

}

func TestControllerGetCapabilities(t *testing.T) {
// test cases
testCases := []struct {
name string
req *csi.ControllerGetCapabilitiesRequest
expResponse *csi.ControllerGetCapabilitiesResponse
expErrCode codes.Code
}{
{
name: "Success controller get capabilities",
req: &csi.ControllerGetCapabilitiesRequest{},
expResponse: &csi.ControllerGetCapabilitiesResponse{
Capabilities: []*csi.ControllerServiceCapability{
{Type: &csi.ControllerServiceCapability_Rpc{Rpc: &csi.ControllerServiceCapability_RPC{Type: csi.ControllerServiceCapability_RPC_CREATE_DELETE_VOLUME}}},
},
},
expErrCode: codes.OK,
},
}

// Run test cases
for _, tc := range testCases {
t.Logf("test case: %s", tc.name)
// Setup new driver each time so no interference
icDriver := inits3Driver(t)

// Call CSI CreateVolume
response, err := icDriver.cs.ControllerGetCapabilities(context.Background(), tc.req)
if tc.expErrCode != codes.OK {
t.Logf("Error code")
assert.NotNil(t, err)
}

if !reflect.DeepEqual(response, tc.expResponse) {
assert.Equal(t, tc.expResponse, response)
}
}
}
Loading