This repository was archived by the owner on Oct 14, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcopier.go
More file actions
245 lines (199 loc) · 5.24 KB
/
copier.go
File metadata and controls
245 lines (199 loc) · 5.24 KB
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
package tests
import (
"bytes"
"crypto/md5"
"errors"
"io"
"io/ioutil"
"math/rand"
"testing"
"github.com/google/uuid"
. "github.com/smartystreets/goconvey/convey"
"github.com/beyondstorage/go-storage/v4/pairs"
"github.com/beyondstorage/go-storage/v4/pkg/randbytes"
"github.com/beyondstorage/go-storage/v4/services"
"github.com/beyondstorage/go-storage/v4/types"
)
func TestCopier(t *testing.T, store types.Storager) {
Convey("Given a basic Storager", t, func() {
c, ok := store.(types.Copier)
So(ok, ShouldBeTrue)
Convey("When Copy a file", func() {
size := rand.Int63n(4 * 1024 * 1024) // Max file size is 4MB
content, _ := ioutil.ReadAll(io.LimitReader(randbytes.NewRand(), size))
src := uuid.New().String()
_, err := store.Write(src, bytes.NewReader(content), size)
if err != nil {
t.Fatal(err)
}
defer func() {
err = store.Delete(src)
if err != nil {
t.Error(err)
}
}()
dst := uuid.New().String()
err = c.Copy(src, dst)
defer func() {
err = store.Delete(dst)
if err != nil {
t.Error(err)
}
}()
Convey("The error should be nil", func() {
So(err, ShouldBeNil)
})
Convey("Read should get dst object data without error", func() {
var buf bytes.Buffer
n, err := store.Read(dst, &buf)
Convey("The error should be nil", func() {
So(err, ShouldBeNil)
})
Convey("The content should be match", func() {
So(buf, ShouldNotBeNil)
So(n, ShouldEqual, size)
So(md5.Sum(buf.Bytes()), ShouldResemble, md5.Sum(content))
})
})
})
Convey("When Copy to an existing file", func() {
srcSize := rand.Int63n(4 * 1024 * 1024) // Max file size is 4MB
content, _ := ioutil.ReadAll(io.LimitReader(randbytes.NewRand(), srcSize))
src := uuid.New().String()
_, err := store.Write(src, bytes.NewReader(content), srcSize)
if err != nil {
t.Fatal(err)
}
defer func() {
err = store.Delete(src)
if err != nil {
t.Error(err)
}
}()
dstSize := rand.Int63n(4 * 1024 * 1024) // Max file size is 4MB
r := io.LimitReader(randbytes.NewRand(), dstSize)
dst := uuid.New().String()
_, err = store.Write(dst, r, dstSize)
if err != nil {
t.Fatal(err)
}
defer func() {
err = store.Delete(dst)
if err != nil {
t.Error(err)
}
}()
err = c.Copy(src, dst)
Convey("The error should be nil", func() {
So(err, ShouldBeNil)
})
Convey("Read should get dst object data without error", func() {
var buf bytes.Buffer
n, err := store.Read(dst, &buf)
Convey("The error should be nil", func() {
So(err, ShouldBeNil)
})
Convey("The content should be match", func() {
So(buf, ShouldNotBeNil)
So(n, ShouldEqual, srcSize)
So(md5.Sum(buf.Bytes()), ShouldResemble, md5.Sum(content))
})
})
})
})
}
func TestCopierWithDir(t *testing.T, store types.Storager) {
Convey("Given a basic Storager", t, func() {
c, ok := store.(types.Copier)
So(ok, ShouldBeTrue)
d := store.(types.Direr)
Convey("When Copy to an existing dir", func() {
srcSize := rand.Int63n(4 * 1024 * 1024) // Max file size is 4MB
r := io.LimitReader(randbytes.NewRand(), srcSize)
src := uuid.New().String()
_, err := store.Write(src, r, srcSize)
if err != nil {
t.Fatal(err)
}
defer func() {
err = store.Delete(src)
if err != nil {
t.Error(err)
}
}()
dst := uuid.New().String()
_, err = d.CreateDir(dst)
if err != nil {
t.Fatal(err)
}
defer func() {
err = store.Delete(dst, pairs.WithObjectMode(types.ModeDir))
if err != nil {
t.Error(err)
}
}()
err = c.Copy(src, dst)
Convey("The error should be ErrObjectModeInvalid", func() {
So(errors.Is(err, services.ErrObjectModeInvalid), ShouldBeTrue)
})
})
})
}
func TestCopierWithVirtualDir(t *testing.T, store types.Storager) {
Convey("Given a basic Storager", t, func() {
c, ok := store.(types.Copier)
So(ok, ShouldBeTrue)
d := store.(types.Direr)
Convey("When Copy to an existing dir", func() {
srcSize := rand.Int63n(4 * 1024 * 1024) // Max file size is 4MB
r := io.LimitReader(randbytes.NewRand(), srcSize)
src := uuid.New().String()
_, err := store.Write(src, r, srcSize)
if err != nil {
t.Fatal(err)
}
defer func() {
err = store.Delete(src)
if err != nil {
t.Error(err)
}
}()
dst := uuid.New().String()
_, err = d.CreateDir(dst)
if err != nil {
t.Fatal(err)
}
defer func() {
err = store.Delete(dst, pairs.WithObjectMode(types.ModeDir))
if err != nil {
t.Error(err)
}
}()
err = c.Copy(src, dst)
defer func() {
err = store.Delete(dst)
if err != nil {
t.Error(err)
}
}()
Convey("The error should be nil", func() {
So(err, ShouldBeNil)
})
Convey("Stat should get dst object without error", func() {
o, err := store.Stat(dst)
So(err, ShouldBeNil)
So(o, ShouldNotBeNil)
Convey("The Object Mode should be read", func() {
So(o.Mode.IsRead(), ShouldBeTrue)
})
Convey("The path and size should be match", func() {
So(o, ShouldNotBeNil)
So(o.Path, ShouldEqual, dst)
osize, ok := o.GetContentLength()
So(ok, ShouldBeTrue)
So(osize, ShouldEqual, srcSize)
})
})
})
})
}