github.com/prysmaticlabs/prysm@v1.4.4/slasher/db/kv/backup_test.go (about)

     1  package kv
     2  
     3  import (
     4  	"context"
     5  	"io/ioutil"
     6  	"os"
     7  	"path/filepath"
     8  	"testing"
     9  
    10  	types "github.com/prysmaticlabs/eth2-types"
    11  	"github.com/prysmaticlabs/prysm/shared/testutil/require"
    12  )
    13  
    14  func TestStore_Backup(t *testing.T) {
    15  	db := setupDB(t)
    16  	ctx := context.Background()
    17  	pubKey := []byte("hello")
    18  	require.NoError(t, db.SavePubKey(ctx, types.ValidatorIndex(1), pubKey))
    19  	require.NoError(t, db.Backup(ctx, "", false))
    20  
    21  	backupsPath := filepath.Join(db.databasePath, backupsDirectoryName)
    22  	files, err := ioutil.ReadDir(backupsPath)
    23  	require.NoError(t, err)
    24  	require.NotEqual(t, 0, len(files), "No backups created")
    25  
    26  	oldFilePath := filepath.Join(backupsPath, files[0].Name())
    27  	newFilePath := filepath.Join(backupsPath, DatabaseFileName)
    28  	// We rename the file to match the database file name
    29  	// our NewKVStore function expects when opening a database.
    30  	require.NoError(t, os.Rename(oldFilePath, newFilePath))
    31  
    32  	backedDB, err := NewKVStore(backupsPath, &Config{})
    33  	require.NoError(t, err, "Failed to instantiate DB")
    34  	t.Cleanup(func() {
    35  		require.NoError(t, backedDB.Close(), "Failed to close database")
    36  	})
    37  	received, err := backedDB.ValidatorPubKey(ctx, types.ValidatorIndex(1))
    38  	require.NoError(t, err)
    39  	require.DeepEqual(t, pubKey, received)
    40  }