github.com/prysmaticlabs/prysm@v1.4.4/validator/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  	"github.com/prysmaticlabs/prysm/shared/testutil/require"
    11  )
    12  
    13  func TestStore_Backup(t *testing.T) {
    14  	db := setupDB(t, nil)
    15  	ctx := context.Background()
    16  	root := [32]byte{1}
    17  	require.NoError(t, db.SaveGenesisValidatorsRoot(ctx, root[:]))
    18  	require.NoError(t, db.Backup(ctx, "", true))
    19  
    20  	backupsPath := filepath.Join(db.databasePath, backupsDirectoryName)
    21  	files, err := ioutil.ReadDir(backupsPath)
    22  	require.NoError(t, err)
    23  	require.NotEqual(t, 0, len(files), "No backups created")
    24  	require.NoError(t, db.Close(), "Failed to close database")
    25  
    26  	oldFilePath := filepath.Join(backupsPath, files[0].Name())
    27  	newFilePath := filepath.Join(backupsPath, ProtectionDbFileName)
    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(ctx, 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  	genesisRoot, err := backedDB.GenesisValidatorsRoot(ctx)
    38  	require.NoError(t, err)
    39  	require.DeepEqual(t, root[:], genesisRoot)
    40  }