You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1087 lines
35 KiB

4 years ago
5 years ago
5 years ago
4 years ago
5 years ago
5 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
4 years ago
4 years ago
4 years ago
6 years ago
4 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
4 years ago
5 years ago
4 years ago
  1. package cos
  2. // Basic imports
  3. import (
  4. "bytes"
  5. "context"
  6. "fmt"
  7. "io/ioutil"
  8. "math/rand"
  9. "net/http"
  10. "net/url"
  11. "os"
  12. "strings"
  13. "testing"
  14. "time"
  15. //"github.com/google/uuid"
  16. "github.com/stretchr/testify/assert"
  17. "github.com/stretchr/testify/suite"
  18. "github.com/tencentyun/cos-go-sdk-v5"
  19. )
  20. // Define the suite, and absorb the built-in basic suite
  21. // functionality from testify - including a T() method which
  22. // returns the current testing context
  23. type CosTestSuite struct {
  24. suite.Suite
  25. VariableThatShouldStartAtFive int
  26. // CI client
  27. Client *cos.Client
  28. // Copy source client
  29. CClient *cos.Client
  30. Region string
  31. Bucket string
  32. Appid string
  33. // test_object
  34. TestObject string
  35. // special_file_name
  36. SepFileName string
  37. }
  38. // 请替换成您的账号及存储桶信息
  39. const (
  40. //uin
  41. kUin = "100010805041"
  42. kAppid = 1259654469
  43. // 常规测试需要的存储桶
  44. kBucket = "cosgosdktest-1259654469"
  45. kRegion = "ap-guangzhou"
  46. // 跨区域复制需要的目标存储桶,地域不能与kBucket存储桶相同, 目的存储桶需要开启多版本
  47. kRepBucket = "cosgosdkreptest"
  48. kRepRegion = "ap-chengdu"
  49. // Batch测试需要的源存储桶和目标存储桶,目前只在成都、重庆地域公测
  50. kBatchBucket = "cosgosdktest-1259654469"
  51. kTargetBatchBucket = "cosgosdktest-1259654469" //复用了存储桶
  52. kBatchRegion = "ap-guangzhou"
  53. )
  54. func (s *CosTestSuite) SetupSuite() {
  55. fmt.Println("Set up test")
  56. // init
  57. s.TestObject = "test.txt"
  58. s.SepFileName = "中文" + "→↓←→↖↗↙↘! \"#$%&'()*+,-./0123456789:;<=>@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~"
  59. // CI client for test interface
  60. // URL like this http://test-1253846586.cos.ap-guangzhou.myqcloud.com
  61. u := "https://" + kBucket + ".cos." + kRegion + ".myqcloud.com"
  62. u2 := "https://" + kUin + ".cos-control." + kBatchRegion + ".myqcloud.com"
  63. // Get the region
  64. bucketurl, _ := url.Parse(u)
  65. batchurl, _ := url.Parse(u2)
  66. p := strings.Split(bucketurl.Host, ".")
  67. assert.Equal(s.T(), 5, len(p), "Bucket host is not right")
  68. s.Region = p[2]
  69. // Bucket name
  70. pi := strings.LastIndex(p[0], "-")
  71. s.Bucket = p[0][:pi]
  72. s.Appid = p[0][pi+1:]
  73. ib := &cos.BaseURL{BucketURL: bucketurl, BatchURL: batchurl}
  74. s.Client = cos.NewClient(ib, &http.Client{
  75. Transport: &cos.AuthorizationTransport{
  76. SecretID: os.Getenv("COS_SECRETID"),
  77. SecretKey: os.Getenv("COS_SECRETKEY"),
  78. },
  79. })
  80. opt := &cos.BucketPutOptions{
  81. XCosACL: "public-read",
  82. }
  83. r, err := s.Client.Bucket.Put(context.Background(), opt)
  84. if err != nil && r != nil && r.StatusCode == 409 {
  85. fmt.Println("BucketAlreadyOwnedByYou")
  86. } else if err != nil {
  87. assert.Nil(s.T(), err, "PutBucket Failed")
  88. }
  89. }
  90. // Begin of api test
  91. // Service API
  92. func (s *CosTestSuite) TestGetService() {
  93. _, _, err := s.Client.Service.Get(context.Background())
  94. assert.Nil(s.T(), err, "GetService Failed")
  95. }
  96. func (s *CosTestSuite) TestGetRegionService() {
  97. u, _ := url.Parse("http://cos.ap-guangzhou.myqcloud.com")
  98. b := &cos.BaseURL{ServiceURL: u}
  99. client := cos.NewClient(b, &http.Client{
  100. Transport: &cos.AuthorizationTransport{
  101. SecretID: os.Getenv("COS_SECRETID"),
  102. SecretKey: os.Getenv("COS_SECRETKEY"),
  103. },
  104. })
  105. _, _, err := client.Service.Get(context.Background())
  106. assert.Nil(s.T(), err, "GetService Failed")
  107. }
  108. // Bucket API
  109. func (s *CosTestSuite) TestPutHeadDeleteBucket() {
  110. // Notic sometimes the bucket host can not analyis, may has i/o timeout problem
  111. u := "http://" + "testgosdkbucket-create-head-del-" + s.Appid + ".cos." + kRegion + ".myqcloud.com"
  112. iu, _ := url.Parse(u)
  113. ib := &cos.BaseURL{BucketURL: iu}
  114. client := cos.NewClient(ib, &http.Client{
  115. Transport: &cos.AuthorizationTransport{
  116. SecretID: os.Getenv("COS_SECRETID"),
  117. SecretKey: os.Getenv("COS_SECRETKEY"),
  118. },
  119. })
  120. r, err := client.Bucket.Put(context.Background(), nil)
  121. if err != nil && r != nil && r.StatusCode == 409 {
  122. fmt.Println("BucketAlreadyOwnedByYou")
  123. } else if err != nil {
  124. assert.Nil(s.T(), err, "PutBucket Failed")
  125. }
  126. if err != nil {
  127. panic(err)
  128. }
  129. time.Sleep(3 * time.Second)
  130. _, err = client.Bucket.Head(context.Background())
  131. assert.Nil(s.T(), err, "HeadBucket Failed")
  132. if err == nil {
  133. _, err = client.Bucket.Delete(context.Background())
  134. assert.Nil(s.T(), err, "DeleteBucket Failed")
  135. }
  136. }
  137. func (s *CosTestSuite) TestPutBucketACLIllegal() {
  138. opt := &cos.BucketPutACLOptions{
  139. Header: &cos.ACLHeaderOptions{
  140. XCosACL: "public-read-writ",
  141. },
  142. }
  143. _, err := s.Client.Bucket.PutACL(context.Background(), opt)
  144. assert.NotNil(s.T(), err, "PutBucketACL illegal Failed")
  145. }
  146. func (s *CosTestSuite) TestPutGetBucketACLNormal() {
  147. // with header
  148. opt := &cos.BucketPutACLOptions{
  149. Header: &cos.ACLHeaderOptions{
  150. XCosACL: "private",
  151. },
  152. }
  153. _, err := s.Client.Bucket.PutACL(context.Background(), opt)
  154. assert.Nil(s.T(), err, "PutBucketACL normal Failed")
  155. v, _, err := s.Client.Bucket.GetACL(context.Background())
  156. assert.Nil(s.T(), err, "GetBucketACL normal Failed")
  157. assert.Equal(s.T(), 1, len(v.AccessControlList), "GetBucketACL normal Failed, must be private")
  158. }
  159. func (s *CosTestSuite) TestGetBucket() {
  160. opt := &cos.BucketGetOptions{
  161. Prefix: "中文",
  162. MaxKeys: 3,
  163. }
  164. _, _, err := s.Client.Bucket.Get(context.Background(), opt)
  165. assert.Nil(s.T(), err, "GetBucket Failed")
  166. }
  167. func (s *CosTestSuite) TestGetObjectVersions() {
  168. opt := &cos.BucketGetObjectVersionsOptions{
  169. Prefix: "中文",
  170. MaxKeys: 3,
  171. }
  172. _, _, err := s.Client.Bucket.GetObjectVersions(context.Background(), opt)
  173. assert.Nil(s.T(), err, "GetObjectVersions Failed")
  174. }
  175. func (s *CosTestSuite) TestGetBucketLocation() {
  176. v, _, err := s.Client.Bucket.GetLocation(context.Background())
  177. assert.Nil(s.T(), err, "GetLocation Failed")
  178. assert.Equal(s.T(), s.Region, v.Location, "GetLocation wrong region")
  179. }
  180. func (s *CosTestSuite) TestPutGetDeleteCORS() {
  181. opt := &cos.BucketPutCORSOptions{
  182. Rules: []cos.BucketCORSRule{
  183. {
  184. AllowedOrigins: []string{"http://www.qq.com"},
  185. AllowedMethods: []string{"PUT", "GET"},
  186. AllowedHeaders: []string{"x-cos-meta-test", "x-cos-xx"},
  187. MaxAgeSeconds: 500,
  188. ExposeHeaders: []string{"x-cos-meta-test1"},
  189. },
  190. },
  191. }
  192. _, err := s.Client.Bucket.PutCORS(context.Background(), opt)
  193. assert.Nil(s.T(), err, "PutBucketCORS Failed")
  194. v, _, err := s.Client.Bucket.GetCORS(context.Background())
  195. assert.Nil(s.T(), err, "GetBucketCORS Failed")
  196. assert.Equal(s.T(), 1, len(v.Rules), "GetBucketCORS wrong number rules")
  197. }
  198. func (s *CosTestSuite) TestVersionAndReplication() {
  199. opt := &cos.BucketPutVersionOptions{
  200. // Enabled or Suspended, the versioning once opened can not close.
  201. Status: "Enabled",
  202. }
  203. _, err := s.Client.Bucket.PutVersioning(context.Background(), opt)
  204. assert.Nil(s.T(), err, "PutVersioning Failed")
  205. time.Sleep(time.Second)
  206. v, _, err := s.Client.Bucket.GetVersioning(context.Background())
  207. assert.Nil(s.T(), err, "GetVersioning Failed")
  208. assert.Equal(s.T(), "Enabled", v.Status, "Get Wrong Version status")
  209. repOpt := &cos.PutBucketReplicationOptions{
  210. // qcs::cam::uin/[UIN]:uin/[Subaccount]
  211. Role: "qcs::cam::uin/" + kUin + ":uin/" + kUin,
  212. Rule: []cos.BucketReplicationRule{
  213. {
  214. ID: "1",
  215. // Enabled or Disabled
  216. Status: "Enabled",
  217. Destination: &cos.ReplicationDestination{
  218. // qcs::cos:[Region]::[Bucketname-Appid]
  219. Bucket: "qcs::cos:" + kRepRegion + "::" + kRepBucket + "-" + s.Appid,
  220. },
  221. },
  222. },
  223. }
  224. _, err = s.Client.Bucket.PutBucketReplication(context.Background(), repOpt)
  225. assert.Nil(s.T(), err, "PutBucketReplication Failed")
  226. time.Sleep(time.Second)
  227. vr, _, err := s.Client.Bucket.GetBucketReplication(context.Background())
  228. assert.Nil(s.T(), err, "GetBucketReplication Failed")
  229. for _, r := range vr.Rule {
  230. assert.Equal(s.T(), "Enabled", r.Status, "Get Wrong Version status")
  231. assert.Equal(s.T(), "qcs::cos:"+kRepRegion+"::"+kRepBucket+"-"+s.Appid, r.Destination.Bucket, "Get Wrong Version status")
  232. }
  233. _, err = s.Client.Bucket.DeleteBucketReplication(context.Background())
  234. assert.Nil(s.T(), err, "DeleteBucketReplication Failed")
  235. }
  236. func (s *CosTestSuite) TestBucketInventory() {
  237. id := "test1"
  238. dBucket := "qcs::cos:" + s.Region + "::" + s.Bucket + "-" + s.Appid
  239. opt := &cos.BucketPutInventoryOptions{
  240. ID: id,
  241. // True or False
  242. IsEnabled: "True",
  243. IncludedObjectVersions: "All",
  244. Filter: &cos.BucketInventoryFilter{
  245. Prefix: "test",
  246. },
  247. OptionalFields: &cos.BucketInventoryOptionalFields{
  248. BucketInventoryFields: []string{
  249. "Size", "LastModifiedDate",
  250. },
  251. },
  252. Schedule: &cos.BucketInventorySchedule{
  253. // Weekly or Daily
  254. Frequency: "Daily",
  255. },
  256. Destination: &cos.BucketInventoryDestination{
  257. Bucket: dBucket,
  258. Format: "CSV",
  259. },
  260. }
  261. _, err := s.Client.Bucket.PutInventory(context.Background(), id, opt)
  262. assert.Nil(s.T(), err, "PutBucketInventory Failed")
  263. v, _, err := s.Client.Bucket.GetInventory(context.Background(), id)
  264. assert.Nil(s.T(), err, "GetBucketInventory Failed")
  265. assert.Equal(s.T(), "test1", v.ID, "Get Wrong inventory id")
  266. assert.Equal(s.T(), "true", v.IsEnabled, "Get Wrong inventory isenabled")
  267. assert.Equal(s.T(), dBucket, v.Destination.Bucket, "Get Wrong inventory isenabled")
  268. _, err = s.Client.Bucket.DeleteInventory(context.Background(), id)
  269. assert.Nil(s.T(), err, "DeleteBucketInventory Failed")
  270. }
  271. func (s *CosTestSuite) TestBucketLogging() {
  272. tBucket := s.Bucket + "-" + s.Appid
  273. opt := &cos.BucketPutLoggingOptions{
  274. LoggingEnabled: &cos.BucketLoggingEnabled{
  275. TargetBucket: tBucket,
  276. },
  277. }
  278. _, err := s.Client.Bucket.PutLogging(context.Background(), opt)
  279. assert.Nil(s.T(), err, "PutLogging Failed")
  280. v, _, err := s.Client.Bucket.GetLogging(context.Background())
  281. assert.Nil(s.T(), err, "GetLogging Failed")
  282. assert.Equal(s.T(), tBucket, v.LoggingEnabled.TargetBucket, "Get Wrong Version status")
  283. }
  284. func (s *CosTestSuite) TestBucketTagging() {
  285. opt := &cos.BucketPutTaggingOptions{
  286. TagSet: []cos.BucketTaggingTag{
  287. {
  288. Key: "testk1",
  289. Value: "testv1",
  290. },
  291. {
  292. Key: "testk2",
  293. Value: "testv2",
  294. },
  295. },
  296. }
  297. _, err := s.Client.Bucket.PutTagging(context.Background(), opt)
  298. assert.Nil(s.T(), err, "Put Tagging Failed")
  299. v, _, err := s.Client.Bucket.GetTagging(context.Background())
  300. assert.Nil(s.T(), err, "Get Tagging Failed")
  301. assert.Equal(s.T(), v.TagSet[0].Key, opt.TagSet[0].Key, "Get Wrong Tag key")
  302. assert.Equal(s.T(), v.TagSet[0].Value, opt.TagSet[0].Value, "Get Wrong Tag value")
  303. assert.Equal(s.T(), v.TagSet[1].Key, opt.TagSet[1].Key, "Get Wrong Tag key")
  304. assert.Equal(s.T(), v.TagSet[1].Value, opt.TagSet[1].Value, "Get Wrong Tag value")
  305. }
  306. func (s *CosTestSuite) TestPutGetDeleteLifeCycle() {
  307. lc := &cos.BucketPutLifecycleOptions{
  308. Rules: []cos.BucketLifecycleRule{
  309. {
  310. ID: "1234",
  311. Filter: &cos.BucketLifecycleFilter{Prefix: "test"},
  312. Status: "Enabled",
  313. Transition: []cos.BucketLifecycleTransition{
  314. {
  315. Days: 10,
  316. StorageClass: "Standard",
  317. },
  318. },
  319. },
  320. },
  321. }
  322. _, err := s.Client.Bucket.PutLifecycle(context.Background(), lc)
  323. assert.Nil(s.T(), err, "PutBucketLifecycle Failed")
  324. _, r, err := s.Client.Bucket.GetLifecycle(context.Background())
  325. // Might cleaned by other case concrrent
  326. if err != nil && 404 != r.StatusCode {
  327. assert.Nil(s.T(), err, "GetBucketLifecycle Failed")
  328. }
  329. _, err = s.Client.Bucket.DeleteLifecycle(context.Background())
  330. assert.Nil(s.T(), err, "DeleteBucketLifecycle Failed")
  331. }
  332. func (s *CosTestSuite) TestPutGetDeleteWebsite() {
  333. opt := &cos.BucketPutWebsiteOptions{
  334. Index: "index.html",
  335. Error: &cos.ErrorDocument{"index_backup.html"},
  336. RoutingRules: &cos.WebsiteRoutingRules{
  337. []cos.WebsiteRoutingRule{
  338. {
  339. ConditionErrorCode: "404",
  340. RedirectProtocol: "https",
  341. RedirectReplaceKey: "404.html",
  342. },
  343. {
  344. ConditionPrefix: "docs/",
  345. RedirectProtocol: "https",
  346. RedirectReplaceKeyPrefix: "documents/",
  347. },
  348. },
  349. },
  350. }
  351. _, err := s.Client.Bucket.PutWebsite(context.Background(), opt)
  352. assert.Nil(s.T(), err, "PutBucketWebsite Failed")
  353. res, rsp, err := s.Client.Bucket.GetWebsite(context.Background())
  354. if err != nil && 404 != rsp.StatusCode {
  355. assert.Nil(s.T(), err, "GetBucketWebsite Failed")
  356. }
  357. assert.Equal(s.T(), opt.Index, res.Index, "GetBucketWebsite Failed")
  358. assert.Equal(s.T(), opt.Error, res.Error, "GetBucketWebsite Failed")
  359. assert.Equal(s.T(), opt.RedirectProtocol, res.RedirectProtocol, "GetBucketWebsite Failed")
  360. _, err = s.Client.Bucket.DeleteWebsite(context.Background())
  361. assert.Nil(s.T(), err, "DeleteBucketWebsite Failed")
  362. }
  363. func (s *CosTestSuite) TestListMultipartUploads() {
  364. // Create new upload
  365. name := "test_multipart" + time.Now().Format(time.RFC3339)
  366. flag := false
  367. v, _, err := s.Client.Object.InitiateMultipartUpload(context.Background(), name, nil)
  368. assert.Nil(s.T(), err, "InitiateMultipartUpload Failed")
  369. id := v.UploadID
  370. // List
  371. r, _, err := s.Client.Bucket.ListMultipartUploads(context.Background(), nil)
  372. assert.Nil(s.T(), err, "ListMultipartUploads Failed")
  373. for _, p := range r.Uploads {
  374. if p.Key == name {
  375. assert.Equal(s.T(), id, p.UploadID, "ListMultipartUploads wrong uploadid")
  376. flag = true
  377. }
  378. }
  379. assert.Equal(s.T(), true, flag, "ListMultipartUploads wrong key")
  380. // Abort
  381. _, err = s.Client.Object.AbortMultipartUpload(context.Background(), name, id)
  382. assert.Nil(s.T(), err, "AbortMultipartUpload Failed")
  383. }
  384. // Object API
  385. func (s *CosTestSuite) TestPutHeadGetDeleteObject_10MB() {
  386. name := "test/objectPut" + time.Now().Format(time.RFC3339)
  387. b := make([]byte, 1024*1024*10)
  388. _, err := rand.Read(b)
  389. content := fmt.Sprintf("%X", b)
  390. f := strings.NewReader(content)
  391. _, err = s.Client.Object.Put(context.Background(), name, f, nil)
  392. assert.Nil(s.T(), err, "PutObject Failed")
  393. _, err = s.Client.Object.Head(context.Background(), name, nil)
  394. assert.Nil(s.T(), err, "HeadObject Failed")
  395. _, err = s.Client.Object.Delete(context.Background(), name)
  396. assert.Nil(s.T(), err, "DeleteObject Failed")
  397. }
  398. func (s *CosTestSuite) TestPutGetDeleteObjectByFile_10MB() {
  399. // Create tmp file
  400. filePath := "tmpfile" + time.Now().Format(time.RFC3339)
  401. newfile, err := os.Create(filePath)
  402. assert.Nil(s.T(), err, "create tmp file Failed")
  403. defer newfile.Close()
  404. name := "test/objectPutByFile" + time.Now().Format(time.RFC3339)
  405. b := make([]byte, 1024*1024*10)
  406. _, err = rand.Read(b)
  407. newfile.Write(b)
  408. _, err = s.Client.Object.PutFromFile(context.Background(), name, filePath, nil)
  409. assert.Nil(s.T(), err, "PutObject Failed")
  410. // Over write tmp file
  411. _, err = s.Client.Object.GetToFile(context.Background(), name, filePath, nil)
  412. assert.Nil(s.T(), err, "HeadObject Failed")
  413. _, err = s.Client.Object.Delete(context.Background(), name)
  414. assert.Nil(s.T(), err, "DeleteObject Failed")
  415. // remove the local tmp file
  416. err = os.Remove(filePath)
  417. assert.Nil(s.T(), err, "remove local file Failed")
  418. }
  419. func (s *CosTestSuite) TestPutGetDeleteObjectByUpload_10MB() {
  420. // Create tmp file
  421. filePath := "tmpfile" + time.Now().Format(time.RFC3339)
  422. newfile, err := os.Create(filePath)
  423. assert.Nil(s.T(), err, "create tmp file Failed")
  424. defer newfile.Close()
  425. name := "test/objectUpload" + time.Now().Format(time.RFC3339)
  426. b := make([]byte, 1024*1024*10)
  427. _, err = rand.Read(b)
  428. newfile.Write(b)
  429. opt := &cos.MultiUploadOptions{
  430. PartSize: 1,
  431. ThreadPoolSize: 3,
  432. }
  433. _, _, err = s.Client.Object.Upload(context.Background(), name, filePath, opt)
  434. assert.Nil(s.T(), err, "PutObject Failed")
  435. // Over write tmp file
  436. _, err = s.Client.Object.GetToFile(context.Background(), name, filePath, nil)
  437. assert.Nil(s.T(), err, "HeadObject Failed")
  438. _, err = s.Client.Object.Delete(context.Background(), name)
  439. assert.Nil(s.T(), err, "DeleteObject Failed")
  440. // remove the local tmp file
  441. err = os.Remove(filePath)
  442. assert.Nil(s.T(), err, "remove local file Failed")
  443. }
  444. func (s *CosTestSuite) TestPutGetDeleteObjectByUploadAndDownload_10MB() {
  445. // Create tmp file
  446. filePath := "tmpfile" + time.Now().Format(time.RFC3339)
  447. newfile, err := os.Create(filePath)
  448. assert.Nil(s.T(), err, "create tmp file Failed")
  449. defer newfile.Close()
  450. name := "test/objectUpload" + time.Now().Format(time.RFC3339)
  451. b := make([]byte, 1024*1024*10)
  452. _, err = rand.Read(b)
  453. newfile.Write(b)
  454. opt := &cos.MultiUploadOptions{
  455. PartSize: 1,
  456. ThreadPoolSize: 3,
  457. }
  458. _, _, err = s.Client.Object.Upload(context.Background(), name, filePath, opt)
  459. assert.Nil(s.T(), err, "PutObject Failed")
  460. // Over write tmp file
  461. _, err = s.Client.Object.Download(context.Background(), name, filePath, nil)
  462. assert.Nil(s.T(), err, "DownloadObject Failed")
  463. _, err = s.Client.Object.Delete(context.Background(), name)
  464. assert.Nil(s.T(), err, "DeleteObject Failed")
  465. // remove the local tmp file
  466. err = os.Remove(filePath)
  467. assert.Nil(s.T(), err, "remove local file Failed")
  468. }
  469. func (s *CosTestSuite) TestPutGetDeleteObjectSpecialName() {
  470. f := strings.NewReader("test")
  471. name := s.SepFileName + time.Now().Format(time.RFC3339)
  472. _, err := s.Client.Object.Put(context.Background(), name, f, nil)
  473. assert.Nil(s.T(), err, "PutObject Failed")
  474. resp, err := s.Client.Object.Get(context.Background(), name, nil)
  475. assert.Nil(s.T(), err, "GetObject Failed")
  476. defer resp.Body.Close()
  477. bs, _ := ioutil.ReadAll(resp.Body)
  478. assert.Equal(s.T(), "test", string(bs), "GetObject failed content wrong")
  479. _, err = s.Client.Object.Delete(context.Background(), name)
  480. assert.Nil(s.T(), err, "DeleteObject Failed")
  481. }
  482. func (s *CosTestSuite) TestPutObjectToNonExistBucket() {
  483. u := "http://gosdknonexistbucket-" + s.Appid + ".cos." + s.Region + ".myqcloud.com"
  484. iu, _ := url.Parse(u)
  485. ib := &cos.BaseURL{BucketURL: iu}
  486. client := cos.NewClient(ib, &http.Client{
  487. Transport: &cos.AuthorizationTransport{
  488. SecretID: os.Getenv("COS_SECRETID"),
  489. SecretKey: os.Getenv("COS_SECRETKEY"),
  490. },
  491. })
  492. name := "test/objectPut.go"
  493. f := strings.NewReader("test")
  494. r, err := client.Object.Put(context.Background(), name, f, nil)
  495. assert.NotNil(s.T(), err, "PutObject ToNonExistBucket Failed")
  496. assert.Equal(s.T(), 404, r.StatusCode, "PutObject ToNonExistBucket, not 404")
  497. }
  498. func (s *CosTestSuite) TestPutGetObjectACL() {
  499. name := "test/objectACL.go" + time.Now().Format(time.RFC3339)
  500. f := strings.NewReader("test")
  501. _, err := s.Client.Object.Put(context.Background(), name, f, nil)
  502. assert.Nil(s.T(), err, "PutObject Failed")
  503. // Put acl
  504. opt := &cos.ObjectPutACLOptions{
  505. Header: &cos.ACLHeaderOptions{
  506. XCosACL: "public-read",
  507. },
  508. }
  509. _, err = s.Client.Object.PutACL(context.Background(), name, opt)
  510. assert.Nil(s.T(), err, "PutObjectACL Failed")
  511. v, _, err := s.Client.Object.GetACL(context.Background(), name)
  512. assert.Nil(s.T(), err, "GetObjectACL Failed")
  513. assert.Equal(s.T(), 2, len(v.AccessControlList), "GetLifecycle wrong number rules")
  514. _, err = s.Client.Object.Delete(context.Background(), name)
  515. assert.Nil(s.T(), err, "DeleteObject Failed")
  516. }
  517. func (s *CosTestSuite) TestPutObjectRestore() {
  518. name := "archivetest"
  519. putOpt := &cos.ObjectPutOptions{
  520. ObjectPutHeaderOptions: &cos.ObjectPutHeaderOptions{
  521. XCosStorageClass: "ARCHIVE",
  522. },
  523. }
  524. f := strings.NewReader("test")
  525. _, err := s.Client.Object.Put(context.Background(), name, f, putOpt)
  526. assert.Nil(s.T(), err, "PutObject Archive faild")
  527. opt := &cos.ObjectRestoreOptions{
  528. Days: 2,
  529. Tier: &cos.CASJobParameters{
  530. // Standard, Exepdited and Bulk
  531. Tier: "Expedited",
  532. },
  533. }
  534. resp, _ := s.Client.Object.PostRestore(context.Background(), name, opt)
  535. retCode := resp.StatusCode
  536. if retCode != 200 && retCode != 202 && retCode != 409 {
  537. right := false
  538. fmt.Println("PutObjectRestore get code is:", retCode)
  539. assert.Equal(s.T(), true, right, "PutObjectRestore Failed")
  540. }
  541. }
  542. func (s *CosTestSuite) TestCopyObject() {
  543. u := "http://" + kRepBucket + "-" + s.Appid + ".cos." + kRepRegion + ".myqcloud.com"
  544. iu, _ := url.Parse(u)
  545. ib := &cos.BaseURL{BucketURL: iu}
  546. c := cos.NewClient(ib, &http.Client{
  547. Transport: &cos.AuthorizationTransport{
  548. SecretID: os.Getenv("COS_SECRETID"),
  549. SecretKey: os.Getenv("COS_SECRETKEY"),
  550. },
  551. })
  552. opt := &cos.BucketPutOptions{
  553. XCosACL: "public-read",
  554. }
  555. // Notice in intranet the bucket host sometimes has i/o timeout problem
  556. r, err := c.Bucket.Put(context.Background(), opt)
  557. if err != nil && r != nil && r.StatusCode == 409 {
  558. fmt.Println("BucketAlreadyOwnedByYou")
  559. } else if err != nil {
  560. assert.Nil(s.T(), err, "PutBucket Failed")
  561. }
  562. source := "test/objectMove1" + time.Now().Format(time.RFC3339)
  563. expected := "test"
  564. f := strings.NewReader(expected)
  565. r, err = c.Object.Put(context.Background(), source, f, nil)
  566. assert.Nil(s.T(), err, "PutObject Failed")
  567. var version_id string
  568. if r.Header["X-Cos-Version-Id"] != nil {
  569. version_id = r.Header.Get("X-Cos-Version-Id")
  570. }
  571. time.Sleep(3 * time.Second)
  572. // Copy file
  573. soruceURL := fmt.Sprintf("%s/%s", iu.Host, source)
  574. dest := "test/objectMove1" + time.Now().Format(time.RFC3339)
  575. //opt := &cos.ObjectCopyOptions{}
  576. if version_id == "" {
  577. _, _, err = s.Client.Object.Copy(context.Background(), dest, soruceURL, nil)
  578. } else {
  579. _, _, err = s.Client.Object.Copy(context.Background(), dest, soruceURL, nil, version_id)
  580. }
  581. assert.Nil(s.T(), err, "PutObjectCopy Failed")
  582. // Check content
  583. resp, err := s.Client.Object.Get(context.Background(), dest, nil)
  584. assert.Nil(s.T(), err, "GetObject Failed")
  585. bs, _ := ioutil.ReadAll(resp.Body)
  586. resp.Body.Close()
  587. result := string(bs)
  588. assert.Equal(s.T(), expected, result, "PutObjectCopy Failed, wrong content")
  589. }
  590. func (s *CosTestSuite) TestCreateAbortMultipartUpload() {
  591. name := "test_multipart" + time.Now().Format(time.RFC3339)
  592. v, _, err := s.Client.Object.InitiateMultipartUpload(context.Background(), name, nil)
  593. assert.Nil(s.T(), err, "InitiateMultipartUpload Failed")
  594. _, err = s.Client.Object.AbortMultipartUpload(context.Background(), name, v.UploadID)
  595. assert.Nil(s.T(), err, "AbortMultipartUpload Failed")
  596. }
  597. func (s *CosTestSuite) TestCreateCompleteMultipartUpload() {
  598. name := "test/test_complete_upload" + time.Now().Format(time.RFC3339)
  599. v, _, err := s.Client.Object.InitiateMultipartUpload(context.Background(), name, nil)
  600. uploadID := v.UploadID
  601. blockSize := 1024 * 1024 * 3
  602. opt := &cos.CompleteMultipartUploadOptions{}
  603. for i := 1; i < 3; i++ {
  604. b := make([]byte, blockSize)
  605. _, err := rand.Read(b)
  606. content := fmt.Sprintf("%X", b)
  607. f := strings.NewReader(content)
  608. resp, err := s.Client.Object.UploadPart(
  609. context.Background(), name, uploadID, i, f, nil,
  610. )
  611. assert.Nil(s.T(), err, "UploadPart Failed")
  612. etag := resp.Header.Get("Etag")
  613. opt.Parts = append(opt.Parts, cos.Object{
  614. PartNumber: i, ETag: etag},
  615. )
  616. }
  617. _, _, err = s.Client.Object.CompleteMultipartUpload(
  618. context.Background(), name, uploadID, opt,
  619. )
  620. assert.Nil(s.T(), err, "CompleteMultipartUpload Failed")
  621. }
  622. func (s *CosTestSuite) TestSSE_C() {
  623. name := "test/TestSSE_C"
  624. content := "test sse-c " + time.Now().Format(time.RFC3339)
  625. f := strings.NewReader(content)
  626. putOpt := &cos.ObjectPutOptions{
  627. ObjectPutHeaderOptions: &cos.ObjectPutHeaderOptions{
  628. ContentType: "text/html",
  629. //XCosServerSideEncryption: "AES256",
  630. XCosSSECustomerAglo: "AES256",
  631. XCosSSECustomerKey: "MDEyMzQ1Njc4OUFCQ0RFRjAxMjM0NTY3ODlBQkNERUY=",
  632. XCosSSECustomerKeyMD5: "U5L61r7jcwdNvT7frmUG8g==",
  633. },
  634. ACLHeaderOptions: &cos.ACLHeaderOptions{
  635. XCosACL: "public-read",
  636. //XCosACL: "private",
  637. },
  638. }
  639. _, err := s.Client.Object.Put(context.Background(), name, f, putOpt)
  640. assert.Nil(s.T(), err, "PutObject with SSE failed")
  641. headOpt := &cos.ObjectHeadOptions{
  642. XCosSSECustomerAglo: "AES256",
  643. XCosSSECustomerKey: "MDEyMzQ1Njc4OUFCQ0RFRjAxMjM0NTY3ODlBQkNERUY=",
  644. XCosSSECustomerKeyMD5: "U5L61r7jcwdNvT7frmUG8g==",
  645. }
  646. _, err = s.Client.Object.Head(context.Background(), name, headOpt)
  647. assert.Nil(s.T(), err, "HeadObject with SSE failed")
  648. getOpt := &cos.ObjectGetOptions{
  649. XCosSSECustomerAglo: "AES256",
  650. XCosSSECustomerKey: "MDEyMzQ1Njc4OUFCQ0RFRjAxMjM0NTY3ODlBQkNERUY=",
  651. XCosSSECustomerKeyMD5: "U5L61r7jcwdNvT7frmUG8g==",
  652. }
  653. var resp *cos.Response
  654. resp, err = s.Client.Object.Get(context.Background(), name, getOpt)
  655. assert.Nil(s.T(), err, "GetObject with SSE failed")
  656. bodyBytes, _ := ioutil.ReadAll(resp.Body)
  657. bodyContent := string(bodyBytes)
  658. assert.Equal(s.T(), content, bodyContent, "GetObject with SSE failed, want: %+v, res: %+v", content, bodyContent)
  659. copyOpt := &cos.ObjectCopyOptions{
  660. &cos.ObjectCopyHeaderOptions{
  661. XCosCopySourceSSECustomerAglo: "AES256",
  662. XCosCopySourceSSECustomerKey: "MDEyMzQ1Njc4OUFCQ0RFRjAxMjM0NTY3ODlBQkNERUY=",
  663. XCosCopySourceSSECustomerKeyMD5: "U5L61r7jcwdNvT7frmUG8g==",
  664. },
  665. &cos.ACLHeaderOptions{},
  666. }
  667. copySource := s.Bucket + "-" + s.Appid + ".cos." + s.Region + ".myqcloud.com/" + name
  668. _, _, err = s.Client.Object.Copy(context.Background(), "test/TestSSE_C_Copy", copySource, copyOpt)
  669. assert.Nil(s.T(), err, "CopyObject with SSE failed")
  670. partIni := &cos.MultiUploadOptions{
  671. OptIni: &cos.InitiateMultipartUploadOptions{
  672. &cos.ACLHeaderOptions{},
  673. &cos.ObjectPutHeaderOptions{
  674. XCosSSECustomerAglo: "AES256",
  675. XCosSSECustomerKey: "MDEyMzQ1Njc4OUFCQ0RFRjAxMjM0NTY3ODlBQkNERUY=",
  676. XCosSSECustomerKeyMD5: "U5L61r7jcwdNvT7frmUG8g==",
  677. },
  678. },
  679. PartSize: 1,
  680. }
  681. filePath := "tmpfile" + time.Now().Format(time.RFC3339)
  682. newFile, err := os.Create(filePath)
  683. assert.Nil(s.T(), err, "create tmp file Failed")
  684. defer newFile.Close()
  685. b := make([]byte, 1024*10)
  686. _, err = rand.Read(b)
  687. newFile.Write(b)
  688. _, _, err = s.Client.Object.MultiUpload(context.Background(), "test/TestSSE_C_MultiUpload", filePath, partIni)
  689. assert.Nil(s.T(), err, "MultiUpload with SSE failed")
  690. err = os.Remove(filePath)
  691. assert.Nil(s.T(), err, "remove local file Failed")
  692. }
  693. func (s *CosTestSuite) TestMultiUpload() {
  694. filePath := "tmpfile" + time.Now().Format(time.RFC3339)
  695. newFile, err := os.Create(filePath)
  696. assert.Nil(s.T(), err, "create tmp file Failed")
  697. defer newFile.Close()
  698. b := make([]byte, 1024*1024*10)
  699. _, err = rand.Read(b)
  700. newFile.Write(b)
  701. partIni := &cos.MultiUploadOptions{}
  702. _, _, err = s.Client.Object.MultiUpload(context.Background(), "test/Test_MultiUpload", filePath, partIni)
  703. err = os.Remove(filePath)
  704. assert.Nil(s.T(), err, "remove tmp file failed")
  705. }
  706. func (s *CosTestSuite) TestAppend() {
  707. name := "append" + time.Now().Format(time.RFC3339)
  708. b1 := make([]byte, 1024*1024*10)
  709. _, err := rand.Read(b1)
  710. pos, _, err := s.Client.Object.Append(context.Background(), name, 0, bytes.NewReader(b1), nil)
  711. assert.Nil(s.T(), err, "append object failed")
  712. assert.Equal(s.T(), len(b1), pos, "append object pos error")
  713. b2 := make([]byte, 12345)
  714. rand.Read(b2)
  715. pos, _, err = s.Client.Object.Append(context.Background(), name, pos, bytes.NewReader(b2), nil)
  716. assert.Nil(s.T(), err, "append object failed")
  717. assert.Equal(s.T(), len(b1)+len(b2), pos, "append object pos error")
  718. }
  719. /*
  720. func (s *CosTestSuite) TestBatch() {
  721. client := cos.NewClient(s.Client.BaseURL, &http.Client{
  722. Transport: &cos.AuthorizationTransport{
  723. SecretID: os.Getenv("COS_SECRETID"),
  724. SecretKey: os.Getenv("COS_SECRETKEY"),
  725. },
  726. })
  727. source_name := "test/1.txt"
  728. sf := strings.NewReader("batch test content")
  729. _, err := client.Object.Put(context.Background(), source_name, sf, nil)
  730. assert.Nil(s.T(), err, "object put Failed")
  731. manifest_name := "test/manifest.csv"
  732. f := strings.NewReader(kBatchBucket + "," + source_name)
  733. resp, err := client.Object.Put(context.Background(), manifest_name, f, nil)
  734. assert.Nil(s.T(), err, "object put Failed")
  735. etag := resp.Header.Get("ETag")
  736. uuid_str := uuid.New().String()
  737. opt := &cos.BatchCreateJobOptions{
  738. ClientRequestToken: uuid_str,
  739. ConfirmationRequired: "true",
  740. Description: "test batch",
  741. Manifest: &cos.BatchJobManifest{
  742. Location: &cos.BatchJobManifestLocation{
  743. ETag: etag,
  744. ObjectArn: "qcs::cos:" + kBatchRegion + ":uid/" + s.Appid + ":" + kBatchBucket + "/" + manifest_name,
  745. },
  746. Spec: &cos.BatchJobManifestSpec{
  747. Fields: []string{"Bucket", "Key"},
  748. Format: "COSBatchOperations_CSV_V1",
  749. },
  750. },
  751. Operation: &cos.BatchJobOperation{
  752. PutObjectCopy: &cos.BatchJobOperationCopy{
  753. TargetResource: "qcs::cos:" + kBatchRegion + ":uid/" + s.Appid + ":" + kTargetBatchBucket,
  754. },
  755. },
  756. Priority: 1,
  757. Report: &cos.BatchJobReport{
  758. Bucket: "qcs::cos:" + kBatchRegion + ":uid/" + s.Appid + ":" + kBatchBucket,
  759. Enabled: "true",
  760. Format: "Report_CSV_V1",
  761. Prefix: "job-result",
  762. ReportScope: "AllTasks",
  763. },
  764. RoleArn: "qcs::cam::uin/" + kUin + ":roleName/COSBatch_QcsRole",
  765. }
  766. headers := &cos.BatchRequestHeaders{
  767. XCosAppid: kAppid,
  768. }
  769. res1, _, err := client.Batch.CreateJob(context.Background(), opt, headers)
  770. assert.Nil(s.T(), err, "create job Failed")
  771. jobid := res1.JobId
  772. res2, _, err := client.Batch.DescribeJob(context.Background(), jobid, headers)
  773. assert.Nil(s.T(), err, "describe job Failed")
  774. assert.Equal(s.T(), res2.Job.ConfirmationRequired, "true", "ConfirmationRequired not right")
  775. assert.Equal(s.T(), res2.Job.Description, "test batch", "Description not right")
  776. assert.Equal(s.T(), res2.Job.JobId, jobid, "jobid not right")
  777. assert.Equal(s.T(), res2.Job.Priority, 1, "priority not right")
  778. assert.Equal(s.T(), res2.Job.RoleArn, "qcs::cam::uin/"+kUin+":roleName/COSBatch_QcsRole", "priority not right")
  779. _, _, err = client.Batch.ListJobs(context.Background(), nil, headers)
  780. assert.Nil(s.T(), err, "list jobs failed")
  781. up_opt := &cos.BatchUpdatePriorityOptions{
  782. JobId: jobid,
  783. Priority: 3,
  784. }
  785. res3, _, err := client.Batch.UpdateJobPriority(context.Background(), up_opt, headers)
  786. assert.Nil(s.T(), err, "list jobs failed")
  787. assert.Equal(s.T(), res3.JobId, jobid, "jobid failed")
  788. assert.Equal(s.T(), res3.Priority, 3, "priority not right")
  789. // 等待状态变成Suspended
  790. for i := 0; i < 50; i = i + 1 {
  791. res, _, err := client.Batch.DescribeJob(context.Background(), jobid, headers)
  792. assert.Nil(s.T(), err, "describe job Failed")
  793. assert.Equal(s.T(), res2.Job.ConfirmationRequired, "true", "ConfirmationRequired not right")
  794. assert.Equal(s.T(), res2.Job.Description, "test batch", "Description not right")
  795. assert.Equal(s.T(), res2.Job.JobId, jobid, "jobid not right")
  796. assert.Equal(s.T(), res2.Job.Priority, 1, "priority not right")
  797. assert.Equal(s.T(), res2.Job.RoleArn, "qcs::cam::uin/"+kUin+":roleName/COSBatch_QcsRole", "priority not right")
  798. if res.Job.Status == "Suspended" {
  799. break
  800. }
  801. if i == 9 {
  802. assert.Error(s.T(), errors.New("Job status is not Suspended or timeout"))
  803. }
  804. time.Sleep(time.Second * 2)
  805. }
  806. us_opt := &cos.BatchUpdateStatusOptions{
  807. JobId: jobid,
  808. RequestedJobStatus: "Ready", // 允许状态转换见 https://cloud.tencent.com/document/product/436/38604
  809. StatusUpdateReason: "to test",
  810. }
  811. res4, _, err := client.Batch.UpdateJobStatus(context.Background(), us_opt, headers)
  812. assert.Nil(s.T(), err, "list jobs failed")
  813. assert.Equal(s.T(), res4.JobId, jobid, "jobid failed")
  814. assert.Equal(s.T(), res4.Status, "Ready", "status failed")
  815. assert.Equal(s.T(), res4.StatusUpdateReason, "to test", "StatusUpdateReason failed")
  816. }
  817. */
  818. func (s *CosTestSuite) TestEncryption() {
  819. opt := &cos.BucketPutEncryptionOptions{
  820. Rule: &cos.BucketEncryptionConfiguration{
  821. SSEAlgorithm: "AES256",
  822. },
  823. }
  824. _, err := s.Client.Bucket.PutEncryption(context.Background(), opt)
  825. assert.Nil(s.T(), err, "PutEncryption Failed")
  826. time.Sleep(time.Second * 2)
  827. res, _, err := s.Client.Bucket.GetEncryption(context.Background())
  828. assert.Nil(s.T(), err, "GetEncryption Failed")
  829. assert.Equal(s.T(), opt.Rule.SSEAlgorithm, res.Rule.SSEAlgorithm, "GetEncryption Failed")
  830. _, err = s.Client.Bucket.DeleteEncryption(context.Background())
  831. assert.Nil(s.T(), err, "DeleteEncryption Failed")
  832. }
  833. func (s *CosTestSuite) TestReferer() {
  834. opt := &cos.BucketPutRefererOptions{
  835. Status: "Enabled",
  836. RefererType: "White-List",
  837. DomainList: []string{
  838. "*.qq.com",
  839. "*.qcloud.com",
  840. },
  841. EmptyReferConfiguration: "Allow",
  842. }
  843. _, err := s.Client.Bucket.PutReferer(context.Background(), opt)
  844. assert.Nil(s.T(), err, "PutReferer Failed")
  845. res, _, err := s.Client.Bucket.GetReferer(context.Background())
  846. assert.Nil(s.T(), err, "GetReferer Failed")
  847. assert.Equal(s.T(), opt.Status, res.Status, "GetReferer Failed")
  848. assert.Equal(s.T(), opt.RefererType, res.RefererType, "GetReferer Failed")
  849. assert.Equal(s.T(), opt.DomainList, res.DomainList, "GetReferer Failed")
  850. assert.Equal(s.T(), opt.EmptyReferConfiguration, res.EmptyReferConfiguration, "GetReferer Failed")
  851. }
  852. func (s *CosTestSuite) TestAccelerate() {
  853. opt := &cos.BucketPutAccelerateOptions{
  854. Status: "Enabled",
  855. Type: "COS",
  856. }
  857. _, err := s.Client.Bucket.PutAccelerate(context.Background(), opt)
  858. assert.Nil(s.T(), err, "PutAccelerate Failed")
  859. time.Sleep(time.Second)
  860. res, _, err := s.Client.Bucket.GetAccelerate(context.Background())
  861. assert.Nil(s.T(), err, "GetAccelerate Failed")
  862. assert.Equal(s.T(), opt.Status, res.Status, "GetAccelerate Failed")
  863. assert.Equal(s.T(), opt.Type, res.Type, "GetAccelerate Failed")
  864. opt.Status = "Suspended"
  865. _, err = s.Client.Bucket.PutAccelerate(context.Background(), opt)
  866. assert.Nil(s.T(), err, "PutAccelerate Failed")
  867. time.Sleep(time.Second)
  868. res, _, err = s.Client.Bucket.GetAccelerate(context.Background())
  869. assert.Nil(s.T(), err, "GetAccelerate Failed")
  870. assert.Equal(s.T(), opt.Status, res.Status, "GetAccelerate Failed")
  871. assert.Equal(s.T(), opt.Type, res.Type, "GetAccelerate Failed")
  872. }
  873. func (s *CosTestSuite) TestMultiCopy() {
  874. u := "http://" + kRepBucket + "-" + s.Appid + ".cos." + kRepRegion + ".myqcloud.com"
  875. iu, _ := url.Parse(u)
  876. ib := &cos.BaseURL{BucketURL: iu}
  877. c := cos.NewClient(ib, &http.Client{
  878. Transport: &cos.AuthorizationTransport{
  879. SecretID: os.Getenv("COS_SECRETID"),
  880. SecretKey: os.Getenv("COS_SECRETKEY"),
  881. },
  882. })
  883. opt := &cos.BucketPutOptions{
  884. XCosACL: "public-read",
  885. }
  886. // Notice in intranet the bucket host sometimes has i/o timeout problem
  887. r, err := c.Bucket.Put(context.Background(), opt)
  888. if err != nil && r != nil && r.StatusCode == 409 {
  889. fmt.Println("BucketAlreadyOwnedByYou")
  890. } else if err != nil {
  891. assert.Nil(s.T(), err, "PutBucket Failed")
  892. }
  893. source := "test/objectMove1" + time.Now().Format(time.RFC3339)
  894. expected := "test"
  895. f := strings.NewReader(expected)
  896. r, err = c.Object.Put(context.Background(), source, f, nil)
  897. assert.Nil(s.T(), err, "PutObject Failed")
  898. time.Sleep(3 * time.Second)
  899. // Copy file
  900. soruceURL := fmt.Sprintf("%s/%s", iu.Host, source)
  901. dest := "test/objectMove1" + time.Now().Format(time.RFC3339)
  902. _, _, err = s.Client.Object.MultiCopy(context.Background(), dest, soruceURL, nil)
  903. assert.Nil(s.T(), err, "MultiCopy Failed")
  904. // Check content
  905. resp, err := s.Client.Object.Get(context.Background(), dest, nil)
  906. assert.Nil(s.T(), err, "GetObject Failed")
  907. bs, _ := ioutil.ReadAll(resp.Body)
  908. resp.Body.Close()
  909. result := string(bs)
  910. assert.Equal(s.T(), expected, result, "MultiCopy Failed, wrong content")
  911. }
  912. // End of api test
  913. // All methods that begin with "Test" are run as tests within a
  914. // suite.
  915. // In order for 'go test' to run this suite, we need to create
  916. // a normal test function and pass our suite to suite.Run
  917. func TestCosTestSuite(t *testing.T) {
  918. suite.Run(t, new(CosTestSuite))
  919. }
  920. func (s *CosTestSuite) TearDownSuite() {
  921. // Clean the file in bucket
  922. // r, _, err := s.Client.Bucket.ListMultipartUploads(context.Background(), nil)
  923. // assert.Nil(s.T(), err, "ListMultipartUploads Failed")
  924. // for _, p := range r.Uploads {
  925. // // Abort
  926. // _, err = s.Client.Object.AbortMultipartUpload(context.Background(), p.Key, p.UploadID)
  927. // assert.Nil(s.T(), err, "AbortMultipartUpload Failed")
  928. // }
  929. // // Delete objects
  930. // opt := &cos.BucketGetOptions{
  931. // MaxKeys: 500,
  932. // }
  933. // v, _, err := s.Client.Bucket.Get(context.Background(), opt)
  934. // assert.Nil(s.T(), err, "GetBucket Failed")
  935. // for _, c := range v.Contents {
  936. // _, err := s.Client.Object.Delete(context.Background(), c.Key)
  937. // assert.Nil(s.T(), err, "DeleteObject Failed")
  938. // }
  939. // When clean up these infos, can not solve the concurrent test problem
  940. fmt.Println("tear down~")
  941. }