github.com/zuoyebang/bitalostable@v1.0.1-0.20240229032404-e3b99a834294/internal/metamorphic/options_test.go (about) 1 // Copyright 2022 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 metamorphic 6 7 import ( 8 "os" 9 "path/filepath" 10 "testing" 11 12 "github.com/stretchr/testify/require" 13 "github.com/zuoyebang/bitalostable" 14 "github.com/zuoyebang/bitalostable/internal/testkeys" 15 "github.com/zuoyebang/bitalostable/vfs" 16 ) 17 18 func TestSetupInitialState(t *testing.T) { 19 // Construct a small database in the test's TempDir. 20 initialStatePath := t.TempDir() 21 { 22 d, err := bitalostable.Open(initialStatePath, &bitalostable.Options{}) 23 require.NoError(t, err) 24 const maxKeyLen = 2 25 ks := testkeys.Alpha(maxKeyLen) 26 var key [maxKeyLen]byte 27 for i := 0; i < ks.Count(); i++ { 28 n := testkeys.WriteKey(key[:], ks, i) 29 require.NoError(t, d.Set(key[:n], key[:n], bitalostable.NoSync)) 30 if i%100 == 0 { 31 require.NoError(t, d.Flush()) 32 } 33 } 34 require.NoError(t, d.Close()) 35 } 36 require.NoError(t, vfs.Default.MkdirAll(filepath.Join(initialStatePath, "wal"), os.ModePerm)) 37 ls, err := vfs.Default.List(initialStatePath) 38 require.NoError(t, err) 39 40 // setupInitialState with an initial state path set to the test's TempDir 41 // should populate opts.opts.FS with the directory's contents. 42 opts := &testOptions{ 43 opts: defaultOptions(), 44 initialStatePath: initialStatePath, 45 initialStateDesc: "test", 46 } 47 require.NoError(t, setupInitialState("", opts)) 48 copied, err := opts.opts.FS.List("") 49 require.NoError(t, err) 50 require.ElementsMatch(t, ls, copied) 51 }