storj.io/minio@v0.0.0-20230509071714-0cbc90f649b1/cmd/fs-v1-metadata_test.go (about) 1 /* 2 * MinIO Cloud Storage, (C) 2016 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 "os" 22 "path/filepath" 23 "reflect" 24 "testing" 25 ) 26 27 // Tests ToObjectInfo function. 28 func TestFSV1MetadataObjInfo(t *testing.T) { 29 fsMeta := newFSMetaV1() 30 objInfo := fsMeta.ToObjectInfo("testbucket", "testobject", nil) 31 if objInfo.Size != 0 { 32 t.Fatal("Unexpected object info value for Size", objInfo.Size) 33 } 34 if !objInfo.ModTime.Equal(timeSentinel) { 35 t.Fatal("Unexpected object info value for ModTime ", objInfo.ModTime) 36 } 37 if objInfo.IsDir { 38 t.Fatal("Unexpected object info value for IsDir", objInfo.IsDir) 39 } 40 if !objInfo.Expires.IsZero() { 41 t.Fatal("Unexpected object info value for Expires ", objInfo.Expires) 42 } 43 } 44 45 // TestReadFSMetadata - readFSMetadata testing with a healthy and faulty disk 46 func TestReadFSMetadata(t *testing.T) { 47 disk := filepath.Join(globalTestTmpDir, "minio-"+nextSuffix()) 48 defer os.RemoveAll(disk) 49 50 obj := initFSObjects(disk, t) 51 fs := obj.(*FSObjects) 52 53 bucketName := "bucket" 54 objectName := "object" 55 56 if err := obj.MakeBucketWithLocation(GlobalContext, bucketName, BucketOptions{}); err != nil { 57 t.Fatal("Unexpected err: ", err) 58 } 59 if _, err := obj.PutObject(GlobalContext, bucketName, objectName, mustGetPutObjReader(t, bytes.NewReader([]byte("abcd")), int64(len("abcd")), "", ""), ObjectOptions{}); err != nil { 60 t.Fatal("Unexpected err: ", err) 61 } 62 63 // Construct the full path of fs.json 64 fsPath := pathJoin(bucketMetaPrefix, bucketName, objectName, "fs.json") 65 fsPath = pathJoin(fs.fsPath, minioMetaBucket, fsPath) 66 67 rlk, err := fs.rwPool.Open(fsPath) 68 if err != nil { 69 t.Fatal("Unexpected error ", err) 70 } 71 defer rlk.Close() 72 73 // Regular fs metadata reading, no errors expected 74 fsMeta := fsMetaV1{} 75 if _, err = fsMeta.ReadFrom(GlobalContext, rlk.LockedFile); err != nil { 76 t.Fatal("Unexpected error ", err) 77 } 78 } 79 80 // TestWriteFSMetadata - tests of writeFSMetadata with healthy disk. 81 func TestWriteFSMetadata(t *testing.T) { 82 disk := filepath.Join(globalTestTmpDir, "minio-"+nextSuffix()) 83 defer os.RemoveAll(disk) 84 85 obj := initFSObjects(disk, t) 86 fs := obj.(*FSObjects) 87 88 bucketName := "bucket" 89 objectName := "object" 90 91 if err := obj.MakeBucketWithLocation(GlobalContext, bucketName, BucketOptions{}); err != nil { 92 t.Fatal("Unexpected err: ", err) 93 } 94 if _, err := obj.PutObject(GlobalContext, bucketName, objectName, mustGetPutObjReader(t, bytes.NewReader([]byte("abcd")), int64(len("abcd")), "", ""), ObjectOptions{}); err != nil { 95 t.Fatal("Unexpected err: ", err) 96 } 97 98 // Construct the full path of fs.json 99 fsPath := pathJoin(bucketMetaPrefix, bucketName, objectName, "fs.json") 100 fsPath = pathJoin(fs.fsPath, minioMetaBucket, fsPath) 101 102 rlk, err := fs.rwPool.Open(fsPath) 103 if err != nil { 104 t.Fatal("Unexpected error ", err) 105 } 106 defer rlk.Close() 107 108 // FS metadata reading, no errors expected (healthy disk) 109 fsMeta := fsMetaV1{} 110 _, err = fsMeta.ReadFrom(GlobalContext, rlk.LockedFile) 111 if err != nil { 112 t.Fatal("Unexpected error ", err) 113 } 114 if fsMeta.Version != fsMetaVersion { 115 t.Fatalf("Unexpected version %s", fsMeta.Version) 116 } 117 } 118 119 func TestFSChecksumV1MarshalJSON(t *testing.T) { 120 var cs FSChecksumInfoV1 121 122 testCases := []struct { 123 checksum FSChecksumInfoV1 124 expectedResult string 125 }{ 126 {cs, `{"algorithm":"","blocksize":0,"hashes":null}`}, 127 {FSChecksumInfoV1{Algorithm: "highwayhash", Blocksize: 500}, `{"algorithm":"highwayhash","blocksize":500,"hashes":null}`}, 128 {FSChecksumInfoV1{Algorithm: "highwayhash", Blocksize: 10, Hashes: [][]byte{[]byte("hello")}}, `{"algorithm":"highwayhash","blocksize":10,"hashes":["68656c6c6f"]}`}, 129 } 130 131 for _, testCase := range testCases { 132 data, _ := testCase.checksum.MarshalJSON() 133 if testCase.expectedResult != string(data) { 134 t.Fatalf("expected: %v, got: %v", testCase.expectedResult, string(data)) 135 } 136 } 137 } 138 139 func TestFSChecksumV1UnMarshalJSON(t *testing.T) { 140 var cs FSChecksumInfoV1 141 142 testCases := []struct { 143 data []byte 144 expectedResult FSChecksumInfoV1 145 }{ 146 {[]byte(`{"algorithm":"","blocksize":0,"hashes":null}`), cs}, 147 {[]byte(`{"algorithm":"highwayhash","blocksize":500,"hashes":null}`), FSChecksumInfoV1{Algorithm: "highwayhash", Blocksize: 500}}, 148 {[]byte(`{"algorithm":"highwayhash","blocksize":10,"hashes":["68656c6c6f"]}`), FSChecksumInfoV1{Algorithm: "highwayhash", Blocksize: 10, Hashes: [][]byte{[]byte("hello")}}}, 149 } 150 151 for _, testCase := range testCases { 152 err := (&cs).UnmarshalJSON(testCase.data) 153 if err != nil { 154 t.Fatal("Unexpected error during checksum unmarshalling ", err) 155 } 156 if !reflect.DeepEqual(testCase.expectedResult, cs) { 157 t.Fatalf("expected: %v, got: %v", testCase.expectedResult, cs) 158 } 159 } 160 }