2022-03-28 10:49:17 -04:00
|
|
|
package storage
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"io"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/Azure/azure-sdk-for-go/sdk/storage/azblob"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
type stubAzureContainerAPI struct {
|
|
|
|
newClientErr error
|
|
|
|
createErr error
|
|
|
|
createCalled *bool
|
|
|
|
blockBlobAPI stubAzureBlockBlobAPI
|
|
|
|
}
|
|
|
|
|
|
|
|
func newStubClientFactory(stub stubAzureContainerAPI) func(ctx context.Context, connectionString, containerName string, opts *azblob.ClientOptions) (azureContainerAPI, error) {
|
|
|
|
return func(ctx context.Context, connectionString, containerName string, opts *azblob.ClientOptions) (azureContainerAPI, error) {
|
|
|
|
return stub, stub.newClientErr
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s stubAzureContainerAPI) Create(ctx context.Context, options *azblob.CreateContainerOptions) (azblob.ContainerCreateResponse, error) {
|
|
|
|
*s.createCalled = true
|
|
|
|
return azblob.ContainerCreateResponse{}, s.createErr
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s stubAzureContainerAPI) NewBlockBlobClient(blobName string) azureBlobAPI {
|
|
|
|
return s.blockBlobAPI
|
|
|
|
}
|
|
|
|
|
|
|
|
type stubAzureBlockBlobAPI struct {
|
|
|
|
downloadBlobToWriterAtErr error
|
|
|
|
downloadBlobToWriterOutput []byte
|
|
|
|
uploadErr error
|
|
|
|
uploadData chan []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s stubAzureBlockBlobAPI) DownloadBlobToWriterAt(ctx context.Context, offset int64, count int64, writer io.WriterAt, o azblob.HighLevelDownloadFromBlobOptions) error {
|
|
|
|
if _, err := writer.WriteAt(s.downloadBlobToWriterOutput, 0); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return s.downloadBlobToWriterAtErr
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s stubAzureBlockBlobAPI) Upload(ctx context.Context, body io.ReadSeekCloser, options *azblob.UploadBlockBlobOptions) (azblob.BlockBlobUploadResponse, error) {
|
|
|
|
res, err := io.ReadAll(body)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
s.uploadData <- res
|
|
|
|
return azblob.BlockBlobUploadResponse{}, s.uploadErr
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAzureGet(t *testing.T) {
|
|
|
|
someErr := errors.New("error")
|
|
|
|
|
|
|
|
testCases := map[string]struct {
|
2022-04-26 10:54:05 -04:00
|
|
|
client stubAzureContainerAPI
|
|
|
|
unsetError bool
|
|
|
|
wantErr bool
|
2022-03-28 10:49:17 -04:00
|
|
|
}{
|
|
|
|
"success": {
|
|
|
|
client: stubAzureContainerAPI{
|
|
|
|
blockBlobAPI: stubAzureBlockBlobAPI{downloadBlobToWriterOutput: []byte("test-data")},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"creating client fails": {
|
2022-04-26 10:54:05 -04:00
|
|
|
client: stubAzureContainerAPI{newClientErr: someErr},
|
|
|
|
wantErr: true,
|
2022-03-28 10:49:17 -04:00
|
|
|
},
|
|
|
|
"DownloadBlobToBuffer fails": {
|
|
|
|
client: stubAzureContainerAPI{
|
|
|
|
blockBlobAPI: stubAzureBlockBlobAPI{downloadBlobToWriterAtErr: someErr},
|
|
|
|
},
|
2022-04-26 10:54:05 -04:00
|
|
|
wantErr: true,
|
2022-03-28 10:49:17 -04:00
|
|
|
},
|
|
|
|
"BlobNotFound error": {
|
|
|
|
client: stubAzureContainerAPI{
|
|
|
|
blockBlobAPI: stubAzureBlockBlobAPI{
|
|
|
|
downloadBlobToWriterAtErr: &azblob.StorageError{
|
|
|
|
ErrorCode: azblob.StorageErrorCodeBlobNotFound,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2022-04-26 10:54:05 -04:00
|
|
|
unsetError: true,
|
|
|
|
wantErr: true,
|
2022-03-28 10:49:17 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range testCases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
client := &AzureStorage{
|
|
|
|
newClient: newStubClientFactory(tc.client),
|
|
|
|
connectionString: "test",
|
|
|
|
containerName: "test",
|
|
|
|
opts: &AzureOpts{},
|
|
|
|
}
|
|
|
|
|
|
|
|
out, err := client.Get(context.Background(), "test-key")
|
2022-04-26 10:54:05 -04:00
|
|
|
if tc.wantErr {
|
2022-03-28 10:49:17 -04:00
|
|
|
assert.Error(err)
|
|
|
|
|
|
|
|
if tc.unsetError {
|
|
|
|
assert.ErrorIs(err, ErrDEKUnset)
|
|
|
|
} else {
|
|
|
|
assert.False(errors.Is(err, ErrDEKUnset))
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
assert.NoError(err)
|
|
|
|
assert.Equal(tc.client.blockBlobAPI.downloadBlobToWriterOutput, out)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAzurePut(t *testing.T) {
|
|
|
|
someErr := errors.New("error")
|
|
|
|
|
|
|
|
testCases := map[string]struct {
|
2022-04-26 10:54:05 -04:00
|
|
|
client stubAzureContainerAPI
|
|
|
|
wantErr bool
|
2022-03-28 10:49:17 -04:00
|
|
|
}{
|
|
|
|
"success": {
|
|
|
|
client: stubAzureContainerAPI{},
|
|
|
|
},
|
|
|
|
"creating client fails": {
|
2022-04-26 10:54:05 -04:00
|
|
|
client: stubAzureContainerAPI{newClientErr: someErr},
|
|
|
|
wantErr: true,
|
2022-03-28 10:49:17 -04:00
|
|
|
},
|
|
|
|
"Upload fails": {
|
|
|
|
client: stubAzureContainerAPI{
|
|
|
|
blockBlobAPI: stubAzureBlockBlobAPI{uploadErr: someErr},
|
|
|
|
},
|
2022-04-26 10:54:05 -04:00
|
|
|
wantErr: true,
|
2022-03-28 10:49:17 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range testCases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
testData := []byte{0x1, 0x2, 0x3}
|
|
|
|
tc.client.blockBlobAPI.uploadData = make(chan []byte, len(testData))
|
|
|
|
|
|
|
|
client := &AzureStorage{
|
|
|
|
newClient: newStubClientFactory(tc.client),
|
|
|
|
connectionString: "test",
|
|
|
|
containerName: "test",
|
|
|
|
opts: &AzureOpts{},
|
|
|
|
}
|
|
|
|
|
|
|
|
err := client.Put(context.Background(), "test-key", testData)
|
2022-04-26 10:54:05 -04:00
|
|
|
if tc.wantErr {
|
2022-03-28 10:49:17 -04:00
|
|
|
assert.Error(err)
|
|
|
|
} else {
|
|
|
|
assert.NoError(err)
|
|
|
|
assert.Equal(testData, <-tc.client.blockBlobAPI.uploadData)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCreateContainerOrContinue(t *testing.T) {
|
|
|
|
someErr := errors.New("error")
|
|
|
|
testCases := map[string]struct {
|
2022-04-26 10:54:05 -04:00
|
|
|
client stubAzureContainerAPI
|
|
|
|
wantErr bool
|
2022-03-28 10:49:17 -04:00
|
|
|
}{
|
|
|
|
"success": {
|
|
|
|
client: stubAzureContainerAPI{},
|
|
|
|
},
|
|
|
|
"container already exists": {
|
|
|
|
client: stubAzureContainerAPI{createErr: &azblob.StorageError{ErrorCode: azblob.StorageErrorCodeContainerAlreadyExists}},
|
|
|
|
},
|
|
|
|
"creating client fails": {
|
2022-04-26 10:54:05 -04:00
|
|
|
client: stubAzureContainerAPI{newClientErr: someErr},
|
|
|
|
wantErr: true,
|
2022-03-28 10:49:17 -04:00
|
|
|
},
|
|
|
|
"Create fails": {
|
2022-04-26 10:54:05 -04:00
|
|
|
client: stubAzureContainerAPI{createErr: someErr},
|
|
|
|
wantErr: true,
|
2022-03-28 10:49:17 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range testCases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
tc.client.createCalled = new(bool)
|
|
|
|
client := &AzureStorage{
|
|
|
|
newClient: newStubClientFactory(tc.client),
|
|
|
|
connectionString: "test",
|
|
|
|
containerName: "test",
|
|
|
|
opts: &AzureOpts{},
|
|
|
|
}
|
|
|
|
|
|
|
|
err := client.createContainerOrContinue(context.Background())
|
2022-04-26 10:54:05 -04:00
|
|
|
if tc.wantErr {
|
2022-03-28 10:49:17 -04:00
|
|
|
assert.Error(err)
|
|
|
|
} else {
|
|
|
|
assert.NoError(err)
|
|
|
|
assert.True(*tc.client.createCalled)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|