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.

292 lines
8.6 KiB

  1. package cos
  2. import (
  3. "context"
  4. "encoding/xml"
  5. "fmt"
  6. "net/http"
  7. "reflect"
  8. "testing"
  9. )
  10. func TestBucketService_PutInventory(t *testing.T) {
  11. setup()
  12. defer teardown()
  13. opt := &BucketPutInventoryOptions{
  14. XMLName: xml.Name{Local: "InventoryConfiguration"},
  15. ID: "list1",
  16. IsEnabled: "True",
  17. IncludedObjectVersions: "All",
  18. Filter: &BucketInventoryFilter{"myPrefix"},
  19. Schedule: &BucketInventorySchedule{"Daily"},
  20. Destination: &BucketInventoryDestination{
  21. Bucket: "qcs::cos:ap-guangzhou::examplebucket-1250000000",
  22. AccountId: "100000000001",
  23. Prefix: "list1",
  24. Format: "CSV",
  25. Encryption: &BucketInventoryEncryption{},
  26. },
  27. OptionalFields: &BucketInventoryOptionalFields{
  28. BucketInventoryFields: []string{
  29. "Size",
  30. "LastModifiedDate",
  31. "ETag",
  32. "StorageClass",
  33. "IsMultipartUploaded",
  34. "ReplicationStatus",
  35. },
  36. },
  37. }
  38. mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
  39. testMethod(t, r, http.MethodPut)
  40. vs := values{
  41. "inventory": "",
  42. "id": "list1",
  43. }
  44. testFormValues(t, r, vs)
  45. body := &BucketPutInventoryOptions{}
  46. xml.NewDecoder(r.Body).Decode(body)
  47. want := opt
  48. if !reflect.DeepEqual(want, body) {
  49. t.Fatalf("Bucket.PutInventory request\n body: %+v\n, want %+v\n", body, want)
  50. }
  51. })
  52. _, err := client.Bucket.PutInventory(context.Background(), "list1", opt)
  53. if err != nil {
  54. t.Fatalf("Bucket.PutInventory failed, error: %v", err)
  55. }
  56. }
  57. func TestBucketService_GetInventory(t *testing.T) {
  58. setup()
  59. defer teardown()
  60. mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
  61. testMethod(t, r, http.MethodGet)
  62. vs := values{
  63. "inventory": "",
  64. "id": "list1",
  65. }
  66. testFormValues(t, r, vs)
  67. fmt.Fprint(w, `<InventoryConfiguration>
  68. <Id>list1</Id>
  69. <IsEnabled>True</IsEnabled>
  70. <Destination>
  71. <COSBucketDestination>
  72. <Format>CSV</Format>
  73. <Bucket>qcs::cos:ap-guangzhou::examplebucket-1250000000</Bucket>
  74. <Prefix>list1</Prefix>
  75. <AccountId>100000000001</AccountId>
  76. </COSBucketDestination>
  77. </Destination>
  78. <Schedule>
  79. <Frequency>Daily</Frequency>
  80. </Schedule>
  81. <Filter>
  82. <Prefix>myPrefix</Prefix>
  83. </Filter>
  84. <IncludedObjectVersions>All</IncludedObjectVersions>
  85. <OptionalFields>
  86. <Field>Size</Field>
  87. <Field>LastModifiedDate</Field>
  88. <Field>ETag</Field>
  89. <Field>StorageClass</Field>
  90. <Field>IsMultipartUploaded</Field>
  91. <Field>ReplicationStatus</Field>
  92. </OptionalFields>
  93. </InventoryConfiguration>`)
  94. })
  95. res, _, err := client.Bucket.GetInventory(context.Background(), "list1")
  96. if err != nil {
  97. t.Fatalf("Bucket.GetInventory failed, error: %v", err)
  98. }
  99. want := &BucketGetInventoryResult{
  100. XMLName: xml.Name{Local: "InventoryConfiguration"},
  101. ID: "list1",
  102. IsEnabled: "True",
  103. IncludedObjectVersions: "All",
  104. Filter: &BucketInventoryFilter{"myPrefix"},
  105. Schedule: &BucketInventorySchedule{"Daily"},
  106. Destination: &BucketInventoryDestination{
  107. Bucket: "qcs::cos:ap-guangzhou::examplebucket-1250000000",
  108. AccountId: "100000000001",
  109. Prefix: "list1",
  110. Format: "CSV",
  111. },
  112. OptionalFields: &BucketInventoryOptionalFields{
  113. BucketInventoryFields: []string{
  114. "Size",
  115. "LastModifiedDate",
  116. "ETag",
  117. "StorageClass",
  118. "IsMultipartUploaded",
  119. "ReplicationStatus",
  120. },
  121. },
  122. }
  123. if !reflect.DeepEqual(res, want) {
  124. t.Errorf("Bucket.GetInventory returned\n%+v, want\n%+v", res, want)
  125. }
  126. }
  127. func TestBucketService_ListInventory(t *testing.T) {
  128. setup()
  129. defer teardown()
  130. mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
  131. testMethod(t, r, http.MethodGet)
  132. vs := values{
  133. "inventory": "",
  134. }
  135. testFormValues(t, r, vs)
  136. fmt.Fprint(w, `<ListInventoryConfigurationResult>
  137. <InventoryConfiguration>
  138. <Id>list1</Id>
  139. <IsEnabled>True</IsEnabled>
  140. <Destination>
  141. <COSBucketDestination>
  142. <Format>CSV</Format>
  143. <AccountId>1250000000</AccountId>
  144. <Bucket>qcs::cos:ap-beijing::examplebucket-1250000000</Bucket>
  145. <Prefix>list1</Prefix>
  146. <Encryption>
  147. <SSE-COS/>
  148. </Encryption>
  149. </COSBucketDestination>
  150. </Destination>
  151. <Schedule>
  152. <Frequency>Daily</Frequency>
  153. </Schedule>
  154. <Filter>
  155. <Prefix>myPrefix</Prefix>
  156. </Filter>
  157. <IncludedObjectVersions>All</IncludedObjectVersions>
  158. <OptionalFields>
  159. <Field>Size</Field>
  160. <Field>LastModifiedDate</Field>
  161. <Field>ETag</Field>
  162. <Field>StorageClass</Field>
  163. <Field>IsMultipartUpload</Field>
  164. <Field>ReplicationStatus</Field>
  165. </OptionalFields>
  166. </InventoryConfiguration>
  167. <InventoryConfiguration>
  168. <Id>list2</Id>
  169. <IsEnabled>True</IsEnabled>
  170. <Destination>
  171. <COSBucketDestination>
  172. <Format>CSV</Format>
  173. <AccountId>1250000000</AccountId>
  174. <Bucket>qcs::cos:ap-beijing::examplebucket-1250000000</Bucket>
  175. <Prefix>list2</Prefix>
  176. </COSBucketDestination>
  177. </Destination>
  178. <Schedule>
  179. <Frequency>Weekly</Frequency>
  180. </Schedule>
  181. <Filter>
  182. <Prefix>myPrefix2</Prefix>
  183. </Filter>
  184. <IncludedObjectVersions>All</IncludedObjectVersions>
  185. <OptionalFields>
  186. <Field>Size</Field>
  187. <Field>LastModifiedDate</Field>
  188. <Field>ETag</Field>
  189. <Field>StorageClass</Field>
  190. </OptionalFields>
  191. </InventoryConfiguration>
  192. <IsTruncated>false</IsTruncated>
  193. <ContinuationToken>...</ContinuationToken>
  194. <IsTruncated>true</IsTruncated>
  195. <NextContinuationToken>1ueSDFASDF1Tr/XDAFdadEADadf2J/wm36Hy4vbOwM=</NextContinuationToken>
  196. </ListInventoryConfigurationResult>`)
  197. })
  198. res, _, err := client.Bucket.ListInventoryConfigurations(context.Background(), "")
  199. if err != nil {
  200. t.Fatalf("Bucket.ListInventory failed, error: %v", err)
  201. }
  202. want := &ListBucketInventoryConfigResult{
  203. XMLName: xml.Name{Local: "ListInventoryConfigurationResult"},
  204. IsTruncated: true,
  205. ContinuationToken: "...",
  206. NextContinuationToken: "1ueSDFASDF1Tr/XDAFdadEADadf2J/wm36Hy4vbOwM=",
  207. InventoryConfigurations: []BucketListInventoryConfiguartion{
  208. BucketListInventoryConfiguartion{
  209. XMLName: xml.Name{Local: "InventoryConfiguration"},
  210. ID: "list1",
  211. IsEnabled: "True",
  212. IncludedObjectVersions: "All",
  213. Filter: &BucketInventoryFilter{"myPrefix"},
  214. Schedule: &BucketInventorySchedule{"Daily"},
  215. Destination: &BucketInventoryDestination{
  216. Bucket: "qcs::cos:ap-beijing::examplebucket-1250000000",
  217. AccountId: "1250000000",
  218. Prefix: "list1",
  219. Format: "CSV",
  220. Encryption: &BucketInventoryEncryption{},
  221. },
  222. OptionalFields: &BucketInventoryOptionalFields{
  223. BucketInventoryFields: []string{
  224. "Size",
  225. "LastModifiedDate",
  226. "ETag",
  227. "StorageClass",
  228. "IsMultipartUpload",
  229. "ReplicationStatus",
  230. },
  231. },
  232. },
  233. BucketListInventoryConfiguartion{
  234. XMLName: xml.Name{Local: "InventoryConfiguration"},
  235. ID: "list2",
  236. IsEnabled: "True",
  237. IncludedObjectVersions: "All",
  238. Filter: &BucketInventoryFilter{"myPrefix2"},
  239. Schedule: &BucketInventorySchedule{"Weekly"},
  240. Destination: &BucketInventoryDestination{
  241. Bucket: "qcs::cos:ap-beijing::examplebucket-1250000000",
  242. AccountId: "1250000000",
  243. Prefix: "list2",
  244. Format: "CSV",
  245. },
  246. OptionalFields: &BucketInventoryOptionalFields{
  247. BucketInventoryFields: []string{
  248. "Size",
  249. "LastModifiedDate",
  250. "ETag",
  251. "StorageClass",
  252. },
  253. },
  254. },
  255. },
  256. }
  257. if !reflect.DeepEqual(res, want) {
  258. t.Fatalf("Bucket.ListInventory failed, \nwant: %+v\nres: %+v", want, res)
  259. }
  260. }
  261. func TestBucketService_DeleteInventory(t *testing.T) {
  262. setup()
  263. defer teardown()
  264. mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
  265. testMethod(t, r, http.MethodDelete)
  266. vs := values{
  267. "inventory": "",
  268. "id": "list1",
  269. }
  270. testFormValues(t, r, vs)
  271. w.WriteHeader(http.StatusNoContent)
  272. })
  273. _, err := client.Bucket.DeleteInventory(context.Background(), "list1")
  274. if err != nil {
  275. t.Fatalf("Bucket.DeleteInventory returned error: %v", err)
  276. }
  277. }