erigon-pulse/ethdb/kv_migrator_test.go

106 lines
2.7 KiB
Go
Raw Normal View History

package ethdb
import (
"context"
"errors"
"testing"
2020-10-28 03:18:10 +00:00
"github.com/ledgerwatch/erigon/common/dbutils"
2020-10-28 03:18:10 +00:00
"github.com/stretchr/testify/require"
)
func TestBucketCRUD(t *testing.T) {
require := require.New(t)
2021-05-17 09:14:08 +00:00
kv := NewMemKV()
defer kv.Close()
ctx := context.Background()
tx, err := kv.BeginRw(ctx)
require.NoError(err)
defer tx.Rollback()
normalBucket := dbutils.Buckets[15]
deprecatedBucket := dbutils.DeprecatedBuckets[0]
migrator, ok := tx.(BucketMigrator)
if !ok {
return
}
// check thad buckets have unique DBI's
2020-10-28 03:18:10 +00:00
uniquness := map[dbutils.DBI]bool{}
castedKv, ok := kv.(*MdbxKV)
if !ok {
t.Skip()
}
for _, bucketCfg := range castedKv.buckets {
if bucketCfg.DBI == NonExistingDBI {
continue
}
_, ok := uniquness[bucketCfg.DBI]
require.False(ok)
uniquness[bucketCfg.DBI] = true
}
require.True(migrator.ExistsBucket(normalBucket))
require.True(errors.Is(migrator.DropBucket(normalBucket), ErrAttemptToDeleteNonDeprecatedBucket))
require.False(migrator.ExistsBucket(deprecatedBucket))
require.NoError(migrator.CreateBucket(deprecatedBucket))
require.True(migrator.ExistsBucket(deprecatedBucket))
require.NoError(migrator.DropBucket(deprecatedBucket))
require.False(migrator.ExistsBucket(deprecatedBucket))
require.NoError(migrator.CreateBucket(deprecatedBucket))
require.True(migrator.ExistsBucket(deprecatedBucket))
c, err := tx.RwCursor(deprecatedBucket)
require.NoError(err)
err = c.Put([]byte{1}, []byte{1})
require.NoError(err)
v, err := tx.GetOne(deprecatedBucket, []byte{1})
require.NoError(err)
require.Equal([]byte{1}, v)
buckets, err := migrator.ExistingBuckets()
require.NoError(err)
require.True(len(buckets) > 10)
// check thad buckets have unique DBI's
2020-10-28 03:18:10 +00:00
uniquness = map[dbutils.DBI]bool{}
for _, bucketCfg := range castedKv.buckets {
if bucketCfg.DBI == NonExistingDBI {
continue
}
_, ok := uniquness[bucketCfg.DBI]
require.False(ok)
uniquness[bucketCfg.DBI] = true
}
}
func TestReadOnlyMode(t *testing.T) {
2021-05-18 12:13:16 +00:00
path := t.TempDir()
2021-05-20 16:46:12 +00:00
db1 := NewMDBX().Path(path).WithBucketsConfig(func(defaultBuckets dbutils.BucketsCfg) dbutils.BucketsCfg {
return dbutils.BucketsCfg{
dbutils.HeadersBucket: dbutils.BucketConfigItem{},
}
}).MustOpen()
db1.Close()
2021-05-20 16:46:12 +00:00
db2 := NewMDBX().Readonly().Path(path).WithBucketsConfig(func(defaultBuckets dbutils.BucketsCfg) dbutils.BucketsCfg {
return dbutils.BucketsCfg{
dbutils.HeadersBucket: dbutils.BucketConfigItem{},
}
}).MustOpen()
2021-05-20 16:46:12 +00:00
defer db2.Close()
2021-04-03 06:26:00 +00:00
tx, err := db2.BeginRo(context.Background())
require.NoError(t, err)
defer tx.Rollback()
c, err := tx.Cursor(dbutils.HeadersBucket)
require.NoError(t, err)
_, _, err = c.Seek([]byte("some prefix"))
require.NoError(t, err)
}