mirror of
https://github.com/outbackdingo/kubernetes.git
synced 2026-01-28 18:19:36 +00:00
pkg/errors is archived and while there is go-errors/errors as an alternative, it lacks wraping methods. kubeadm has specific neends and it's better to implement something minimal locally instead of introducing another depedency. - Implement basic wrapped errors and stack trace support. cmd/kubeadm/app/util/errors. - Remove unused error codes >1. At some point it seems we broke these and 1 was returned for all error types. - Remove the Error type in preflight and separate the printing of '[preflight]' message and the error return from preflight checks. - Print an 'error:' prefix for all errors.
82 lines
1.9 KiB
Go
82 lines
1.9 KiB
Go
/*
|
|
Copyright 2025 The Kubernetes 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 errors
|
|
|
|
import (
|
|
"flag"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestCallStack(t *testing.T) {
|
|
testLeafFunc(t)
|
|
}
|
|
|
|
func testLeafFunc(t *testing.T) {
|
|
e := New("foo")
|
|
s := StackTrace(e)
|
|
if !strings.HasPrefix(s, "k8s.io/kubernetes/cmd/kubeadm/app/util/errors.testLeafFunc") {
|
|
t.Fatalf("unexpected stack trace")
|
|
}
|
|
}
|
|
|
|
func TestHandleError(t *testing.T) {
|
|
_ = flag.CommandLine.String("v", "", "")
|
|
|
|
tests := []struct {
|
|
name string
|
|
vValue string
|
|
errMessage string
|
|
expectedContains []string
|
|
}{
|
|
{
|
|
name: "without stack trace",
|
|
vValue: "1",
|
|
errMessage: "foo",
|
|
expectedContains: []string{"foo"},
|
|
},
|
|
{
|
|
name: "with stack trace",
|
|
vValue: "5",
|
|
errMessage: "bar",
|
|
expectedContains: []string{"bar", "errors.TestHandleError"},
|
|
},
|
|
}
|
|
|
|
for _, tc := range tests {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
_ = flag.CommandLine.Set("v", tc.vValue)
|
|
flag.Parse()
|
|
|
|
err := New(tc.errMessage)
|
|
|
|
handleFunc := func(msg string, code int) {
|
|
if code != defaultErrorExitCode {
|
|
t.Errorf("expected error code: %d", defaultErrorExitCode)
|
|
}
|
|
for _, c := range tc.expectedContains {
|
|
if !strings.Contains(msg, c) {
|
|
t.Errorf("expected error message to contain: %s", c)
|
|
}
|
|
}
|
|
}
|
|
|
|
handleError(err, handleFunc)
|
|
})
|
|
}
|
|
}
|