Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 2 commits into from
May 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/pkg/scan/sbom/sbom.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,14 @@
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")
}

Check warning on line 252 in src/pkg/scan/sbom/sbom.go

View check run for this annotation

Codecov / codecov/patch

src/pkg/scan/sbom/sbom.go#L251-L252

Added lines #L251 - L252 were not covered by tests
}

for _, rpt := range sbomReports {
if rpt.MimeType != v1.MimeTypeSBOMReport {
continue
Expand Down
1 change: 1 addition & 0 deletions src/pkg/scan/sbom/sbom_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
18 changes: 18 additions & 0 deletions src/pkg/task/mock_task_manager_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions src/pkg/task/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@
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
Expand Down Expand Up @@ -299,3 +301,11 @@
}
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()

Check warning on line 310 in src/pkg/task/task.go

View check run for this annotation

Codecov / codecov/patch

src/pkg/task/task.go#L305-L310

Added lines #L305 - L310 were not covered by tests
}
18 changes: 18 additions & 0 deletions src/testing/pkg/task/manager.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading