prysm-pulse/slasher/db/restore_test.go
terence tsao 3edfa8cb88
Use Custom Type ValidatorIndex Across Prysm (#8478)
* Use ValidtorIndex across Prysm. Build ok

* First take at fixing tests

* Clean up e2e, fuzz... etc

* Fix new lines

* Update beacon-chain/cache/proposer_indices_test.go

Co-authored-by: Raul Jordan <raul@prysmaticlabs.com>

* Update beacon-chain/core/helpers/rewards_penalties.go

Co-authored-by: Raul Jordan <raul@prysmaticlabs.com>

* Update beacon-chain/core/helpers/shuffle.go

Co-authored-by: Raul Jordan <raul@prysmaticlabs.com>

* Update validator/graffiti/parse_graffiti_test.go

Co-authored-by: Raul Jordan <raul@prysmaticlabs.com>

* Raul's feedback

* Fix downcast int -> uint64

* Victor's feedback

Co-authored-by: Raul Jordan <raul@prysmaticlabs.com>
Co-authored-by: prylabs-bulldozer[bot] <58059840+prylabs-bulldozer[bot]@users.noreply.github.com>
2021-02-23 00:14:50 +00:00

63 lines
2.1 KiB
Go

package db
import (
"context"
"flag"
"io/ioutil"
"os"
"path"
"testing"
types "github.com/prysmaticlabs/eth2-types"
"github.com/prysmaticlabs/prysm/shared/cmd"
"github.com/prysmaticlabs/prysm/shared/testutil/assert"
"github.com/prysmaticlabs/prysm/shared/testutil/require"
"github.com/prysmaticlabs/prysm/slasher/db/kv"
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(t.TempDir(), &kv.Config{})
defer func() {
require.NoError(t, backupDb.Close())
}()
require.NoError(t, err)
pubKey := []byte("hello")
require.NoError(t, backupDb.SavePubKey(ctx, types.ValidatorIndex(1), pubKey))
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.SlasherDbDirName))
require.NoError(t, err)
assert.Equal(t, 1, len(files))
assert.Equal(t, kv.DatabaseFileName, files[0].Name())
restoredDb, err := kv.NewKVStore(path.Join(restoreDir, kv.SlasherDbDirName), &kv.Config{})
defer func() {
require.NoError(t, restoredDb.Close())
}()
require.NoError(t, err)
received, err := restoredDb.ValidatorPubKey(ctx, types.ValidatorIndex(1))
require.NoError(t, err)
require.DeepEqual(t, pubKey, received, "Restored database has incorrect data")
assert.LogsContain(t, logHook, "Restore completed successfully")
}