Fix bug when scanner is unhealthy

The function GetRegistrationByProject should not return err when Ping
return err.  The return value 'registration' has 'Health' field which
shows the scanner health status.

Resolves: #11051
See also: #9788, #9807

Signed-off-by: qinshaoxuan <qinshaoxuan@baidu.com>
This commit is contained in:
qinshaoxuan 2020-03-17 14:30:39 +08:00
parent da8902da53
commit df9c2bdc46
2 changed files with 29 additions and 3 deletions

View File

@ -242,7 +242,7 @@ func (bc *basicController) GetRegistrationByProject(projectID int64, options ...
}
}
return registration, err
return registration, nil
}
// Ping ...

View File

@ -15,6 +15,7 @@
package scanner
import (
"fmt"
"testing"
"github.com/goharbor/harbor/src/common/models"
@ -46,8 +47,8 @@ func TestController(t *testing.T) {
suite.Run(t, new(ControllerTestSuite))
}
// SetupSuite prepares env for the controller test suite
func (suite *ControllerTestSuite) SetupSuite() {
// SetupTest prepares env for the controller test suite
func (suite *ControllerTestSuite) SetupTest() {
suite.mMgr = &scannertesting.Manager{}
suite.mMeta = new(MockProMetaManager)
@ -227,6 +228,31 @@ func (suite *ControllerTestSuite) TestGetRegistrationByProject() {
assert.Equal(suite.T(), "forUT", r.Name)
}
// TestGetRegistrationByProjectWhenPingError tests GetRegistrationByProject
func (suite *ControllerTestSuite) TestGetRegistrationByProjectWhenPingError() {
m := make(map[string]string, 1)
m[proScannerMetaKey] = "uuid"
// Configured at project level
var pid int64 = 1
suite.sample.UUID = "uuid"
suite.mMeta.On("Get", pid, []string{proScannerMetaKey}).Return(m, nil)
suite.mMgr.On("Get", "uuid").Return(suite.sample, nil)
// Ping error
mc := &v1testing.Client{}
mc.On("GetMetadata").Return(nil, fmt.Errorf("getMetadata error"))
mcp := &v1testing.ClientPool{}
mocktesting.OnAnything(mcp, "Get").Return(mc, nil)
suite.c.clientPool = mcp
r, err := suite.c.GetRegistrationByProject(pid)
require.NoError(suite.T(), err)
assert.Equal(suite.T(), "unhealthy", r.Health)
}
// TestPing ...
func (suite *ControllerTestSuite) TestPing() {
meta, err := suite.c.Ping(suite.sample)