diff options
| -rw-r--r-- | cloud/cmd/cloudcore/app/server.go | 10 | ||||
| -rw-r--r-- | edge/cmd/edgecore/app/server.go | 12 | ||||
| -rw-r--r-- | edgesite/cmd/edgesite/app/server.go | 10 | ||||
| -rw-r--r-- | keadm/cmd/keadm/keadm.go | 7 | ||||
| -rw-r--r-- | pkg/util/uitl_test.go | 145 | ||||
| -rw-r--r-- | pkg/util/util.go | 40 |
6 files changed, 99 insertions, 125 deletions
diff --git a/cloud/cmd/cloudcore/app/server.go b/cloud/cmd/cloudcore/app/server.go index 2fd7e7003..2d82ad436 100644 --- a/cloud/cmd/cloudcore/app/server.go +++ b/cloud/cmd/cloudcore/app/server.go @@ -2,7 +2,6 @@ package app import ( "fmt" - "os" "github.com/spf13/cobra" "k8s.io/apiserver/pkg/util/term" @@ -40,19 +39,16 @@ kubernetes controller which manages devices so that the device metadata/status d flag.PrintFlags(cmd.Flags()) if errs := opts.Validate(); len(errs) > 0 { - util.PrintByLine(os.Stderr, errs) - os.Exit(1) + klog.Fatal(util.SpliceErrors(errs)) } config, err := opts.Config() if err != nil { - util.PrintByLine(os.Stderr, err) - os.Exit(1) + klog.Fatal(err) } if errs := validation.ValidateCloudCoreConfiguration(config); len(errs) > 0 { - util.PrintByLine(os.Stderr, errs) - os.Exit(1) + klog.Fatal(util.SpliceErrors(errs)) } // To help debugging, immediately log version diff --git a/edge/cmd/edgecore/app/server.go b/edge/cmd/edgecore/app/server.go index d64a82230..017a61ecf 100644 --- a/edge/cmd/edgecore/app/server.go +++ b/edge/cmd/edgecore/app/server.go @@ -54,19 +54,16 @@ offering HTTP client capabilities to components of cloud to reach HTTP servers r flag.PrintFlags(cmd.Flags()) if errs := opts.Validate(); len(errs) > 0 { - util.PrintByLine(os.Stderr, errs) - os.Exit(1) + klog.Fatal(util.SpliceErrors(errs)) } config, err := opts.Config() if err != nil { - util.PrintByLine(os.Stderr, err) - os.Exit(1) + klog.Fatal(err) } if errs := validation.ValidateEdgeCoreConfiguration(config); len(errs) > 0 { - util.PrintByLine(os.Stderr, errs) - os.Exit(1) + klog.Fatal(util.SpliceErrors(errs)) } // To help debugging, immediately log version @@ -77,8 +74,7 @@ offering HTTP client capabilities to components of cloud to reach HTTP servers r if checkEnv != "false" { // Check running environment before run edge core if err := environmentCheck(); err != nil { - klog.Errorf("Failed to check the running environment: %v", err) - os.Exit(1) + klog.Fatal(fmt.Errorf("Failed to check the running environment: %v", err)) } } diff --git a/edgesite/cmd/edgesite/app/server.go b/edgesite/cmd/edgesite/app/server.go index 4f864e783..6be60eec0 100644 --- a/edgesite/cmd/edgesite/app/server.go +++ b/edgesite/cmd/edgesite/app/server.go @@ -2,7 +2,6 @@ package app import ( "fmt" - "os" "github.com/spf13/cobra" "k8s.io/apiserver/pkg/util/term" @@ -40,19 +39,16 @@ runs on edge nodes and manages containerized applications.`, flag.PrintFlags(cmd.Flags()) if errs := opts.Validate(); len(errs) > 0 { - util.PrintByLine(os.Stderr, errs) - os.Exit(1) + klog.Fatal(util.SpliceErrors(errs)) } config, err := opts.Config() if err != nil { - util.PrintByLine(os.Stderr, err) - os.Exit(1) + klog.Fatal(err) } if errs := validation.ValidateEdgeSiteConfiguration(config); len(errs) > 0 { - util.PrintByLine(os.Stderr, errs) - os.Exit(1) + klog.Fatal(util.SpliceErrors(errs)) } // To help debugging, immediately log version diff --git a/keadm/cmd/keadm/keadm.go b/keadm/cmd/keadm/keadm.go index 01d0b935e..559452b3d 100644 --- a/keadm/cmd/keadm/keadm.go +++ b/keadm/cmd/keadm/keadm.go @@ -17,16 +17,13 @@ limitations under the License. package main import ( - "os" + "k8s.io/klog" "github.com/kubeedge/kubeedge/keadm/cmd/keadm/app" - "github.com/kubeedge/kubeedge/pkg/util" ) func main() { if err := app.Run(); err != nil { - util.PrintByLine(os.Stderr, err) - os.Exit(1) + klog.Fatal(err) } - os.Exit(0) } diff --git a/pkg/util/uitl_test.go b/pkg/util/uitl_test.go index ef315319b..df50e3c28 100644 --- a/pkg/util/uitl_test.go +++ b/pkg/util/uitl_test.go @@ -1,107 +1,88 @@ package util import ( - "bytes" "errors" "fmt" - "io" - "os" + "k8s.io/apimachinery/pkg/util/validation/field" "strings" "testing" ) -func TestPrintByLine(t *testing.T) { - err1 := errors.New("This is error 1. ") - err2 := errors.New("This is error 2. ") - err3 := errors.New("This is error 3. ") +func TestSpliceErrors(t *testing.T) { + err1 := errors.New("this is error 1") + err2 := errors.New("this is error 2") + err3 := errors.New("this is error 3") - // case 1: slice stderr - const sliceHead = "error: [\n" - var sliceLine1 = fmt.Sprintf(" %s\n", err1) - var sliceLine2 = fmt.Sprintf(" %s\n", err2) - var sliceLine3 = fmt.Sprintf(" %s\n", err3) - const sliceTail = "]\n" + const head = "[\n" + var line1 = fmt.Sprintf(" %s\n", err1) + var line2 = fmt.Sprintf(" %s\n", err2) + var line3 = fmt.Sprintf(" %s\n", err3) + const tail = "]\n" - slice := []error{err1, err2, err3} - outSlice := helpGenStderrString(func() { - PrintByLine(os.Stderr, slice) - }) + errList := field.ErrorList{} + errList = append(errList, + field.InternalError(field.NewPath("test path 1"), err1), + field.InternalError(field.NewPath("test path 2"), err2), + field.InternalError(field.NewPath("test path 3"), err3), + ) - if strings.Index(outSlice, sliceHead) != 0 || - strings.Index(outSlice, sliceLine1) != len(sliceHead) || - strings.Index(outSlice, sliceLine2) != len(sliceHead+sliceLine1) || - strings.Index(outSlice, sliceLine3) != len(sliceHead+sliceLine1+sliceLine2) || - strings.Index(outSlice, sliceTail) != len(sliceHead+sliceLine1+sliceLine2+sliceLine3) { - t.Error("The func format the slice errors unexpected.") + // case 1: none error + if SpliceErrors("") != "" || + SpliceErrors("test text") != "" || + SpliceErrors([]int{1}) != "" || + SpliceErrors([]int{1, 2, 3}) != "" { + t.Error("the func format the none error unexpected") return } - // case 2: map stdout - m := map[int]error{1: err1, 2: err2} - outMap := helpGenStdoutString(func() { - PrintByLine(os.Stdout, m) - }) - mapHead := "[\n" - var mapMiddle []string - for k, v := range m { - mapMiddle = append(mapMiddle, fmt.Sprintf(" %v: %v\n", k, v)) - } - mapTail := "]\n" - if strings.Index(outMap, mapHead) != 0 || - strings.Index(outMap, mapTail) != len(mapHead+mapMiddle[0]+mapMiddle[1]) || - (strings.Index(outMap, mapMiddle[0]) != len(mapHead) && strings.Index(outMap, mapMiddle[0]) != len(mapHead+mapMiddle[1])) { - t.Error("The func format the map errors unexpected.") + // case 2: single error + singleOutput := SpliceErrors(err1) + if singleOutput != err1.Error() { + t.Error("the func format the single error unexpected") return } - // case 3: error stderr - outError := helpGenStderrString(func() { - PrintByLine(os.Stderr, err1) - }) - if outError != fmt.Sprintf("error: %v\n", err1) { - t.Error("The func format the single error unexpected.") + // case 3-1: single element error slice + sliceOutput := SpliceErrors([]error{err1}) + if sliceOutput != err1.Error() { + t.Error("the func format the single element error slice unexpected") return } -} - -func helpGenStderrString(f func()) string { - old := os.Stderr - r, w, _ := os.Pipe() - os.Stderr = w - - f() - - outC := make(chan string) - go func() { - var buf bytes.Buffer - io.Copy(&buf, r) - outC <- buf.String() - }() - - w.Close() - os.Stderr = old - out := <-outC - - return out -} - -func helpGenStdoutString(f func()) string { - old := os.Stdout - r, w, _ := os.Pipe() - os.Stdout = w - f() + // case 3-2: multiple elements error slice + sliceOutput = SpliceErrors([]error{err1, err2, err3}) + if strings.Index(sliceOutput, head) != 0 || + strings.Index(sliceOutput, line1) != len(head) || + strings.Index(sliceOutput, line2) != len(head+line1) || + strings.Index(sliceOutput, line3) != len(head+line1+line2) || + strings.Index(sliceOutput, tail) != len(head+line1+line2+line3) { + t.Error("the func format the multiple elements error slice unexpected") + return + } - outC := make(chan string) - go func() { - var buf bytes.Buffer - io.Copy(&buf, r) - outC <- buf.String() - }() + // case 4: single complex error + if SpliceErrors(errList[0]) != errList[0].Error() { + t.Error("the func format the single complex error unexpected") + return + } - w.Close() - os.Stdout = old - out := <-outC + // case 5-1: single element complex error slice + if SpliceErrors(field.ErrorList{errList[0]}) != errList[0].Error() { + t.Error("the func format the single element complex error slice unexpected") + return + } - return out + // case 5-2: multiple element complex error slice + cpx1 := fmt.Sprintf(" %s\n", errList[0].Error()) + cpx2 := fmt.Sprintf(" %s\n", errList[1].Error()) + cpx3 := fmt.Sprintf(" %s\n", errList[2].Error()) + complexOutput := SpliceErrors(errList) + if strings.Index(complexOutput, head) != 0 || + strings.Index(complexOutput, cpx1) != len(head) || + strings.Index(complexOutput, cpx2) != len(head+cpx1) || + strings.Index(complexOutput, cpx3) != len(head+cpx1+cpx2) || + strings.Index(complexOutput, tail) != len(head+cpx1+cpx2+cpx3) { + t.Error("the func format the multiple element complex error slice unexpected") + return + } } diff --git a/pkg/util/util.go b/pkg/util/util.go index 4707fe427..0d4199bf8 100644 --- a/pkg/util/util.go +++ b/pkg/util/util.go @@ -516,28 +516,36 @@ func ReadDirNoStat(dirname string) ([]string, error) { return f.Readdirnames(-1) } -func PrintByLine(w io.Writer, errors interface{}) { - if w == os.Stderr { - fmt.Fprintf(os.Stderr, "error: ") - } +func SpliceErrors(errors interface{}) string { t := reflect.TypeOf(errors) + var errStrings []string switch t.Kind() { case reflect.Slice, reflect.Array: - fmt.Fprintf(w, "[\n") v := reflect.ValueOf(errors) for i := 0; i < v.Len(); i++ { - fmt.Fprintf(w, " %v\n", v.Index(i)) - } - fmt.Fprintf(w, "]\n") - case reflect.Map: - fmt.Fprintf(w, "[\n") - v := reflect.ValueOf(errors) - iter := v.MapRange() - for iter.Next() { - fmt.Fprintf(w, " %v: %v\n", iter.Key(), iter.Value()) + if err, ok := v.Index(i).Interface().(error); ok { + errStrings = append(errStrings, err.Error()) + } } - fmt.Fprintf(w, "]\n") default: - fmt.Fprintf(w, "%v\n", errors) + if err, ok := reflect.ValueOf(errors).Interface().(error); ok { + errStrings = append(errStrings, err.Error()) + } + } + + if len(errStrings) == 0 { + return "" + } + + if len(errStrings) == 1 { + return errStrings[0] + } + + var stb strings.Builder + stb.WriteString("[\n") + for _, errString := range errStrings { + stb.WriteString(fmt.Sprintf(" %s\n", errString)) } + stb.WriteString("]\n") + return stb.String() } |
