update the code of other components influenced by the js code change

Signed-off-by: Steven Zou <szou@vmware.com>
This commit is contained in:
Steven Zou 2019-04-22 20:04:57 +08:00
parent 3937c8b0dc
commit ad68a3f79d
7 changed files with 33 additions and 140 deletions

View File

@ -1,104 +0,0 @@
// 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 impl
import (
"context"
"fmt"
"os"
"testing"
"time"
"github.com/goharbor/harbor/src/jobservice/config"
"github.com/goharbor/harbor/src/jobservice/env"
"github.com/goharbor/harbor/src/jobservice/job"
"github.com/goharbor/harbor/src/jobservice/models"
)
func TestDefaultContext(t *testing.T) {
defaultContext := NewDefaultContext(context.Background())
jobData := env.JobData{
ID: "fake_id",
Name: "DEMO",
Args: make(map[string]interface{}),
ExtraData: make(map[string]interface{}),
}
var opCmdFund job.CheckOPCmdFunc = func() (string, bool) {
return "stop", true
}
var checkInFunc job.CheckInFunc = func(msg string) {
fmt.Println(msg)
}
var launchJobFunc job.LaunchJobFunc = func(req models.JobRequest) (models.JobStats, error) {
return models.JobStats{
Stats: &models.JobStatData{
JobID: "fake_sub_job_id",
Status: "pending",
JobName: "DEMO",
JobKind: job.KindGeneric,
EnqueueTime: time.Now().Unix(),
UpdateTime: time.Now().Unix(),
},
}, nil
}
jobData.ExtraData["opCommandFunc"] = opCmdFund
jobData.ExtraData["checkInFunc"] = checkInFunc
jobData.ExtraData["launchJobFunc"] = launchJobFunc
oldLogConfig := config.DefaultConfig.JobLoggerConfigs
defer func() {
config.DefaultConfig.JobLoggerConfigs = oldLogConfig
}()
logSettings := map[string]interface{}{}
logSettings["base_dir"] = os.TempDir()
config.DefaultConfig.JobLoggerConfigs = []*config.LoggerConfig{
{
Level: "DEBUG",
Name: "FILE",
Settings: logSettings,
},
}
newJobContext, err := defaultContext.Build(jobData)
if err != nil {
t.Fatal(err)
}
cmd, ok := newJobContext.OPCommand()
if !ok || cmd != "stop" {
t.Fatalf("expect op command 'stop' but got %s", cmd)
}
if err := newJobContext.Checkin("hello"); err != nil {
t.Fatal(err)
}
stats, err := newJobContext.LaunchJob(models.JobRequest{})
if err != nil {
t.Fatal(err)
}
if stats.Stats.JobID != "fake_sub_job_id" {
t.Fatalf("expect job id 'fake_sub_job_id' but got %s", stats.Stats.JobID)
}
ctx := newJobContext.SystemContext()
if ctx == nil {
t.Fatal("got nil system context")
}
}

View File

