storj.io/minio@v0.0.0-20230509071714-0cbc90f649b1/cmd/object-api-getobjectinfo_test.go (about) 1 /* 2 * MinIO Cloud Storage, (C) 2016, 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 cmd 18 19 import ( 20 "bytes" 21 "context" 22 "testing" 23 ) 24 25 // Wrapper for calling GetObjectInfo tests for both Erasure multiple disks and single node setup. 26 func TestGetObjectInfo(t *testing.T) { 27 ExecObjectLayerTest(t, testGetObjectInfo) 28 } 29 30 // Testing GetObjectInfo(). 31 func testGetObjectInfo(obj ObjectLayer, instanceType string, t TestErrHandler) { 32 // This bucket is used for testing getObjectInfo operations. 33 err := obj.MakeBucketWithLocation(context.Background(), "test-getobjectinfo", BucketOptions{}) 34 if err != nil { 35 t.Fatalf("%s : %s", instanceType, err.Error()) 36 } 37 opts := ObjectOptions{} 38 _, err = obj.PutObject(context.Background(), "test-getobjectinfo", "Asia/asiapics.jpg", mustGetPutObjReader(t, bytes.NewBufferString("asiapics"), int64(len("asiapics")), "", ""), opts) 39 if err != nil { 40 t.Fatalf("%s : %s", instanceType, err.Error()) 41 } 42 43 // Put an empty directory 44 _, err = obj.PutObject(context.Background(), "test-getobjectinfo", "Asia/empty-dir/", mustGetPutObjReader(t, bytes.NewBufferString(""), int64(len("")), "", ""), opts) 45 if err != nil { 46 t.Fatalf("%s : %s", instanceType, err.Error()) 47 } 48 49 resultCases := []ObjectInfo{ 50 // ObjectInfo -1. 51 // ObjectName set to a existing object in the test case (Test case 14). 52 {Bucket: "test-getobjectinfo", Name: "Asia/asiapics.jpg", ContentType: "image/jpeg", IsDir: false}, 53 {Bucket: "test-getobjectinfo", Name: "Asia/empty-dir/", ContentType: "application/octet-stream", IsDir: true}, 54 } 55 testCases := []struct { 56 bucketName string 57 objectName string 58 59 // Expected output of GetObjectInfo. 60 result ObjectInfo 61 err error 62 // Flag indicating whether the test is expected to pass or not. 63 shouldPass bool 64 }{ 65 // Test cases with invalid bucket names ( Test number 1-4 ). 66 {".test", "", ObjectInfo{}, BucketNameInvalid{Bucket: ".test"}, false}, 67 {"---", "", ObjectInfo{}, BucketNameInvalid{Bucket: "---"}, false}, 68 {"ad", "", ObjectInfo{}, BucketNameInvalid{Bucket: "ad"}, false}, 69 // Test cases with valid but non-existing bucket names (Test number 5-6). 70 {"abcdefgh", "abc", ObjectInfo{}, BucketNotFound{Bucket: "abcdefgh"}, false}, 71 {"ijklmnop", "efg", ObjectInfo{}, BucketNotFound{Bucket: "ijklmnop"}, false}, 72 // Test cases with valid but non-existing bucket names and invalid object name (Test number 7-8). 73 {"test-getobjectinfo", "", ObjectInfo{}, ObjectNameInvalid{Bucket: "test-getobjectinfo", Object: ""}, false}, 74 {"test-getobjectinfo", "", ObjectInfo{}, ObjectNameInvalid{Bucket: "test-getobjectinfo", Object: ""}, false}, 75 // Test cases with non-existing object name with existing bucket (Test number 9-11). 76 {"test-getobjectinfo", "Africa", ObjectInfo{}, ObjectNotFound{Bucket: "test-getobjectinfo", Object: "Africa"}, false}, 77 {"test-getobjectinfo", "Antartica", ObjectInfo{}, ObjectNotFound{Bucket: "test-getobjectinfo", Object: "Antartica"}, false}, 78 {"test-getobjectinfo", "Asia/myfile", ObjectInfo{}, ObjectNotFound{Bucket: "test-getobjectinfo", Object: "Asia/myfile"}, false}, 79 // Valid case with existing object (Test number 12). 80 {"test-getobjectinfo", "Asia/asiapics.jpg", resultCases[0], nil, true}, 81 {"test-getobjectinfo", "Asia/empty-dir/", resultCases[1], nil, true}, 82 } 83 for i, testCase := range testCases { 84 result, err := obj.GetObjectInfo(context.Background(), testCase.bucketName, testCase.objectName, opts) 85 if err != nil && testCase.shouldPass { 86 t.Errorf("Test %d: %s: Expected to pass, but failed with: <ERROR> %s", i+1, instanceType, err.Error()) 87 } 88 if err == nil && !testCase.shouldPass { 89 t.Errorf("Test %d: %s: Expected to fail with <ERROR> \"%s\", but passed instead", i+1, instanceType, testCase.err.Error()) 90 } 91 // Failed as expected, but does it fail for the expected reason. 92 if err != nil && !testCase.shouldPass { 93 if testCase.err.Error() != err.Error() { 94 t.Errorf("Test %d: %s: Expected to fail with error \"%s\", but instead failed with error \"%s\" instead", i+1, instanceType, testCase.err.Error(), err.Error()) 95 } 96 } 97 98 // Test passes as expected, but the output values are verified for correctness here. 99 if err == nil && testCase.shouldPass { 100 if testCase.result.Bucket != result.Bucket { 101 t.Fatalf("Test %d: %s: Expected Bucket name to be '%s', but found '%s' instead", i+1, instanceType, testCase.result.Bucket, result.Bucket) 102 } 103 if testCase.result.Name != result.Name { 104 t.Errorf("Test %d: %s: Expected Object name to be %s, but instead found it to be %s", i+1, instanceType, testCase.result.Name, result.Name) 105 } 106 if testCase.result.ContentType != result.ContentType { 107 t.Errorf("Test %d: %s: Expected Content Type of the object to be %v, but instead found it to be %v", i+1, instanceType, testCase.result.ContentType, result.ContentType) 108 } 109 if testCase.result.IsDir != result.IsDir { 110 t.Errorf("Test %d: %s: Expected IsDir flag of the object to be %v, but instead found it to be %v", i+1, instanceType, testCase.result.IsDir, result.IsDir) 111 } 112 } 113 } 114 }