Response an error message when there is incomplete sbom generate job (#20526)

Response an error message when there is uncomplete sbom generate job

Signed-off-by: stonezdj <stone.zhang@broadcom.com>
Co-authored-by: Wang Yan <wangyan@vmware.com>
This commit is contained in:
stonezdj(Daojun Zhang) 2024-05-31 17:20:53 +08:00 committed by GitHub
parent 7645ec7ccc
commit 30767f6612
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 55 additions and 0 deletions

View File

@ -244,6 +244,14 @@ func (h *scanHandler) delete(ctx context.Context, art *artifact.Artifact, mimeTy
if err != nil {
return err
}
// check if any report has running task associate with it
taskMgr := h.TaskMgrFunc()
for _, rpt := range sbomReports {
if !taskMgr.IsTaskFinished(ctx, rpt.UUID) {
return errors.ConflictError(nil).WithMessage("a previous sbom generate process is running")
}
}
for _, rpt := range sbomReports {
if rpt.MimeType != v1.MimeTypeSBOMReport {
continue

View File

@ -219,6 +219,7 @@ func (suite *SBOMTestSuite) TestMakeReportPlaceHolder() {
mock.OnAnything(suite.sbomManager, "Create").Return("uuid", nil).Once()
mock.OnAnything(suite.sbomManager, "Delete").Return(nil).Once()
mock.OnAnything(suite.taskMgr, "ListScanTasksByReportUUID").Return([]*task.Task{{Status: "Success"}}, nil)
mock.OnAnything(suite.taskMgr, "IsTaskFinished").Return(true).Once()
mock.OnAnything(suite.artifactCtl, "Get").Return(art, nil)
mock.OnAnything(suite.artifactCtl, "Delete").Return(nil)
rps, err := suite.handler.MakePlaceHolder(ctx, art, r)

View File

@ -197,6 +197,24 @@ func (_m *mockTaskManager) GetLogByJobID(ctx context.Context, jobID string) ([]b
return r0, r1
}
// IsTaskFinished provides a mock function with given fields: ctx, reportID
func (_m *mockTaskManager) IsTaskFinished(ctx context.Context, reportID string) bool {
ret := _m.Called(ctx, reportID)
if len(ret) == 0 {
panic("no return value specified for IsTaskFinished")
}
var r0 bool
if rf, ok := ret.Get(0).(func(context.Context, string) bool); ok {
r0 = rf(ctx, reportID)
} else {
r0 = ret.Get(0).(bool)
}
return r0
}
// List provides a mock function with given fields: ctx, query
func (_m *mockTaskManager) List(ctx context.Context, query *q.Query) ([]*Task, error) {
ret := _m.Called(ctx, query)

View File

@ -69,6 +69,8 @@ type Manager interface {
ListScanTasksByReportUUID(ctx context.Context, uuid string) (tasks []*Task, err error)
// RetrieveStatusFromTask retrieve status from task
RetrieveStatusFromTask(ctx context.Context, reportID string) string
// IsTaskFinished checks if the scan task is finished by report UUID
IsTaskFinished(ctx context.Context, reportID string) bool
}
// NewManager creates an instance of the default task manager
@ -299,3 +301,11 @@ func (m *manager) RetrieveStatusFromTask(ctx context.Context, reportID string) s
}
return ""
}
func (m *manager) IsTaskFinished(ctx context.Context, reportID string) bool {
status := m.RetrieveStatusFromTask(ctx, reportID)
if len(status) == 0 {
return true
}
return job.Status(status).Final()
}

View File

@ -199,6 +199,24 @@ func (_m *Manager) GetLogByJobID(ctx context.Context, jobID string) ([]byte, err
return r0, r1
}
// IsTaskFinished provides a mock function with given fields: ctx, reportID
func (_m *Manager) IsTaskFinished(ctx context.Context, reportID string) bool {
ret := _m.Called(ctx, reportID)
if len(ret) == 0 {
panic("no return value specified for IsTaskFinished")
}
var r0 bool
if rf, ok := ret.Get(0).(func(context.Context, string) bool); ok {
r0 = rf(ctx, reportID)
} else {
r0 = ret.Get(0).(bool)
}
return r0
}
// List provides a mock function with given fields: ctx, query
func (_m *Manager) List(ctx context.Context, query *q.Query) ([]*task.Task, error) {
ret := _m.Called(ctx, query)