forked from TrueCloudLab/frostfs-node
70 lines
1.7 KiB
Go
70 lines
1.7 KiB
Go
|
package blobovniczatree
|
||
|
|
||
|
import (
|
||
|
"os"
|
||
|
"path/filepath"
|
||
|
"strings"
|
||
|
"testing"
|
||
|
|
||
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/blobovnicza"
|
||
|
"github.com/stretchr/testify/require"
|
||
|
)
|
||
|
|
||
|
func TestDBExtensionFix(t *testing.T) {
|
||
|
root := t.TempDir()
|
||
|
createTestTree(t, 0, 2, 3, root)
|
||
|
t.Run("adds suffix if not exists", func(t *testing.T) {
|
||
|
openAndCloseTestTree(t, 2, 3, root)
|
||
|
validateTestTree(t, root)
|
||
|
})
|
||
|
|
||
|
t.Run("not adds second suffix if exists", func(t *testing.T) {
|
||
|
openAndCloseTestTree(t, 2, 3, root)
|
||
|
validateTestTree(t, root)
|
||
|
})
|
||
|
}
|
||
|
|
||
|
func createTestTree(t *testing.T, currentDepth, depth, width uint64, path string) {
|
||
|
if currentDepth == depth {
|
||
|
var w uint64
|
||
|
for ; w < width; w++ {
|
||
|
dbPath := filepath.Join(path, u64ToHexString(w))
|
||
|
b := blobovnicza.New(blobovnicza.WithPath(dbPath))
|
||
|
require.NoError(t, b.Open())
|
||
|
require.NoError(t, b.Init())
|
||
|
require.NoError(t, b.Close())
|
||
|
}
|
||
|
return
|
||
|
}
|
||
|
|
||
|
var w uint64
|
||
|
for ; w < width; w++ {
|
||
|
createTestTree(t, currentDepth+1, depth, width, filepath.Join(path, u64ToHexString(w)))
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func openAndCloseTestTree(t *testing.T, depth, width uint64, path string) {
|
||
|
blz := NewBlobovniczaTree(
|
||
|
WithBlobovniczaShallowDepth(depth),
|
||
|
WithBlobovniczaShallowWidth(width),
|
||
|
WithRootPath(path),
|
||
|
)
|
||
|
require.NoError(t, blz.Open(false))
|
||
|
require.NoError(t, blz.Init())
|
||
|
require.NoError(t, blz.Close())
|
||
|
}
|
||
|
|
||
|
func validateTestTree(t *testing.T, path string) {
|
||
|
entries, err := os.ReadDir(path)
|
||
|
require.NoError(t, err)
|
||
|
|
||
|
for _, entry := range entries {
|
||
|
if entry.IsDir() {
|
||
|
validateTestTree(t, filepath.Join(path, entry.Name()))
|
||
|
} else {
|
||
|
require.True(t, strings.HasSuffix(entry.Name(), dbExtension))
|
||
|
require.False(t, strings.HasSuffix(strings.TrimSuffix(entry.Name(), dbExtension), dbExtension))
|
||
|
}
|
||
|
}
|
||
|
}
|