...

Source file src/k8s.io/kubernetes/cmd/kubeadm/app/util/error_test.go

Documentation: k8s.io/kubernetes/cmd/kubeadm/app/util

     1  /*
     2  Copyright 2016 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package util
    18  
    19  import (
    20  	"testing"
    21  
    22  	"github.com/pkg/errors"
    23  )
    24  
    25  type pferror struct{}
    26  
    27  func (p *pferror) Preflight() bool { return true }
    28  func (p *pferror) Error() string   { return "" }
    29  func TestCheckErr(t *testing.T) {
    30  	var codeReturned int
    31  	errHandle := func(err string, code int) {
    32  		codeReturned = code
    33  	}
    34  
    35  	var tests = []struct {
    36  		name     string
    37  		e        error
    38  		expected int
    39  	}{
    40  		{"error is nil", nil, 0},
    41  		{"empty error", errors.New(""), DefaultErrorExitCode},
    42  		{"preflight error", &pferror{}, PreFlightExitCode},
    43  	}
    44  
    45  	for _, rt := range tests {
    46  		t.Run(rt.name, func(t *testing.T) {
    47  			codeReturned = 0
    48  			checkErr(rt.e, errHandle)
    49  			if codeReturned != rt.expected {
    50  				t.Errorf(
    51  					"failed checkErr:\n\texpected: %d\n\t  actual: %d",
    52  					rt.expected,
    53  					codeReturned,
    54  				)
    55  			}
    56  		})
    57  	}
    58  }
    59  
    60  func TestFormatErrMsg(t *testing.T) {
    61  	errMsg1 := "specified version to upgrade to v1.9.0-alpha.3 is equal to or lower than the cluster version v1.10.0-alpha.0.69+638add6ddfb6d2. Downgrades are not supported yet"
    62  	errMsg2 := "specified version to upgrade to v1.9.0-alpha.3 is higher than the kubeadm version v1.9.0-alpha.1.3121+84178212527295-dirty. Upgrade kubeadm first using the tool you used to install kubeadm"
    63  
    64  	testCases := []struct {
    65  		name   string
    66  		errs   []error
    67  		expect string
    68  	}{
    69  		{
    70  			name: "two errors",
    71  			errs: []error{
    72  				errors.New(errMsg1),
    73  				errors.New(errMsg2),
    74  			},
    75  			expect: "\t- " + errMsg1 + "\n" + "\t- " + errMsg2 + "\n",
    76  		},
    77  		{
    78  			name: "one error",
    79  			errs: []error{
    80  				errors.New(errMsg1),
    81  			},
    82  			expect: "\t- " + errMsg1 + "\n",
    83  		},
    84  	}
    85  
    86  	for _, testCase := range testCases {
    87  		t.Run(testCase.name, func(t *testing.T) {
    88  			got := FormatErrMsg(testCase.errs)
    89  			if got != testCase.expect {
    90  				t.Errorf("FormatErrMsg error, expect: %v, got: %v", testCase.expect, got)
    91  			}
    92  		})
    93  	}
    94  }
    95  

View as plain text