github.com/cockroachdb/pebble@v0.0.0-20231214172447-ab4952c5f87b/internal/base/filenames_test.go (about) 1 // Copyright 2012 The LevelDB-Go and Pebble Authors. All rights reserved. Use 2 // of this source code is governed by a BSD-style license that can be found in 3 // the LICENSE file. 4 5 package base 6 7 import ( 8 "bytes" 9 "fmt" 10 "os" 11 "testing" 12 13 "github.com/cockroachdb/pebble/vfs" 14 "github.com/cockroachdb/redact" 15 "github.com/stretchr/testify/require" 16 ) 17 18 func TestParseFilename(t *testing.T) { 19 testCases := map[string]bool{ 20 "000000.log": true, 21 "000000.log.zip": false, 22 "000000..log": false, 23 "a000000.log": false, 24 "abcdef.log": false, 25 "000001ldb": false, 26 "000001.sst": true, 27 "CURRENT": true, 28 "CURRaNT": false, 29 "LOCK": true, 30 "xLOCK": false, 31 "x.LOCK": false, 32 "MANIFEST": false, 33 "MANIFEST123456": false, 34 "MANIFEST-": false, 35 "MANIFEST-123456": true, 36 "MANIFEST-123456.doc": false, 37 "OPTIONS": false, 38 "OPTIONS123456": false, 39 "OPTIONS-": false, 40 "OPTIONS-123456": true, 41 "OPTIONS-123456.doc": false, 42 "CURRENT.123456": false, 43 "CURRENT.dbtmp": false, 44 "CURRENT.123456.dbtmp": true, 45 "temporary.123456.dbtmp": true, 46 } 47 fs := vfs.NewMem() 48 for tc, want := range testCases { 49 _, _, got := ParseFilename(fs, fs.PathJoin("foo", tc)) 50 if got != want { 51 t.Errorf("%q: got %v, want %v", tc, got, want) 52 } 53 } 54 } 55 56 func TestFilenameRoundTrip(t *testing.T) { 57 testCases := map[FileType]bool{ 58 // CURRENT and LOCK files aren't numbered. 59 FileTypeCurrent: false, 60 FileTypeLock: false, 61 // The remaining file types are numbered. 62 FileTypeLog: true, 63 FileTypeManifest: true, 64 FileTypeTable: true, 65 FileTypeOptions: true, 66 FileTypeOldTemp: true, 67 FileTypeTemp: true, 68 } 69 fs := vfs.NewMem() 70 for fileType, numbered := range testCases { 71 fileNums := []FileNum{0} 72 if numbered { 73 fileNums = []FileNum{0, 1, 2, 3, 10, 42, 99, 1001} 74 } 75 for _, fileNum := range fileNums { 76 filename := MakeFilepath(fs, "foo", fileType, fileNum.DiskFileNum()) 77 gotFT, gotFN, gotOK := ParseFilename(fs, filename) 78 if !gotOK { 79 t.Errorf("could not parse %q", filename) 80 continue 81 } 82 if gotFT != fileType || gotFN.FileNum() != fileNum { 83 t.Errorf("filename=%q: got %v, %v, want %v, %v", filename, gotFT, gotFN, fileType, fileNum) 84 continue 85 } 86 } 87 } 88 } 89 90 type bufferFataler struct { 91 buf bytes.Buffer 92 } 93 94 func (b *bufferFataler) Fatalf(msg string, args ...interface{}) { 95 fmt.Fprintf(&b.buf, msg, args...) 96 } 97 98 func TestMustExist(t *testing.T) { 99 err := os.ErrNotExist 100 fs := vfs.Default 101 var buf bufferFataler 102 filename := fs.PathJoin("..", "..", "testdata", "db-stage-4", "000000.sst") 103 104 MustExist(fs, filename, &buf, err) 105 require.Equal(t, `000000.sst: 106 file does not exist 107 directory contains 9 files, 2 unknown, 1 tables, 1 logs, 2 manifests`, buf.buf.String()) 108 } 109 110 func TestRedactFileNum(t *testing.T) { 111 // Ensure that redaction never redacts file numbers. 112 require.Equal(t, redact.RedactableString("000005"), redact.Sprint(FileNum(5))) 113 require.Equal(t, redact.RedactableString("000005"), redact.Sprint(DiskFileNum(5))) 114 }