mirror of
https://github.com/goharbor/harbor.git
synced 2024-11-22 18:25:56 +01:00
add UT cases for the common error pkg
Signed-off-by: Steven Zou <szou@vmware.com>
This commit is contained in:
parent
afb46188b2
commit
eb8ec49f4f
@ -23,8 +23,8 @@ import (
|
||||
"github.com/goharbor/harbor/src/common/utils"
|
||||
coreutils "github.com/goharbor/harbor/src/core/utils"
|
||||
"github.com/goharbor/harbor/src/jobservice/logger"
|
||||
"github.com/goharbor/harbor/src/pkg/errs"
|
||||
"github.com/goharbor/harbor/src/pkg/scan/api/scan"
|
||||
"github.com/goharbor/harbor/src/pkg/scan/errs"
|
||||
"github.com/goharbor/harbor/src/pkg/scan/report"
|
||||
v1 "github.com/goharbor/harbor/src/pkg/scan/rest/v1"
|
||||
"github.com/pkg/errors"
|
||||
|
@ -28,13 +28,13 @@ import (
|
||||
"github.com/goharbor/harbor/src/core/service/token"
|
||||
"github.com/goharbor/harbor/src/jobservice/job"
|
||||
"github.com/goharbor/harbor/src/jobservice/logger"
|
||||
"github.com/goharbor/harbor/src/pkg/errs"
|
||||
"github.com/goharbor/harbor/src/pkg/robot"
|
||||
"github.com/goharbor/harbor/src/pkg/robot/model"
|
||||
sca "github.com/goharbor/harbor/src/pkg/scan"
|
||||
sc "github.com/goharbor/harbor/src/pkg/scan/api/scanner"
|
||||
"github.com/goharbor/harbor/src/pkg/scan/dao/scan"
|
||||
"github.com/goharbor/harbor/src/pkg/scan/dao/scanner"
|
||||
"github.com/goharbor/harbor/src/pkg/scan/errs"
|
||||
"github.com/goharbor/harbor/src/pkg/scan/report"
|
||||
v1 "github.com/goharbor/harbor/src/pkg/scan/rest/v1"
|
||||
"github.com/google/uuid"
|
||||
@ -127,7 +127,7 @@ func (bc *basicController) Scan(artifact *v1.Artifact) error {
|
||||
|
||||
// In case it does not exist
|
||||
if r == nil {
|
||||
return errs.WithCode(errs.PreconditionFailed, errs.Errorf("no available scanner for project: %s", artifact.NamespaceID))
|
||||
return errs.WithCode(errs.PreconditionFailed, errs.Errorf("no available scanner for project: %d", artifact.NamespaceID))
|
||||
}
|
||||
|
||||
// Check if it is disabled
|
||||
|
@ -34,7 +34,7 @@ func codeTexts(code uint16) string {
|
||||
case Common:
|
||||
return "common"
|
||||
case Conflict:
|
||||
return "not found"
|
||||
return "conflict"
|
||||
case PreconditionFailed:
|
||||
return "Precondition failed"
|
||||
default:
|
||||
@ -56,7 +56,7 @@ type Error struct {
|
||||
|
||||
// Error message
|
||||
func (e *Error) Error() string {
|
||||
emsg := fmt.Sprintf("error: code %d:%s : %s", e.Code, e.TextCode, e.Message)
|
||||
emsg := fmt.Sprintf("error: %d(%s) : %s", e.Code, e.TextCode, e.Message)
|
||||
if e.Cause != nil {
|
||||
emsg = fmt.Sprintf("%s : cause: %s", emsg, e.Cause.Error())
|
||||
}
|
118
src/pkg/scan/errs/error_test.go
Normal file
118
src/pkg/scan/errs/error_test.go
Normal file
@ -0,0 +1,118 @@
|
||||
// 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 errs
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"github.com/stretchr/testify/require"
|
||||
"github.com/stretchr/testify/suite"
|
||||
)
|
||||
|
||||
// ErrorSuite is a test suite for testing Error.
|
||||
type ErrorSuite struct {
|
||||
suite.Suite
|
||||
}
|
||||
|
||||
// TestError is the entry point of ErrorSuite.
|
||||
func TestError(t *testing.T) {
|
||||
suite.Run(t, &ErrorSuite{})
|
||||
}
|
||||
|
||||
// TestErrorNew ...
|
||||
func (suite *ErrorSuite) TestErrorNew() {
|
||||
err := New("error-testing")
|
||||
require.Error(suite.T(), err)
|
||||
|
||||
suite.Equal(true, AsError(err, Common))
|
||||
suite.Condition(func() (success bool) {
|
||||
return -1 != strings.Index(err.Error(), "error-testing")
|
||||
})
|
||||
suite.Condition(func() (success bool) {
|
||||
success = strings.Contains(err.Error(), codeTexts(Common))
|
||||
return
|
||||
})
|
||||
}
|
||||
|
||||
// TestErrorWrap ...
|
||||
func (suite *ErrorSuite) TestErrorWrap() {
|
||||
err := errors.New("error-stack")
|
||||
e := Wrap(err, "wrap-message")
|
||||
require.Error(suite.T(), e)
|
||||
|
||||
suite.Equal(true, AsError(e, Common))
|
||||
suite.Condition(func() (success bool) {
|
||||
success = -1 != strings.Index(e.Error(), "error-stack")
|
||||
return
|
||||
})
|
||||
suite.Condition(func() (success bool) {
|
||||
success = -1 != strings.Index(e.Error(), "wrap-message")
|
||||
return
|
||||
})
|
||||
suite.Condition(func() (success bool) {
|
||||
success = strings.Contains(e.Error(), codeTexts(Common))
|
||||
return
|
||||
})
|
||||
}
|
||||
|
||||
// TestErrorErrorf ...
|
||||
func (suite *ErrorSuite) TestErrorErrorf() {
|
||||
err := Errorf("a=%d", 1000)
|
||||
require.Error(suite.T(), err)
|
||||
|
||||
suite.Equal(true, AsError(err, Common))
|
||||
suite.Condition(func() (success bool) {
|
||||
success = strings.Contains(err.Error(), "a=1000")
|
||||
return
|
||||
})
|
||||
suite.Condition(func() (success bool) {
|
||||
success = strings.Contains(err.Error(), codeTexts(Common))
|
||||
return
|
||||
})
|
||||
}
|
||||
|
||||
// TestErrorString ...
|
||||
func (suite *ErrorSuite) TestErrorString() {
|
||||
err := New("well-formatted-error")
|
||||
require.Error(suite.T(), err)
|
||||
|
||||
str := err.(*Error).String()
|
||||
require.Condition(suite.T(), func() (success bool) {
|
||||
success = len(str) > 0
|
||||
return
|
||||
})
|
||||
|
||||
e := &Error{}
|
||||
er := json.Unmarshal([]byte(str), e)
|
||||
suite.NoError(er)
|
||||
suite.Equal(e.Message, "well-formatted-error")
|
||||
}
|
||||
|
||||
// TestErrorWithCode ...
|
||||
func (suite *ErrorSuite) TestErrorWithCode() {
|
||||
err := New("error-with-code")
|
||||
require.Error(suite.T(), err)
|
||||
|
||||
err = WithCode(Conflict, err)
|
||||
require.Error(suite.T(), err)
|
||||
suite.Equal(true, AsError(err, Conflict))
|
||||
suite.Condition(func() (success bool) {
|
||||
success = strings.Contains(err.Error(), codeTexts(Conflict))
|
||||
return
|
||||
})
|
||||
}
|
@ -18,9 +18,9 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/goharbor/harbor/src/jobservice/job"
|
||||
"github.com/goharbor/harbor/src/pkg/errs"
|
||||
"github.com/goharbor/harbor/src/pkg/q"
|
||||
"github.com/goharbor/harbor/src/pkg/scan/dao/scan"
|
||||
"github.com/goharbor/harbor/src/pkg/scan/errs"
|
||||
"github.com/google/uuid"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
Loading…
Reference in New Issue
Block a user