storj.io/minio@v0.0.0-20230509071714-0cbc90f649b1/cmd/gateway/s3/gateway-s3_test.go (about)

     1  /*
     2   * MinIO Cloud Storage, (C) 2017 MinIO, Inc.
     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 s3
    18  
    19  import (
    20  	"fmt"
    21  	"testing"
    22  
    23  	miniogo "github.com/minio/minio-go/v7"
    24  
    25  	"storj.io/minio/pkg/hash"
    26  
    27  	minio "storj.io/minio/cmd"
    28  )
    29  
    30  func errResponse(code string) miniogo.ErrorResponse {
    31  	return miniogo.ErrorResponse{
    32  		Code: code,
    33  	}
    34  }
    35  
    36  func TestS3ToObjectError(t *testing.T) {
    37  	testCases := []struct {
    38  		inputErr       error
    39  		expectedErr    error
    40  		bucket, object string
    41  	}{
    42  		{
    43  			inputErr:    errResponse("BucketAlreadyOwnedByYou"),
    44  			expectedErr: minio.BucketAlreadyOwnedByYou{},
    45  		},
    46  		{
    47  			inputErr:    errResponse("BucketNotEmpty"),
    48  			expectedErr: minio.BucketNotEmpty{},
    49  		},
    50  		{
    51  			inputErr:    errResponse("InvalidBucketName"),
    52  			expectedErr: minio.BucketNameInvalid{},
    53  		},
    54  		{
    55  			inputErr:    errResponse("InvalidPart"),
    56  			expectedErr: minio.InvalidPart{},
    57  		},
    58  		{
    59  			inputErr:    errResponse("NoSuchBucketPolicy"),
    60  			expectedErr: minio.BucketPolicyNotFound{},
    61  		},
    62  		{
    63  			inputErr:    errResponse("NoSuchBucket"),
    64  			expectedErr: minio.BucketNotFound{},
    65  		},
    66  		// with empty Object in miniogo.ErrorRepsonse, NoSuchKey
    67  		// is interpreted as BucketNotFound
    68  		{
    69  			inputErr:    errResponse("NoSuchKey"),
    70  			expectedErr: minio.BucketNotFound{},
    71  		},
    72  		{
    73  			inputErr:    errResponse("NoSuchUpload"),
    74  			expectedErr: minio.InvalidUploadID{},
    75  		},
    76  		{
    77  			inputErr:    errResponse("XMinioInvalidObjectName"),
    78  			expectedErr: minio.ObjectNameInvalid{},
    79  		},
    80  		{
    81  			inputErr:    errResponse("AccessDenied"),
    82  			expectedErr: minio.PrefixAccessDenied{},
    83  		},
    84  		{
    85  			inputErr:    errResponse("XAmzContentSHA256Mismatch"),
    86  			expectedErr: hash.SHA256Mismatch{},
    87  		},
    88  		{
    89  			inputErr:    errResponse("EntityTooSmall"),
    90  			expectedErr: minio.PartTooSmall{},
    91  		},
    92  		{
    93  			inputErr:    nil,
    94  			expectedErr: nil,
    95  		},
    96  		// Special test case for NoSuchKey with object name
    97  		{
    98  			inputErr: miniogo.ErrorResponse{
    99  				Code: "NoSuchKey",
   100  			},
   101  			expectedErr: minio.ObjectNotFound{
   102  				Bucket: "bucket",
   103  				Object: "object",
   104  			},
   105  			bucket: "bucket",
   106  			object: "object",
   107  		},
   108  
   109  		// N B error values that aren't of expected types
   110  		// should be left untouched.
   111  		// Special test case for error that is not of type
   112  		// miniogo.ErrorResponse
   113  		{
   114  			inputErr:    fmt.Errorf("not a ErrorResponse"),
   115  			expectedErr: fmt.Errorf("not a ErrorResponse"),
   116  		},
   117  	}
   118  
   119  	for i, tc := range testCases {
   120  		actualErr := minio.ErrorRespToObjectError(tc.inputErr, tc.bucket, tc.object)
   121  		if actualErr != nil && tc.expectedErr != nil && actualErr.Error() != tc.expectedErr.Error() {
   122  			t.Errorf("Test case %d: Expected error %v but received error %v", i+1, tc.expectedErr, actualErr)
   123  		}
   124  	}
   125  }