2020-06-16 11:58:58 +02:00
|
|
|
// 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 task
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/mock"
|
|
|
|
"github.com/stretchr/testify/suite"
|
2022-07-20 05:33:08 +02:00
|
|
|
|
|
|
|
"github.com/goharbor/harbor/src/jobservice/job"
|
|
|
|
"github.com/goharbor/harbor/src/pkg/task/dao"
|
2020-06-16 11:58:58 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type hookHandlerTestSuite struct {
|
|
|
|
suite.Suite
|
|
|
|
handler *HookHandler
|
|
|
|
execDAO *mockExecutionDAO
|
|
|
|
taskDAO *mockTaskDAO
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *hookHandlerTestSuite) SetupTest() {
|
|
|
|
h.execDAO = &mockExecutionDAO{}
|
|
|
|
h.taskDAO = &mockTaskDAO{}
|
|
|
|
h.handler = &HookHandler{
|
|
|
|
taskDAO: h.taskDAO,
|
|
|
|
executionDAO: h.execDAO,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *hookHandlerTestSuite) TestHandle() {
|
|
|
|
// handle check in data
|
2021-05-11 15:19:36 +02:00
|
|
|
checkInProcessorRegistry["test"] = func(ctx context.Context, task *Task, sc *job.StatusChange) (err error) { return nil }
|
2020-09-30 09:14:00 +02:00
|
|
|
defer delete(checkInProcessorRegistry, "test")
|
|
|
|
h.taskDAO.On("List", mock.Anything, mock.Anything).Return([]*dao.Task{
|
|
|
|
{
|
|
|
|
ID: 1,
|
|
|
|
ExecutionID: 1,
|
|
|
|
},
|
2020-06-16 11:58:58 +02:00
|
|
|
}, nil)
|
|
|
|
h.execDAO.On("Get", mock.Anything, mock.Anything).Return(&dao.Execution{
|
|
|
|
ID: 1,
|
|
|
|
VendorType: "test",
|
|
|
|
}, nil)
|
|
|
|
sc := &job.StatusChange{
|
2020-09-30 09:14:00 +02:00
|
|
|
CheckIn: "data",
|
|
|
|
Metadata: &job.StatsInfo{},
|
2020-06-16 11:58:58 +02:00
|
|
|
}
|
2020-09-30 09:14:00 +02:00
|
|
|
err := h.handler.Handle(nil, sc)
|
2020-06-16 11:58:58 +02:00
|
|
|
h.Require().Nil(err)
|
|
|
|
h.taskDAO.AssertExpectations(h.T())
|
|
|
|
h.execDAO.AssertExpectations(h.T())
|
|
|
|
|
|
|
|
// reset mock
|
|
|
|
h.SetupTest()
|
|
|
|
|
|
|
|
// handle status changing
|
2020-09-30 09:14:00 +02:00
|
|
|
h.taskDAO.On("List", mock.Anything, mock.Anything).Return([]*dao.Task{
|
|
|
|
{
|
|
|
|
ID: 1,
|
|
|
|
ExecutionID: 1,
|
|
|
|
},
|
2020-07-29 12:17:11 +02:00
|
|
|
}, nil)
|
2020-06-16 11:58:58 +02:00
|
|
|
h.taskDAO.On("UpdateStatus", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(nil)
|
2020-09-30 09:14:00 +02:00
|
|
|
h.execDAO.On("Get", mock.Anything, mock.Anything).Return(&dao.Execution{
|
|
|
|
ID: 1,
|
|
|
|
VendorType: "test",
|
|
|
|
}, nil)
|
|
|
|
h.execDAO.On("RefreshStatus", mock.Anything, mock.Anything).Return(true, job.RunningStatus.String(), nil)
|
2020-06-16 11:58:58 +02:00
|
|
|
sc = &job.StatusChange{
|
|
|
|
Status: job.SuccessStatus.String(),
|
|
|
|
Metadata: &job.StatsInfo{
|
|
|
|
Revision: time.Now().Unix(),
|
|
|
|
},
|
|
|
|
}
|
2020-09-30 09:14:00 +02:00
|
|
|
err = h.handler.Handle(nil, sc)
|
2020-06-16 11:58:58 +02:00
|
|
|
h.Require().Nil(err)
|
|
|
|
h.taskDAO.AssertExpectations(h.T())
|
2020-09-30 09:14:00 +02:00
|
|
|
h.execDAO.AssertExpectations(h.T())
|
2020-06-16 11:58:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestHookHandlerTestSuite(t *testing.T) {
|
|
|
|
suite.Run(t, &hookHandlerTestSuite{})
|
|
|
|
}
|