github.com/minio/minio@v0.0.0-20240328213742-3f72439b8a27/cmd/erasure-metadata_test.go (about) 1 // Copyright (c) 2015-2021 MinIO, Inc. 2 // 3 // This file is part of MinIO Object Storage stack 4 // 5 // This program is free software: you can redistribute it and/or modify 6 // it under the terms of the GNU Affero General Public License as published by 7 // the Free Software Foundation, either version 3 of the License, or 8 // (at your option) any later version. 9 // 10 // This program is distributed in the hope that it will be useful 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 // GNU Affero General Public License for more details. 14 // 15 // You should have received a copy of the GNU Affero General Public License 16 // along with this program. If not, see <http://www.gnu.org/licenses/>. 17 18 package cmd 19 20 import ( 21 "context" 22 "strconv" 23 "testing" 24 "time" 25 26 "github.com/dustin/go-humanize" 27 ) 28 29 const ActualSize = 1000 30 31 // Test FileInfo.AddObjectPart() 32 func TestAddObjectPart(t *testing.T) { 33 testCases := []struct { 34 partNum int 35 expectedIndex int 36 }{ 37 {1, 0}, 38 {2, 1}, 39 {4, 2}, 40 {5, 3}, 41 {7, 4}, 42 // Insert part. 43 {3, 2}, 44 // Replace existing part. 45 {4, 3}, 46 // Missing part. 47 {6, -1}, 48 } 49 50 // Setup. 51 fi := newFileInfo("test-object", 8, 8) 52 fi.Erasure.Index = 1 53 if !fi.IsValid() { 54 t.Fatalf("unable to get xl meta") 55 } 56 57 // Test them. 58 for _, testCase := range testCases { 59 if testCase.expectedIndex > -1 { 60 partNumString := strconv.Itoa(testCase.partNum) 61 fi.AddObjectPart(testCase.partNum, "etag."+partNumString, int64(testCase.partNum+humanize.MiByte), ActualSize, UTCNow(), nil, nil) 62 } 63 64 if index := objectPartIndex(fi.Parts, testCase.partNum); index != testCase.expectedIndex { 65 t.Fatalf("%+v: expected = %d, got: %d", testCase, testCase.expectedIndex, index) 66 } 67 } 68 } 69 70 // Test objectPartIndex(). generates a sample FileInfo data and asserts 71 // the output of objectPartIndex() with the expected value. 72 func TestObjectPartIndex(t *testing.T) { 73 testCases := []struct { 74 partNum int 75 expectedIndex int 76 }{ 77 {2, 1}, 78 {1, 0}, 79 {5, 3}, 80 {4, 2}, 81 {7, 4}, 82 } 83 84 // Setup. 85 fi := newFileInfo("test-object", 8, 8) 86 fi.Erasure.Index = 1 87 if !fi.IsValid() { 88 t.Fatalf("unable to get xl meta") 89 } 90 91 // Add some parts for testing. 92 for _, testCase := range testCases { 93 partNumString := strconv.Itoa(testCase.partNum) 94 fi.AddObjectPart(testCase.partNum, "etag."+partNumString, int64(testCase.partNum+humanize.MiByte), ActualSize, UTCNow(), nil, nil) 95 } 96 97 // Add failure test case. 98 testCases = append(testCases, struct { 99 partNum int 100 expectedIndex int 101 }{6, -1}) 102 103 // Test them. 104 for _, testCase := range testCases { 105 if index := objectPartIndex(fi.Parts, testCase.partNum); index != testCase.expectedIndex { 106 t.Fatalf("%+v: expected = %d, got: %d", testCase, testCase.expectedIndex, index) 107 } 108 } 109 } 110 111 // Test FileInfo.ObjectToPartOffset(). 112 func TestObjectToPartOffset(t *testing.T) { 113 // Setup. 114 fi := newFileInfo("test-object", 8, 8) 115 fi.Erasure.Index = 1 116 if !fi.IsValid() { 117 t.Fatalf("unable to get xl meta") 118 } 119 120 // Add some parts for testing. 121 // Total size of all parts is 5,242,899 bytes. 122 for _, partNum := range []int{1, 2, 4, 5, 7} { 123 partNumString := strconv.Itoa(partNum) 124 fi.AddObjectPart(partNum, "etag."+partNumString, int64(partNum+humanize.MiByte), ActualSize, UTCNow(), nil, nil) 125 } 126 127 testCases := []struct { 128 offset int64 129 expectedIndex int 130 expectedOffset int64 131 expectedErr error 132 }{ 133 {0, 0, 0, nil}, 134 {1 * humanize.MiByte, 0, 1 * humanize.MiByte, nil}, 135 {1 + humanize.MiByte, 1, 0, nil}, 136 {2 + humanize.MiByte, 1, 1, nil}, 137 // Its valid for zero sized object. 138 {-1, 0, -1, nil}, 139 // Max fffset is always (size - 1). 140 {(1 + 2 + 4 + 5 + 7) + (5 * humanize.MiByte) - 1, 4, 1048582, nil}, 141 // Error if offset is size. 142 {(1 + 2 + 4 + 5 + 7) + (5 * humanize.MiByte), 0, 0, InvalidRange{}}, 143 } 144 145 // Test them. 146 for _, testCase := range testCases { 147 index, offset, err := fi.ObjectToPartOffset(context.Background(), testCase.offset) 148 if err != testCase.expectedErr { 149 t.Fatalf("%+v: expected = %s, got: %s", testCase, testCase.expectedErr, err) 150 } 151 if index != testCase.expectedIndex { 152 t.Fatalf("%+v: index: expected = %d, got: %d", testCase, testCase.expectedIndex, index) 153 } 154 if offset != testCase.expectedOffset { 155 t.Fatalf("%+v: offset: expected = %d, got: %d", testCase, testCase.expectedOffset, offset) 156 } 157 } 158 } 159 160 func TestFindFileInfoInQuorum(t *testing.T) { 161 getNFInfo := func(n int, quorum int, t int64, dataDir string, succModTimes []time.Time) []FileInfo { 162 fi := newFileInfo("test", 8, 8) 163 fi.AddObjectPart(1, "etag", 100, 100, UTCNow(), nil, nil) 164 fi.ModTime = time.Unix(t, 0) 165 fi.DataDir = dataDir 166 fis := make([]FileInfo, n) 167 for i := range fis { 168 fis[i] = fi 169 fis[i].Erasure.Index = i + 1 170 if succModTimes != nil { 171 fis[i].SuccessorModTime = succModTimes[i] 172 fis[i].IsLatest = succModTimes[i].IsZero() 173 } 174 quorum-- 175 if quorum == 0 { 176 break 177 } 178 } 179 return fis 180 } 181 182 commonSuccModTime := time.Date(2023, time.August, 25, 0, 0, 0, 0, time.UTC) 183 succModTimesInQuorum := make([]time.Time, 16) 184 succModTimesNoQuorum := make([]time.Time, 16) 185 for i := 0; i < 16; i++ { 186 if i < 4 { 187 continue 188 } 189 succModTimesInQuorum[i] = commonSuccModTime 190 if i < 9 { 191 continue 192 } 193 succModTimesNoQuorum[i] = commonSuccModTime 194 } 195 tests := []struct { 196 fis []FileInfo 197 modTime time.Time 198 succmodTimes []time.Time 199 expectedErr error 200 expectedQuorum int 201 expectedSuccModTime time.Time 202 expectedIsLatest bool 203 }{ 204 { 205 fis: getNFInfo(16, 16, 1603863445, "36a21454-a2ca-11eb-bbaa-93a81c686f21", nil), 206 modTime: time.Unix(1603863445, 0), 207 expectedErr: nil, 208 expectedQuorum: 8, 209 }, 210 { 211 fis: getNFInfo(16, 7, 1603863445, "36a21454-a2ca-11eb-bbaa-93a81c686f21", nil), 212 modTime: time.Unix(1603863445, 0), 213 expectedErr: errErasureReadQuorum, 214 expectedQuorum: 8, 215 }, 216 { 217 fis: getNFInfo(16, 16, 1603863445, "36a21454-a2ca-11eb-bbaa-93a81c686f21", nil), 218 modTime: time.Unix(1603863445, 0), 219 expectedErr: errErasureReadQuorum, 220 expectedQuorum: 0, 221 }, 222 { 223 fis: getNFInfo(16, 16, 1603863445, "36a21454-a2ca-11eb-bbaa-93a81c686f21", succModTimesInQuorum), 224 modTime: time.Unix(1603863445, 0), 225 expectedErr: nil, 226 expectedQuorum: 12, 227 expectedSuccModTime: commonSuccModTime, 228 expectedIsLatest: false, 229 }, 230 { 231 fis: getNFInfo(16, 16, 1603863445, "36a21454-a2ca-11eb-bbaa-93a81c686f21", succModTimesNoQuorum), 232 modTime: time.Unix(1603863445, 0), 233 expectedErr: nil, 234 expectedQuorum: 12, 235 expectedSuccModTime: time.Time{}, 236 expectedIsLatest: true, 237 }, 238 } 239 240 for _, test := range tests { 241 test := test 242 t.Run("", func(t *testing.T) { 243 fi, err := findFileInfoInQuorum(context.Background(), test.fis, test.modTime, "", test.expectedQuorum) 244 if err != test.expectedErr { 245 t.Errorf("Expected %s, got %s", test.expectedErr, err) 246 } 247 if test.succmodTimes != nil { 248 if !test.expectedSuccModTime.Equal(fi.SuccessorModTime) { 249 t.Errorf("Expected successor mod time to be %v but got %v", test.expectedSuccModTime, fi.SuccessorModTime) 250 } 251 if test.expectedIsLatest != fi.IsLatest { 252 t.Errorf("Expected IsLatest to be %v but got %v", test.expectedIsLatest, fi.IsLatest) 253 } 254 } 255 }) 256 } 257 } 258 259 func TestTransitionInfoEquals(t *testing.T) { 260 inputs := []struct { 261 tier string 262 remoteObjName string 263 remoteVersionID string 264 status string 265 }{ 266 { 267 tier: "S3TIER-1", 268 remoteObjName: mustGetUUID(), 269 remoteVersionID: mustGetUUID(), 270 status: "complete", 271 }, 272 { 273 tier: "S3TIER-2", 274 remoteObjName: mustGetUUID(), 275 remoteVersionID: mustGetUUID(), 276 status: "complete", 277 }, 278 } 279 280 var i uint 281 for i = 0; i < 8; i++ { 282 fi := FileInfo{ 283 TransitionTier: inputs[0].tier, 284 TransitionedObjName: inputs[0].remoteObjName, 285 TransitionVersionID: inputs[0].remoteVersionID, 286 TransitionStatus: inputs[0].status, 287 } 288 ofi := fi 289 if i&(1<<0) != 0 { 290 ofi.TransitionTier = inputs[1].tier 291 } 292 if i&(1<<1) != 0 { 293 ofi.TransitionedObjName = inputs[1].remoteObjName 294 } 295 if i&(1<<2) != 0 { 296 ofi.TransitionVersionID = inputs[1].remoteVersionID 297 } 298 actual := fi.TransitionInfoEquals(ofi) 299 if i == 0 && !actual { 300 t.Fatalf("Test %d: Expected FileInfo's transition info to be equal: fi %v ofi %v", i, fi, ofi) 301 } 302 if i != 0 && actual { 303 t.Fatalf("Test %d: Expected FileInfo's transition info to be inequal: fi %v ofi %v", i, fi, ofi) 304 } 305 } 306 fi := FileInfo{ 307 TransitionTier: inputs[0].tier, 308 TransitionedObjName: inputs[0].remoteObjName, 309 TransitionVersionID: inputs[0].remoteVersionID, 310 TransitionStatus: inputs[0].status, 311 } 312 ofi := FileInfo{} 313 if fi.TransitionInfoEquals(ofi) { 314 t.Fatalf("Expected to be inequal: fi %v ofi %v", fi, ofi) 315 } 316 } 317 318 func TestSkipTierFreeVersion(t *testing.T) { 319 fi := newFileInfo("object", 8, 8) 320 fi.SetSkipTierFreeVersion() 321 if ok := fi.SkipTierFreeVersion(); !ok { 322 t.Fatal("Expected SkipTierFreeVersion to be set on FileInfo but wasn't") 323 } 324 }