|
| 1 | +/* |
| 2 | +Copyright 2025 Iguazio Systems Ltd. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License") with |
| 5 | +an addition restriction as set forth herein. You may not use this |
| 6 | +file except in compliance with the License. You may obtain a copy of |
| 7 | +the License at http://www.apache.org/licenses/LICENSE-2.0. |
| 8 | +
|
| 9 | +Unless required by applicable law or agreed to in writing, software |
| 10 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or |
| 12 | +implied. See the License for the specific language governing |
| 13 | +permissions and limitations under the License. |
| 14 | +
|
| 15 | +In addition, you may not use the software for any purposes that are |
| 16 | +illegal under applicable law, and the grant of the foregoing license |
| 17 | +under the Apache 2.0 license is conditioned upon your compliance with |
| 18 | +such restriction. |
| 19 | +*/ |
| 20 | + |
| 21 | +package common |
| 22 | + |
| 23 | +import ( |
| 24 | + "fmt" |
| 25 | + "testing" |
| 26 | + |
| 27 | + "github.com/nuclio/errors" |
| 28 | + v3ioerrors "github.com/v3io/v3io-go/pkg/errors" |
| 29 | + |
| 30 | + "github.com/stretchr/testify/suite" |
| 31 | +) |
| 32 | + |
| 33 | +type helperTestSuite struct { |
| 34 | + suite.Suite |
| 35 | +} |
| 36 | + |
| 37 | +func (suite *helperTestSuite) TestEngineErrorIsNonFatalNestedErrorFromLog() { |
| 38 | + // Create the nested error structure with http 503 Service Temporarily Unavailable as the root cause |
| 39 | + |
| 40 | + // Create HTTP response string contains the 503 error |
| 41 | + httpResponseStr := `HTTP/1.1 503 Service Temporarily Unavailable\r\nServer: nginx\r\nDate: Tue, 03 Jun 2025 07:37:09 GMT\r\nContent-Type: application/json\r\nContent-Length: 89\r\nConnection: keep-alive\r\n\r\n{\n\t\"ErrorCode\": -117440512,\n\t\"ErrorMessage\": \"Failed to send a control message request\"\n}` |
| 42 | + sanitizedRequest := `PUT /projects/perform044wp2gqixmhkv1/datafetch_output_stream/20 HTTP/1.1\r\nUser-Agent: fasthttp\r\nHost: v3io-webapi:8081\r\nContent-Type: application/json\r\nContent-Length: 58\r\nX-V3io-Session-Key: SANITIZED\r\nX-V3io-Function: GetItem\r\n\r\n{\"AttributesToGet\": \"__serving_committed_sequence_number\"}` |
| 43 | + httpBody := fmt.Errorf("Expected a 2xx response status code: %s\nRequest details:\n%s", |
| 44 | + httpResponseStr, sanitizedRequest) |
| 45 | + |
| 46 | + // Wrap with ErrorWithStatusCode |
| 47 | + statusCodeErr := v3ioerrors.NewErrorWithStatusCode(httpBody, 503) |
| 48 | + |
| 49 | + // Further wrap the error to simulate the nested error chain |
| 50 | + shardItemErr := errors.Wrap(statusCodeErr, "Failed getting shard item") |
| 51 | + sequenceNumberErr := errors.Wrap(shardItemErr, "Failed to get shard sequenceNumber from item attributes") |
| 52 | + persistencyErr := errors.Wrap(sequenceNumberErr, "Failed to get shard location from persistency") |
| 53 | + locationErr := errors.Wrap(persistencyErr, "Failed to get shard location") |
| 54 | + finalErr := errors.Wrapf(locationErr, "Failed to get shard location state, attempts exhausted. shard id: %d", 20) |
| 55 | + |
| 56 | + // Test that EngineErrorIsNonFatal correctly unwraps the nested error chain |
| 57 | + // Since status code 503 is now in nonFatalStatusCodes, expect true |
| 58 | + result := EngineErrorIsNonFatal(finalErr) |
| 59 | + suite.Require().True(result, "Expected EngineErrorIsNonFatal to return true (503 is in nonFatalStatusCodes)") |
| 60 | +} |
| 61 | + |
| 62 | +func (suite *helperTestSuite) TestMatchErrorString() { |
| 63 | + for _, testCase := range []struct { |
| 64 | + name string |
| 65 | + errMsg string |
| 66 | + expected bool |
| 67 | + }{ |
| 68 | + { |
| 69 | + name: "timeout error", |
| 70 | + errMsg: "connection timeout occurred", |
| 71 | + expected: true, |
| 72 | + }, |
| 73 | + { |
| 74 | + name: "dial timeout error", |
| 75 | + errMsg: "dialing to the given TCP address timed out", |
| 76 | + expected: true, |
| 77 | + }, |
| 78 | + { |
| 79 | + name: "connection refused", |
| 80 | + errMsg: "connection refused", |
| 81 | + expected: true, |
| 82 | + }, |
| 83 | + { |
| 84 | + name: "generic error", |
| 85 | + errMsg: "something went wrong", |
| 86 | + expected: false, |
| 87 | + }, |
| 88 | + { |
| 89 | + name: "empty error", |
| 90 | + errMsg: "", |
| 91 | + expected: false, |
| 92 | + }, |
| 93 | + } { |
| 94 | + suite.Run(testCase.name, func() { |
| 95 | + err := fmt.Errorf(testCase.errMsg) |
| 96 | + result := isNonFatalErrorString(err) |
| 97 | + suite.Require().Equal(testCase.expected, result) |
| 98 | + }) |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +func (suite *helperTestSuite) TestMatchErrorStatusCode() { |
| 103 | + baseErr := fmt.Errorf("test error") |
| 104 | + for _, testCase := range []struct { |
| 105 | + name string |
| 106 | + statusCode int |
| 107 | + expected bool |
| 108 | + }{ |
| 109 | + { |
| 110 | + name: "non-fatal status code 503", |
| 111 | + statusCode: 503, |
| 112 | + expected: true, |
| 113 | + }, |
| 114 | + { |
| 115 | + name: "fatal status code 500", |
| 116 | + statusCode: 500, |
| 117 | + expected: false, |
| 118 | + }, |
| 119 | + { |
| 120 | + name: "fatal status code 404", |
| 121 | + statusCode: 404, |
| 122 | + expected: false, |
| 123 | + }, |
| 124 | + { |
| 125 | + name: "fatal status code 200", |
| 126 | + statusCode: 200, |
| 127 | + expected: false, |
| 128 | + }, |
| 129 | + } { |
| 130 | + suite.Run(testCase.name, func() { |
| 131 | + err := v3ioerrors.NewErrorWithStatusCode(baseErr, testCase.statusCode) |
| 132 | + result := isNonFatalStatusCode(err) |
| 133 | + suite.Require().Equal(testCase.expected, result) |
| 134 | + }) |
| 135 | + } |
| 136 | +} |
| 137 | + |
| 138 | +func (suite *helperTestSuite) TestMatchErrorStatusCodeNonV3ioError() { |
| 139 | + err := fmt.Errorf("regular error") |
| 140 | + result := isNonFatalStatusCode(err) |
| 141 | + suite.Require().False(result) |
| 142 | +} |
| 143 | + |
| 144 | +func (suite *helperTestSuite) TestEngineErrorIsNonFatalStringMatch() { |
| 145 | + for _, testCase := range []struct { |
| 146 | + name string |
| 147 | + errMsg string |
| 148 | + expected bool |
| 149 | + }{ |
| 150 | + { |
| 151 | + name: "timeout error", |
| 152 | + errMsg: "operation timeout", |
| 153 | + expected: true, |
| 154 | + }, |
| 155 | + { |
| 156 | + name: "connection refused", |
| 157 | + errMsg: "connection refused by server", |
| 158 | + expected: true, |
| 159 | + }, |
| 160 | + { |
| 161 | + name: "generic error", |
| 162 | + errMsg: "something went wrong", |
| 163 | + expected: false, |
| 164 | + }, |
| 165 | + } { |
| 166 | + suite.Run(testCase.name, func() { |
| 167 | + err := fmt.Errorf(testCase.errMsg) |
| 168 | + result := EngineErrorIsNonFatal(err) |
| 169 | + suite.Require().Equal(testCase.expected, result) |
| 170 | + }) |
| 171 | + } |
| 172 | +} |
| 173 | + |
| 174 | +func (suite *helperTestSuite) TestEngineErrorIsNonFatalStatusCodeMatch() { |
| 175 | + baseErr := fmt.Errorf("test error") |
| 176 | + for _, testCase := range []struct { |
| 177 | + name string |
| 178 | + statusCode int |
| 179 | + expected bool |
| 180 | + }{ |
| 181 | + { |
| 182 | + name: "Service Temporarily Unavailable error", |
| 183 | + statusCode: 503, |
| 184 | + expected: true, |
| 185 | + }, { |
| 186 | + name: "no error", |
| 187 | + statusCode: 200, |
| 188 | + expected: false, |
| 189 | + }, |
| 190 | + } { |
| 191 | + suite.Run(testCase.name, func() { |
| 192 | + err := v3ioerrors.NewErrorWithStatusCode(baseErr, testCase.statusCode) |
| 193 | + result := EngineErrorIsNonFatal(err) |
| 194 | + suite.Require().Equal(testCase.expected, result) |
| 195 | + }) |
| 196 | + } |
| 197 | + |
| 198 | +} |
| 199 | + |
| 200 | +func TestHelperTestSuite(t *testing.T) { |
| 201 | + suite.Run(t, new(helperTestSuite)) |
| 202 | +} |
0 commit comments