Skip to content

Commit

Permalink
fix UTs
Browse files Browse the repository at this point in the history
Signed-off-by: alice-px <[email protected]>
  • Loading branch information
alicelyy committed Jan 22, 2024
1 parent bf51bf6 commit c8c6d55
Show file tree
Hide file tree
Showing 622 changed files with 16,703 additions and 47,607 deletions.
24 changes: 12 additions & 12 deletions api/server/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,12 +141,12 @@ func (d *driver) errorResponse(method string, w http.ResponseWriter, err error)
}
}

func (d *driver) volFromName(name string) (*api.Volume, error) {
func (d *driver) volFromName(ctx context.Context, name string) (*api.Volume, error) {
v, err := volumedrivers.Get(d.name)
if err != nil {
return nil, fmt.Errorf("Cannot locate volume driver for %s: %s", d.name, err.Error())
return nil, fmt.Errorf("cannot locate volume driver for %s: %s", d.name, err.Error())
}
return util.VolumeFromName(v, name)
return util.VolumeFromName(ctx, v, name)
}

func (d *driver) volFromNameOrIDSdk(ctx context.Context, volumes api.OpenStorageVolumeClient, name string) (*api.Volume, error) {
Expand Down Expand Up @@ -254,10 +254,10 @@ func (d *driver) attachTokenMount(ctx context.Context, request *mountRequest) (c

// parseTokenInput reads token input from the given name and opts.
// The following is the order of precedence for token in types:
// 1. token=<token> in name
// 2. token in opts
// 3. token_secret=<secret> in name
// 4. token_secret in opts
// 1. token=<token> in name
// 2. token in opts
// 3. token_secret=<secret> in name
// 4. token_secret in opts
func (d *driver) parseTokenInput(name string, opts map[string]string) (string, error) {
// get token from name
tokenFromName, tokenInName := d.GetTokenFromString(name)
Expand Down Expand Up @@ -484,7 +484,7 @@ func (d *driver) scaleUp(
return nil, err
}
id := resp.GetVolumeId()
if outVol, err = d.volFromName(id); err != nil {
if outVol, err = d.volFromName(ctx, id); err != nil {
return nil, err
}
_, err = mountClient.Attach(ctx, &api.SdkVolumeAttachRequest{
Expand Down Expand Up @@ -571,7 +571,7 @@ func (d *driver) attachScale(
return d.scaleUp(ctx, conn, method, vd, inSpec, inVol, allVols, attachOptions)
}
id := resp.GetVolumeId()
outVol, err := d.volFromName(id)
outVol, err := d.volFromName(ctx, id)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -791,7 +791,7 @@ func (d *driver) path(w http.ResponseWriter, r *http.Request) {
}

_, _, _, _, name := d.SpecFromString(request.Name)
vol, err := d.volFromName(name)
vol, err := d.volFromName(r.Context(), name)
if err != nil {
e := d.volNotFound(method, request.Name, err, w)
d.errorResponse(method, w, e)
Expand Down Expand Up @@ -824,7 +824,7 @@ func (d *driver) get(w http.ResponseWriter, r *http.Request) {
} else {
returnName = name
}
vol, err := d.volFromName(name)
vol, err := d.volFromName(correlation.TODO(), name)
if err != nil {
e := d.volNotFound(method, request.Name, err, w)
d.errorResponse(method, w, e)
Expand Down Expand Up @@ -859,7 +859,7 @@ func (d *driver) unmount(w http.ResponseWriter, r *http.Request) {

_, _, _, _, name := d.SpecFromString(request.Name)
nameWithID := name + request.ID
vol, err := d.volFromName(name)
vol, err := d.volFromName(ctx, name)
if err != nil {
e := d.volNotFound(method, name, err, w)
d.errorResponse(method, w, e)
Expand Down
3 changes: 2 additions & 1 deletion api/server/sdk/node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"time"

"github.com/stretchr/testify/assert"
"github.com/golang/mock/gomock"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"

Expand Down Expand Up @@ -460,7 +461,7 @@ func TestSdkVolumeUsageByNode(t *testing.T) {

s.MockCluster().EXPECT().Enumerate().Return(cluster, nil).Times(1)
s.MockCluster().EXPECT().Inspect(nodeid).Return(node, nil).Times(2)
s.MockDriver().EXPECT().VolumeUsageByNode(nodeid).Return(&volumeUsageInfo, nil).Times(1)
s.MockDriver().EXPECT().VolumeUsageByNode(gomock.Any(), nodeid).Return(&volumeUsageInfo, nil).Times(1)

// Setup client
c := api.NewOpenStorageNodeClient(s.Conn())
Expand Down
2 changes: 1 addition & 1 deletion api/server/sdk/server_interceptors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ func TestAuthorizationServerInterceptorCreateVolume(t *testing.T) {
gomock.InOrder(
s.MockDriver().
EXPECT().
Inspect([]string{name}).
Inspect(gomock.Any(), []string{name}).
Return(nil, fmt.Errorf("not found")).
AnyTimes(),
s.MockDriver().
Expand Down
10 changes: 5 additions & 5 deletions api/server/sdk/volume_ops.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ import (

"github.com/libopenstorage/openstorage/api"
"github.com/libopenstorage/openstorage/pkg/auth"
"github.com/libopenstorage/openstorage/pkg/correlation"
policy "github.com/libopenstorage/openstorage/pkg/storagepolicy"
"github.com/libopenstorage/openstorage/pkg/correlation"
"github.com/libopenstorage/openstorage/pkg/util"
"github.com/libopenstorage/openstorage/volume"
"github.com/portworx/kvdb"
Expand All @@ -54,7 +54,7 @@ func (s *VolumeServer) waitForVolumeReady(ctx context.Context, id string) (*api.
func() (bool, error) {
var err error
// Get the latest status from the volume
v, err = util.VolumeFromName(s.driver(ctx), id)
v, err = util.VolumeFromName(correlation.TODO(), s.driver(ctx), id)
if err != nil {
return false, status.Errorf(codes.Internal, err.Error())
}
Expand Down Expand Up @@ -89,7 +89,7 @@ func (s *VolumeServer) waitForVolumeRemoved(ctx context.Context, id string) erro
250*time.Millisecond, // period
func() (bool, error) {
// Get the latest status from the volume
if _, err := util.VolumeFromName(s.driver(ctx), id); err != nil {
if _, err := util.VolumeFromName(correlation.TODO(), s.driver(ctx), id); err != nil {
// Removed
return false, nil
}
Expand All @@ -109,7 +109,7 @@ func (s *VolumeServer) create(

// Check if the volume has already been created or is in process of creation
volName := locator.GetName()
v, err := util.VolumeFromName(s.driver(ctx), volName)
v, err := util.VolumeFromName(ctx, s.driver(ctx), volName)
// If the volume is still there but it is being delete, then wait until it is removed
if err == nil && v.GetState() == api.VolumeState_VOLUME_STATE_DELETED {
if err = s.waitForVolumeRemoved(ctx, volName); err != nil {
Expand Down Expand Up @@ -156,7 +156,7 @@ func (s *VolumeServer) create(
var id string
if len(source.GetParent()) != 0 {
// Get parent volume information
parent, err := util.VolumeFromName(s.driver(ctx), source.Parent)
parent, err := util.VolumeFromName(correlation.TODO(), s.driver(ctx), source.Parent)
if err != nil {
return "", status.Errorf(
codes.NotFound,
Expand Down
54 changes: 27 additions & 27 deletions api/server/sdk/volume_ops_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,22 +76,22 @@ func TestSdkVolumeCreateCheckIdempotencyWaitForRemoved(t *testing.T) {
gomock.InOrder(
s.MockDriver().
EXPECT().
Inspect([]string{name}).
Inspect(gomock.Any(), []string{name}).
Return([]*api.Volume{vol}, nil),

s.MockDriver().
EXPECT().
Inspect([]string{name}).
Inspect(gomock.Any(), []string{name}).
Return([]*api.Volume{vol}, nil),

s.MockDriver().
EXPECT().
Inspect([]string{name}).
Inspect(gomock.Any(), []string{name}).
Return([]*api.Volume{vol}, nil),

s.MockDriver().
EXPECT().
Inspect([]string{name}).
Inspect(gomock.Any(), []string{name}).
Return(nil, fmt.Errorf("MOCK ERROR")),

s.MockDriver().
Expand Down Expand Up @@ -150,8 +150,8 @@ func TestSdkVolumeCreateCheckIdempotencyWaitForReady(t *testing.T) {
// 1 for waiting but getting that the volume is up
s.MockDriver().
EXPECT().
Inspect([]string{name}).
Do(func([]string) {
Inspect(gomock.Any(), []string{name}).
Do(func(context.Context, []string) {
count++
if count == 4 {
vol.Status = api.VolumeStatus_VOLUME_STATUS_UP
Expand Down Expand Up @@ -187,7 +187,7 @@ func TestSdkVolumeCreateCheckIdempotency(t *testing.T) {
id := "myid"
s.MockDriver().
EXPECT().
Inspect([]string{name}).
Inspect(gomock.Any(), []string{name}).
Return([]*api.Volume{
{
Id: id,
Expand Down Expand Up @@ -231,7 +231,7 @@ func TestSdkVolumeCreate(t *testing.T) {
gomock.InOrder(
s.MockDriver().
EXPECT().
Inspect([]string{name}).
Inspect(gomock.Any(), []string{name}).
Return(nil, fmt.Errorf("not found")).
Times(1),

Expand Down Expand Up @@ -295,7 +295,7 @@ func TestSdkVolumeClone(t *testing.T) {

s.MockDriver().
EXPECT().
Inspect([]string{name}).
Inspect(gomock.Any(), []string{name}).
Return(nil, fmt.Errorf("not found")).
Times(1),

Expand All @@ -307,7 +307,7 @@ func TestSdkVolumeClone(t *testing.T) {

s.MockDriver().
EXPECT().
Inspect([]string{parentid}).
Inspect(gomock.Any(), []string{parentid}).
Return([]*api.Volume{parentVol}, nil).
Times(1),

Expand Down Expand Up @@ -456,7 +456,7 @@ func TestSdkVolumeInspect(t *testing.T) {
req.Options = &api.VolumeInspectOptions{Deep: true}
s.MockDriver().
EXPECT().
Inspect([]string{id}).
Inspect(gomock.Any(), []string{id}).
Return([]*api.Volume{
{
Id: id,
Expand Down Expand Up @@ -505,7 +505,7 @@ func TestSdkVolumeInspectKeyNotFound(t *testing.T) {
// Returns key not found
s.MockDriver().
EXPECT().
Inspect([]string{id}).
Inspect(gomock.Any(), []string{id}).
Return([]*api.Volume{}, kvdb.ErrNotFound).
Times(1)

Expand All @@ -522,7 +522,7 @@ func TestSdkVolumeInspectKeyNotFound(t *testing.T) {
// Key not found, err is nil but empty list returned
s.MockDriver().
EXPECT().
Inspect([]string{id}).
Inspect(gomock.Any(), []string{id}).
Return([]*api.Volume{}, nil).
Times(1)

Expand All @@ -539,7 +539,7 @@ func TestSdkVolumeInspectKeyNotFound(t *testing.T) {
expectedErr := fmt.Errorf("WEIRD ERROR")
s.MockDriver().
EXPECT().
Inspect([]string{id}).
Inspect(gomock.Any(), []string{id}).
Return([]*api.Volume{}, expectedErr).
Times(1)

Expand Down Expand Up @@ -720,7 +720,7 @@ func TestSdkVolumeStats(t *testing.T) {
Times(1)
s.MockDriver().
EXPECT().
Stats(id, cumulative).
Stats(gomock.Any(), id, cumulative).
Return(&api.Stats{
Reads: 12345,
}, nil).
Expand Down Expand Up @@ -1069,7 +1069,7 @@ func TestSdkCloneOwnership(t *testing.T) {

mv.
EXPECT().
Inspect([]string{name}).
Inspect(gomock.Any(), []string{name}).
Return(nil, fmt.Errorf("not found")).
Times(1),

Expand All @@ -1081,7 +1081,7 @@ func TestSdkCloneOwnership(t *testing.T) {

mv.
EXPECT().
Inspect([]string{parentid}).
Inspect(gomock.Any(), []string{parentid}).
Return([]*api.Volume{parentVol}, nil).
Times(1),

Expand Down Expand Up @@ -1116,7 +1116,7 @@ func TestSdkCloneOwnership(t *testing.T) {

mv.
EXPECT().
Inspect([]string{name}).
Inspect(gomock.Any(), []string{name}).
Return(nil, fmt.Errorf("not found")).
Times(1),

Expand All @@ -1128,7 +1128,7 @@ func TestSdkCloneOwnership(t *testing.T) {

mv.
EXPECT().
Inspect([]string{parentid}).
Inspect(gomock.Any(), []string{parentid}).
Return([]*api.Volume{parentVol}, nil).
Times(1),

Expand Down Expand Up @@ -1187,7 +1187,7 @@ func TestSdkCloneOwnership(t *testing.T) {

mv.
EXPECT().
Inspect([]string{name}).
Inspect(gomock.Any(), []string{name}).
Return(nil, fmt.Errorf("not found")).
Times(1),

Expand All @@ -1199,7 +1199,7 @@ func TestSdkCloneOwnership(t *testing.T) {

mv.
EXPECT().
Inspect([]string{parentid}).
Inspect(gomock.Any(), []string{parentid}).
Return([]*api.Volume{parentVol}, nil).
Times(1),

Expand Down Expand Up @@ -1243,7 +1243,7 @@ func TestSdkCloneOwnership(t *testing.T) {

mv.
EXPECT().
Inspect([]string{name}).
Inspect(gomock.Any(), []string{name}).
Return(nil, fmt.Errorf("not found")).
Times(1),

Expand All @@ -1255,7 +1255,7 @@ func TestSdkCloneOwnership(t *testing.T) {

mv.
EXPECT().
Inspect([]string{parentid}).
Inspect(gomock.Any(), []string{parentid}).
Return([]*api.Volume{parentVol}, nil).
Times(1),

Expand Down Expand Up @@ -1386,7 +1386,7 @@ func TestSdkVolumeCreateEnforced(t *testing.T) {
gomock.InOrder(
s.MockDriver().
EXPECT().
Inspect([]string{name}).
Inspect(gomock.Any(), []string{name}).
Return(nil, fmt.Errorf("not found")).
Times(1),

Expand Down Expand Up @@ -1562,7 +1562,7 @@ func TestSdkVolumeCreateDefaultPolicyOwnership(t *testing.T) {
id := "myid"
gomock.InOrder(
mv.EXPECT().
Inspect([]string{name}).
Inspect(gomock.Any(), []string{name}).
Return(nil, fmt.Errorf("not found")).
Times(1),

Expand Down Expand Up @@ -1615,7 +1615,7 @@ func TestSdkVolumeCreateDefaultPolicyOwnership(t *testing.T) {
// Create response
gomock.InOrder(
mv.EXPECT().
Inspect([]string{name}).
Inspect(gomock.Any(), []string{name}).
Return(nil, fmt.Errorf("not found")).
Times(1),

Expand Down Expand Up @@ -1763,7 +1763,7 @@ func TestSdkVolumeUpdatePolicyOwnership(t *testing.T) {
id := "myid"
gomock.InOrder(
mv.EXPECT().
Inspect([]string{name}).
Inspect(gomock.Any(), []string{name}).
Return(nil, fmt.Errorf("not found")).
Times(1),

Expand Down
2 changes: 1 addition & 1 deletion api/server/volume.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ func (vd *volAPI) annotateContext(r *http.Request) (context.Context, context.Can
return ctx, cancel, nil
}
func (vd *volAPI) getVolDriver(r *http.Request) (volume.VolumeDriver, error) {
// Check if the driver has registered by it's user agent name
// Check if the driver has registered by its user agent name
userAgent := r.Header.Get("User-Agent")
if len(userAgent) > 0 {
clientName := strings.Split(userAgent, "/")
Expand Down
Loading

0 comments on commit c8c6d55

Please sign in to comment.