Skip to content

Commit

Permalink
change IsEnableBlobVersioning from bool to *bool
Browse files Browse the repository at this point in the history
  • Loading branch information
cvvz committed Jan 13, 2023
1 parent 8aa7c2d commit 1acc7f0
Show file tree
Hide file tree
Showing 2 changed files with 228 additions and 10 deletions.
8 changes: 4 additions & 4 deletions pkg/provider/azure_storageaccount.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ type AccountOptions struct {
SubnetName string
AccessTier string
MatchTags bool
EnableBlobVersioning bool
EnableBlobVersioning *bool
SoftDeleteBlobs int32
SoftDeleteContainers int32
}
Expand Down Expand Up @@ -369,7 +369,7 @@ func (az *Cloud) EnsureStorageAccount(ctx context.Context, accountOptions *Accou
return "", "", fmt.Errorf("failed to create storage account %s, error: %v", accountName, rerr)
}

if accountOptions.EnableBlobVersioning || accountOptions.SoftDeleteBlobs > 0 || accountOptions.SoftDeleteContainers > 0 {
if pointer.BoolDeref(accountOptions.EnableBlobVersioning, false) || accountOptions.SoftDeleteBlobs > 0 || accountOptions.SoftDeleteContainers > 0 {
var blobPolicy, containerPolicy *storage.DeleteRetentionPolicy
if accountOptions.SoftDeleteContainers > 0 {
containerPolicy = &storage.DeleteRetentionPolicy{
Expand All @@ -386,7 +386,7 @@ func (az *Cloud) EnsureStorageAccount(ctx context.Context, accountOptions *Accou

property := storage.BlobServiceProperties{
BlobServicePropertiesProperties: &storage.BlobServicePropertiesProperties{
IsVersioningEnabled: pointer.Bool(accountOptions.EnableBlobVersioning),
IsVersioningEnabled: accountOptions.EnableBlobVersioning,
ContainerDeleteRetentionPolicy: containerPolicy,
DeleteRetentionPolicy: blobPolicy,
},
Expand Down Expand Up @@ -805,5 +805,5 @@ func isSoftDeleteContainersEqual(property storage.BlobServiceProperties, account
}

func isEnableBlobVersioningEqual(property storage.BlobServiceProperties, accountOptions *AccountOptions) bool {
return accountOptions.EnableBlobVersioning == pointer.BoolDeref(property.IsVersioningEnabled, false)
return pointer.BoolDeref(accountOptions.EnableBlobVersioning, false) == pointer.BoolDeref(property.IsVersioningEnabled, false)
}
230 changes: 224 additions & 6 deletions pkg/provider/azure_storageaccount_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import (
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/assert"
"k8s.io/utils/pointer"

"sigs.k8s.io/cloud-provider-azure/pkg/azureclients/blobclient/mockblobclient"
"sigs.k8s.io/cloud-provider-azure/pkg/azureclients/fileclient/mockfileclient"
"sigs.k8s.io/cloud-provider-azure/pkg/azureclients/privatednsclient/mockprivatednsclient"
"sigs.k8s.io/cloud-provider-azure/pkg/azureclients/privatednszonegroupclient/mockprivatednszonegroupclient"
Expand Down Expand Up @@ -143,9 +143,13 @@ func TestGetStorageAccount(t *testing.T) {
}

mockStorageAccountsClient := mockstorageaccountclient.NewMockInterface(ctrl)
mockBlobClient := mockblobclient.NewMockInterface(ctrl)
cloud.StorageAccountClient = mockStorageAccountsClient
cloud.BlobClient = mockBlobClient

mockStorageAccountsClient.EXPECT().ListByResourceGroup(gomock.Any(), "", "rg").Return(testResourceGroups, nil).Times(1)
mockBlobClient.EXPECT().GetServiceProperties(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(storage.BlobServiceProperties{
BlobServicePropertiesProperties: &storage.BlobServicePropertiesProperties{}}, nil).Times(1)

accountsWithLocations, err := cloud.getStorageAccounts(ctx, accountOptions)
if err != nil {
Expand Down Expand Up @@ -447,6 +451,12 @@ func TestEnsureStorageAccount(t *testing.T) {
}

for _, test := range tests {
mockBlobClient := mockblobclient.NewMockInterface(ctrl)
cloud.BlobClient = mockBlobClient
mockBlobClient.EXPECT().GetServiceProperties(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(storage.BlobServiceProperties{
BlobServicePropertiesProperties: &storage.BlobServicePropertiesProperties{}}, nil).AnyTimes()
mockBlobClient.EXPECT().SetServiceProperties(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(storage.BlobServiceProperties{}, nil).AnyTimes()

mockStorageAccountsClient := mockstorageaccountclient.NewMockInterface(ctrl)
if test.mockStorageAccountsClient {
cloud.StorageAccountClient = mockStorageAccountsClient
Expand Down Expand Up @@ -502,6 +512,9 @@ func TestEnsureStorageAccount(t *testing.T) {
SubscriptionID: test.subscriptionID,
AccessTier: test.accessTier,
StorageType: test.storageType,
EnableBlobVersioning: pointer.BoolPtr(true),
SoftDeleteBlobs: 7,
SoftDeleteContainers: 7,
}
}

Expand Down Expand Up @@ -677,7 +690,7 @@ func TestIsHnsPropertyEqual(t *testing.T) {
},
},
accountOptions: &AccountOptions{},
expectedResult: true,
expectedResult: false,
},
{
account: storage.Account{
Expand Down Expand Up @@ -740,7 +753,7 @@ func TestIsEnableNfsV3PropertyEqual(t *testing.T) {
},
},
accountOptions: &AccountOptions{},
expectedResult: true,
expectedResult: false,
},
{
account: storage.Account{
Expand Down Expand Up @@ -803,7 +816,7 @@ func TestIsAllowBlobPublicAccessEqual(t *testing.T) {
},
},
accountOptions: &AccountOptions{},
expectedResult: true,
expectedResult: false,
},
{
account: storage.Account{
Expand Down Expand Up @@ -866,7 +879,7 @@ func TestIsAllowSharedKeyAccessEqual(t *testing.T) {
},
},
accountOptions: &AccountOptions{},
expectedResult: true,
expectedResult: false,
},
{
account: storage.Account{
Expand Down Expand Up @@ -931,7 +944,7 @@ func TestIsRequireInfrastructureEncryptionEqual(t *testing.T) {
},
},
accountOptions: &AccountOptions{},
expectedResult: true,
expectedResult: false,
},
{
account: storage.Account{
Expand Down Expand Up @@ -1431,3 +1444,208 @@ func TestIsDisableFileServiceDeleteRetentionPolicyEqual(t *testing.T) {
assert.Equal(t, test.expectedResult, result, test.desc)
}
}

func Test_isSoftDeleteBlobsEqual(t *testing.T) {
type args struct {
property storage.BlobServiceProperties
accountOptions *AccountOptions
}
tests := []struct {
name string
args args
want bool
}{
{
name: "not equal for property nil",
args: args{
property: storage.BlobServiceProperties{
BlobServicePropertiesProperties: &storage.BlobServicePropertiesProperties{},
},
accountOptions: &AccountOptions{
SoftDeleteBlobs: 7,
},
},
want: false,
},
{
name: "not equal for property not enable",
args: args{
property: storage.BlobServiceProperties{
BlobServicePropertiesProperties: &storage.BlobServicePropertiesProperties{
DeleteRetentionPolicy: &storage.DeleteRetentionPolicy{
Enabled: pointer.Bool(false),
},
},
},
accountOptions: &AccountOptions{
SoftDeleteBlobs: 7,
},
},
want: false,
},
{
name: "not equal for accountOptions nil",
args: args{
property: storage.BlobServiceProperties{
BlobServicePropertiesProperties: &storage.BlobServicePropertiesProperties{
DeleteRetentionPolicy: &storage.DeleteRetentionPolicy{
Enabled: pointer.Bool(true),
Days: pointer.Int32(7),
},
},
},
accountOptions: &AccountOptions{},
},
want: false,
},
{
name: "qual",
args: args{
property: storage.BlobServiceProperties{
BlobServicePropertiesProperties: &storage.BlobServicePropertiesProperties{
DeleteRetentionPolicy: &storage.DeleteRetentionPolicy{
Enabled: pointer.Bool(true),
Days: pointer.Int32(7),
},
},
},
accountOptions: &AccountOptions{
SoftDeleteBlobs: 7,
},
},
want: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := isSoftDeleteBlobsEqual(tt.args.property, tt.args.accountOptions); got != tt.want {
t.Errorf("isSoftDeleteBlobsEqual() = %v, want %v", got, tt.want)
}
})
}
}

func Test_isSoftDeleteContainersEqual(t *testing.T) {
type args struct {
property storage.BlobServiceProperties
accountOptions *AccountOptions
}
tests := []struct {
name string
args args
want bool
}{
{
name: "not equal for property nil",
args: args{
property: storage.BlobServiceProperties{
BlobServicePropertiesProperties: &storage.BlobServicePropertiesProperties{},
},
accountOptions: &AccountOptions{
SoftDeleteContainers: 7,
},
},
want: false,
},
{
name: "not equal for property not enable",
args: args{
property: storage.BlobServiceProperties{
BlobServicePropertiesProperties: &storage.BlobServicePropertiesProperties{
ContainerDeleteRetentionPolicy: &storage.DeleteRetentionPolicy{
Enabled: pointer.Bool(false),
},
},
},
accountOptions: &AccountOptions{
SoftDeleteContainers: 7,
},
},
want: false,
},
{
name: "not equal for accountOptions nil",
args: args{
property: storage.BlobServiceProperties{
BlobServicePropertiesProperties: &storage.BlobServicePropertiesProperties{
ContainerDeleteRetentionPolicy: &storage.DeleteRetentionPolicy{
Enabled: pointer.Bool(true),
Days: pointer.Int32(7),
},
},
},
accountOptions: &AccountOptions{},
},
want: false,
},
{
name: "qual",
args: args{
property: storage.BlobServiceProperties{
BlobServicePropertiesProperties: &storage.BlobServicePropertiesProperties{
ContainerDeleteRetentionPolicy: &storage.DeleteRetentionPolicy{
Enabled: pointer.Bool(true),
Days: pointer.Int32(7),
},
},
},
accountOptions: &AccountOptions{
SoftDeleteContainers: 7,
},
},
want: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := isSoftDeleteContainersEqual(tt.args.property, tt.args.accountOptions); got != tt.want {
t.Errorf("isSoftDeleteContainersEqual() = %v, want %v", got, tt.want)
}
})
}
}

func Test_isEnableBlobVersioningEqual(t *testing.T) {
type args struct {
property storage.BlobServiceProperties
accountOptions *AccountOptions
}
tests := []struct {
name string
args args
want bool
}{
{
name: "equal",
args: args{
property: storage.BlobServiceProperties{
BlobServicePropertiesProperties: &storage.BlobServicePropertiesProperties{},
},
accountOptions: &AccountOptions{
EnableBlobVersioning: pointer.Bool(false),
},
},
want: true,
},
{
name: "not equal",
args: args{
property: storage.BlobServiceProperties{
BlobServicePropertiesProperties: &storage.BlobServicePropertiesProperties{
IsVersioningEnabled: pointer.Bool(true),
},
},
accountOptions: &AccountOptions{
EnableBlobVersioning: pointer.Bool(false),
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := isEnableBlobVersioningEqual(tt.args.property, tt.args.accountOptions); got != tt.want {
t.Errorf("isEnableBlobVersioningEqual() = %v, want %v", got, tt.want)
}
})
}
}

0 comments on commit 1acc7f0

Please sign in to comment.