Skip to content

Commit

Permalink
test: add mock client in UT for azurefile_dataplane_client.go
Browse files Browse the repository at this point in the history
  • Loading branch information
ScottZhuMS committed Jan 24, 2025
1 parent 84def57 commit 0f1f9c2
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 23 deletions.
29 changes: 24 additions & 5 deletions pkg/azurefile/azurefile_dataplane_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,28 @@ var (
type azureFileDataplaneClient struct {
accountName string
accountKey string
Client serviceClientInterface
}

type serviceClientInterface interface {
NewShareClient(shareName string) shareClientInterface
}

type serviceClient_impl struct {
*service.Client
}

func (m *serviceClient_impl) NewShareClient(shareName string) shareClientInterface {
return m.NewShareClient(shareName)
}

type shareClientInterface interface {
Create(ctx context.Context, options *share.CreateOptions) (share.CreateResponse, error)
Delete(ctx context.Context, options *share.DeleteOptions) (share.DeleteResponse, error)
GetProperties(ctx context.Context, options *share.GetPropertiesOptions) (share.GetPropertiesResponse, error)
SetProperties(ctx context.Context, options *share.SetPropertiesOptions) (share.SetPropertiesResponse, error)
}

func newAzureFileClient(accountName, accountKey, storageEndpointSuffix string) (azureFileClient, error) {
if storageEndpointSuffix == "" {
storageEndpointSuffix = defaultStorageEndPointSuffix
Expand All @@ -68,15 +87,15 @@ func newAzureFileClient(accountName, accountKey, storageEndpointSuffix string) (
return &azureFileDataplaneClient{
accountName: accountName,
accountKey: accountKey,
Client: fileClient,
Client: &serviceClient_impl{fileClient},
}, nil
}

func (f *azureFileDataplaneClient) CreateFileShare(ctx context.Context, shareOptions *ShareOptions) error {
if shareOptions == nil {
return fmt.Errorf("shareOptions of account(%s) is nil", f.accountName)
}
shareClient := f.Client.NewShareClient(shareOptions.Name)
shareClient := newShareClient(f, shareOptions.Name)
_, err := shareClient.Create(ctx, &share.CreateOptions{
Quota: to.Ptr(int32(shareOptions.RequestGiB)),
})
Expand All @@ -89,12 +108,12 @@ func (f *azureFileDataplaneClient) CreateFileShare(ctx context.Context, shareOpt

// delete a file share
func (f *azureFileDataplaneClient) DeleteFileShare(ctx context.Context, shareName string) error {
_, err := f.Client.NewShareClient(shareName).Delete(ctx, nil)
_, err := newShareClient(f, shareName).Delete(ctx, nil)
return err
}

func (f *azureFileDataplaneClient) ResizeFileShare(ctx context.Context, shareName string, sizeGiB int) error {
shareClient := f.Client.NewShareClient(shareName)
shareClient := newShareClient(f, shareName)
shareProps, err := shareClient.GetProperties(ctx, nil)
if err != nil {
return fmt.Errorf("failed to set quota on file share %s, err: %v", shareName, err)
Expand All @@ -114,7 +133,7 @@ func (f *azureFileDataplaneClient) ResizeFileShare(ctx context.Context, shareNam
}

func (f *azureFileDataplaneClient) GetFileShareQuota(ctx context.Context, name string) (int, error) {
shareClient := f.Client.NewShareClient(name)
shareClient := newShareClient(f, name)
shareProps, err := shareClient.GetProperties(ctx, nil)
if err != nil {
return -1, err
Expand Down
78 changes: 60 additions & 18 deletions pkg/azurefile/azurefile_dataplane_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,50 @@ import (
"context"
"fmt"
"reflect"
"strings"
"testing"

"github.com/Azure/azure-sdk-for-go/sdk/azcore/to"
"github.com/Azure/azure-sdk-for-go/sdk/storage/azfile/share"
)

// mockShareClient implements the necessary methods of share.Client.
type mockShareClient struct {
// Embed any necessary fields
}

// GetProperties returns dummy share properties.
func (m *mockShareClient) GetProperties(_ context.Context, _ *share.GetPropertiesOptions) (share.GetPropertiesResponse, error) {
return share.GetPropertiesResponse{
Quota: to.Ptr(int32(10)),
}, nil
}

func (m *mockShareClient) Create(_ context.Context, _ *share.CreateOptions) (share.CreateResponse, error) {
return share.CreateResponse{}, nil
}

func (m *mockShareClient) Delete(_ context.Context, _ *share.DeleteOptions) (share.DeleteResponse, error) {
return share.DeleteResponse{}, nil
}

func (m *mockShareClient) SetProperties(_ context.Context, _ *share.SetPropertiesOptions) (share.SetPropertiesResponse, error) {
return share.SetPropertiesResponse{}, nil
}

func init() {
newShareClient = func(_ *azureFileDataplaneClient, _ string) ShareClientInterface {
return &mockShareClient{}
}
}

func TestCreateFileShare(t *testing.T) {

testCases := []struct {
name string
testFunc func(t *testing.T)
}{
{
name: "",
name: "success on create",
testFunc: func(t *testing.T) {
options := &ShareOptions{
Name: "devstoreaccount1",
Expand All @@ -42,9 +74,8 @@ func TestCreateFileShare(t *testing.T) {
t.Errorf("error creating azure client: %v", err)
}
actualErr := f.CreateFileShare(context.Background(), options)
expectedErr := fmt.Errorf("failed to create file share, err: ")
if !strings.HasPrefix(actualErr.Error(), expectedErr.Error()) {
t.Errorf("actualErr: (%v), expectedErr: (%v)", actualErr, expectedErr)
if actualErr != nil {
t.Errorf("actualErr: (%v), expectedErr: nil", actualErr)
}
},
},
Expand All @@ -70,17 +101,16 @@ func TestDeleteFileShare(t *testing.T) {
testFunc func(t *testing.T)
}{
{
name: "expect error on delete",
name: "success on delete",
testFunc: func(t *testing.T) {
f, err := newAzureFileClient("test", "dW5pdHRlc3Q=", "ut")
if err != nil {
t.Errorf("error creating azure client: %v", err)
}
shareName := "nonexistent"
actualErr := f.DeleteFileShare(context.Background(), shareName)
expectedErr := fmt.Errorf("Delete \"https://test.file.ut/%s?restype=share\"", shareName)
if actualErr == nil || !strings.HasPrefix(actualErr.Error(), expectedErr.Error()) {
t.Errorf("actualErr: (%v), expectedErr: (%v)", actualErr, expectedErr)
if actualErr != nil {
t.Errorf("actualErr: (%v), expectedErr: nil", actualErr)
}
},
},
Expand All @@ -96,17 +126,30 @@ func TestResizeFileShare(t *testing.T) {
testFunc func(t *testing.T)
}{
{
name: "expect error on resize",
name: "success (already greater) on resize",
testFunc: func(t *testing.T) {
f, err := newAzureFileClient("test", "dW5pdHRlc3Q=", "ut")
if err != nil {
t.Errorf("error creating azure client: %v", err)
}
shareName := "nonexistent"
actualErr := f.ResizeFileShare(context.Background(), shareName, 5)
if actualErr != nil {
t.Errorf("actualErr: (%v), expectedErr: nil", actualErr)
}
},
},
{
name: "success on resize",
testFunc: func(t *testing.T) {
f, err := newAzureFileClient("test", "dW5pdHRlc3Q=", "ut")
if err != nil {
t.Errorf("error creating azure client: %v", err)
}
shareName := "nonexistent"
actualErr := f.ResizeFileShare(context.Background(), shareName, 20)
expectedErr := fmt.Errorf("failed to set quota on file share %s", shareName)
if actualErr == nil || !strings.HasPrefix(actualErr.Error(), expectedErr.Error()) {
t.Errorf("actualErr: (%v), expectedErr: (%v)", actualErr, expectedErr)
if actualErr != nil {
t.Errorf("actualErr: (%v), expectedErr: nil", actualErr)
}
},
},
Expand All @@ -122,18 +165,17 @@ func TestGetFileShareQuotaDataPlane(t *testing.T) {
testFunc func(t *testing.T)
}{
{
name: "expect error on resize",
name: "success on get quota",
testFunc: func(t *testing.T) {
f, err := newAzureFileClient("test", "dW5pdHRlc3Q=", "ut")
if err != nil {
t.Errorf("error creating azure client: %v", err)
}
shareName := "nonexistent"
actualQuota, actualErr := f.GetFileShareQuota(context.Background(), shareName)
expectedErr := fmt.Errorf("Get \"https://test.file.ut/%s?restype=share\"", shareName)
expectedQuota := -1
if actualErr == nil || !strings.HasPrefix(actualErr.Error(), expectedErr.Error()) || actualQuota != -1 {
t.Errorf("actualErr: (%v), expectedErr: (%v), actualQuota: (%v), expectedQuota: (%v)", actualErr, expectedErr, actualQuota, expectedQuota)
expectedQuota := 10
if actualErr != nil || actualQuota != expectedQuota {
t.Errorf("actualErr: (%v), expectedErr: (nil), actualQuota: (%v), expectedQuota: (%v)", actualErr, actualQuota, expectedQuota)
}
},
},
Expand Down

0 comments on commit 0f1f9c2

Please sign in to comment.