...

Source file src/k8s.io/kubernetes/test/utils/ktesting/errorcontext_test.go

Documentation: k8s.io/kubernetes/test/utils/ktesting

     1  /*
     2  Copyright 2024 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 ktesting
    18  
    19  import (
    20  	"errors"
    21  	"testing"
    22  
    23  	"github.com/stretchr/testify/assert"
    24  )
    25  
    26  func TestWithError(t *testing.T) {
    27  	t.Run("panic", func(t *testing.T) {
    28  		assert.Panics(t, func() {
    29  			tCtx := Init(t)
    30  			var err error
    31  			_, finalize := WithError(tCtx, &err)
    32  			defer finalize()
    33  
    34  			panic("pass me through")
    35  		})
    36  	})
    37  
    38  	normalErr := errors.New("normal error")
    39  
    40  	for name, tc := range map[string]struct {
    41  		cb           func(TContext)
    42  		expectNoFail bool
    43  		expectError  string
    44  	}{
    45  		"none": {
    46  			cb:           func(tCtx TContext) {},
    47  			expectNoFail: true,
    48  			expectError:  normalErr.Error(),
    49  		},
    50  		"Error": {
    51  			cb: func(tCtx TContext) {
    52  				tCtx.Error("some error")
    53  			},
    54  			expectError: "some error",
    55  		},
    56  		"Errorf": {
    57  			cb: func(tCtx TContext) {
    58  				tCtx.Errorf("some %s", "error")
    59  			},
    60  			expectError: "some error",
    61  		},
    62  		"Fatal": {
    63  			cb: func(tCtx TContext) {
    64  				tCtx.Fatal("some error")
    65  				tCtx.Error("another error")
    66  			},
    67  			expectError: "some error",
    68  		},
    69  		"Fatalf": {
    70  			cb: func(tCtx TContext) {
    71  				tCtx.Fatalf("some %s", "error")
    72  				tCtx.Error("another error")
    73  			},
    74  			expectError: "some error",
    75  		},
    76  		"Fail": {
    77  			cb: func(tCtx TContext) {
    78  				tCtx.Fatalf("some %s", "error")
    79  				tCtx.Error("another error")
    80  			},
    81  			expectError: "some error",
    82  		},
    83  		"FailNow": {
    84  			cb: func(tCtx TContext) {
    85  				tCtx.FailNow()
    86  				tCtx.Error("another error")
    87  			},
    88  			expectError: errFailedWithNoExplanation.Error(),
    89  		},
    90  		"many": {
    91  			cb: func(tCtx TContext) {
    92  				tCtx.Error("first error")
    93  				tCtx.Error("second error")
    94  			},
    95  			expectError: `first error
    96  second error`,
    97  		},
    98  	} {
    99  		t.Run(name, func(t *testing.T) {
   100  			tCtx := Init(t)
   101  			err := normalErr
   102  			tCtx, finalize := WithError(tCtx, &err)
   103  			func() {
   104  				defer finalize()
   105  				tc.cb(tCtx)
   106  			}()
   107  
   108  			assert.Equal(t, !tc.expectNoFail, tCtx.Failed(), "Failed()")
   109  			if tc.expectError == "" {
   110  				assert.NoError(t, err)
   111  			} else if assert.NotNil(t, err) {
   112  				assert.Equal(t, tc.expectError, err.Error())
   113  			}
   114  		})
   115  	}
   116  }
   117  

View as plain text