github.com/kyma-project/kyma-environment-broker@v0.0.1/internal/error/errors_test.go (about) 1 package error_test 2 3 import ( 4 "fmt" 5 "net/http" 6 "testing" 7 8 "github.com/kyma-project/kyma-environment-broker/internal/avs" 9 "github.com/kyma-project/kyma-environment-broker/internal/edp" 10 kebError "github.com/kyma-project/kyma-environment-broker/internal/error" 11 "github.com/kyma-project/kyma-environment-broker/internal/reconciler" 12 "github.com/kyma-project/kyma-environment-broker/internal/storage/dberr" 13 "github.com/stretchr/testify/assert" 14 apierr "k8s.io/apimachinery/pkg/api/errors" 15 apierr2 "k8s.io/apimachinery/pkg/api/meta" 16 ) 17 18 func TestLastError(t *testing.T) { 19 t.Run("report correct reason and component", func(t *testing.T) { 20 // given 21 edpErr := edp.NewEDPBadRequestError("id", fmt.Sprintf("Bad request: %s", "response")) 22 expectEdpMsg := fmt.Sprintf("Bad request: %s", "response") 23 24 edpConfErr := edp.NewEDPConflictError("id", fmt.Sprintf("Resource %s already exists", "id")) 25 expectEdpConfMsg := "Resource id already exists" 26 27 avsErr := fmt.Errorf("something: %w", avs.NewAvsError("avs server returned %d status code", http.StatusUnauthorized)) 28 expectAvsMsg := fmt.Sprintf("something: avs server returned %d status code", http.StatusUnauthorized) 29 30 reccErr := fmt.Errorf("something: %w", reconciler.NewReconcilerError(nil, "reconciler error")) 31 expectReccMsg := "something: reconciler error" 32 33 dbErr := fmt.Errorf("something: %w", dberr.NotFound("Some NotFound apperror, %s", "Some pkg err")) 34 expectDbErr := fmt.Sprintf("something: Some NotFound apperror, Some pkg err") 35 36 timeoutErr := fmt.Errorf("something: %w", fmt.Errorf("operation has reached the time limit: 2h")) 37 expectTimeoutMsg := "something: operation has reached the time limit: 2h" 38 39 // when 40 edpLastErr := kebError.ReasonForError(edpErr) 41 edpConfLastErr := kebError.ReasonForError(edpConfErr) 42 avsLastErr := kebError.ReasonForError(avsErr) 43 reccLastErr := kebError.ReasonForError(reccErr) 44 dbLastErr := kebError.ReasonForError(dbErr) 45 timeoutLastErr := kebError.ReasonForError(timeoutErr) 46 47 // then 48 assert.Equal(t, edp.ErrEDPBadRequest, edpLastErr.Reason()) 49 assert.Equal(t, kebError.ErrEDP, edpLastErr.Component()) 50 assert.Equal(t, expectEdpMsg, edpLastErr.Error()) 51 52 assert.Equal(t, edp.ErrEDPConflict, edpConfLastErr.Reason()) 53 assert.Equal(t, kebError.ErrEDP, edpConfLastErr.Component()) 54 assert.Equal(t, expectEdpConfMsg, edpConfLastErr.Error()) 55 assert.True(t, edp.IsConflictError(edpConfErr)) 56 57 assert.Equal(t, kebError.ErrHttpStatusCode, avsLastErr.Reason()) 58 assert.Equal(t, kebError.ErrAVS, avsLastErr.Component()) 59 assert.Equal(t, expectAvsMsg, avsLastErr.Error()) 60 assert.False(t, edp.IsConflictError(avsErr)) 61 62 assert.Equal(t, kebError.ErrReconcilerNilFailures, reccLastErr.Reason()) 63 assert.Equal(t, kebError.ErrReconciler, reccLastErr.Component()) 64 assert.Equal(t, expectReccMsg, reccLastErr.Error()) 65 66 assert.Equal(t, dberr.ErrDBNotFound, dbLastErr.Reason()) 67 assert.Equal(t, kebError.ErrDB, dbLastErr.Component()) 68 assert.Equal(t, expectDbErr, dbLastErr.Error()) 69 70 assert.Equal(t, kebError.ErrKEBTimeOut, timeoutLastErr.Reason()) 71 assert.Equal(t, kebError.ErrKEB, timeoutLastErr.Component()) 72 assert.Equal(t, expectTimeoutMsg, timeoutLastErr.Error()) 73 }) 74 } 75 76 func TestTemporaryErrorToLastError(t *testing.T) { 77 t.Run("wrapped temporary error", func(t *testing.T) { 78 // given 79 err := kebError.LastError{}. 80 SetMessage(fmt.Sprintf("Got status %d", 502)). 81 SetReason(kebError.ErrHttpStatusCode). 82 SetComponent(kebError.ErrReconciler) 83 tempErr := fmt.Errorf("something else: %w", kebError.WrapNewTemporaryError(fmt.Errorf("something: %w", err))) 84 expectMsg := fmt.Sprintf("something else: something: Got status %d", 502) 85 86 avsTempErr := kebError.WrapNewTemporaryError(avs.NewAvsError("avs server returned %d status code", 503)) 87 expectAvsMsg := fmt.Sprintf("avs server returned %d status code", 503) 88 89 edpTempErr := kebError.WrapNewTemporaryError(edp.NewEDPOtherError("id", http.StatusRequestTimeout, "EDP server returns failed status %s", "501")) 90 expectEdpMsg := fmt.Sprintf("EDP server returns failed status %s", "501") 91 92 // when 93 lastErr := kebError.ReasonForError(tempErr) 94 avsLastErr := kebError.ReasonForError(avsTempErr) 95 edpLastErr := kebError.ReasonForError(edpTempErr) 96 97 // then 98 assert.Equal(t, kebError.ErrHttpStatusCode, lastErr.Reason()) 99 assert.Equal(t, kebError.ErrReconciler, lastErr.Component()) 100 assert.Equal(t, expectMsg, lastErr.Error()) 101 assert.True(t, kebError.IsTemporaryError(tempErr)) 102 103 assert.Equal(t, kebError.ErrHttpStatusCode, avsLastErr.Reason()) 104 assert.Equal(t, kebError.ErrAVS, avsLastErr.Component()) 105 assert.Equal(t, expectAvsMsg, avsLastErr.Error()) 106 assert.True(t, kebError.IsTemporaryError(avsTempErr)) 107 108 assert.Equal(t, edp.ErrEDPTimeout, edpLastErr.Reason()) 109 assert.Equal(t, kebError.ErrEDP, edpLastErr.Component()) 110 assert.Equal(t, expectEdpMsg, edpLastErr.Error()) 111 assert.True(t, kebError.IsTemporaryError(edpTempErr)) 112 }) 113 114 t.Run("new temporary error", func(t *testing.T) { 115 // given 116 tempErr := fmt.Errorf("something: %w", kebError.NewTemporaryError("temporary error...")) 117 expectMsg := "something: temporary error..." 118 119 // when 120 lastErr := kebError.ReasonForError(tempErr) 121 122 // then 123 assert.Equal(t, kebError.ErrKEBInternal, lastErr.Reason()) 124 assert.Equal(t, kebError.ErrKEB, lastErr.Component()) 125 assert.Equal(t, expectMsg, lastErr.Error()) 126 assert.True(t, kebError.IsTemporaryError(tempErr)) 127 }) 128 } 129 130 func TestNotFoundError(t *testing.T) { 131 // given 132 err := fmt.Errorf("something: %w", kebError.NotFoundError{}) 133 134 // when 135 lastErr := kebError.ReasonForError(err) 136 137 // then 138 assert.EqualError(t, lastErr, "something: not found") 139 assert.Equal(t, kebError.ErrClusterNotFound, lastErr.Reason()) 140 assert.Equal(t, kebError.ErrReconciler, lastErr.Component()) 141 assert.True(t, kebError.IsNotFoundError(err)) 142 } 143 144 func TestK8SLastError(t *testing.T) { 145 // given 146 errBadReq := fmt.Errorf("something: %w", apierr.NewBadRequest("bad request here")) 147 errUnexpObj := fmt.Errorf("something: %w", &apierr.UnexpectedObjectError{}) 148 errAmbi := fmt.Errorf("something: %w", &apierr2.AmbiguousResourceError{}) 149 errNoMatch := fmt.Errorf("something: %w", &apierr2.NoKindMatchError{}) 150 151 // when 152 lastErrBadReq := kebError.ReasonForError(errBadReq) 153 lastErrUnexpObj := kebError.ReasonForError(errUnexpObj) 154 lastErrAmbi := kebError.ReasonForError(errAmbi) 155 lastErrNoMatch := kebError.ReasonForError(errNoMatch) 156 157 // then 158 assert.EqualError(t, lastErrBadReq, "something: bad request here") 159 assert.Equal(t, kebError.ErrReason("BadRequest"), lastErrBadReq.Reason()) 160 assert.Equal(t, kebError.ErrK8SClient, lastErrBadReq.Component()) 161 162 assert.ErrorContains(t, lastErrUnexpObj, "something: unexpected object: ") 163 assert.Equal(t, kebError.ErrK8SUnexpectedObjectError, lastErrUnexpObj.Reason()) 164 assert.Equal(t, kebError.ErrK8SClient, lastErrUnexpObj.Component()) 165 166 assert.ErrorContains(t, lastErrAmbi, "matches multiple resources or kinds") 167 assert.Equal(t, kebError.ErrK8SAmbiguousError, lastErrAmbi.Reason()) 168 assert.Equal(t, kebError.ErrK8SClient, lastErrAmbi.Component()) 169 170 assert.ErrorContains(t, lastErrNoMatch, "something: no matches for kind") 171 assert.Equal(t, kebError.ErrK8SNoMatchError, lastErrNoMatch.Reason()) 172 assert.Equal(t, kebError.ErrK8SClient, lastErrNoMatch.Component()) 173 }