@ -18,6 +18,7 @@ import (
"encoding/json"
"fmt"
"github.com/goharbor/harbor/src/jobservice/job"
"github.com/goharbor/harbor/src/replication/model"
"github.com/goharbor/harbor/src/replication/transfer"
@ -30,7 +31,6 @@ import (
// register the DockerHub adapter
_ "github.com/goharbor/harbor/src/replication/adapter/dockerhub"
// register the Native adapter
"github.com/goharbor/harbor/src/jobservice/job"
_ "github.com/goharbor/harbor/src/replication/adapter/native"
// register the Huawei adapter
_ "github.com/goharbor/harbor/src/replication/adapter/huawei"

View File

@ -90,7 +90,7 @@ func (f *fakedExecutionManager) GetTaskLog(int64) ([]byte, error) {
type fakedScheduler struct{}
func (f *fakedScheduler) Preprocess(src []*model.Resource, dst []*model.Resource) ([]*scheduler.ScheduleItem, error) {
items := []*scheduler.ScheduleItem{}
items := make([]*scheduler.ScheduleItem, 0)
for i, res := range src {
items = append(items, &scheduler.ScheduleItem{
SrcResource: res,
@ -100,7 +100,7 @@ func (f *fakedScheduler) Preprocess(src []*model.Resource, dst []*model.Resource
return items, nil
}
func (f *fakedScheduler) Schedule(items []*scheduler.ScheduleItem) ([]*scheduler.ScheduleResult, error) {
results := []*scheduler.ScheduleResult{}
results := make([]*scheduler.ScheduleResult, 0)
for _, item := range items {
results = append(results, &scheduler.ScheduleResult{
TaskID: item.TaskID,

View File

@ -22,18 +22,19 @@ import (
// UpdateTask update the status of the task
func UpdateTask(ctl operation.Controller, id int64, status string) error {
jobStatus := job.Status(status)
// convert the job status to task status
s := ""
switch status {
case job.JobStatusPending:
switch jobStatus {
case job.PendingStatus:
s = models.TaskStatusPending
case job.JobStatusScheduled, job.JobStatusRunning:
case job.ScheduledStatus, job.RunningStatus:
s = models.TaskStatusInProgress
case job.JobStatusStopped, job.JobStatusCancelled:
case job.StoppedStatus:
s = models.TaskStatusStopped
case job.JobStatusError:
case job.ErrorStatus:
s = models.TaskStatusFailed
case job.JobStatusSuccess:
case job.SuccessStatus:
s = models.TaskStatusSucceed
}
return ctl.UpdateTaskStatus(id, s)

View File

@ -61,31 +61,27 @@ func TestUpdateTask(t *testing.T) {
expectedStatus string
}{
{
inputStatus: job.JobStatusPending,
inputStatus: job.PendingStatus.String(),
expectedStatus: models.TaskStatusPending,
},
{
inputStatus: job.JobStatusScheduled,
inputStatus: job.ScheduledStatus.String(),
expectedStatus: models.TaskStatusInProgress,
},
{
inputStatus: job.JobStatusRunning,
inputStatus: job.RunningStatus.String(),
expectedStatus: models.TaskStatusInProgress,
},
{
inputStatus: job.JobStatusStopped,
inputStatus: job.StoppedStatus.String(),
expectedStatus: models.TaskStatusStopped,
},
{
inputStatus: job.JobStatusCancelled,
expectedStatus: models.TaskStatusStopped,
},
{
inputStatus: job.JobStatusError,
inputStatus: job.ErrorStatus.String(),
expectedStatus: models.TaskStatusFailed,
},
{
inputStatus: job.JobStatusSuccess,
inputStatus: job.SuccessStatus.String(),
expectedStatus: models.TaskStatusSucceed,
},
}

View File

@ -19,20 +19,19 @@ import (
"errors"
"fmt"
"github.com/goharbor/harbor/src/common/job"
common_job "github.com/goharbor/harbor/src/common/job"
cjob "github.com/goharbor/harbor/src/common/job"
"github.com/goharbor/harbor/src/common/job/models"
"github.com/goharbor/harbor/src/jobservice/opm"
"github.com/goharbor/harbor/src/jobservice/job"
"github.com/goharbor/harbor/src/replication/config"
"github.com/goharbor/harbor/src/replication/model"
)
type defaultScheduler struct {
client job.Client
client cjob.Client
}
// NewScheduler returns an instance of Scheduler
func NewScheduler(js job.Client) Scheduler {
func NewScheduler(js cjob.Client) Scheduler {
return &defaultScheduler{
client: js,
}
@ -95,14 +94,14 @@ func (d *defaultScheduler) Schedule(items []*ScheduleItem) ([]*ScheduleResult, e
results = append(results, result)
continue
}
job := &models.JobData{
j := &models.JobData{
Metadata: &models.JobMetadata{
JobKind: job.JobKindGeneric,
JobKind: job.KindGeneric,
},
StatusHook: fmt.Sprintf("%s/service/notifications/jobs/replication/task/%d", config.Config.CoreURL, item.TaskID),
}
job.Name = common_job.Replication
j.Name = job.Replication
src, err := json.Marshal(item.SrcResource)
if err != nil {
result.Error = err
@ -115,11 +114,11 @@ func (d *defaultScheduler) Schedule(items []*ScheduleItem) ([]*ScheduleResult, e
results = append(results, result)
continue
}
job.Parameters = map[string]interface{}{
j.Parameters = map[string]interface{}{
"src_resource": string(src),
"dst_resource": string(dest),
}
id, joberr := d.client.SubmitJob(job)
id, joberr := d.client.SubmitJob(j)
if joberr != nil {
result.Error = joberr
results = append(results, result)
@ -133,7 +132,7 @@ func (d *defaultScheduler) Schedule(items []*ScheduleItem) ([]*ScheduleResult, e
// Stop the transfer job
func (d *defaultScheduler) Stop(id string) error {
err := d.client.PostAction(id, opm.CtlCommandStop)
err := d.client.PostAction(id, string(job.StopCommand))
if err != nil {
return err
}

View File

@ -19,10 +19,11 @@ import (
"net/http"
"time"
common_http "github.com/goharbor/harbor/src/common/http"
commonHttp "github.com/goharbor/harbor/src/common/http"
"github.com/goharbor/harbor/src/common/job"
job_models "github.com/goharbor/harbor/src/common/job/models"
jobModels "github.com/goharbor/harbor/src/common/job/models"
"github.com/goharbor/harbor/src/common/utils/log"
jsJob "github.com/goharbor/harbor/src/jobservice/job"
"github.com/goharbor/harbor/src/replication/config"
"github.com/goharbor/harbor/src/replication/dao"
"github.com/goharbor/harbor/src/replication/dao/models"
@ -61,13 +62,13 @@ func (s *scheduler) Schedule(policyID int64, cron string) error {
log.Debugf("the schedule job record %d added", id)
statusHookURL := fmt.Sprintf("%s/service/notifications/jobs/replication/%d", config.Config.CoreURL, id)
jobID, err := s.jobservice.SubmitJob(&job_models.JobData{
Name: job.ReplicationScheduler,
jobID, err := s.jobservice.SubmitJob(&jobModels.JobData{
Name: jsJob.ReplicationScheduler,
Parameters: map[string]interface{}{
"url": config.Config.CoreURL,
"policy_id": policyID,
},
Metadata: &job_models.JobMetadata{
Metadata: &jobModels.JobMetadata{
JobKind: job.JobKindPeriodic,
Cron: cron,
},
@ -103,7 +104,7 @@ func (s *scheduler) Unschedule(policyID int64) error {
if err = s.jobservice.PostAction(sj.JobID, job.JobActionStop); err != nil {
// if the job specified by jobID is not found in jobservice, just delete
// the record from database
if e, ok := err.(*common_http.Error); !ok || e.Code != http.StatusNotFound {
if e, ok := err.(*commonHttp.Error); !ok || e.Code != http.StatusNotFound {
return err
}
log.Debugf("the stop action for schedule job %s submitted to the jobservice", sj.JobID)