From 8760213279b64e154924ab568636e624dfa7ba2c Mon Sep 17 00:00:00 2001 From: Ted Guan Date: Mon, 11 May 2020 18:29:01 +0800 Subject: [PATCH] release 2.0 webhook replication fix (#11882) * Core panic fix when triggering a webhook of docker-registry replication Signed-off-by: guanxiatao * Add UT for webhook when replicating with docker registry Signed-off-by: guanxiatao --- .../handler/webhook/artifact/replication.go | 4 ++++ .../webhook/artifact/replication_test.go | 24 ++++++++++++++++--- 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/src/controller/event/handler/webhook/artifact/replication.go b/src/controller/event/handler/webhook/artifact/replication.go index b7c1f809f..c4ac1aa6a 100644 --- a/src/controller/event/handler/webhook/artifact/replication.go +++ b/src/controller/event/handler/webhook/artifact/replication.go @@ -201,6 +201,10 @@ func constructReplicationPayload(event *event.ReplicationEvent) (*model.Payload, } func getMetadataFromResource(resource string) (namespace, nameAndTag string) { + // Usually resource format likes 'library/busybox:v1', but it could be 'busybox:v1' in docker registry meta := strings.Split(resource, "/") + if len(meta) == 1 { + return "", meta[0] + } return meta[0], meta[1] } diff --git a/src/controller/event/handler/webhook/artifact/replication_test.go b/src/controller/event/handler/webhook/artifact/replication_test.go index 5ddc38ec8..46ff1116d 100644 --- a/src/controller/event/handler/webhook/artifact/replication_test.go +++ b/src/controller/event/handler/webhook/artifact/replication_test.go @@ -92,7 +92,15 @@ func (f *fakedReplicationMgr) GetExecution(int64) (*daoModels.Execution, error) func (f *fakedReplicationMgr) ListTasks(...*daoModels.TaskQuery) (int64, []*daoModels.Task, error) { return 0, nil, nil } -func (f *fakedReplicationMgr) GetTask(int64) (*daoModels.Task, error) { +func (f *fakedReplicationMgr) GetTask(id int64) (*daoModels.Task, error) { + if id == 1 { + return &daoModels.Task{ + ExecutionID: 1, + // project info not included when replicating with docker registry + SrcResource: "alpine:[v1]", + DstResource: "gxt/alpine:[v1] ", + }, nil + } return &daoModels.Task{ ExecutionID: 1, SrcResource: "library/alpine:[v1]", @@ -254,14 +262,14 @@ func TestReplicationHandler_Handle(t *testing.T) { wantErr bool }{ { - name: "ImagePreprocessHandler Want Error 1", + name: "ReplicationHandler Want Error 1", args: args{ data: "", }, wantErr: true, }, { - name: "ImagePreprocessHandler 1", + name: "ReplicationHandler 1", args: args{ data: &event.ReplicationEvent{ OccurAt: time.Now(), @@ -269,6 +277,16 @@ func TestReplicationHandler_Handle(t *testing.T) { }, wantErr: false, }, + { + name: "ReplicationHandler with docker registry", + args: args{ + data: &event.ReplicationEvent{ + OccurAt: time.Now(), + ReplicationTaskID: 1, + }, + }, + wantErr: false, + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) {