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.

389 lines
12 KiB

5 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
5 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
5 years ago
5 years ago
5 years ago
  1. package cos
  2. import (
  3. "context"
  4. "encoding/xml"
  5. "fmt"
  6. "net/http"
  7. "reflect"
  8. "testing"
  9. "github.com/google/uuid"
  10. )
  11. func TestBatchService_CreateJob(t *testing.T) {
  12. setup()
  13. defer teardown()
  14. uuid_str := uuid.New().String()
  15. opt := &BatchCreateJobOptions{
  16. ClientRequestToken: uuid_str,
  17. Description: "test batch",
  18. Manifest: &BatchJobManifest{
  19. Location: &BatchJobManifestLocation{
  20. ETag: "15150651828fa9cdcb8356b6d1c7638b",
  21. ObjectArn: "qcs::cos:ap-chengdu:uid/1250000000:sourcebucket-1250000000/manifests/batch-copy-manifest.csv",
  22. },
  23. Spec: &BatchJobManifestSpec{
  24. Fields: []string{"Bucket", "Key"},
  25. Format: "COSBatchOperations_CSV_V1",
  26. },
  27. },
  28. Operation: &BatchJobOperation{
  29. PutObjectCopy: &BatchJobOperationCopy{
  30. TargetResource: "qcs::cos:ap-chengdu:uid/1250000000:destinationbucket-1250000000",
  31. },
  32. },
  33. Priority: 1,
  34. Report: &BatchJobReport{
  35. Bucket: "qcs::cos:ap-chengdu:uid/1250000000:sourcebucket-1250000000",
  36. Enabled: "true",
  37. Format: "Report_CSV_V1",
  38. Prefix: "job-result",
  39. ReportScope: "AllTasks",
  40. },
  41. RoleArn: "qcs::cam::uin/100000000001:roleName/COS_Batch_QcsRole",
  42. }
  43. mux.HandleFunc("/jobs", func(w http.ResponseWriter, r *http.Request) {
  44. testHeader(t, r, "x-cos-appid", "1250000000")
  45. testMethod(t, r, http.MethodPost)
  46. v := new(BatchCreateJobOptions)
  47. xml.NewDecoder(r.Body).Decode(v)
  48. want := opt
  49. want.XMLName = xml.Name{Local: "CreateJobRequest"}
  50. if !reflect.DeepEqual(v, want) {
  51. t.Errorf("Batch.CreateJob request body: %+v, want %+v", v, want)
  52. }
  53. fmt.Fprint(w, `<?xml version='1.0' encoding='utf-8' ?>
  54. <CreateJobResult>
  55. <JobId>53dc6228-c50b-46f7-8ad7-65e7159f1aae</JobId>
  56. </CreateJobResult>`)
  57. })
  58. headers := &BatchRequestHeaders{
  59. XCosAppid: 1250000000,
  60. }
  61. ref, _, err := client.Batch.CreateJob(context.Background(), opt, headers)
  62. if err != nil {
  63. t.Fatalf("Batch.CreateJob returned error: %v", err)
  64. }
  65. want := &BatchCreateJobResult{
  66. XMLName: xml.Name{Local: "CreateJobResult"},
  67. JobId: "53dc6228-c50b-46f7-8ad7-65e7159f1aae",
  68. }
  69. if !reflect.DeepEqual(ref, want) {
  70. t.Errorf("Batch.CreateJob returned %+v, want %+v", ref, want)
  71. }
  72. }
  73. func TestBatchService_DescribeJob(t *testing.T) {
  74. setup()
  75. defer teardown()
  76. mux.HandleFunc("/jobs/53dc6228-c50b-46f7-8ad7-65e7159f1aae", func(w http.ResponseWriter, r *http.Request) {
  77. testHeader(t, r, "x-cos-appid", "1250000000")
  78. testMethod(t, r, http.MethodGet)
  79. fmt.Fprint(w, `<?xml version='1.0' encoding='utf-8' ?>
  80. <DescribeJobResult>
  81. <Job>
  82. <ConfirmationRequired>false</ConfirmationRequired>
  83. <CreationTime>2019-12-19T18:00:30Z</CreationTime>
  84. <Description>example-job</Description>
  85. <FailureReasons>
  86. <JobFailure>
  87. <FailureCode/>
  88. <FailureReason/>
  89. </JobFailure>
  90. </FailureReasons>
  91. <JobId>53dc6228-c50b-46f7-8ad7-65e7159f1aae</JobId>
  92. <Manifest>
  93. <Location>
  94. <ETag>&quot;15150651828fa9cdcb8356b6d1c7638b&quot;</ETag>
  95. <ObjectArn>qcs::cos:ap-chengdu:uid/1250000000:sourcebucket-1250000000/manifests/batch-copy-manifest.csv</ObjectArn>
  96. </Location>
  97. <Spec>
  98. <Fields>
  99. <member>Bucket</member>
  100. <member>Key</member>
  101. </Fields>
  102. <Format>COSBatchOperations_CSV_V1</Format>
  103. </Spec>
  104. </Manifest>
  105. <Operation>
  106. <COSPutObjectCopy>
  107. <TargetResource>qcs::cos:ap-chengdu:uid/1250000000:destinationbucket-1250000000</TargetResource>
  108. </COSPutObjectCopy>
  109. </Operation>
  110. <Priority>10</Priority>
  111. <ProgressSummary>
  112. <NumberOfTasksFailed>0</NumberOfTasksFailed>
  113. <NumberOfTasksSucceeded>10</NumberOfTasksSucceeded>
  114. <TotalNumberOfTasks>10</TotalNumberOfTasks>
  115. </ProgressSummary>
  116. <Report>
  117. <Bucket>qcs::cos:ap-chengdu:uid/1250000000:sourcebucket-1250000000</Bucket>
  118. <Enabled>true</Enabled>
  119. <Format>Report_CSV_V1</Format>
  120. <Prefix>job-result</Prefix>
  121. <ReportScope>AllTasks</ReportScope>
  122. </Report>
  123. <RoleArn>qcs::cam::uin/100000000001:roleName/COS_Batch_QcsRole</RoleArn>
  124. <Status>Complete</Status>
  125. <StatusUpdateReason>Job complete</StatusUpdateReason>
  126. <TerminationDate>2019-12-19T18:00:42Z</TerminationDate>
  127. </Job>
  128. </DescribeJobResult>`)
  129. })
  130. headers := &BatchRequestHeaders{
  131. XCosAppid: 1250000000,
  132. }
  133. ref, _, err := client.Batch.DescribeJob(context.Background(), "53dc6228-c50b-46f7-8ad7-65e7159f1aae", headers)
  134. if err != nil {
  135. t.Fatalf("Batch.DescribeJob returned error: %v", err)
  136. }
  137. want := &BatchDescribeJobResult{
  138. XMLName: xml.Name{Local: "DescribeJobResult"},
  139. Job: &BatchDescribeJob{
  140. ConfirmationRequired: "false",
  141. CreationTime: "2019-12-19T18:00:30Z",
  142. Description: "example-job",
  143. FailureReasons: &BatchJobFailureReasons{},
  144. JobId: "53dc6228-c50b-46f7-8ad7-65e7159f1aae",
  145. Manifest: &BatchJobManifest{
  146. Location: &BatchJobManifestLocation{
  147. ETag: "\"15150651828fa9cdcb8356b6d1c7638b\"",
  148. ObjectArn: "qcs::cos:ap-chengdu:uid/1250000000:sourcebucket-1250000000/manifests/batch-copy-manifest.csv",
  149. },
  150. Spec: &BatchJobManifestSpec{
  151. Fields: []string{"Bucket", "Key"},
  152. Format: "COSBatchOperations_CSV_V1",
  153. },
  154. },
  155. Operation: &BatchJobOperation{
  156. PutObjectCopy: &BatchJobOperationCopy{
  157. TargetResource: "qcs::cos:ap-chengdu:uid/1250000000:destinationbucket-1250000000",
  158. },
  159. },
  160. Priority: 10,
  161. ProgressSummary: &BatchProgressSummary{
  162. NumberOfTasksFailed: 0,
  163. NumberOfTasksSucceeded: 10,
  164. TotalNumberOfTasks: 10,
  165. },
  166. Report: &BatchJobReport{
  167. Bucket: "qcs::cos:ap-chengdu:uid/1250000000:sourcebucket-1250000000",
  168. Enabled: "true",
  169. Format: "Report_CSV_V1",
  170. Prefix: "job-result",
  171. ReportScope: "AllTasks",
  172. },
  173. RoleArn: "qcs::cam::uin/100000000001:roleName/COS_Batch_QcsRole",
  174. Status: "Complete",
  175. StatusUpdateReason: "Job complete",
  176. TerminationDate: "2019-12-19T18:00:42Z",
  177. },
  178. }
  179. if !reflect.DeepEqual(ref, want) {
  180. t.Errorf("Batch.DescribeJob returned %+v, want %+v", ref, want)
  181. }
  182. }
  183. func TestBatchService_ListJobs(t *testing.T) {
  184. setup()
  185. defer teardown()
  186. mux.HandleFunc("/jobs", func(w http.ResponseWriter, r *http.Request) {
  187. testHeader(t, r, "x-cos-appid", "1250000000")
  188. testMethod(t, r, http.MethodGet)
  189. vs := values{
  190. "maxResults": "2",
  191. }
  192. testFormValues(t, r, vs)
  193. fmt.Fprint(w, `<?xml version='1.0' encoding='utf-8' ?>
  194. <ListJobsResult>
  195. <Jobs>
  196. <member>
  197. <CreationTime>2019-12-19T11:05:40Z</CreationTime>
  198. <Description>example-job</Description>
  199. <JobId>021140d8-67ca-4e89-8089-0de9a1e40943</JobId>
  200. <Operation>COSPutObjectCopy</Operation>
  201. <Priority>10</Priority>
  202. <ProgressSummary>
  203. <NumberOfTasksFailed>0</NumberOfTasksFailed>
  204. <NumberOfTasksSucceeded>10</NumberOfTasksSucceeded>
  205. <TotalNumberOfTasks>10</TotalNumberOfTasks>
  206. </ProgressSummary>
  207. <Status>Complete</Status>
  208. <TerminationDate>2019-12-19T11:05:56Z</TerminationDate>
  209. </member>
  210. <member>
  211. <CreationTime>2019-12-19T11:07:05Z</CreationTime>
  212. <Description>example-job</Description>
  213. <JobId>066d919e-49b9-429e-b844-e17ea7b16421</JobId>
  214. <Operation>COSPutObjectCopy</Operation>
  215. <Priority>10</Priority>
  216. <ProgressSummary>
  217. <NumberOfTasksFailed>0</NumberOfTasksFailed>
  218. <NumberOfTasksSucceeded>10</NumberOfTasksSucceeded>
  219. <TotalNumberOfTasks>10</TotalNumberOfTasks>
  220. </ProgressSummary>
  221. <Status>Complete</Status>
  222. <TerminationDate>2019-12-19T11:07:21Z</TerminationDate>
  223. </member>
  224. </Jobs>
  225. <NextToken>066d919e-49b9-429e-b844-e17ea7b16421</NextToken>
  226. </ListJobsResult>`)
  227. })
  228. opt := &BatchListJobsOptions{
  229. MaxResults: 2,
  230. }
  231. headers := &BatchRequestHeaders{
  232. XCosAppid: 1250000000,
  233. }
  234. ref, _, err := client.Batch.ListJobs(context.Background(), opt, headers)
  235. if err != nil {
  236. t.Fatalf("Batch.DescribeJob returned error: %v", err)
  237. }
  238. want := &BatchListJobsResult{
  239. XMLName: xml.Name{Local: "ListJobsResult"},
  240. Jobs: &BatchListJobs{
  241. Members: []BatchListJobsMember{
  242. {
  243. CreationTime: "2019-12-19T11:05:40Z",
  244. Description: "example-job",
  245. JobId: "021140d8-67ca-4e89-8089-0de9a1e40943",
  246. Operation: "COSPutObjectCopy",
  247. Priority: 10,
  248. ProgressSummary: &BatchProgressSummary{
  249. NumberOfTasksFailed: 0,
  250. NumberOfTasksSucceeded: 10,
  251. TotalNumberOfTasks: 10,
  252. },
  253. Status: "Complete",
  254. TerminationDate: "2019-12-19T11:05:56Z",
  255. },
  256. {
  257. CreationTime: "2019-12-19T11:07:05Z",
  258. Description: "example-job",
  259. JobId: "066d919e-49b9-429e-b844-e17ea7b16421",
  260. Operation: "COSPutObjectCopy",
  261. Priority: 10,
  262. ProgressSummary: &BatchProgressSummary{
  263. NumberOfTasksFailed: 0,
  264. NumberOfTasksSucceeded: 10,
  265. TotalNumberOfTasks: 10,
  266. },
  267. Status: "Complete",
  268. TerminationDate: "2019-12-19T11:07:21Z",
  269. },
  270. },
  271. },
  272. NextToken: "066d919e-49b9-429e-b844-e17ea7b16421",
  273. }
  274. if !reflect.DeepEqual(ref, want) {
  275. t.Errorf("Batch.ListJobs returned %+v, want %+v", ref, want)
  276. }
  277. }
  278. func TestBatchService_UpdateJobsPriority(t *testing.T) {
  279. setup()
  280. defer teardown()
  281. mux.HandleFunc("/jobs/021140d8-67ca-4e89-8089-0de9a1e40943/priority", func(w http.ResponseWriter, r *http.Request) {
  282. testHeader(t, r, "x-cos-appid", "1250000000")
  283. testMethod(t, r, http.MethodPost)
  284. vs := values{
  285. "priority": "10",
  286. }
  287. testFormValues(t, r, vs)
  288. fmt.Fprint(w, `<?xml version='1.0' encoding='utf-8' ?>
  289. <UpdateJobPriorityResult>
  290. <JobId>021140d8-67ca-4e89-8089-0de9a1e40943</JobId>
  291. <Priority>10</Priority>
  292. </UpdateJobPriorityResult>`)
  293. })
  294. opt := &BatchUpdatePriorityOptions{
  295. JobId: "021140d8-67ca-4e89-8089-0de9a1e40943",
  296. Priority: 10,
  297. }
  298. headers := &BatchRequestHeaders{
  299. XCosAppid: 1250000000,
  300. }
  301. ref, _, err := client.Batch.UpdateJobPriority(context.Background(), opt, headers)
  302. if err != nil {
  303. t.Fatalf("Batch.UpdateJobPriority returned error: %v", err)
  304. }
  305. want := &BatchUpdatePriorityResult{
  306. XMLName: xml.Name{Local: "UpdateJobPriorityResult"},
  307. JobId: "021140d8-67ca-4e89-8089-0de9a1e40943",
  308. Priority: 10,
  309. }
  310. if !reflect.DeepEqual(ref, want) {
  311. t.Errorf("Batch.UpdateJobsPriority returned %+v, want %+v", ref, want)
  312. }
  313. }
  314. func TestBatchService_UpdateJobsStatus(t *testing.T) {
  315. setup()
  316. defer teardown()
  317. mux.HandleFunc("/jobs/021140d8-67ca-4e89-8089-0de9a1e40943/status", func(w http.ResponseWriter, r *http.Request) {
  318. testHeader(t, r, "x-cos-appid", "1250000000")
  319. testMethod(t, r, http.MethodPost)
  320. vs := values{
  321. "requestedJobStatus": "Ready",
  322. "statusUpdateReason": "to do",
  323. }
  324. testFormValues(t, r, vs)
  325. fmt.Fprint(w, `<?xml version='1.0' encoding='utf-8' ?>
  326. <UpdateJobStatusResult>
  327. <JobId>021140d8-67ca-4e89-8089-0de9a1e40943</JobId>
  328. <Status>Ready</Status>
  329. <StatusUpdateReason>to do</StatusUpdateReason>
  330. </UpdateJobStatusResult>`)
  331. })
  332. opt := &BatchUpdateStatusOptions{
  333. JobId: "021140d8-67ca-4e89-8089-0de9a1e40943",
  334. RequestedJobStatus: "Ready",
  335. StatusUpdateReason: "to do",
  336. }
  337. headers := &BatchRequestHeaders{
  338. XCosAppid: 1250000000,
  339. }
  340. ref, _, err := client.Batch.UpdateJobStatus(context.Background(), opt, headers)
  341. if err != nil {
  342. t.Fatalf("Batch.UpdateJobStatus returned error: %v", err)
  343. }
  344. want := &BatchUpdateStatusResult{
  345. XMLName: xml.Name{Local: "UpdateJobStatusResult"},
  346. JobId: "021140d8-67ca-4e89-8089-0de9a1e40943",
  347. Status: "Ready",
  348. StatusUpdateReason: "to do",
  349. }
  350. if !reflect.DeepEqual(ref, want) {
  351. t.Errorf("Batch.UpdateJobsStatus returned %+v, want %+v", ref, want)
  352. }
  353. }