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 19, 2024
1 parent 36bbfe5 commit bc58db7
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 80 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
24 changes: 11 additions & 13 deletions src/pkg/p2p/preheat/provider/dragonfly.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,16 @@ 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"

"github.com/olekukonko/tablewriter"
)

Expand Down Expand Up @@ -90,7 +91,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 +225,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 @@ -256,7 +257,8 @@ func (dd *DragonflyDriver) CheckProgress(taskID string) (*PreheatingStatus, erro
successMessage string
errorMessage string
)
state := provider.PreheatingStatusPending

var state string
switch resp.State {
case dragonflyJobPendingState:
state = provider.PreheatingStatusRunning
Expand All @@ -283,26 +285,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
60 changes: 2 additions & 58 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,34 +73,7 @@ func MockDragonflyProvider() *httptest.Server {
}

var resp = &dragonflyJobResponse{
ID: "1",
State: dragonflyJobPendingState,
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
}

bytes, err := json.Marshal(resp)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
_, _ = w.Write([]byte(err.Error()))
return
}

if _, err := w.Write(bytes); err != nil {
w.WriteHeader(http.StatusInternalServerError)
_, _ = w.Write([]byte(err.Error()))
return
}

w.WriteHeader(http.StatusOK)
case fmt.Sprintf("%s/%s", dragonflyJobPath, "2"):
if r.Method != http.MethodGet {
w.WriteHeader(http.StatusNotImplemented)
return
}

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

w.WriteHeader(http.StatusOK)
case fmt.Sprintf("%s/%s", dragonflyJobPath, "3"):
if r.Method != http.MethodGet {
w.WriteHeader(http.StatusNotImplemented)
return
}

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

bytes, err := json.Marshal(resp)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
_, _ = w.Write([]byte(err.Error()))
return
}

if _, err := w.Write(bytes); err != nil {
w.WriteHeader(http.StatusInternalServerError)
_, _ = w.Write([]byte(err.Error()))
return
}

w.WriteHeader(http.StatusOK)
default:
w.WriteHeader(http.StatusNotImplemented)
}
}))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,9 @@ <h3 *ngIf="!isEdit" class="modal-title">
<!-- scope -->
<div class="clr-form-control">
<clr-select-container>
<label class="clr-control-label width-6rem">{{ 'P2P_PROVIDER.SCOPE' | translate }}</label>
<label class="clr-control-label width-6rem">
{{ 'P2P_PROVIDER.SCOPE' | translate }}
</label>
<select
class="width-380"
[disabled]="loading"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,7 @@ export class AddP2pPolicyComponent implements OnInit, OnDestroy {
TRIGGER.SCHEDULED,
TRIGGER.EVENT_BASED,
];
scopes: string[] = [
SCOPE.SINGLE_PEER,
SCOPE.ALL_PEERS,
];
scopes: string[] = [SCOPE.SINGLE_PEER, SCOPE.ALL_PEERS];
enableContentTrust: boolean = false;
private _nameSubject: Subject<string> = new Subject<string>();
private _nameSubscription: Subscription;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,9 @@ export enum SCOPE {
}

export const SCOPE_I18N_MAP = {
'single_peer': 'P2P_PROVIDER.SCOPE_SINGLE_PEER',
'all_peers': 'P2P_PROVIDER.SCOPE_ALL_PEERS',
}
single_peer: 'P2P_PROVIDER.SCOPE_SINGLE_PEER',
all_peers: 'P2P_PROVIDER.SCOPE_ALL_PEERS',
};

export const TIME_OUT: number = 7000;

Expand Down

0 comments on commit bc58db7

Please sign in to comment.