storj.io/minio@v0.0.0-20230509071714-0cbc90f649b1/cmd/format-fs_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 cmd 18 19 import ( 20 "context" 21 "os" 22 "path/filepath" 23 "testing" 24 ) 25 26 // TestFSFormatFS - tests initFormatFS, formatMetaGetFormatBackendFS, formatFSGetVersion. 27 func TestFSFormatFS(t *testing.T) { 28 // Prepare for testing 29 disk := filepath.Join(globalTestTmpDir, "minio-"+nextSuffix()) 30 defer os.RemoveAll(disk) 31 32 fsFormatPath := pathJoin(disk, minioMetaBucket, formatConfigFile) 33 34 // Assign a new UUID. 35 uuid := mustGetUUID() 36 37 // Initialize meta volume, if volume already exists ignores it. 38 if err := initMetaVolumeFS(disk, uuid); err != nil { 39 t.Fatal(err) 40 } 41 42 rlk, err := initFormatFS(context.Background(), disk) 43 if err != nil { 44 t.Fatal(err) 45 } 46 rlk.Close() 47 48 // Do the basic sanity checks to check if initFormatFS() did its job. 49 f, err := os.OpenFile(fsFormatPath, os.O_RDWR|os.O_SYNC, 0) 50 if err != nil { 51 t.Fatal(err) 52 } 53 defer f.Close() 54 55 format, err := formatMetaGetFormatBackendFS(f) 56 if err != nil { 57 t.Fatal(err) 58 } 59 if format != formatBackendFS { 60 t.Fatalf(`expected: %s, got: %s`, formatBackendFS, format) 61 } 62 version, err := formatFSGetVersion(f) 63 if err != nil { 64 t.Fatal(err) 65 } 66 if version != formatFSVersionV2 { 67 t.Fatalf(`expected: %s, got: %s`, formatFSVersionV2, version) 68 } 69 70 // Corrupt the format.json file and test the functions. 71 // formatMetaGetFormatBackendFS, formatFSGetVersion, initFormatFS should return errors. 72 if err = f.Truncate(0); err != nil { 73 t.Fatal(err) 74 } 75 if _, err = f.WriteString("b"); err != nil { 76 t.Fatal(err) 77 } 78 79 if _, err = formatMetaGetFormatBackendFS(f); err == nil { 80 t.Fatal("expected to fail") 81 } 82 if _, err = formatFSGetVersion(rlk); err == nil { 83 t.Fatal("expected to fail") 84 } 85 if _, err = initFormatFS(context.Background(), disk); err == nil { 86 t.Fatal("expected to fail") 87 } 88 89 // With unknown formatMetaV1.Version formatMetaGetFormatBackendFS, initFormatFS should return error. 90 if err = f.Truncate(0); err != nil { 91 t.Fatal(err) 92 } 93 // Here we set formatMetaV1.Version to "2" 94 if _, err = f.WriteString(`{"version":"2","format":"fs","fs":{"version":"1"}}`); err != nil { 95 t.Fatal(err) 96 } 97 if _, err = formatMetaGetFormatBackendFS(f); err == nil { 98 t.Fatal("expected to fail") 99 } 100 if _, err = initFormatFS(context.Background(), disk); err == nil { 101 t.Fatal("expected to fail") 102 } 103 }