forked from tencentyun/coscli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcmd_test.go
More file actions
338 lines (296 loc) · 11 KB
/
cmd_test.go
File metadata and controls
338 lines (296 loc) · 11 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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
package main
import (
"coscli/util"
"fmt"
"math/rand"
"os"
"os/exec"
"testing"
"time"
"github.com/mitchellh/go-homedir"
"github.com/spf13/viper"
logger "github.com/sirupsen/logrus"
. "github.com/smartystreets/goconvey/convey"
)
var testDir = "test-tmp-dir"
var config util.Config
var param util.Param
var appID string
var testBucket1 = "coscli-test1"
var testAlias1 = "coscli-test1"
var testEndpoint1 = "cos.ap-guangzhou.myqcloud.com"
var testBucket2 = "coscli-test2"
var testAlias2 = "coscli-test2"
var testEndpoint2 = "cos.ap-guangzhou.myqcloud.com"
func init() {
// 读取配置文件
getConfig()
// 初始化 app-id
name := config.Buckets[0].Name
appID = name[len(name)-10:]
}
func getConfig() {
home, err := homedir.Dir()
if err != nil {
logger.Errorln(err)
}
viper.SetConfigFile(home + "/.cos.yaml")
if err = viper.ReadInConfig(); err != nil {
logger.Errorln(err)
}
if err = viper.UnmarshalKey("cos", &config); err != nil {
logger.Errorln(err)
}
}
func setUp(testBucket, testAlias, testEndpoint string) {
// 创建测试桶
logger.Infoln(fmt.Sprintf("创建测试桶:%s-%s %s", testBucket, appID, testEndpoint))
cmd := exec.Command("bash", "-c",
fmt.Sprintf("./coscli mb cos://%s-%s -e %s", testBucket, appID, testEndpoint))
if err := cmd.Run(); err != nil {
logger.Errorln("SetUp error: 创建测试桶失败")
}
// 更新配置文件
logger.Infoln(fmt.Sprintf("更新配置文件:%s", testAlias))
cmd = exec.Command("bash", "-c",
fmt.Sprintf("./coscli config add -b %s-%s -e %s -a %s", testBucket, appID, testEndpoint, testAlias))
if err := cmd.Run(); err != nil {
logger.Errorln("SetUp error: 更新配置文件失败")
}
// 更新 Config
getConfig()
}
func tearDown(testBucket, testAlias, testEndpoint string) {
// 清空测试桶
logger.Infoln(fmt.Sprintf("清空测试桶:%s", testAlias))
cmd := exec.Command("bash", "-c",
fmt.Sprintf("./coscli rm cos://%s -r -f", testAlias))
if err := cmd.Run(); err != nil {
logger.Errorln("TearDown error: 清空测试桶失败")
}
cmd = exec.Command("bash", "-c",
fmt.Sprintf("./coscli abort cos://%s", testAlias))
if err := cmd.Run(); err != nil {
logger.Errorln("TearDown error: 清空测试桶失败")
}
// 删除测试桶
logger.Infoln(fmt.Sprintf("删除测试桶:%s-%s %s", testBucket, appID, testEndpoint))
cmd = exec.Command("bash", "-c",
fmt.Sprintf("./coscli rb cos://%s-%s -e %s", testBucket, appID, testEndpoint))
if err := cmd.Run(); err != nil {
logger.Errorln("TearDown error: 删除测试桶失败")
}
// 更新配置文件
logger.Infoln(fmt.Sprintf("更新配置文件:%s", testAlias))
cmd = exec.Command("bash", "-c",
fmt.Sprintf("./coscli config delete -a %s", testAlias))
if err := cmd.Run(); err != nil {
logger.Errorln("TearDown error: 更新配置文件失败")
}
}
func genFile(fileName string, size int) {
data := make([]byte, 0)
rand.Seed(time.Now().Unix())
for i := 0; i < size; i++ {
u := uint8(rand.Intn(256))
data = append(data, u)
}
f, err := os.Create(fileName)
if err != nil {
logger.Errorln("genFile error: 创建文件失败")
}
defer f.Close()
n, err := f.Write(data)
if err != nil || n != size {
logger.Errorln("genFile error: 数据写入失败")
}
}
func genDir(dirName string, num int) {
if err := os.MkdirAll(fmt.Sprintf("%s/small-file", dirName), os.ModePerm); err != nil {
logger.Errorln("genDir error: 创建文件夹失败")
}
if err := os.MkdirAll(fmt.Sprintf("%s/big-file", dirName), os.ModePerm); err != nil {
logger.Errorln("genDir error: 创建文件夹失败")
}
logger.Infoln(fmt.Sprintf("生成小文件:%s/small-file", dirName))
for i := 0; i < num; i++ {
genFile(fmt.Sprintf("%s/small-file/%d", dirName, i), 30*1024)
}
logger.Infoln(fmt.Sprintf("生成大文件:%s/big-file", dirName))
for i := 0; i < 3; i++ {
genFile(fmt.Sprintf("%s/big-file/%d", dirName, i), 5*1024*1024)
}
}
func delDir(dirName string) {
logger.Infoln(fmt.Sprintf("删除测试临时文件夹:%s", dirName))
if err := os.RemoveAll(dirName); err != nil {
logger.Errorln("delDir error: 删除文件夹失败")
}
}
func getCRC(cosPath string) string {
bucketName, key := util.ParsePath(cosPath)
param.Endpoint = "cos.ap-guangzhou.myqcloud.com"
c := util.NewClient(&config, ¶m, bucketName)
h, _ ,_:= util.ShowHash(c, key, "crc64")
return h
}
func TestCpCmd(t *testing.T) {
setUp(testBucket1, testAlias1, testEndpoint1)
defer tearDown(testBucket1, testAlias1, testEndpoint1)
setUp(testBucket2, testAlias2, testEndpoint2)
defer tearDown(testBucket2, testAlias2, testEndpoint2)
genDir(testDir, 10)
defer delDir(testDir)
Convey("Test coscli cp", t, func() {
Convey("Upload", func() {
Convey("上传单个小文件", func() {
localFileName := fmt.Sprintf("%s/small-file/0", testDir)
cosFileName := fmt.Sprintf("cos://%s/%s", testAlias1, "single-small")
cmd := exec.Command("bash", "-c",
fmt.Sprintf("./coscli cp %s %s", localFileName, cosFileName))
e := cmd.Run()
So(e, ShouldBeNil)
})
Convey("上传多个小文件", func() {
localFileName := fmt.Sprintf("%s/small-file", testDir)
cosFileName := fmt.Sprintf("cos://%s/%s", testAlias1, "multi-small")
cmd := exec.Command("bash", "-c",
fmt.Sprintf("./coscli cp %s %s -r", localFileName, cosFileName))
e := cmd.Run()
So(e, ShouldBeNil)
})
Convey("上传单个大文件", func() {
localFileName := fmt.Sprintf("%s/big-file/0", testDir)
cosFileName := fmt.Sprintf("cos://%s/%s", testAlias1, "single-big")
cmd := exec.Command("bash", "-c",
fmt.Sprintf("./coscli cp %s %s", localFileName, cosFileName))
e := cmd.Run()
So(e, ShouldBeNil)
})
Convey("上传多个大文件", func() {
localFileName := fmt.Sprintf("%s/big-file", testDir)
cosFileName := fmt.Sprintf("cos://%s/%s", testAlias1, "multi-big")
cmd := exec.Command("bash", "-c",
fmt.Sprintf("./coscli cp %s %s -r", localFileName, cosFileName))
e := cmd.Run()
So(e, ShouldBeNil)
})
})
Convey("Copy", func() {
Convey("桶内拷贝单个文件", func() {
srcPath := fmt.Sprintf("cos://%s/%s", testAlias1, "single-big")
dstPath := fmt.Sprintf("cos://%s/%s", testAlias1, "single-copy")
cmd := exec.Command("bash", "-c",
fmt.Sprintf("./coscli cp %s %s", srcPath, dstPath))
e := cmd.Run()
So(e, ShouldBeNil)
})
Convey("桶内拷贝多个文件", func() {
srcPath := fmt.Sprintf("cos://%s/%s", testAlias1, "multi-big")
dstPath := fmt.Sprintf("cos://%s/%s", testAlias1, "multi-copy")
cmd := exec.Command("bash", "-c",
fmt.Sprintf("./coscli cp %s %s -r", srcPath, dstPath))
e := cmd.Run()
So(e, ShouldBeNil)
})
Convey("跨桶拷贝单个小文件", func() {
srcPath := fmt.Sprintf("cos://%s/%s", testAlias1, "single-small")
dstPath := fmt.Sprintf("cos://%s/%s", testAlias2, "single-copy-small")
cmd := exec.Command("bash", "-c",
fmt.Sprintf("./coscli cp %s %s", srcPath, dstPath))
e := cmd.Run()
So(e, ShouldBeNil)
})
Convey("跨桶拷贝多个小文件", func() {
srcPath := fmt.Sprintf("cos://%s/%s", testAlias1, "multi-small")
dstPath := fmt.Sprintf("cos://%s/%s", testAlias2, "multi-copy-small")
cmd := exec.Command("bash", "-c",
fmt.Sprintf("./coscli cp %s %s -r", srcPath, dstPath))
e := cmd.Run()
So(e, ShouldBeNil)
})
Convey("跨桶拷贝单个大文件", func() {
srcPath := fmt.Sprintf("cos://%s/%s", testAlias1, "single-big")
dstPath := fmt.Sprintf("cos://%s/%s", testAlias2, "single-copy-big")
cmd := exec.Command("bash", "-c",
fmt.Sprintf("./coscli cp %s %s", srcPath, dstPath))
e := cmd.Run()
So(e, ShouldBeNil)
})
Convey("跨桶拷贝多个大文件", func() {
srcPath := fmt.Sprintf("cos://%s/%s", testAlias1, "multi-big")
dstPath := fmt.Sprintf("cos://%s/%s", testAlias2, "multi-copy-big")
cmd := exec.Command("bash", "-c",
fmt.Sprintf("./coscli cp %s %s -r", srcPath, dstPath))
e := cmd.Run()
So(e, ShouldBeNil)
})
})
Convey("Download", func() {
Convey("下载单个小文件", func() {
localFileName := fmt.Sprintf("%s/download/single-small", testDir)
cosFileName := fmt.Sprintf("cos://%s/%s", testAlias2, "single-copy-small")
cmd := exec.Command("bash", "-c",
fmt.Sprintf("./coscli cp %s %s", cosFileName, localFileName))
e := cmd.Run()
So(e, ShouldBeNil)
})
Convey("下载多个小文件", func() {
localFileName := fmt.Sprintf("%s/download/small-file", testDir)
cosFileName := fmt.Sprintf("cos://%s/%s", testAlias2, "multi-copy-small")
cmd := exec.Command("bash", "-c",
fmt.Sprintf("./coscli cp %s %s -r", cosFileName, localFileName))
e := cmd.Run()
So(e, ShouldBeNil)
})
Convey("下载单个大文件", func() {
localFileName := fmt.Sprintf("%s/download/single-big", testDir)
cosFileName := fmt.Sprintf("cos://%s/%s", testAlias2, "single-copy-big")
cmd := exec.Command("bash", "-c",
fmt.Sprintf("./coscli cp %s %s", cosFileName, localFileName))
e := cmd.Run()
So(e, ShouldBeNil)
})
Convey("下载多个大文件", func() {
localFileName := fmt.Sprintf("%s/download/big-file", testDir)
cosFileName := fmt.Sprintf("cos://%s/%s", testAlias2, "multi-copy-big")
cmd := exec.Command("bash", "-c",
fmt.Sprintf("./coscli cp %s %s -r", cosFileName, localFileName))
e := cmd.Run()
So(e, ShouldBeNil)
})
})
Convey("Hash", func() {
Convey("单个小文件哈希校验", func() {
hash1, _ := util.CalculateHash(fmt.Sprintf("%s/small-file/0", testDir), "crc64")
hash2, _ := util.CalculateHash(fmt.Sprintf("%s/download/single-small", testDir), "crc64")
So(hash1, ShouldEqual, hash2)
})
Convey("单个大文件哈希校验", func() {
hash1, _ := util.CalculateHash(fmt.Sprintf("%s/big-file/0", testDir), "crc64")
hash2, _ := util.CalculateHash(fmt.Sprintf("%s/download/single-big", testDir), "crc64")
So(hash1, ShouldEqual, hash2)
})
Convey("多个小文件哈希校验", func() {
fileList1 := util.GetLocalFilesListRecursive(fmt.Sprintf("%s/small-file", testDir), "", "")
fileList2 := util.GetLocalFilesListRecursive(fmt.Sprintf("%s/download/small-file", testDir), "", "")
So(len(fileList1), ShouldEqual, len(fileList2))
for i := 0; i < len(fileList1); i++ {
hash1, _ := util.CalculateHash(fmt.Sprintf("%s/small-file/%s", testDir, fileList1[i]), "crc64")
hash2, _ := util.CalculateHash(fmt.Sprintf("%s/download/small-file/%s", testDir, fileList2[i]), "crc64")
So(hash1, ShouldEqual, hash2)
}
})
Convey("多个大文件哈希校验", func() {
fileList1 := util.GetLocalFilesListRecursive(fmt.Sprintf("%s/big-file", testDir), "", "")
fileList2 := util.GetLocalFilesListRecursive(fmt.Sprintf("%s/download/big-file", testDir), "", "")
So(len(fileList1), ShouldEqual, len(fileList2))
for i := 0; i < len(fileList1); i++ {
hash1, _ := util.CalculateHash(fmt.Sprintf("%s/big-file/%s", testDir, fileList1[i]), "crc64")
hash2, _ := util.CalculateHash(fmt.Sprintf("%s/download/big-file/%s", testDir, fileList2[i]), "crc64")
So(hash1, ShouldEqual, hash2)
}
})
})
})
}