-
Notifications
You must be signed in to change notification settings - Fork 1
/
azure_test.go
171 lines (148 loc) · 4.42 KB
/
azure_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package stores
import (
"context"
"fmt"
"io"
"testing"
"time"
"github.com/Azure/azure-sdk-for-go/sdk/storage/azblob"
"github.com/Azure/azure-sdk-for-go/sdk/storage/azblob/blob"
"github.com/discentem/cavorite/testutils"
"github.com/spf13/afero"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
type aferoAzureBlobServer struct {
containers map[string]afero.Fs
}
var (
// ensure test server meets interface
_ = azureBlobishClient(aferoAzureBlobServer{})
)
func (s aferoAzureBlobServer) UploadStream(ctx context.Context, containerName string, blobName string, body io.Reader, o *azblob.UploadStreamOptions) (azblob.UploadStreamResponse, error) {
// check if the containerName exists in s.containers
_, ok := s.containers[containerName]
if !ok {
return azblob.UploadStreamResponse{},
fmt.Errorf(
"%s does not exist in this aferoAzureBlobServer",
containerName,
)
}
b, err := io.ReadAll(body)
if err != nil {
return azblob.UploadStreamResponse{}, err
}
// create a filesystem for container referenced in input
containerfs, _ := testutils.MemMapFsWith(map[string]testutils.MapFile{
blobName: {
// write input body to bucketfs
Content: b,
},
})
// write containerfs to associated "container"
s.containers[containerName] = *containerfs
return azblob.UploadStreamResponse{}, nil
}
func (s aferoAzureBlobServer) DownloadStream(ctx context.Context, containerName string, blobName string, o *azblob.DownloadStreamOptions) (azblob.DownloadStreamResponse, error) {
_, ok := s.containers[containerName]
if !ok {
return azblob.DownloadStreamResponse{},
fmt.Errorf(
"%s does not exist in this aferoAzureBlobServer",
containerName,
)
}
objectHandle, err := s.containers[containerName].Open(blobName)
if err != nil {
return azblob.DownloadStreamResponse{},
fmt.Errorf("could not find %s in container %s: %w", blobName, containerName, err)
}
_, err = objectHandle.Seek(0, io.SeekStart)
if err != nil {
return azblob.DownloadStreamResponse{}, err
}
return azblob.DownloadStreamResponse{
DownloadResponse: blob.DownloadResponse{
Body: io.NopCloser(objectHandle),
},
}, nil
}
func TestAzureBlobStoreUpload(t *testing.T) {
mTime, _ := time.Parse("2006-01-02T15:04:05.000Z", "2014-11-12T11:45:26.371Z")
memfs, err := testutils.MemMapFsWith(map[string]testutils.MapFile{
"test": {
Content: []byte(`tree`),
ModTime: &mTime,
},
})
assert.NoError(t, err)
fakeAzureBlobServer := aferoAzureBlobServer{
containers: map[string]afero.Fs{
// create a bucket in our fake azure blob server
"test": afero.NewMemMapFs(),
},
}
store := AzureBlobStore{
Options: Options{
BackendAddress: "http://whatever/test",
MetadataFileExtension: "cfile",
},
fsys: *memfs,
containerClient: fakeAzureBlobServer,
}
err = store.Upload(context.Background(), "test")
require.NoError(t, err)
buck := fakeAzureBlobServer.containers["test"]
f, err := buck.Open("test")
assert.NoError(t, err)
fstat, err := f.Stat()
assert.NoError(t, err)
b := make([]byte, fstat.Size())
_, err = f.Read(b)
assert.NoError(t, err)
assert.Equal(t, []byte(`tree`), b)
}
func TestAzureBlobStoreRetrieve(t *testing.T) {
mTime, _ := time.Parse("2006-01-02T15:04:05.000Z", "2014-11-12T11:45:26.371Z")
// create bucket content
bucketfs, err := testutils.MemMapFsWith(map[string]testutils.MapFile{
"someObject": {
Content: []byte("tla"),
ModTime: &mTime,
},
})
assert.NoError(t, err)
// bfs := *bucketfs
// mfs := bfs.(*afero.MemMapFs)
fakeAzureBlobServer := aferoAzureBlobServer{
containers: map[string]afero.Fs{
// create a bucket in our fake azure blob server
"aFakeBucket": *bucketfs,
},
}
localFs, err := testutils.MemMapFsWith(map[string]testutils.MapFile{
"someObject.cfile": {
Content: []byte(`{
"name": "someObject",
"checksum": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
"date_modified": "2014-11-12T11:45:26.371Z"
}`),
ModTime: &mTime,
},
})
assert.NoError(t, err)
store := AzureBlobStore{
Options: Options{
BackendAddress: "http://whatever/aFakeBucket",
MetadataFileExtension: "cfile",
},
fsys: *localFs,
containerClient: fakeAzureBlobServer,
}
err = store.Retrieve(context.Background(), "someObject.cfile")
assert.NoError(t, err)
// ensure the content of the file is correct
b, _ := afero.ReadFile(*localFs, "someObject")
assert.Equal(t, `tla`, string(b))
}