storj.io/minio@v0.0.0-20230509071714-0cbc90f649b1/cmd/object-api-deleteobject_test.go (about) 1 /* 2 * MinIO Cloud Storage, (C) 2018 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 "context" 21 "crypto/md5" 22 "encoding/hex" 23 "strings" 24 "testing" 25 ) 26 27 // Wrapper for calling DeleteObject tests for both Erasure multiple disks and single node setup. 28 func TestDeleteObject(t *testing.T) { 29 ExecObjectLayerTest(t, testDeleteObject) 30 } 31 32 // Unit test for DeleteObject in general. 33 func testDeleteObject(obj ObjectLayer, instanceType string, t TestErrHandler) { 34 35 type objectUpload struct { 36 name string 37 content string 38 } 39 40 testCases := []struct { 41 bucketName string 42 objectToUploads []objectUpload 43 pathToDelete string 44 objectsAfterDelete []string 45 }{ 46 // Test 1: removes an object and checks it is the only object 47 // that has been deleted. 48 { 49 "bucket1", 50 []objectUpload{{"object0", "content"}, {"object1", "content"}}, 51 "object0", 52 []string{"object1"}, 53 }, 54 // Test 2: remove an object inside a directory and checks it is deleted 55 // with its parent since this former becomes empty 56 { 57 "bucket2", 58 []objectUpload{{"object0", "content"}, {"dir/object1", "content"}}, 59 "dir/object1", 60 []string{"object0"}, 61 }, 62 // Test 3: remove an object inside a directory and checks if it is deleted 63 // but other sibling object in the same directory still exists 64 { 65 "bucket3", 66 []objectUpload{{"dir/object1", "content"}, {"dir/object2", "content"}}, 67 "dir/object1", 68 []string{"dir/object2"}, 69 }, 70 // Test 4: remove a non empty directory and checks it has no effect 71 { 72 "bucket4", 73 []objectUpload{{"object0", "content"}, {"dir/object1", "content"}}, 74 "dir/", 75 []string{"dir/object1", "object0"}, 76 }, 77 // Test 5: Remove an empty directory and checks it is really removed 78 { 79 "bucket5", 80 []objectUpload{{"object0", "content"}, {"dir/", ""}}, 81 "dir/", 82 []string{"object0"}, 83 }, 84 } 85 86 for i, testCase := range testCases { 87 err := obj.MakeBucketWithLocation(context.Background(), testCase.bucketName, BucketOptions{}) 88 if err != nil { 89 t.Fatalf("%s : %s", instanceType, err.Error()) 90 } 91 92 for _, object := range testCase.objectToUploads { 93 md5Bytes := md5.Sum([]byte(object.content)) 94 oi, err := obj.PutObject(context.Background(), testCase.bucketName, object.name, mustGetPutObjReader(t, strings.NewReader(object.content), 95 int64(len(object.content)), hex.EncodeToString(md5Bytes[:]), ""), ObjectOptions{}) 96 t.Log(oi) 97 if err != nil { 98 t.Fatalf("%s : %s", instanceType, err.Error()) 99 } 100 } 101 102 oi, err := obj.DeleteObject(context.Background(), testCase.bucketName, testCase.pathToDelete, ObjectOptions{}) 103 t.Log(oi, err) 104 105 result, err := obj.ListObjects(context.Background(), testCase.bucketName, "", "", "", 1000) 106 if err != nil { 107 t.Errorf("Test %d: %s: Expected to pass, but failed with: <ERROR> %s", i+1, instanceType, err.Error()) 108 continue 109 } 110 111 if len(result.Objects) != len(testCase.objectsAfterDelete) { 112 t.Errorf("Test %d: %s: mismatch number of objects after delete, expected = %v, found = %v", i+1, instanceType, testCase.objectsAfterDelete, result.Objects) 113 continue 114 } 115 116 for idx := range result.Objects { 117 if result.Objects[idx].Name != testCase.objectsAfterDelete[idx] { 118 t.Errorf("Test %d: %s: Unexpected object found after delete, found = `%v`", i+1, instanceType, result.Objects[idx].Name) 119 } 120 } 121 } 122 }