Skip to content

Commit

Permalink
Merge pull request #630 from leonwanghui/leon-fixit
Browse files Browse the repository at this point in the history
Add some test cases in client package
  • Loading branch information
xing-yang authored Mar 20, 2019
2 parents c39ff25 + c46ae98 commit df1121c
Show file tree
Hide file tree
Showing 5 changed files with 232 additions and 16 deletions.
24 changes: 14 additions & 10 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,15 +101,20 @@ func (c *Client) Reset() *Client {
}

func processListParam(args []interface{}) (string, error) {
var filter map[string]string
var u string
var urlParam []string
var output string

if len(args) > 0 {
if len(args) > 1 {
return "", errors.New("only support one parameter that must be map[string]string")
}
filter = args[0].(map[string]string)
// If args is empty, just return the output immeadiately.
if len(args) == 0 {
return "", nil
}
// Add some limits for input args parameter.
if len(args) != 1 {
return "", errors.New("args should only support one parameter")
}
filter, ok := args[0].(map[string]string)
if !ok {
return "", errors.New("args element type should be map[string]string")
}

if filter != nil {
Expand All @@ -120,10 +125,9 @@ func processListParam(args []interface{}) (string, error) {
urlParam = append(urlParam, k+"="+v)
}
}

if len(urlParam) > 0 {
u = strings.Join(urlParam, "&")
output = strings.Join(urlParam, "&")
}

return u, nil
return output, nil
}
59 changes: 59 additions & 0 deletions client/client_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Copyright (c) 2019 Huawei Technologies Co., Ltd. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package client

import (
"reflect"
"testing"
)

func TestProcessListParam(t *testing.T) {
// Test case 1: The args should only support one parameter.
argA := map[string]string{"limit": "3"}
argB := map[string]string{"offset": "4"}
args := []interface{}{argA, argB}
_, err := processListParam(args)
expectedError := "args should only support one parameter"
if err == nil {
t.Errorf("expected Non-%v, got %v\n", nil, err)
} else {
if expectedError != err.Error() {
t.Errorf("expected Non-%v, got %v\n", expectedError, err.Error())
}
}

// Test case 2: The args type should only be map[string]string.
args = []interface{}{"limit=3&offset=4"}
_, err = processListParam(args)
expectedError = "args element type should be map[string]string"
if err == nil {
t.Errorf("expected Non-%v, got %v\n", nil, err)
} else {
if expectedError != err.Error() {
t.Errorf("expected Non-%v, got %v\n", expectedError, err.Error())
}
}

// Test case 3: Test the output format if everything works well.
arg := map[string]string{"limit": "3", "offset": "4"}
expected := "limit=3&offset=4"
params, err := processListParam([]interface{}{arg})
if err != nil {
t.Error(err)
}
if !reflect.DeepEqual(params, expected) {
t.Errorf("expected %v, got %v\n", expected, params)
}
}
2 changes: 0 additions & 2 deletions client/receiver.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,13 +107,11 @@ func request(urlStr string, method string, headers HeaderOption, input interface
// Set the request timeout a little bit longer upload snapshot to cloud temporarily.
req.SetTimeout(time.Minute*6, time.Minute*6)
// init body
log.Printf("%s %s\n", strings.ToUpper(method), urlStr)
if input != nil {
body, err := json.MarshalIndent(input, "", " ")
if err != nil {
return err
}
log.Printf("Request body:\n%s\n", string(body))
req.Body(body)
}

Expand Down
155 changes: 155 additions & 0 deletions client/replication_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
// Copyright (c) 2019 Huawei Technologies Co., Ltd. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package client

import (
"reflect"
"testing"

"github.com/opensds/opensds/pkg/model"
)

var fr = &ReplicationMgr{
Receiver: NewFakeReplicationReceiver(),
}

func TestCreateReplication(t *testing.T) {
expected := &model.ReplicationSpec{
BaseModel: &model.BaseModel{
Id: "c299a978-4f3e-11e8-8a5c-977218a83359",
},
PrimaryVolumeId: "bd5b12a8-a101-11e7-941e-d77981b584d8",
SecondaryVolumeId: "bd5b12a8-a101-11e7-941e-d77981b584d8",
Name: "sample-replication-01",
Description: "This is a sample replication for testing",
PoolId: "084bf71e-a102-11e7-88a8-e31fe6d52248",
ProfileId: "1106b972-66ef-11e7-b172-db03f3689c9c",
}

replica, err := fr.CreateReplication(&model.ReplicationSpec{})
if err != nil {
t.Error(err)
return
}

if !reflect.DeepEqual(replica, expected) {
t.Errorf("expected %v, got %v", expected, replica)
return
}
}

func TestGetReplication(t *testing.T) {
var replicaID = "c299a978-4f3e-11e8-8a5c-977218a83359"
expected := &model.ReplicationSpec{
BaseModel: &model.BaseModel{
Id: "c299a978-4f3e-11e8-8a5c-977218a83359",
},
PrimaryVolumeId: "bd5b12a8-a101-11e7-941e-d77981b584d8",
SecondaryVolumeId: "bd5b12a8-a101-11e7-941e-d77981b584d8",
Name: "sample-replication-01",
Description: "This is a sample replication for testing",
PoolId: "084bf71e-a102-11e7-88a8-e31fe6d52248",
ProfileId: "1106b972-66ef-11e7-b172-db03f3689c9c",
}

replica, err := fr.GetReplication(replicaID)
if err != nil {
t.Error(err)
return
}

if !reflect.DeepEqual(replica, expected) {
t.Errorf("expected %v, got %v", expected, replica)
return
}
}

func TestListReplications(t *testing.T) {
expected := []*model.ReplicationSpec{
{
BaseModel: &model.BaseModel{
Id: "c299a978-4f3e-11e8-8a5c-977218a83359",
},
PrimaryVolumeId: "bd5b12a8-a101-11e7-941e-d77981b584d8",
SecondaryVolumeId: "bd5b12a8-a101-11e7-941e-d77981b584d8",
Name: "sample-replication-01",
Description: "This is a sample replication for testing",
PoolId: "084bf71e-a102-11e7-88a8-e31fe6d52248",
ProfileId: "1106b972-66ef-11e7-b172-db03f3689c9c",
},
{
BaseModel: &model.BaseModel{
Id: "73bfdd58-4f3f-11e8-91c0-d39a05f391ee",
},
PrimaryVolumeId: "bd5b12a8-a101-11e7-941e-d77981b584d8",
SecondaryVolumeId: "bd5b12a8-a101-11e7-941e-d77981b584d8",
Name: "sample-replication-02",
Description: "This is a sample replication for testing",
PoolId: "084bf71e-a102-11e7-88a8-e31fe6d52248",
ProfileId: "1106b972-66ef-11e7-b172-db03f3689c9c",
},
}

replicas, err := fr.ListReplications(map[string]string{"limit": "3", "offset": "4"})
if err != nil {
t.Error(err)
return
}

if !reflect.DeepEqual(replicas, expected) {
t.Errorf("expected %v, got %v", expected, replicas)
return
}
}

func TestDeleteReplication(t *testing.T) {
var replicaID = "c299a978-4f3e-11e8-8a5c-977218a83359"

if err := fr.DeleteReplication(replicaID, &model.ReplicationSpec{}); err != nil {
t.Error(err)
return
}
}

func TestUpdateReplication(t *testing.T) {
var replicaID = "c299a978-4f3e-11e8-8a5c-977218a83359"
replica := &model.ReplicationSpec{
Name: "sample-replication-03",
Description: "This is a sample replication for testing",
}

result, err := fr.UpdateReplication(replicaID, replica)
if err != nil {
t.Error(err)
return
}

expected := &model.ReplicationSpec{
BaseModel: &model.BaseModel{
Id: "c299a978-4f3e-11e8-8a5c-977218a83359",
},
PrimaryVolumeId: "bd5b12a8-a101-11e7-941e-d77981b584d8",
SecondaryVolumeId: "bd5b12a8-a101-11e7-941e-d77981b584d8",
Name: "sample-replication-01",
Description: "This is a sample replication for testing",
PoolId: "084bf71e-a102-11e7-88a8-e31fe6d52248",
ProfileId: "1106b972-66ef-11e7-b172-db03f3689c9c",
}

if !reflect.DeepEqual(result, expected) {
t.Errorf("expected %v, got %v", expected, result)
return
}
}
8 changes: 4 additions & 4 deletions client/volume_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func TestCreateVolume(t *testing.T) {
}

func TestGetVolume(t *testing.T) {
var prfID = "bd5b12a8-a101-11e7-941e-d77981b584d8"
var volID = "bd5b12a8-a101-11e7-941e-d77981b584d8"
expected := &model.VolumeSpec{
BaseModel: &model.BaseModel{
Id: "bd5b12a8-a101-11e7-941e-d77981b584d8",
Expand All @@ -64,7 +64,7 @@ func TestGetVolume(t *testing.T) {
ProfileId: "1106b972-66ef-11e7-b172-db03f3689c9c",
}

vol, err := fv.GetVolume(prfID)
vol, err := fv.GetVolume(volID)
if err != nil {
t.Error(err)
return
Expand Down Expand Up @@ -114,12 +114,12 @@ func TestDeleteVolume(t *testing.T) {

func TestUpdateVolume(t *testing.T) {
var volID = "bd5b12a8-a101-11e7-941e-d77981b584d8"
vol := model.VolumeSpec{
vol := &model.VolumeSpec{
Name: "sample-volume",
Description: "This is a sample volume for testing",
}

result, err := fv.UpdateVolume(volID, &vol)
result, err := fv.UpdateVolume(volID, vol)
if err != nil {
t.Error(err)
return
Expand Down

0 comments on commit df1121c

Please sign in to comment.