Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit c1e1379

Browse files
committedJul 13, 2022
Create a mock blob client for controllerserver_test.go
added testcases for CreateBlobContainer and DeleteBlobContainer
1 parent ed854d8 commit c1e1379

File tree

1 file changed

+96
-1
lines changed

1 file changed

+96
-1
lines changed
 

Diff for: ‎pkg/blob/controllerserver_test.go

+96-1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,57 @@ import (
3636
"sigs.k8s.io/cloud-provider-azure/pkg/retry"
3737
)
3838

39+
type errType int32
40+
41+
const (
42+
DATAPLANE errType = iota
43+
MANAGEMENT
44+
CUSTOM
45+
NULL
46+
)
47+
48+
//mock blobclient that returns the errortype for create/delete/get operations (default value nil)
49+
type mockBlobClient struct {
50+
//type of error returned
51+
errorType errType
52+
//custom string for error type CUSTOM
53+
custom string
54+
}
55+
56+
func (c *mockBlobClient) CreateContainer(ctx context.Context, resourceGroupName, accountName, containerName string, blobContainer storage.BlobContainer) error {
57+
switch c.errorType {
58+
case DATAPLANE:
59+
return fmt.Errorf(containerBeingDeletedDataplaneAPIError)
60+
case MANAGEMENT:
61+
return fmt.Errorf(containerBeingDeletedManagementAPIError)
62+
case CUSTOM:
63+
return fmt.Errorf(c.custom)
64+
}
65+
return nil
66+
}
67+
func (c *mockBlobClient) DeleteContainer(ctx context.Context, resourceGroupName, accountName, containerName string) error {
68+
switch c.errorType {
69+
case DATAPLANE:
70+
return fmt.Errorf(containerBeingDeletedDataplaneAPIError)
71+
case MANAGEMENT:
72+
return fmt.Errorf(containerBeingDeletedManagementAPIError)
73+
case CUSTOM:
74+
return fmt.Errorf(c.custom)
75+
}
76+
return nil
77+
}
78+
func (c *mockBlobClient) GetContainer(ctx context.Context, resourceGroupName, accountName, containerName string) (storage.BlobContainer, error) {
79+
switch c.errorType {
80+
case DATAPLANE:
81+
return storage.BlobContainer{}, fmt.Errorf(containerBeingDeletedDataplaneAPIError)
82+
case MANAGEMENT:
83+
return storage.BlobContainer{}, fmt.Errorf(containerBeingDeletedManagementAPIError)
84+
case CUSTOM:
85+
return storage.BlobContainer{}, fmt.Errorf(c.custom)
86+
}
87+
return storage.BlobContainer{}, nil
88+
}
89+
3990
func TestControllerGetCapabilities(t *testing.T) {
4091
d := NewFakeDriver()
4192
controlCap := []*csi.ControllerServiceCapability{
@@ -783,16 +834,19 @@ func TestControllerExpandVolume(t *testing.T) {
783834
}
784835
}
785836

837+
//management error returning timed out error instead of nil, but working properly for deleteblobcontainer
786838
func TestCreateBlobContainer(t *testing.T) {
787839
tests := []struct {
788840
desc string
789841
rg string
790842
accountName string
791843
containerName string
792844
secrets map[string]string
845+
clientErr errType
793846
expectedErr error
794847
}{
795848
{
849+
clientErr: NULL,
796850
expectedErr: fmt.Errorf("containerName is empty"),
797851
},
798852
{
@@ -801,15 +855,34 @@ func TestCreateBlobContainer(t *testing.T) {
801855
defaultSecretAccountName: "accountname",
802856
defaultSecretAccountKey: "key",
803857
},
858+
clientErr: NULL,
804859
expectedErr: fmt.Errorf("azure: base storage service url required"),
805860
},
861+
{
862+
containerName: "Secrets is Empty",
863+
secrets: map[string]string{},
864+
clientErr: NULL,
865+
expectedErr: nil,
866+
},
867+
{
868+
containerName: "Dataplane API Error",
869+
secrets: map[string]string{},
870+
clientErr: DATAPLANE,
871+
expectedErr: nil,
872+
},
873+
/*{
874+
containerName: "Management API Error",
875+
secrets: map[string]string{},
876+
clientErr: MANAGEMENT,
877+
expectedErr: nil,
878+
},*/
806879
}
807880

808881
d := NewFakeDriver()
809882
d.cloud = &azure.Cloud{}
810-
811883
for _, test := range tests {
812884
err := d.CreateBlobContainer(context.Background(), test.rg, test.accountName, test.containerName, test.secrets)
885+
d.cloud.BlobClient = &mockBlobClient{errorType: test.clientErr}
813886
if !reflect.DeepEqual(err, test.expectedErr) {
814887
t.Errorf("test(%s), actualErr: (%v), expectedErr: (%v)", test.desc, err, test.expectedErr)
815888
}
@@ -823,9 +896,11 @@ func TestDeleteBlobContainer(t *testing.T) {
823896
accountName string
824897
containerName string
825898
secrets map[string]string
899+
clientErr errType
826900
expectedErr error
827901
}{
828902
{
903+
clientErr: NULL,
829904
expectedErr: fmt.Errorf("containerName is empty"),
830905
},
831906
{
@@ -834,15 +909,35 @@ func TestDeleteBlobContainer(t *testing.T) {
834909
defaultSecretAccountName: "accountname",
835910
defaultSecretAccountKey: "key",
836911
},
912+
clientErr: NULL,
837913
expectedErr: fmt.Errorf("azure: base storage service url required"),
838914
},
915+
{
916+
containerName: "Secrets is Empty",
917+
secrets: map[string]string{},
918+
clientErr: NULL,
919+
expectedErr: nil,
920+
},
921+
{
922+
containerName: "Dataplane API Error",
923+
secrets: map[string]string{},
924+
clientErr: DATAPLANE,
925+
expectedErr: nil,
926+
},
927+
{
928+
containerName: "Management API Error",
929+
secrets: map[string]string{},
930+
clientErr: MANAGEMENT,
931+
expectedErr: nil,
932+
},
839933
}
840934

841935
d := NewFakeDriver()
842936
d.cloud = &azure.Cloud{}
843937

844938
for _, test := range tests {
845939
err := d.DeleteBlobContainer(context.Background(), test.rg, test.accountName, test.containerName, test.secrets)
940+
d.cloud.BlobClient = &mockBlobClient{errorType: test.clientErr}
846941
if !reflect.DeepEqual(err, test.expectedErr) {
847942
t.Errorf("test(%s), actualErr: (%v), expectedErr: (%v)", test.desc, err, test.expectedErr)
848943
}

0 commit comments

Comments
 (0)
Please sign in to comment.