| 
									
										
										
										
											2017-10-06 03:24:45 +08:00
										 |  |  | /* | 
					
						
							| 
									
										
										
										
											2019-04-10 02:39:42 +08:00
										 |  |  |  * MinIO Cloud Storage, (C) 2017 MinIO, Inc. | 
					
						
							| 
									
										
										
										
											2017-10-06 03:24:45 +08:00
										 |  |  |  * | 
					
						
							|  |  |  |  * Licensed under the Apache License, Version 2.0 (the "License"); | 
					
						
							|  |  |  |  * you may not use this file except in compliance with the License. | 
					
						
							|  |  |  |  * You may obtain a copy of the License at | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  *     http://www.apache.org/licenses/LICENSE-2.0
 | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Unless required by applicable law or agreed to in writing, software | 
					
						
							|  |  |  |  * distributed under the License is distributed on an "AS IS" BASIS, | 
					
						
							|  |  |  |  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
					
						
							|  |  |  |  * See the License for the specific language governing permissions and | 
					
						
							|  |  |  |  * limitations under the License. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-06 09:58:09 +08:00
										 |  |  | package s3 | 
					
						
							| 
									
										
										
										
											2017-10-06 03:24:45 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							| 
									
										
										
										
											2017-12-06 09:58:09 +08:00
										 |  |  | 	"fmt" | 
					
						
							| 
									
										
										
										
											2017-10-06 03:24:45 +08:00
										 |  |  | 	"testing" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-30 07:35:12 +08:00
										 |  |  | 	miniogo "github.com/minio/minio-go/v6" | 
					
						
							| 
									
										
										
										
											2017-10-22 13:30:34 +08:00
										 |  |  | 	"github.com/minio/minio/pkg/hash" | 
					
						
							| 
									
										
										
										
											2017-12-06 09:58:09 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	minio "github.com/minio/minio/cmd" | 
					
						
							| 
									
										
										
										
											2017-10-06 03:24:45 +08:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-06 09:58:09 +08:00
										 |  |  | func errResponse(code string) miniogo.ErrorResponse { | 
					
						
							|  |  |  | 	return miniogo.ErrorResponse{ | 
					
						
							| 
									
										
										
										
											2017-10-06 03:24:45 +08:00
										 |  |  | 		Code: code, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func TestS3ToObjectError(t *testing.T) { | 
					
						
							|  |  |  | 	testCases := []struct { | 
					
						
							|  |  |  | 		inputErr       error | 
					
						
							|  |  |  | 		expectedErr    error | 
					
						
							|  |  |  | 		bucket, object string | 
					
						
							|  |  |  | 	}{ | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			inputErr:    errResponse("BucketAlreadyOwnedByYou"), | 
					
						
							| 
									
										
										
										
											2017-12-06 09:58:09 +08:00
										 |  |  | 			expectedErr: minio.BucketAlreadyOwnedByYou{}, | 
					
						
							| 
									
										
										
										
											2017-10-06 03:24:45 +08:00
										 |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			inputErr:    errResponse("BucketNotEmpty"), | 
					
						
							| 
									
										
										
										
											2017-12-06 09:58:09 +08:00
										 |  |  | 			expectedErr: minio.BucketNotEmpty{}, | 
					
						
							| 
									
										
										
										
											2017-10-06 03:24:45 +08:00
										 |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			inputErr:    errResponse("InvalidBucketName"), | 
					
						
							| 
									
										
										
										
											2017-12-06 09:58:09 +08:00
										 |  |  | 			expectedErr: minio.BucketNameInvalid{}, | 
					
						
							| 
									
										
										
										
											2017-10-06 03:24:45 +08:00
										 |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2018-10-18 00:20:58 +08:00
										 |  |  | 		{ | 
					
						
							|  |  |  | 			inputErr:    errResponse("InvalidPart"), | 
					
						
							|  |  |  | 			expectedErr: minio.InvalidPart{}, | 
					
						
							|  |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2017-10-06 03:24:45 +08:00
										 |  |  | 		{ | 
					
						
							|  |  |  | 			inputErr:    errResponse("NoSuchBucketPolicy"), | 
					
						
							| 
									
										
										
										
											2018-04-25 06:53:30 +08:00
										 |  |  | 			expectedErr: minio.BucketPolicyNotFound{}, | 
					
						
							| 
									
										
										
										
											2017-10-06 03:24:45 +08:00
										 |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			inputErr:    errResponse("NoSuchBucket"), | 
					
						
							| 
									
										
										
										
											2017-12-06 09:58:09 +08:00
										 |  |  | 			expectedErr: minio.BucketNotFound{}, | 
					
						
							| 
									
										
										
										
											2017-10-06 03:24:45 +08:00
										 |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2017-12-06 09:58:09 +08:00
										 |  |  | 		// with empty Object in miniogo.ErrorRepsonse, NoSuchKey
 | 
					
						
							| 
									
										
										
										
											2017-10-06 03:24:45 +08:00
										 |  |  | 		// is interpreted as BucketNotFound
 | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			inputErr:    errResponse("NoSuchKey"), | 
					
						
							| 
									
										
										
										
											2017-12-06 09:58:09 +08:00
										 |  |  | 			expectedErr: minio.BucketNotFound{}, | 
					
						
							| 
									
										
										
										
											2017-10-06 03:24:45 +08:00
										 |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			inputErr:    errResponse("NoSuchUpload"), | 
					
						
							| 
									
										
										
										
											2017-12-06 09:58:09 +08:00
										 |  |  | 			expectedErr: minio.InvalidUploadID{}, | 
					
						
							| 
									
										
										
										
											2017-10-06 03:24:45 +08:00
										 |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			inputErr:    errResponse("XMinioInvalidObjectName"), | 
					
						
							| 
									
										
										
										
											2017-12-06 09:58:09 +08:00
										 |  |  | 			expectedErr: minio.ObjectNameInvalid{}, | 
					
						
							| 
									
										
										
										
											2017-10-06 03:24:45 +08:00
										 |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			inputErr:    errResponse("AccessDenied"), | 
					
						
							| 
									
										
										
										
											2017-12-06 09:58:09 +08:00
										 |  |  | 			expectedErr: minio.PrefixAccessDenied{}, | 
					
						
							| 
									
										
										
										
											2017-10-06 03:24:45 +08:00
										 |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			inputErr:    errResponse("XAmzContentSHA256Mismatch"), | 
					
						
							| 
									
										
										
										
											2017-10-22 13:30:34 +08:00
										 |  |  | 			expectedErr: hash.SHA256Mismatch{}, | 
					
						
							| 
									
										
										
										
											2017-10-06 03:24:45 +08:00
										 |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			inputErr:    errResponse("EntityTooSmall"), | 
					
						
							| 
									
										
										
										
											2017-12-06 09:58:09 +08:00
										 |  |  | 			expectedErr: minio.PartTooSmall{}, | 
					
						
							| 
									
										
										
										
											2017-10-06 03:24:45 +08:00
										 |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			inputErr:    nil, | 
					
						
							|  |  |  | 			expectedErr: nil, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		// Special test case for NoSuchKey with object name
 | 
					
						
							|  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2017-12-06 09:58:09 +08:00
										 |  |  | 			inputErr: miniogo.ErrorResponse{ | 
					
						
							| 
									
										
										
										
											2017-10-06 03:24:45 +08:00
										 |  |  | 				Code: "NoSuchKey", | 
					
						
							|  |  |  | 			}, | 
					
						
							| 
									
										
										
										
											2017-12-06 09:58:09 +08:00
										 |  |  | 			expectedErr: minio.ObjectNotFound{ | 
					
						
							|  |  |  | 				Bucket: "bucket", | 
					
						
							|  |  |  | 				Object: "object", | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			bucket: "bucket", | 
					
						
							|  |  |  | 			object: "object", | 
					
						
							| 
									
										
										
										
											2017-10-06 03:24:45 +08:00
										 |  |  | 		}, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// N B error values that aren't of expected types
 | 
					
						
							|  |  |  | 		// should be left untouched.
 | 
					
						
							|  |  |  | 		// Special test case for error that is not of type
 | 
					
						
							| 
									
										
										
										
											2017-12-06 09:58:09 +08:00
										 |  |  | 		// miniogo.ErrorResponse
 | 
					
						
							| 
									
										
										
										
											2017-10-06 03:24:45 +08:00
										 |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2019-01-06 06:16:43 +08:00
										 |  |  | 			inputErr:    fmt.Errorf("not a ErrorResponse"), | 
					
						
							|  |  |  | 			expectedErr: fmt.Errorf("not a ErrorResponse"), | 
					
						
							| 
									
										
										
										
											2017-10-06 03:24:45 +08:00
										 |  |  | 		}, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for i, tc := range testCases { | 
					
						
							| 
									
										
										
										
											2018-04-06 06:04:40 +08:00
										 |  |  | 		actualErr := minio.ErrorRespToObjectError(tc.inputErr, tc.bucket, tc.object) | 
					
						
							| 
									
										
										
										
											2018-04-11 00:36:37 +08:00
										 |  |  | 		if actualErr != nil && tc.expectedErr != nil && actualErr.Error() != tc.expectedErr.Error() { | 
					
						
							| 
									
										
										
										
											2018-04-06 06:04:40 +08:00
										 |  |  | 			t.Errorf("Test case %d: Expected error %v but received error %v", i+1, tc.expectedErr, actualErr) | 
					
						
							| 
									
										
										
										
											2017-10-06 03:24:45 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } |