From 120be36fec6555a0c20aa6c5f53bd22a737c2a2c Mon Sep 17 00:00:00 2001 From: He Weiwei Date: Tue, 31 Mar 2020 10:08:22 +0000 Subject: [PATCH] fix(scan): ongoing is true for schedule scan all only when job is running Closes #11289 Signed-off-by: He Weiwei --- src/core/api/scan_all.go | 14 +++++---- src/core/api/scan_all_test.go | 53 +++++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+), 5 deletions(-) diff --git a/src/core/api/scan_all.go b/src/core/api/scan_all.go index fce953e4ee..0c1b38c73d 100644 --- a/src/core/api/scan_all.go +++ b/src/core/api/scan_all.go @@ -127,7 +127,7 @@ func (sc *ScanAllAPI) getMetrics(kind string) { return } - setOngoing(sts, aj.Status) + setOngoing(sts, aj.Status, kind) } // Return empty @@ -155,14 +155,18 @@ func isScanEnabled() (bool, error) { return len(l) > 0, nil } -func setOngoing(stats *all.Stats, st string) { +func setOngoing(stats *all.Stats, jobStatus, jobKind string) { status := job.PendingStatus - if st == cm.JobFinished { + if jobStatus == cm.JobFinished { status = job.SuccessStatus } else { - status = job.Status(strings.ToTitle(st)) + status = job.Status(strings.Title(jobStatus)) } - stats.Ongoing = !status.Final() || stats.Total != stats.Completed + if jobKind == common_job.JobKindPeriodic { + stats.Ongoing = (status == job.RunningStatus) || stats.Total != stats.Completed + } else if jobKind == common_job.JobKindGeneric { + stats.Ongoing = !status.Final() || stats.Total != stats.Completed + } } diff --git a/src/core/api/scan_all_test.go b/src/core/api/scan_all_test.go index 604ec0fe22..71f4983762 100644 --- a/src/core/api/scan_all_test.go +++ b/src/core/api/scan_all_test.go @@ -17,6 +17,9 @@ package api import ( "testing" + common_job "github.com/goharbor/harbor/src/common/job" + cm "github.com/goharbor/harbor/src/common/models" + "github.com/goharbor/harbor/src/pkg/scan/all" "github.com/goharbor/harbor/src/pkg/scan/dao/scanner" sc "github.com/goharbor/harbor/src/pkg/scan/scanner" "github.com/goharbor/harbor/src/testing/apitests/apilib" @@ -80,3 +83,53 @@ func (suite *ScanAllAPITestSuite) TestScanAllGet() { require.NoError(suite.T(), err, "Error occurred while get a scan all job") suite.Equal(200, code, "Get scan all status should be 200") } + +func (suite *ScanAllAPITestSuite) TestSetOngoing() { + { + stats := &all.Stats{} + setOngoing(stats, cm.JobPending, common_job.JobKindPeriodic) + suite.False(stats.Ongoing) + } + + { + stats := &all.Stats{} + setOngoing(stats, cm.JobRunning, common_job.JobKindPeriodic) + suite.True(stats.Ongoing) + } + + { + stats := &all.Stats{} + setOngoing(stats, cm.JobFinished, common_job.JobKindPeriodic) + suite.False(stats.Ongoing) + } + + { + stats := &all.Stats{} + setOngoing(stats, cm.JobError, common_job.JobKindPeriodic) + suite.False(stats.Ongoing) + } + + { + stats := &all.Stats{} + setOngoing(stats, cm.JobPending, common_job.JobKindGeneric) + suite.True(stats.Ongoing) + } + + { + stats := &all.Stats{} + setOngoing(stats, cm.JobRunning, common_job.JobKindGeneric) + suite.True(stats.Ongoing) + } + + { + stats := &all.Stats{} + setOngoing(stats, cm.JobFinished, common_job.JobKindGeneric) + suite.False(stats.Ongoing) + } + + { + stats := &all.Stats{} + setOngoing(stats, cm.JobError, common_job.JobKindGeneric) + suite.False(stats.Ongoing) + } +}