type_test.go 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. package oss
  2. import (
  3. "net/url"
  4. "sort"
  5. . "gopkg.in/check.v1"
  6. )
  7. type OssTypeSuite struct{}
  8. var _ = Suite(&OssTypeSuite{})
  9. var (
  10. goStr = "go go + go <> go"
  11. chnStr = "试问闲情几许"
  12. goURLStr = url.QueryEscape(goStr)
  13. chnURLStr = url.QueryEscape(chnStr)
  14. )
  15. func (s *OssTypeSuite) TestDecodeDeleteObjectsResult(c *C) {
  16. var res DeleteObjectVersionsResult
  17. err := decodeDeleteObjectsResult(&res)
  18. c.Assert(err, IsNil)
  19. res.DeletedObjectsDetail = []DeletedKeyInfo{DeletedKeyInfo{Key: ""}}
  20. err = decodeDeleteObjectsResult(&res)
  21. c.Assert(err, IsNil)
  22. c.Assert(res.DeletedObjectsDetail[0].Key, Equals, "")
  23. res.DeletedObjectsDetail = []DeletedKeyInfo{DeletedKeyInfo{Key: goURLStr}, DeletedKeyInfo{Key: chnURLStr}}
  24. err = decodeDeleteObjectsResult(&res)
  25. c.Assert(err, IsNil)
  26. c.Assert(res.DeletedObjectsDetail[0].Key, Equals, goStr)
  27. c.Assert(res.DeletedObjectsDetail[1].Key, Equals, chnStr)
  28. }
  29. func (s *OssTypeSuite) TestDecodeListObjectsResult(c *C) {
  30. var res ListObjectsResult
  31. err := decodeListObjectsResult(&res)
  32. c.Assert(err, IsNil)
  33. res = ListObjectsResult{}
  34. err = decodeListObjectsResult(&res)
  35. c.Assert(err, IsNil)
  36. res = ListObjectsResult{Prefix: goURLStr, Marker: goURLStr,
  37. Delimiter: goURLStr, NextMarker: goURLStr,
  38. Objects: []ObjectProperties{{Key: chnURLStr}},
  39. CommonPrefixes: []string{chnURLStr}}
  40. err = decodeListObjectsResult(&res)
  41. c.Assert(err, IsNil)
  42. c.Assert(res.Prefix, Equals, goStr)
  43. c.Assert(res.Marker, Equals, goStr)
  44. c.Assert(res.Delimiter, Equals, goStr)
  45. c.Assert(res.NextMarker, Equals, goStr)
  46. c.Assert(res.Objects[0].Key, Equals, chnStr)
  47. c.Assert(res.CommonPrefixes[0], Equals, chnStr)
  48. }
  49. func (s *OssTypeSuite) TestDecodeListMultipartUploadResult(c *C) {
  50. res := ListMultipartUploadResult{}
  51. err := decodeListMultipartUploadResult(&res)
  52. c.Assert(err, IsNil)
  53. res = ListMultipartUploadResult{Prefix: goURLStr, KeyMarker: goURLStr,
  54. Delimiter: goURLStr, NextKeyMarker: goURLStr,
  55. Uploads: []UncompletedUpload{{Key: chnURLStr}}}
  56. err = decodeListMultipartUploadResult(&res)
  57. c.Assert(err, IsNil)
  58. c.Assert(res.Prefix, Equals, goStr)
  59. c.Assert(res.KeyMarker, Equals, goStr)
  60. c.Assert(res.Delimiter, Equals, goStr)
  61. c.Assert(res.NextKeyMarker, Equals, goStr)
  62. c.Assert(res.Uploads[0].Key, Equals, chnStr)
  63. }
  64. func (s *OssTypeSuite) TestSortUploadPart(c *C) {
  65. parts := []UploadPart{}
  66. sort.Sort(UploadParts(parts))
  67. c.Assert(len(parts), Equals, 0)
  68. parts = []UploadPart{
  69. {PartNumber: 5, ETag: "E5"},
  70. {PartNumber: 1, ETag: "E1"},
  71. {PartNumber: 4, ETag: "E4"},
  72. {PartNumber: 2, ETag: "E2"},
  73. {PartNumber: 3, ETag: "E3"},
  74. }
  75. sort.Sort(UploadParts(parts))
  76. c.Assert(parts[0].PartNumber, Equals, 1)
  77. c.Assert(parts[0].ETag, Equals, "E1")
  78. c.Assert(parts[1].PartNumber, Equals, 2)
  79. c.Assert(parts[1].ETag, Equals, "E2")
  80. c.Assert(parts[2].PartNumber, Equals, 3)
  81. c.Assert(parts[2].ETag, Equals, "E3")
  82. c.Assert(parts[3].PartNumber, Equals, 4)
  83. c.Assert(parts[3].ETag, Equals, "E4")
  84. c.Assert(parts[4].PartNumber, Equals, 5)
  85. c.Assert(parts[4].ETag, Equals, "E5")
  86. }
  87. func (s *OssTypeSuite) TestValidateLifecleRules(c *C) {
  88. expiration := LifecycleExpiration{
  89. Days: 30,
  90. CreatedBeforeDate: "2015-11-11T00:00:00.000Z",
  91. }
  92. rule := LifecycleRule{
  93. ID: "ruleID",
  94. Prefix: "prefix",
  95. Status: "Enabled",
  96. Expiration: &expiration,
  97. }
  98. rules := []LifecycleRule{rule}
  99. err := verifyLifecycleRules(rules)
  100. c.Assert(err, IsNil)
  101. expiration = LifecycleExpiration{
  102. Date: "2015-11-11T00:00:00.000Z",
  103. CreatedBeforeDate: "2015-11-11T00:00:00.000Z",
  104. }
  105. rule = LifecycleRule{
  106. ID: "ruleID",
  107. Prefix: "prefix",
  108. Status: "Enabled",
  109. Expiration: &expiration,
  110. }
  111. rules = []LifecycleRule{rule}
  112. err = verifyLifecycleRules(rules)
  113. c.Assert(err, IsNil)
  114. expiration = LifecycleExpiration{
  115. Days: 0,
  116. CreatedBeforeDate: "",
  117. Date: "",
  118. }
  119. rule = LifecycleRule{
  120. ID: "ruleID",
  121. Prefix: "prefix",
  122. Status: "Enabled",
  123. Expiration: &expiration,
  124. }
  125. rules = []LifecycleRule{rule}
  126. err = verifyLifecycleRules(rules)
  127. c.Assert(err, IsNil)
  128. abortMPU := LifecycleAbortMultipartUpload{
  129. Days: 30,
  130. CreatedBeforeDate: "2015-11-11T00:00:00.000Z",
  131. }
  132. rule = LifecycleRule{
  133. ID: "ruleID",
  134. Prefix: "prefix",
  135. Status: "Enabled",
  136. AbortMultipartUpload: &abortMPU,
  137. }
  138. rules = []LifecycleRule{rule}
  139. err = verifyLifecycleRules(rules)
  140. c.Assert(err, NotNil)
  141. abortMPU = LifecycleAbortMultipartUpload{
  142. Days: 0,
  143. CreatedBeforeDate: "",
  144. }
  145. rule = LifecycleRule{
  146. ID: "ruleID",
  147. Prefix: "prefix",
  148. Status: "Enabled",
  149. AbortMultipartUpload: &abortMPU,
  150. }
  151. rules = []LifecycleRule{rule}
  152. err = verifyLifecycleRules(rules)
  153. c.Assert(err, NotNil)
  154. transition := LifecycleTransition{
  155. Days: 30,
  156. CreatedBeforeDate: "2015-11-11T00:00:00.000Z",
  157. StorageClass: StorageIA,
  158. }
  159. rule = LifecycleRule{
  160. ID: "ruleID",
  161. Prefix: "prefix",
  162. Status: "Enabled",
  163. Transitions: []LifecycleTransition{transition},
  164. }
  165. rules = []LifecycleRule{rule}
  166. err = verifyLifecycleRules(rules)
  167. c.Assert(err, NotNil)
  168. transition = LifecycleTransition{
  169. Days: 0,
  170. CreatedBeforeDate: "",
  171. StorageClass: StorageIA,
  172. }
  173. rule = LifecycleRule{
  174. ID: "ruleID",
  175. Prefix: "prefix",
  176. Status: "Enabled",
  177. Transitions: []LifecycleTransition{transition},
  178. }
  179. rules = []LifecycleRule{rule}
  180. err = verifyLifecycleRules(rules)
  181. c.Assert(err, NotNil)
  182. transition = LifecycleTransition{
  183. Days: 30,
  184. StorageClass: StorageStandard,
  185. }
  186. rule = LifecycleRule{
  187. ID: "ruleID",
  188. Prefix: "prefix",
  189. Status: "Enabled",
  190. Transitions: []LifecycleTransition{transition},
  191. }
  192. rules = []LifecycleRule{rule}
  193. err = verifyLifecycleRules(rules)
  194. c.Assert(err, NotNil)
  195. transition = LifecycleTransition{
  196. CreatedBeforeDate: "2015-11-11T00:00:00.000Z",
  197. StorageClass: StorageStandard,
  198. }
  199. rule = LifecycleRule{
  200. ID: "ruleID",
  201. Prefix: "prefix",
  202. Status: "Enabled",
  203. Transitions: []LifecycleTransition{transition},
  204. }
  205. rules = []LifecycleRule{rule}
  206. err = verifyLifecycleRules(rules)
  207. c.Assert(err, IsNil)
  208. transition1 := LifecycleTransition{
  209. Days: 30,
  210. StorageClass: StorageIA,
  211. }
  212. transition2 := LifecycleTransition{
  213. Days: 60,
  214. StorageClass: StorageArchive,
  215. }
  216. transition3 := LifecycleTransition{
  217. Days: 100,
  218. StorageClass: StorageArchive,
  219. }
  220. rule = LifecycleRule{
  221. ID: "ruleID",
  222. Prefix: "prefix",
  223. Status: "Enabled",
  224. Transitions: []LifecycleTransition{transition1, transition2, transition3},
  225. }
  226. rules = []LifecycleRule{rule}
  227. err = verifyLifecycleRules(rules)
  228. c.Assert(err, IsNil)
  229. rule = LifecycleRule{
  230. ID: "ruleID",
  231. Prefix: "prefix",
  232. Status: "Enabled",
  233. }
  234. rules = []LifecycleRule{rule}
  235. err = verifyLifecycleRules(rules)
  236. c.Assert(err, IsNil)
  237. rules = []LifecycleRule{}
  238. err1 := verifyLifecycleRules(rules)
  239. c.Assert(err1, NotNil)
  240. expiration = LifecycleExpiration{
  241. Days: 30,
  242. }
  243. rule = LifecycleRule{
  244. ID: "ruleID",
  245. Prefix: "prefix",
  246. Status: "Enabled",
  247. Expiration: &expiration,
  248. }
  249. rules = []LifecycleRule{rule}
  250. err = verifyLifecycleRules(rules)
  251. c.Assert(err, IsNil)
  252. expiration = LifecycleExpiration{
  253. CreatedBeforeDate: "2015-11-11T00:00:00.000Z",
  254. }
  255. rule = LifecycleRule{
  256. ID: "ruleID",
  257. Prefix: "prefix",
  258. Status: "Enabled",
  259. Expiration: &expiration,
  260. }
  261. rules = []LifecycleRule{rule}
  262. err = verifyLifecycleRules(rules)
  263. c.Assert(err, IsNil)
  264. abortMPU = LifecycleAbortMultipartUpload{
  265. Days: 30,
  266. }
  267. rule = LifecycleRule{
  268. ID: "ruleID",
  269. Prefix: "prefix",
  270. Status: "Enabled",
  271. AbortMultipartUpload: &abortMPU,
  272. }
  273. rules = []LifecycleRule{rule}
  274. err = verifyLifecycleRules(rules)
  275. c.Assert(err, IsNil)
  276. abortMPU = LifecycleAbortMultipartUpload{
  277. CreatedBeforeDate: "2015-11-11T00:00:00.000Z",
  278. }
  279. rule = LifecycleRule{
  280. ID: "ruleID",
  281. Prefix: "prefix",
  282. Status: "Enabled",
  283. AbortMultipartUpload: &abortMPU,
  284. }
  285. rules = []LifecycleRule{rule}
  286. err = verifyLifecycleRules(rules)
  287. c.Assert(err, IsNil)
  288. expiration = LifecycleExpiration{
  289. Days: 30,
  290. }
  291. abortMPU = LifecycleAbortMultipartUpload{
  292. Days: 30,
  293. }
  294. rule = LifecycleRule{
  295. ID: "ruleID",
  296. Prefix: "prefix",
  297. Status: "Enabled",
  298. Expiration: &expiration,
  299. AbortMultipartUpload: &abortMPU,
  300. }
  301. rules = []LifecycleRule{rule}
  302. err = verifyLifecycleRules(rules)
  303. c.Assert(err, IsNil)
  304. expiration = LifecycleExpiration{
  305. CreatedBeforeDate: "2015-11-11T00:00:00.000Z",
  306. }
  307. abortMPU = LifecycleAbortMultipartUpload{
  308. Days: 30,
  309. }
  310. transition = LifecycleTransition{
  311. Days: 30,
  312. StorageClass: StorageIA,
  313. }
  314. rule = LifecycleRule{
  315. ID: "ruleID",
  316. Prefix: "prefix",
  317. Status: "Enabled",
  318. Expiration: &expiration,
  319. AbortMultipartUpload: &abortMPU,
  320. Transitions: []LifecycleTransition{transition},
  321. }
  322. rules = []LifecycleRule{rule}
  323. err = verifyLifecycleRules(rules)
  324. c.Assert(err, IsNil)
  325. expiration = LifecycleExpiration{
  326. CreatedBeforeDate: "2015-11-11T00:00:00.000Z",
  327. }
  328. abortMPU = LifecycleAbortMultipartUpload{
  329. Days: 30,
  330. }
  331. transition1 = LifecycleTransition{
  332. Days: 30,
  333. StorageClass: StorageIA,
  334. }
  335. transition2 = LifecycleTransition{
  336. Days: 60,
  337. StorageClass: StorageArchive,
  338. }
  339. rule = LifecycleRule{
  340. ID: "ruleID",
  341. Prefix: "prefix",
  342. Status: "Enabled",
  343. Expiration: &expiration,
  344. AbortMultipartUpload: &abortMPU,
  345. Transitions: []LifecycleTransition{transition1, transition2},
  346. }
  347. rules = []LifecycleRule{rule}
  348. err = verifyLifecycleRules(rules)
  349. c.Assert(err, IsNil)
  350. }