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 (
|
|
|
|
"testing"
|
2020-09-22 11:47:04 +02:00
|
|
|
"time"
|
2020-06-16 11:58:58 +02:00
|
|
|
|
|
|
|
"github.com/goharbor/harbor/src/jobservice/job"
|
2022-01-20 02:57:22 +01:00
|
|
|
"github.com/goharbor/harbor/src/lib"
|
2020-06-16 11:58:58 +02:00
|
|
|
"github.com/goharbor/harbor/src/lib/errors"
|
2020-08-06 10:59:38 +02:00
|
|
|
"github.com/goharbor/harbor/src/lib/q"
|
2020-06-16 11:58:58 +02:00
|
|
|
"github.com/goharbor/harbor/src/pkg/task/dao"
|
2021-01-06 09:41:46 +01:00
|
|
|
"github.com/goharbor/harbor/src/testing/lib/orm"
|
2020-06-16 11:58:58 +02:00
|
|
|
"github.com/stretchr/testify/mock"
|
|
|
|
"github.com/stretchr/testify/suite"
|
|
|
|
)
|
|
|
|
|
|
|
|
type executionManagerTestSuite struct {
|
|
|
|
suite.Suite
|
2021-01-06 09:41:46 +01:00
|
|
|
execMgr *executionManager
|
|
|
|
taskMgr *mockTaskManager
|
|
|
|
execDAO *mockExecutionDAO
|
|
|
|
taskDAO *mockTaskDAO
|
|
|
|
ormCreator *orm.Creator
|
2020-06-16 11:58:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (e *executionManagerTestSuite) SetupTest() {
|
|
|
|
e.taskMgr = &mockTaskManager{}
|
|
|
|
e.execDAO = &mockExecutionDAO{}
|
|
|
|
e.taskDAO = &mockTaskDAO{}
|
2021-01-06 09:41:46 +01:00
|
|
|
e.ormCreator = &orm.Creator{}
|
2020-06-16 11:58:58 +02:00
|
|
|
e.execMgr = &executionManager{
|
|
|
|
executionDAO: e.execDAO,
|
|
|
|
taskMgr: e.taskMgr,
|
|
|
|
taskDAO: e.taskDAO,
|
2021-01-06 09:41:46 +01:00
|
|
|
ormCreator: e.ormCreator,
|
2022-01-20 02:57:22 +01:00
|
|
|
wp: lib.NewWorkerPool(10),
|
2020-06-16 11:58:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-06 10:59:38 +02:00
|
|
|
func (e *executionManagerTestSuite) TestCount() {
|
|
|
|
e.execDAO.On("Count", mock.Anything, mock.Anything).Return(int64(10), nil)
|
|
|
|
total, err := e.execMgr.Count(nil, &q.Query{})
|
|
|
|
e.Require().Nil(err)
|
|
|
|
e.Equal(int64(10), total)
|
|
|
|
e.execDAO.AssertExpectations(e.T())
|
|
|
|
}
|
|
|
|
|
2020-06-16 11:58:58 +02:00
|
|
|
func (e *executionManagerTestSuite) TestCreate() {
|
2021-02-05 05:15:15 +01:00
|
|
|
SetExecutionSweeperCount("vendor", 50)
|
|
|
|
|
2020-06-16 11:58:58 +02:00
|
|
|
e.execDAO.On("Create", mock.Anything, mock.Anything).Return(int64(1), nil)
|
2021-01-06 09:41:46 +01:00
|
|
|
e.ormCreator.On("Create").Return(&orm.FakeOrmer{})
|
|
|
|
e.execDAO.On("List", mock.Anything, mock.Anything).Return(nil, nil)
|
2020-06-16 11:58:58 +02:00
|
|
|
id, err := e.execMgr.Create(nil, "vendor", 0, ExecutionTriggerManual,
|
|
|
|
map[string]interface{}{"k": "v"})
|
|
|
|
e.Require().Nil(err)
|
|
|
|
e.Equal(int64(1), id)
|
2021-01-06 09:41:46 +01:00
|
|
|
// sleep to make sure the function in the goroutine run
|
|
|
|
time.Sleep(1 * time.Second)
|
2020-06-16 11:58:58 +02:00
|
|
|
e.execDAO.AssertExpectations(e.T())
|
2021-01-06 09:41:46 +01:00
|
|
|
e.ormCreator.AssertExpectations(e.T())
|
2020-06-16 11:58:58 +02:00
|
|
|
}
|
|
|
|
|
2021-01-08 03:10:31 +01:00
|
|
|
func (e *executionManagerTestSuite) TestUpdateExtraAttrs() {
|
|
|
|
e.execDAO.On("Update", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(nil)
|
|
|
|
err := e.execMgr.UpdateExtraAttrs(nil, 1, map[string]interface{}{"key": "value"})
|
|
|
|
e.Require().Nil(err)
|
|
|
|
e.execDAO.AssertExpectations(e.T())
|
|
|
|
}
|
|
|
|
|
2020-06-16 11:58:58 +02:00
|
|
|
func (e *executionManagerTestSuite) TestMarkDone() {
|
2020-12-10 13:26:54 +01:00
|
|
|
e.execDAO.On("Update", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(nil)
|
2020-06-16 11:58:58 +02:00
|
|
|
err := e.execMgr.MarkDone(nil, 1, "success")
|
|
|
|
e.Require().Nil(err)
|
|
|
|
e.execDAO.AssertExpectations(e.T())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *executionManagerTestSuite) TestMarkError() {
|
2020-12-10 13:26:54 +01:00
|
|
|
e.execDAO.On("Update", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(nil)
|
2020-06-16 11:58:58 +02:00
|
|
|
err := e.execMgr.MarkError(nil, 1, "error")
|
|
|
|
e.Require().Nil(err)
|
|
|
|
e.execDAO.AssertExpectations(e.T())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *executionManagerTestSuite) TestStop() {
|
2021-03-12 12:29:25 +01:00
|
|
|
// the execution contains no tasks and the status is final
|
|
|
|
e.execDAO.On("Get", mock.Anything, mock.Anything).Return(&dao.Execution{
|
|
|
|
ID: 1,
|
|
|
|
Status: job.SuccessStatus.String(),
|
|
|
|
}, nil)
|
|
|
|
e.taskDAO.On("List", mock.Anything, mock.Anything).Return(nil, nil)
|
|
|
|
err := e.execMgr.Stop(nil, 1)
|
|
|
|
e.Require().Nil(err)
|
|
|
|
e.taskDAO.AssertExpectations(e.T())
|
|
|
|
e.execDAO.AssertExpectations(e.T())
|
|
|
|
|
|
|
|
// reset the mocks
|
|
|
|
e.SetupTest()
|
|
|
|
|
2020-09-22 11:47:04 +02:00
|
|
|
// the execution contains no tasks and the status isn't final
|
|
|
|
e.execDAO.On("Get", mock.Anything, mock.Anything).Return(&dao.Execution{
|
|
|
|
ID: 1,
|
|
|
|
Status: job.RunningStatus.String(),
|
|
|
|
}, nil)
|
|
|
|
e.taskDAO.On("List", mock.Anything, mock.Anything).Return(nil, nil)
|
2021-03-12 12:29:25 +01:00
|
|
|
e.execDAO.On("Update", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(nil)
|
|
|
|
err = e.execMgr.Stop(nil, 1)
|
2020-09-22 11:47:04 +02:00
|
|
|
e.Require().Nil(err)
|
|
|
|
e.taskDAO.AssertExpectations(e.T())
|
|
|
|
e.execDAO.AssertExpectations(e.T())
|
|
|
|
|
|
|
|
// reset the mocks
|
|
|
|
e.SetupTest()
|
|
|
|
|
|
|
|
// the execution contains tasks
|
|
|
|
e.execDAO.On("Get", mock.Anything, mock.Anything).Return(&dao.Execution{
|
|
|
|
ID: 1,
|
|
|
|
Status: job.RunningStatus.String(),
|
|
|
|
}, nil)
|
2020-06-16 11:58:58 +02:00
|
|
|
e.taskDAO.On("List", mock.Anything, mock.Anything).Return([]*dao.Task{
|
|
|
|
{
|
|
|
|
ID: 1,
|
|
|
|
ExecutionID: 1,
|
|
|
|
},
|
|
|
|
}, nil)
|
|
|
|
e.taskMgr.On("Stop", mock.Anything, mock.Anything).Return(nil)
|
2020-09-22 11:47:04 +02:00
|
|
|
err = e.execMgr.Stop(nil, 1)
|
2020-06-16 11:58:58 +02:00
|
|
|
e.Require().Nil(err)
|
|
|
|
e.taskDAO.AssertExpectations(e.T())
|
2020-09-22 11:47:04 +02:00
|
|
|
e.execDAO.AssertExpectations(e.T())
|
|
|
|
e.taskMgr.AssertExpectations(e.T())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *executionManagerTestSuite) TestStopAndWait() {
|
|
|
|
// timeout
|
|
|
|
e.execDAO.On("Get", mock.Anything, mock.Anything).Return(&dao.Execution{
|
|
|
|
ID: 1,
|
|
|
|
Status: job.RunningStatus.String(),
|
|
|
|
}, nil)
|
|
|
|
e.taskDAO.On("List", mock.Anything, mock.Anything).Return([]*dao.Task{
|
|
|
|
{
|
|
|
|
ID: 1,
|
|
|
|
ExecutionID: 1,
|
|
|
|
},
|
|
|
|
}, nil)
|
|
|
|
e.taskMgr.On("Stop", mock.Anything, mock.Anything).Return(nil)
|
|
|
|
err := e.execMgr.StopAndWait(nil, 1, 1*time.Second)
|
|
|
|
e.Require().NotNil(err)
|
|
|
|
e.taskDAO.AssertExpectations(e.T())
|
|
|
|
e.execDAO.AssertExpectations(e.T())
|
|
|
|
e.taskMgr.AssertExpectations(e.T())
|
|
|
|
|
|
|
|
// reset mocks
|
|
|
|
e.SetupTest()
|
|
|
|
|
|
|
|
// pass
|
|
|
|
e.execDAO.On("Get", mock.Anything, mock.Anything).Return(&dao.Execution{
|
|
|
|
ID: 1,
|
|
|
|
Status: job.StoppedStatus.String(),
|
|
|
|
}, nil)
|
|
|
|
e.taskDAO.On("List", mock.Anything, mock.Anything).Return([]*dao.Task{
|
|
|
|
{
|
|
|
|
ID: 1,
|
|
|
|
ExecutionID: 1,
|
|
|
|
},
|
|
|
|
}, nil)
|
|
|
|
e.taskMgr.On("Stop", mock.Anything, mock.Anything).Return(nil)
|
|
|
|
err = e.execMgr.StopAndWait(nil, 1, 1*time.Second)
|
|
|
|
e.Require().Nil(err)
|
|
|
|
e.taskDAO.AssertExpectations(e.T())
|
|
|
|
e.execDAO.AssertExpectations(e.T())
|
2020-06-16 11:58:58 +02:00
|
|
|
e.taskMgr.AssertExpectations(e.T())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *executionManagerTestSuite) TestDelete() {
|
|
|
|
// try to delete the execution which contains running tasks
|
|
|
|
e.taskDAO.On("List", mock.Anything, mock.Anything).Return([]*dao.Task{
|
|
|
|
{
|
|
|
|
ID: 1,
|
|
|
|
ExecutionID: 1,
|
|
|
|
Status: job.RunningStatus.String(),
|
|
|
|
},
|
|
|
|
}, nil)
|
|
|
|
err := e.execMgr.Delete(nil, 1)
|
|
|
|
e.Require().NotNil(err)
|
|
|
|
e.True(errors.IsErr(err, errors.PreconditionCode))
|
|
|
|
e.taskDAO.AssertExpectations(e.T())
|
|
|
|
|
|
|
|
// reset the mock
|
|
|
|
e.SetupTest()
|
|
|
|
|
|
|
|
e.taskDAO.On("List", mock.Anything, mock.Anything).Return([]*dao.Task{
|
|
|
|
{
|
|
|
|
ID: 1,
|
|
|
|
ExecutionID: 1,
|
|
|
|
Status: job.SuccessStatus.String(),
|
|
|
|
},
|
|
|
|
}, nil)
|
|
|
|
e.taskDAO.On("Delete", mock.Anything, mock.Anything).Return(nil)
|
|
|
|
e.execDAO.On("Delete", mock.Anything, mock.Anything).Return(nil)
|
|
|
|
err = e.execMgr.Delete(nil, 1)
|
|
|
|
e.Require().Nil(err)
|
|
|
|
e.taskDAO.AssertExpectations(e.T())
|
|
|
|
e.execDAO.AssertExpectations(e.T())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *executionManagerTestSuite) TestGet() {
|
|
|
|
e.execDAO.On("Get", mock.Anything, mock.Anything).Return(&dao.Execution{
|
|
|
|
ID: 1,
|
|
|
|
Status: job.SuccessStatus.String(),
|
|
|
|
}, nil)
|
2020-07-29 12:17:11 +02:00
|
|
|
e.execDAO.On("GetMetrics", mock.Anything, mock.Anything).Return(&dao.Metrics{
|
|
|
|
TaskCount: 1,
|
|
|
|
SuccessTaskCount: 1,
|
|
|
|
}, nil)
|
2020-06-16 11:58:58 +02:00
|
|
|
exec, err := e.execMgr.Get(nil, 1)
|
|
|
|
e.Require().Nil(err)
|
|
|
|
e.Equal(int64(1), exec.ID)
|
|
|
|
e.Equal(job.SuccessStatus.String(), exec.Status)
|
2020-07-29 12:17:11 +02:00
|
|
|
e.Equal(int64(1), exec.Metrics.TaskCount)
|
|
|
|
e.Equal(int64(1), exec.Metrics.SuccessTaskCount)
|
2020-06-16 11:58:58 +02:00
|
|
|
e.execDAO.AssertExpectations(e.T())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *executionManagerTestSuite) TestList() {
|
|
|
|
e.execDAO.On("List", mock.Anything, mock.Anything).Return([]*dao.Execution{
|
|
|
|
{
|
|
|
|
ID: 1,
|
|
|
|
Status: job.SuccessStatus.String(),
|
|
|
|
},
|
|
|
|
}, nil)
|
2020-07-29 12:17:11 +02:00
|
|
|
e.execDAO.On("GetMetrics", mock.Anything, mock.Anything).Return(&dao.Metrics{
|
|
|
|
TaskCount: 1,
|
|
|
|
SuccessTaskCount: 1,
|
|
|
|
}, nil)
|
2020-06-16 11:58:58 +02:00
|
|
|
execs, err := e.execMgr.List(nil, nil)
|
|
|
|
e.Require().Nil(err)
|
|
|
|
e.Require().Len(execs, 1)
|
|
|
|
e.Equal(int64(1), execs[0].ID)
|
|
|
|
e.Equal(job.SuccessStatus.String(), execs[0].Status)
|
2020-07-29 12:17:11 +02:00
|
|
|
e.Equal(int64(1), execs[0].Metrics.TaskCount)
|
|
|
|
e.Equal(int64(1), execs[0].Metrics.SuccessTaskCount)
|
2020-06-16 11:58:58 +02:00
|
|
|
e.execDAO.AssertExpectations(e.T())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestExecutionManagerSuite(t *testing.T) {
|
|
|
|
suite.Run(t, &executionManagerTestSuite{})
|
|
|
|
}
|