2020-12-03 22:28:57 +00:00
|
|
|
package kv
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
|
|
|
|
2021-02-23 00:14:50 +00:00
|
|
|
types "github.com/prysmaticlabs/eth2-types"
|
2020-12-03 22:28:57 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/testutil/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestStore_Backup(t *testing.T) {
|
|
|
|
db := setupDB(t)
|
|
|
|
ctx := context.Background()
|
|
|
|
pubKey := []byte("hello")
|
2021-02-23 00:14:50 +00:00
|
|
|
require.NoError(t, db.SavePubKey(ctx, types.ValidatorIndex(1), pubKey))
|
2020-12-03 22:28:57 +00:00
|
|
|
require.NoError(t, db.Backup(ctx, ""))
|
|
|
|
|
|
|
|
backupsPath := filepath.Join(db.databasePath, backupsDirectoryName)
|
|
|
|
files, err := ioutil.ReadDir(backupsPath)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotEqual(t, 0, len(files), "No backups created")
|
|
|
|
|
|
|
|
oldFilePath := filepath.Join(backupsPath, files[0].Name())
|
2020-12-07 20:36:43 +00:00
|
|
|
newFilePath := filepath.Join(backupsPath, DatabaseFileName)
|
2020-12-03 22:28:57 +00:00
|
|
|
// We rename the file to match the database file name
|
|
|
|
// our NewKVStore function expects when opening a database.
|
|
|
|
require.NoError(t, os.Rename(oldFilePath, newFilePath))
|
|
|
|
|
|
|
|
backedDB, err := NewKVStore(backupsPath, &Config{})
|
|
|
|
require.NoError(t, err, "Failed to instantiate DB")
|
|
|
|
t.Cleanup(func() {
|
|
|
|
require.NoError(t, backedDB.Close(), "Failed to close database")
|
|
|
|
})
|
2021-02-23 00:14:50 +00:00
|
|
|
received, err := backedDB.ValidatorPubKey(ctx, types.ValidatorIndex(1))
|
2020-12-03 22:28:57 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.DeepEqual(t, pubKey, received)
|
|
|
|
}
|