package cos import ( "context" "encoding/xml" "fmt" "net/http" "reflect" "testing" "github.com/google/uuid" ) func TestBatchService_CreateJob(t *testing.T) { setup() defer teardown() uuid_str := uuid.New().String() opt := &BatchCreateJobOptions{ ClientRequestToken: uuid_str, Description: "test batch", Manifest: &BatchJobManifest{ Location: &BatchJobManifestLocation{ ETag: "15150651828fa9cdcb8356b6d1c7638b", ObjectArn: "qcs::cos:ap-chengdu:uid/1250000000:sourcebucket-1250000000/manifests/batch-copy-manifest.csv", }, Spec: &BatchJobManifestSpec{ Fields: []string{"Bucket", "Key"}, Format: "COSBatchOperations_CSV_V1", }, }, Operation: &BatchJobOperation{ PutObjectCopy: &BatchJobOperationCopy{ TargetResource: "qcs::cos:ap-chengdu:uid/1250000000:destinationbucket-1250000000", }, }, Priority: 1, Report: &BatchJobReport{ Bucket: "qcs::cos:ap-chengdu:uid/1250000000:sourcebucket-1250000000", Enabled: "true", Format: "Report_CSV_V1", Prefix: "job-result", ReportScope: "AllTasks", }, RoleArn: "qcs::cam::uin/100000000001:roleName/COS_Batch_QcsRole", } mux.HandleFunc("/jobs", func(w http.ResponseWriter, r *http.Request) { testHeader(t, r, "x-cos-appid", "1250000000") testMethod(t, r, http.MethodPost) v := new(BatchCreateJobOptions) xml.NewDecoder(r.Body).Decode(v) want := opt want.XMLName = xml.Name{Local: "CreateJobRequest"} if !reflect.DeepEqual(v, want) { t.Errorf("Batch.CreateJob request body: %+v, want %+v", v, want) } fmt.Fprint(w, ` 53dc6228-c50b-46f7-8ad7-65e7159f1aae `) }) headers := &BatchRequestHeaders{ XCosAppid: 1250000000, } ref, _, err := client.Batch.CreateJob(context.Background(), opt, headers) if err != nil { t.Fatalf("Batch.CreateJob returned error: %v", err) } want := &BatchCreateJobResult{ XMLName: xml.Name{Local: "CreateJobResult"}, JobId: "53dc6228-c50b-46f7-8ad7-65e7159f1aae", } if !reflect.DeepEqual(ref, want) { t.Errorf("Batch.CreateJob returned %+v, want %+v", ref, want) } } func TestBatchService_DescribeJob(t *testing.T) { setup() defer teardown() mux.HandleFunc("/jobs/53dc6228-c50b-46f7-8ad7-65e7159f1aae", func(w http.ResponseWriter, r *http.Request) { testHeader(t, r, "x-cos-appid", "1250000000") testMethod(t, r, http.MethodGet) fmt.Fprint(w, ` false 2019-12-19T18:00:30Z example-job 53dc6228-c50b-46f7-8ad7-65e7159f1aae "15150651828fa9cdcb8356b6d1c7638b" qcs::cos:ap-chengdu:uid/1250000000:sourcebucket-1250000000/manifests/batch-copy-manifest.csv Bucket Key COSBatchOperations_CSV_V1 qcs::cos:ap-chengdu:uid/1250000000:destinationbucket-1250000000 10 0 10 10 qcs::cos:ap-chengdu:uid/1250000000:sourcebucket-1250000000 true Report_CSV_V1 job-result AllTasks qcs::cam::uin/100000000001:roleName/COS_Batch_QcsRole Complete Job complete 2019-12-19T18:00:42Z `) }) headers := &BatchRequestHeaders{ XCosAppid: 1250000000, } ref, _, err := client.Batch.DescribeJob(context.Background(), "53dc6228-c50b-46f7-8ad7-65e7159f1aae", headers) if err != nil { t.Fatalf("Batch.DescribeJob returned error: %v", err) } want := &BatchDescribeJobResult{ XMLName: xml.Name{Local: "DescribeJobResult"}, Job: &BatchDescribeJob{ ConfirmationRequired: "false", CreationTime: "2019-12-19T18:00:30Z", Description: "example-job", FailureReasons: &BatchJobFailureReasons{}, JobId: "53dc6228-c50b-46f7-8ad7-65e7159f1aae", Manifest: &BatchJobManifest{ Location: &BatchJobManifestLocation{ ETag: "\"15150651828fa9cdcb8356b6d1c7638b\"", ObjectArn: "qcs::cos:ap-chengdu:uid/1250000000:sourcebucket-1250000000/manifests/batch-copy-manifest.csv", }, Spec: &BatchJobManifestSpec{ Fields: []string{"Bucket", "Key"}, Format: "COSBatchOperations_CSV_V1", }, }, Operation: &BatchJobOperation{ PutObjectCopy: &BatchJobOperationCopy{ TargetResource: "qcs::cos:ap-chengdu:uid/1250000000:destinationbucket-1250000000", }, }, Priority: 10, ProgressSummary: &BatchProgressSummary{ NumberOfTasksFailed: 0, NumberOfTasksSucceeded: 10, TotalNumberOfTasks: 10, }, Report: &BatchJobReport{ Bucket: "qcs::cos:ap-chengdu:uid/1250000000:sourcebucket-1250000000", Enabled: "true", Format: "Report_CSV_V1", Prefix: "job-result", ReportScope: "AllTasks", }, RoleArn: "qcs::cam::uin/100000000001:roleName/COS_Batch_QcsRole", Status: "Complete", StatusUpdateReason: "Job complete", TerminationDate: "2019-12-19T18:00:42Z", }, } if !reflect.DeepEqual(ref, want) { t.Errorf("Batch.DescribeJob returned %+v, want %+v", ref, want) } } func TestBatchService_ListJobs(t *testing.T) { setup() defer teardown() mux.HandleFunc("/jobs", func(w http.ResponseWriter, r *http.Request) { testHeader(t, r, "x-cos-appid", "1250000000") testMethod(t, r, http.MethodGet) vs := values{ "maxResults": "2", } testFormValues(t, r, vs) fmt.Fprint(w, ` 2019-12-19T11:05:40Z example-job 021140d8-67ca-4e89-8089-0de9a1e40943 COSPutObjectCopy 10 0 10 10 Complete 2019-12-19T11:05:56Z 2019-12-19T11:07:05Z example-job 066d919e-49b9-429e-b844-e17ea7b16421 COSPutObjectCopy 10 0 10 10 Complete 2019-12-19T11:07:21Z 066d919e-49b9-429e-b844-e17ea7b16421 `) }) opt := &BatchListJobsOptions{ MaxResults: 2, } headers := &BatchRequestHeaders{ XCosAppid: 1250000000, } ref, _, err := client.Batch.ListJobs(context.Background(), opt, headers) if err != nil { t.Fatalf("Batch.DescribeJob returned error: %v", err) } want := &BatchListJobsResult{ XMLName: xml.Name{Local: "ListJobsResult"}, Jobs: &BatchListJobs{ Members: []BatchListJobsMember{ { CreationTime: "2019-12-19T11:05:40Z", Description: "example-job", JobId: "021140d8-67ca-4e89-8089-0de9a1e40943", Operation: "COSPutObjectCopy", Priority: 10, ProgressSummary: &BatchProgressSummary{ NumberOfTasksFailed: 0, NumberOfTasksSucceeded: 10, TotalNumberOfTasks: 10, }, Status: "Complete", TerminationDate: "2019-12-19T11:05:56Z", }, { CreationTime: "2019-12-19T11:07:05Z", Description: "example-job", JobId: "066d919e-49b9-429e-b844-e17ea7b16421", Operation: "COSPutObjectCopy", Priority: 10, ProgressSummary: &BatchProgressSummary{ NumberOfTasksFailed: 0, NumberOfTasksSucceeded: 10, TotalNumberOfTasks: 10, }, Status: "Complete", TerminationDate: "2019-12-19T11:07:21Z", }, }, }, NextToken: "066d919e-49b9-429e-b844-e17ea7b16421", } if !reflect.DeepEqual(ref, want) { t.Errorf("Batch.ListJobs returned %+v, want %+v", ref, want) } } func TestBatchService_UpdateJobsPriority(t *testing.T) { setup() defer teardown() mux.HandleFunc("/jobs/021140d8-67ca-4e89-8089-0de9a1e40943/priority", func(w http.ResponseWriter, r *http.Request) { testHeader(t, r, "x-cos-appid", "1250000000") testMethod(t, r, http.MethodPost) vs := values{ "priority": "10", } testFormValues(t, r, vs) fmt.Fprint(w, ` 021140d8-67ca-4e89-8089-0de9a1e40943 10 `) }) opt := &BatchUpdatePriorityOptions{ JobId: "021140d8-67ca-4e89-8089-0de9a1e40943", Priority: 10, } headers := &BatchRequestHeaders{ XCosAppid: 1250000000, } ref, _, err := client.Batch.UpdateJobPriority(context.Background(), opt, headers) if err != nil { t.Fatalf("Batch.UpdateJobPriority returned error: %v", err) } want := &BatchUpdatePriorityResult{ XMLName: xml.Name{Local: "UpdateJobPriorityResult"}, JobId: "021140d8-67ca-4e89-8089-0de9a1e40943", Priority: 10, } if !reflect.DeepEqual(ref, want) { t.Errorf("Batch.UpdateJobsPriority returned %+v, want %+v", ref, want) } } func TestBatchService_UpdateJobsStatus(t *testing.T) { setup() defer teardown() mux.HandleFunc("/jobs/021140d8-67ca-4e89-8089-0de9a1e40943/status", func(w http.ResponseWriter, r *http.Request) { testHeader(t, r, "x-cos-appid", "1250000000") testMethod(t, r, http.MethodPost) vs := values{ "requestedJobStatus": "Ready", "statusUpdateReason": "to do", } testFormValues(t, r, vs) fmt.Fprint(w, ` 021140d8-67ca-4e89-8089-0de9a1e40943 Ready to do `) }) opt := &BatchUpdateStatusOptions{ JobId: "021140d8-67ca-4e89-8089-0de9a1e40943", RequestedJobStatus: "Ready", StatusUpdateReason: "to do", } headers := &BatchRequestHeaders{ XCosAppid: 1250000000, } ref, _, err := client.Batch.UpdateJobStatus(context.Background(), opt, headers) if err != nil { t.Fatalf("Batch.UpdateJobStatus returned error: %v", err) } want := &BatchUpdateStatusResult{ XMLName: xml.Name{Local: "UpdateJobStatusResult"}, JobId: "021140d8-67ca-4e89-8089-0de9a1e40943", Status: "Ready", StatusUpdateReason: "to do", } if !reflect.DeepEqual(ref, want) { t.Errorf("Batch.UpdateJobsStatus returned %+v, want %+v", ref, want) } }