From 679b0d3d6ac1055feb0fca43503446edfbb72b23 Mon Sep 17 00:00:00 2001 From: Wenkai Yin Date: Fri, 22 Mar 2019 17:59:55 +0800 Subject: [PATCH] Convert job status to replication task status This commits converts job status to task status Signed-off-by: Wenkai Yin --- .../service/notifications/jobs/handler.go | 9 +- src/replication/ng/hook/task.go | 40 ++++++++ src/replication/ng/hook/task_test.go | 98 +++++++++++++++++++ 3 files changed, 144 insertions(+), 3 deletions(-) create mode 100644 src/replication/ng/hook/task.go create mode 100644 src/replication/ng/hook/task_test.go diff --git a/src/core/service/notifications/jobs/handler.go b/src/core/service/notifications/jobs/handler.go index ceb5f5a27..d5fe7be58 100644 --- a/src/core/service/notifications/jobs/handler.go +++ b/src/core/service/notifications/jobs/handler.go @@ -24,6 +24,7 @@ import ( "github.com/goharbor/harbor/src/common/utils/log" "github.com/goharbor/harbor/src/core/api" "github.com/goharbor/harbor/src/replication/ng" + "github.com/goharbor/harbor/src/replication/ng/hook" ) var statusMap = map[string]string{ @@ -39,8 +40,9 @@ var statusMap = map[string]string{ // Handler handles reqeust on /service/notifications/jobs/*, which listens to the webhook of jobservice. type Handler struct { api.BaseController - id int64 - status string + id int64 + status string + rawStatus string } // Prepare ... @@ -60,6 +62,7 @@ func (h *Handler) Prepare() { h.Abort("200") return } + h.rawStatus = data.Status status, ok := statusMap[data.Status] if !ok { log.Debugf("drop the job status update event: job id-%d, status-%s", id, status) @@ -92,7 +95,7 @@ func (h *Handler) HandleReplication() { // HandleReplicationTask handles the webhook of replication task func (h *Handler) HandleReplicationTask() { log.Debugf("received replication task status update event: task-%d, status-%s", h.id, h.status) - if err := ng.OperationCtl.UpdateTaskStatus(h.id, h.status); err != nil { + if err := hook.UpdateTask(ng.OperationCtl, h.id, h.rawStatus); err != nil { log.Errorf("Failed to update replication task status, id: %d, status: %s", h.id, h.status) h.HandleInternalServerError(err.Error()) return diff --git a/src/replication/ng/hook/task.go b/src/replication/ng/hook/task.go new file mode 100644 index 000000000..ad8e070d6 --- /dev/null +++ b/src/replication/ng/hook/task.go @@ -0,0 +1,40 @@ +// Copyright Project Harbor Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package hook + +import ( + "github.com/goharbor/harbor/src/jobservice/job" + "github.com/goharbor/harbor/src/replication/ng/dao/models" + "github.com/goharbor/harbor/src/replication/ng/operation" +) + +// UpdateTask update the status of the task +func UpdateTask(ctl operation.Controller, id int64, status string) error { + // convert the job status to task status + s := "" + switch status { + case job.JobStatusPending: + s = models.TaskStatusPending + case job.JobStatusScheduled, job.JobStatusRunning: + s = models.TaskStatusInProgress + case job.JobStatusStopped, job.JobStatusCancelled: + s = models.TaskStatusStopped + case job.JobStatusError: + s = models.TaskStatusFailed + case job.JobStatusSuccess: + s = models.TaskStatusSucceed + } + return ctl.UpdateTaskStatus(id, s) +} diff --git a/src/replication/ng/hook/task_test.go b/src/replication/ng/hook/task_test.go new file mode 100644 index 000000000..547743d45 --- /dev/null +++ b/src/replication/ng/hook/task_test.go @@ -0,0 +1,98 @@ +// Copyright Project Harbor Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package hook + +import ( + "testing" + + "github.com/goharbor/harbor/src/jobservice/job" + "github.com/goharbor/harbor/src/replication/ng/dao/models" + "github.com/goharbor/harbor/src/replication/ng/model" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +type fakedOperationController struct { + status string +} + +func (f *fakedOperationController) StartReplication(*model.Policy, *model.Resource) (int64, error) { + return 0, nil +} +func (f *fakedOperationController) StopReplication(int64) error { + return nil +} +func (f *fakedOperationController) ListExecutions(...*models.ExecutionQuery) (int64, []*models.Execution, error) { + return 0, nil, nil +} +func (f *fakedOperationController) GetExecution(int64) (*models.Execution, error) { + return nil, nil +} +func (f *fakedOperationController) ListTasks(...*models.TaskQuery) (int64, []*models.Task, error) { + return 0, nil, nil +} +func (f *fakedOperationController) GetTask(int64) (*models.Task, error) { + return nil, nil +} +func (f *fakedOperationController) UpdateTaskStatus(id int64, status string, statusCondition ...string) error { + f.status = status + return nil +} +func (f *fakedOperationController) GetTaskLog(int64) ([]byte, error) { + return nil, nil +} + +func TestUpdateTask(t *testing.T) { + mgr := &fakedOperationController{} + cases := []struct { + inputStatus string + expectedStatus string + }{ + { + inputStatus: job.JobStatusPending, + expectedStatus: models.TaskStatusPending, + }, + { + inputStatus: job.JobStatusScheduled, + expectedStatus: models.TaskStatusInProgress, + }, + { + inputStatus: job.JobStatusRunning, + expectedStatus: models.TaskStatusInProgress, + }, + { + inputStatus: job.JobStatusStopped, + expectedStatus: models.TaskStatusStopped, + }, + { + inputStatus: job.JobStatusCancelled, + expectedStatus: models.TaskStatusStopped, + }, + { + inputStatus: job.JobStatusError, + expectedStatus: models.TaskStatusFailed, + }, + { + inputStatus: job.JobStatusSuccess, + expectedStatus: models.TaskStatusSucceed, + }, + } + + for _, c := range cases { + err := UpdateTask(mgr, 1, c.inputStatus) + require.Nil(t, err) + assert.Equal(t, c.expectedStatus, mgr.status) + } +}