mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-29 14:37:00 +00:00
Merge pull request #9500 from andronat/make_CheckErr_testable
Make check err testable
This commit is contained in:
commit
07f8d58010
@ -53,6 +53,10 @@ type debugError interface {
|
||||
// This method is generic to the command in use and may be used by non-Kubectl
|
||||
// commands.
|
||||
func CheckErr(err error) {
|
||||
checkErr(err, fatal)
|
||||
}
|
||||
|
||||
func checkErr(err error, handleErr func(string)) {
|
||||
if err == nil {
|
||||
return
|
||||
}
|
||||
@ -61,22 +65,22 @@ func CheckErr(err error) {
|
||||
details := err.(*errors.StatusError).Status().Details
|
||||
prefix := fmt.Sprintf("The %s %q is invalid:", details.Kind, details.Name)
|
||||
errs := statusCausesToAggrError(details.Causes)
|
||||
fatal(MultilineError(prefix, errs))
|
||||
handleErr(MultilineError(prefix, errs))
|
||||
}
|
||||
|
||||
// handle multiline errors
|
||||
if clientcmd.IsConfigurationInvalid(err) {
|
||||
fatal(MultilineError("Error in configuration: ", err))
|
||||
handleErr(MultilineError("Error in configuration: ", err))
|
||||
}
|
||||
if agg, ok := err.(utilerrors.Aggregate); ok && len(agg.Errors()) > 0 {
|
||||
fatal(MultipleErrors("", agg.Errors()))
|
||||
handleErr(MultipleErrors("", agg.Errors()))
|
||||
}
|
||||
|
||||
msg, ok := StandardErrorMessage(err)
|
||||
if !ok {
|
||||
msg = fmt.Sprintf("error: %s\n", err.Error())
|
||||
}
|
||||
fatal(msg)
|
||||
handleErr(msg)
|
||||
}
|
||||
|
||||
func statusCausesToAggrError(scs []api.StatusCause) utilerrors.Aggregate {
|
||||
|
@ -25,7 +25,9 @@ import (
|
||||
"testing"
|
||||
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/errors"
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/runtime"
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/util/fielderrors"
|
||||
)
|
||||
|
||||
func TestMerge(t *testing.T) {
|
||||
@ -399,3 +401,36 @@ func TestReadConfigData(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestCheckInvalidErr(t *testing.T) {
|
||||
tests := []struct {
|
||||
err error
|
||||
expected string
|
||||
}{
|
||||
{
|
||||
errors.NewInvalid("Invalid1", "invalidation", fielderrors.ValidationErrorList{fielderrors.NewFieldInvalid("Cause", "single", "details")}),
|
||||
`Error from server: Invalid1 "invalidation" is invalid: Cause: invalid value 'single': details`,
|
||||
},
|
||||
{
|
||||
errors.NewInvalid("Invalid2", "invalidation", fielderrors.ValidationErrorList{fielderrors.NewFieldInvalid("Cause", "multi1", "details"), fielderrors.NewFieldInvalid("Cause", "multi2", "details")}),
|
||||
`Error from server: Invalid2 "invalidation" is invalid: [Cause: invalid value 'multi1': details, Cause: invalid value 'multi2': details]`,
|
||||
},
|
||||
{
|
||||
errors.NewInvalid("Invalid3", "invalidation", fielderrors.ValidationErrorList{}),
|
||||
`Error from server: Invalid3 "invalidation" is invalid: <nil>`,
|
||||
},
|
||||
}
|
||||
|
||||
var errReturned string
|
||||
errHandle := func(err string) {
|
||||
errReturned = err
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
checkErr(test.err, errHandle)
|
||||
|
||||
if errReturned != test.expected {
|
||||
t.Fatalf("Got: %s, expected: %s", errReturned, test.expected)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user