mirror of
https://gitlab.com/pulsechaincom/prysm-pulse.git
synced 2024-12-25 12:57:18 +00:00
71 lines
2.3 KiB
Go
71 lines
2.3 KiB
Go
package db
|
|
|
|
import (
|
|
"context"
|
|
"flag"
|
|
"io/ioutil"
|
|
"os"
|
|
"path"
|
|
"testing"
|
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/db/kv"
|
|
"github.com/prysmaticlabs/prysm/shared/cmd"
|
|
"github.com/prysmaticlabs/prysm/shared/testutil"
|
|
"github.com/prysmaticlabs/prysm/shared/testutil/assert"
|
|
"github.com/prysmaticlabs/prysm/shared/testutil/require"
|
|
logTest "github.com/sirupsen/logrus/hooks/test"
|
|
"github.com/urfave/cli/v2"
|
|
)
|
|
|
|
func TestRestore(t *testing.T) {
|
|
logHook := logTest.NewGlobal()
|
|
ctx := context.Background()
|
|
|
|
backupDb, err := kv.NewKVStore(context.Background(), t.TempDir())
|
|
defer func() {
|
|
require.NoError(t, backupDb.Close())
|
|
}()
|
|
require.NoError(t, err)
|
|
head := testutil.NewBeaconBlock()
|
|
head.Block.Slot = 5000
|
|
require.NoError(t, backupDb.SaveBlock(ctx, head))
|
|
root, err := head.Block.HashTreeRoot()
|
|
require.NoError(t, err)
|
|
st := testutil.NewBeaconState()
|
|
require.NoError(t, backupDb.SaveState(ctx, st, root))
|
|
require.NoError(t, backupDb.SaveHeadBlockRoot(ctx, root))
|
|
require.NoError(t, err)
|
|
require.NoError(t, backupDb.Close())
|
|
// We rename the backup file so that we can later verify
|
|
// whether the restored db has been renamed correctly.
|
|
require.NoError(t, os.Rename(
|
|
path.Join(backupDb.DatabasePath(), kv.DatabaseFileName),
|
|
path.Join(backupDb.DatabasePath(), "backup.db")))
|
|
|
|
restoreDir := t.TempDir()
|
|
app := cli.App{}
|
|
set := flag.NewFlagSet("test", 0)
|
|
set.String(cmd.RestoreSourceFileFlag.Name, "", "")
|
|
set.String(cmd.RestoreTargetDirFlag.Name, "", "")
|
|
require.NoError(t, set.Set(cmd.RestoreSourceFileFlag.Name, path.Join(backupDb.DatabasePath(), "backup.db")))
|
|
require.NoError(t, set.Set(cmd.RestoreTargetDirFlag.Name, restoreDir))
|
|
cliCtx := cli.NewContext(&app, set, nil)
|
|
|
|
assert.NoError(t, restore(cliCtx))
|
|
|
|
files, err := ioutil.ReadDir(path.Join(restoreDir, kv.BeaconNodeDbDirName))
|
|
require.NoError(t, err)
|
|
assert.Equal(t, 1, len(files))
|
|
assert.Equal(t, kv.DatabaseFileName, files[0].Name())
|
|
restoredDb, err := kv.NewKVStore(context.Background(), path.Join(restoreDir, kv.BeaconNodeDbDirName))
|
|
defer func() {
|
|
require.NoError(t, restoredDb.Close())
|
|
}()
|
|
require.NoError(t, err)
|
|
headBlock, err := restoredDb.HeadBlock(ctx)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, uint64(5000), headBlock.Block.Slot, "Restored database has incorrect data")
|
|
assert.LogsContain(t, logHook, "Restore completed successfully")
|
|
|
|
}
|