2022-09-05 03:06:08 -04:00
|
|
|
/*
|
|
|
|
Copyright (c) Edgeless Systems GmbH
|
|
|
|
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2022-03-28 10:49:17 -04:00
|
|
|
package storage
|
|
|
|
|
|
|
|
import (
|
2022-10-19 02:57:09 -04:00
|
|
|
"bytes"
|
2022-03-28 10:49:17 -04:00
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"io"
|
|
|
|
"testing"
|
|
|
|
|
2022-10-19 02:57:09 -04:00
|
|
|
"github.com/Azure/azure-sdk-for-go/sdk/azcore"
|
2022-03-28 10:49:17 -04:00
|
|
|
"github.com/Azure/azure-sdk-for-go/sdk/storage/azblob"
|
2022-10-19 02:57:09 -04:00
|
|
|
"github.com/Azure/azure-sdk-for-go/sdk/storage/azblob/blob"
|
|
|
|
"github.com/Azure/azure-sdk-for-go/sdk/storage/azblob/bloberror"
|
|
|
|
"github.com/Azure/azure-sdk-for-go/sdk/storage/azblob/container"
|
2022-03-28 10:49:17 -04:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestAzureGet(t *testing.T) {
|
|
|
|
testCases := map[string]struct {
|
2022-10-19 02:57:09 -04:00
|
|
|
client stubAzureBlobAPI
|
2022-04-26 10:54:05 -04:00
|
|
|
unsetError bool
|
|
|
|
wantErr bool
|
2022-03-28 10:49:17 -04:00
|
|
|
}{
|
|
|
|
"success": {
|
2022-10-19 02:57:09 -04:00
|
|
|
client: stubAzureBlobAPI{downloadData: []byte{0x1, 0x2, 0x3}},
|
2022-03-28 10:49:17 -04:00
|
|
|
},
|
2022-10-19 02:57:09 -04:00
|
|
|
"DownloadBuffer fails": {
|
|
|
|
client: stubAzureBlobAPI{downloadErr: errors.New("failed")},
|
2022-04-26 10:54:05 -04:00
|
|
|
wantErr: true,
|
2022-03-28 10:49:17 -04:00
|
|
|
},
|
|
|
|
"BlobNotFound error": {
|
2022-10-19 02:57:09 -04:00
|
|
|
client: stubAzureBlobAPI{downloadErr: &azcore.ResponseError{ErrorCode: string(bloberror.BlobNotFound)}},
|
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{
|
2022-10-19 02:57:09 -04:00
|
|
|
client: &tc.client,
|
2022-03-28 10:49:17 -04:00
|
|
|
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))
|
|
|
|
}
|
2022-10-19 02:57:09 -04:00
|
|
|
return
|
2022-03-28 10:49:17 -04:00
|
|
|
}
|
2022-10-19 02:57:09 -04:00
|
|
|
assert.NoError(err)
|
|
|
|
assert.Equal(tc.client.downloadData, out)
|
2022-03-28 10:49:17 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAzurePut(t *testing.T) {
|
|
|
|
testCases := map[string]struct {
|
2022-10-19 02:57:09 -04:00
|
|
|
client stubAzureBlobAPI
|
2022-04-26 10:54:05 -04:00
|
|
|
wantErr bool
|
2022-03-28 10:49:17 -04:00
|
|
|
}{
|
|
|
|
"success": {
|
2022-10-19 02:57:09 -04:00
|
|
|
client: stubAzureBlobAPI{},
|
2022-03-28 10:49:17 -04:00
|
|
|
},
|
|
|
|
"Upload fails": {
|
2022-10-19 02:57:09 -04:00
|
|
|
client: stubAzureBlobAPI{uploadErr: errors.New("failed")},
|
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}
|
|
|
|
|
|
|
|
client := &AzureStorage{
|
2022-10-19 02:57:09 -04:00
|
|
|
client: &tc.client,
|
2022-03-28 10:49:17 -04:00
|
|
|
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)
|
2022-10-19 02:57:09 -04:00
|
|
|
return
|
2022-03-28 10:49:17 -04:00
|
|
|
}
|
2022-10-19 02:57:09 -04:00
|
|
|
assert.NoError(err)
|
|
|
|
assert.Equal(testData, tc.client.uploadData)
|
2022-03-28 10:49:17 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCreateContainerOrContinue(t *testing.T) {
|
|
|
|
testCases := map[string]struct {
|
2022-10-19 02:57:09 -04:00
|
|
|
client stubAzureBlobAPI
|
2022-04-26 10:54:05 -04:00
|
|
|
wantErr bool
|
2022-03-28 10:49:17 -04:00
|
|
|
}{
|
|
|
|
"success": {
|
2022-10-19 02:57:09 -04:00
|
|
|
client: stubAzureBlobAPI{},
|
2022-03-28 10:49:17 -04:00
|
|
|
},
|
|
|
|
"container already exists": {
|
2022-10-19 02:57:09 -04:00
|
|
|
client: stubAzureBlobAPI{createErr: &azcore.ResponseError{ErrorCode: string(bloberror.ContainerAlreadyExists)}},
|
2022-03-28 10:49:17 -04:00
|
|
|
},
|
2022-10-19 02:57:09 -04:00
|
|
|
"CreateContainer fails": {
|
|
|
|
client: stubAzureBlobAPI{createErr: errors.New("failed")},
|
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)
|
|
|
|
|
|
|
|
client := &AzureStorage{
|
2022-10-19 02:57:09 -04:00
|
|
|
client: &tc.client,
|
2022-03-28 10:49:17 -04:00
|
|
|
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)
|
2022-10-19 02:57:09 -04:00
|
|
|
assert.True(tc.client.createCalled)
|
2022-03-28 10:49:17 -04:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2022-10-19 02:57:09 -04:00
|
|
|
|
|
|
|
type stubAzureBlobAPI struct {
|
|
|
|
createErr error
|
|
|
|
createCalled bool
|
|
|
|
downloadErr error
|
|
|
|
downloadData []byte
|
|
|
|
uploadErr error
|
|
|
|
uploadData []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *stubAzureBlobAPI) CreateContainer(context.Context, string, *container.CreateOptions) (azblob.CreateContainerResponse, error) {
|
|
|
|
s.createCalled = true
|
|
|
|
return azblob.CreateContainerResponse{}, s.createErr
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *stubAzureBlobAPI) DownloadStream(context.Context, string, string, *blob.DownloadStreamOptions) (blob.DownloadStreamResponse, error) {
|
|
|
|
res := blob.DownloadStreamResponse{}
|
|
|
|
res.Body = io.NopCloser(bytes.NewReader(s.downloadData))
|
|
|
|
return res, s.downloadErr
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *stubAzureBlobAPI) UploadStream(_ context.Context, _, _ string, data io.Reader, _ *azblob.UploadStreamOptions) (azblob.UploadStreamResponse, error) {
|
|
|
|
uploadData, err := io.ReadAll(data)
|
|
|
|
if err != nil {
|
|
|
|
return azblob.UploadStreamResponse{}, err
|
|
|
|
}
|
|
|
|
s.uploadData = uploadData
|
|
|
|
return azblob.UploadStreamResponse{}, s.uploadErr
|
|
|
|
}
|