Skip to content

Commit

Permalink
test: add dragonfly p2p tests
Browse files Browse the repository at this point in the history
Signed-off-by: Gaius <gaius.qi@gmail.com>
  • Loading branch information
gaius-qi authored and chlins committed Sep 18, 2024
1 parent 36bbfe5 commit 8494276
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 17 deletions.
14 changes: 13 additions & 1 deletion src/pkg/p2p/preheat/instance/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package instance

import (
"context"
"encoding/json"

"github.com/goharbor/harbor/src/lib/q"
dao "github.com/goharbor/harbor/src/pkg/p2p/preheat/dao/instance"
Expand Down Expand Up @@ -114,7 +115,18 @@ func (dm *manager) Update(ctx context.Context, inst *provider.Instance, props ..

// Get implements @Manager.Get
func (dm *manager) Get(ctx context.Context, id int64) (*provider.Instance, error) {
return dm.dao.Get(ctx, id)
ins, err := dm.dao.Get(ctx, id)
if err != nil {
return nil, err
}
// mapping auth data to auth info.
if len(ins.AuthData) > 0 {
if err := json.Unmarshal([]byte(ins.AuthData), &ins.AuthInfo); err != nil {
return nil, err
}
}

return ins, nil
}

// Get implements @Manager.GetByName
Expand Down
5 changes: 5 additions & 0 deletions src/pkg/p2p/preheat/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,11 @@ func (j *Job) Run(ctx job.Context, params job.Parameters) error {
return preheatJobRunningError(errors.Errorf("preheat failed: %s", s))
case provider.PreheatingStatusSuccess:
// Finished
// log the message if received message from provider.
if s.Message != "" {
myLogger.Infof("Preheat job finished, message from provider: \n%s", s.Message)
}

return nil
default:
// do nothing, check again
Expand Down
20 changes: 8 additions & 12 deletions src/pkg/p2p/preheat/provider/dragonfly.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ package provider
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"strings"
"time"

"github.com/goharbor/harbor/src/lib"
"github.com/goharbor/harbor/src/lib/errors"
"github.com/goharbor/harbor/src/pkg/p2p/preheat/models/provider"
"github.com/goharbor/harbor/src/pkg/p2p/preheat/provider/auth"
"github.com/goharbor/harbor/src/pkg/p2p/preheat/provider/client"
Expand Down Expand Up @@ -90,7 +90,7 @@ type dragonflyCreateJobRequestArgs struct {

type dragonflyJobResponse struct {
// ID is the job id.
ID string `json:"id"`
ID int `json:"id"`

// CreatedAt is the job created time.
CreatedAt time.Time `json:"created_at"`
Expand Down Expand Up @@ -224,7 +224,7 @@ func (dd *DragonflyDriver) Preheat(preheatingImage *PreheatImage) (*PreheatingSt
}

return &PreheatingStatus{
TaskID: resp.ID,
TaskID: fmt.Sprintf("%d", resp.ID),
Status: provider.PreheatingStatusPending,
StartTime: resp.CreatedAt.Format(time.RFC3339),
FinishTime: resp.UpdatedAt.Format(time.RFC3339),
Expand Down Expand Up @@ -283,26 +283,22 @@ func (dd *DragonflyDriver) CheckProgress(taskID string) (*PreheatingStatus, erro
table.Render()
successMessage = buffer.String()
case dragonflyJobFailureState:
var errs errors.Errors
state = provider.PreheatingStatusFail

var buffer bytes.Buffer
table := tablewriter.NewWriter(&buffer)
table.SetHeader([]string{"Error Message"})
for _, jobState := range resp.Result.JobStates {
table.Append([]string{jobState.Error})
errs = append(errs, errors.New(jobState.Error))
}

table.Render()
if len(resp.Result.JobStates) > 0 {
errorMessage = buffer.String()
if len(errs) > 0 {
errorMessage = errs.Error()
}
default:
state = provider.PreheatingStatusFail
errorMessage = fmt.Sprintf("unknown state: %s", resp.State)
}

return &PreheatingStatus{
TaskID: resp.ID,
TaskID: fmt.Sprintf("%d", resp.ID),
Status: state,
Message: successMessage,
Error: errorMessage,
Expand Down
8 changes: 4 additions & 4 deletions src/pkg/p2p/preheat/provider/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func MockDragonflyProvider() *httptest.Server {
}

var resp = &dragonflyJobResponse{
ID: "1",
ID: 1,
State: dragonflyJobPendingState,
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
Expand All @@ -73,7 +73,7 @@ func MockDragonflyProvider() *httptest.Server {
}

var resp = &dragonflyJobResponse{
ID: "1",
ID: 1,
State: dragonflyJobPendingState,
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
Expand All @@ -100,7 +100,7 @@ func MockDragonflyProvider() *httptest.Server {
}

var resp = &dragonflyJobResponse{
ID: "2",
ID: 2,
State: dragonflyJobSuccessState,
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
Expand All @@ -127,7 +127,7 @@ func MockDragonflyProvider() *httptest.Server {
}

var resp = &dragonflyJobResponse{
ID: "3",
ID: 3,
State: dragonflyJobFailureState,
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
Expand Down

0 comments on commit 8494276

Please sign in to comment.