prysm-pulse/validator/accounts/v2/export_test.go
Ivan Martinez c72db6f96a
Accounts V2: Refactor prompts, deduplicate into directory and password (#6674)
* Refactor input code, deduplicate into directory and password
* Change to filepath
* Rename
* Make consts visible to whole validator package
* Fix
* Try commands and fix
* Fix defaults
* Fixes
* Merge branch 'master' into refactor-input-prompts
* Merge refs/heads/master into refactor-input-prompts
* Merge refs/heads/master into refactor-input-prompts
* Merge refs/heads/master into refactor-input-prompts
* Merge refs/heads/master into refactor-input-prompts
* Remove consts and add comment
* Merge branch 'refactor-input-prompts' of github.com:prysmaticlabs/prysm into refactor-input-prompts
* Undo
* Fixes
* Update validator/accounts/v2/prompt.go
* Merge refs/heads/master into refactor-input-prompts
* gofmt
* Merge refs/heads/master into refactor-input-prompts
* Merge refs/heads/master into refactor-input-prompts
* Merge refs/heads/master into refactor-input-prompts
* Merge refs/heads/master into refactor-input-prompts
* Merge branch 'master' into refactor-input-prompts
* Merge refs/heads/master into refactor-input-prompts
* Fix
* Merge refs/heads/master into refactor-input-prompts
* Merge refs/heads/master into refactor-input-prompts
* Accounts V2: Simplify Wallet Save/Read To and From Disk Functions (#6686)

* simplify wallet functions

* fix build

* futher simplify wallet

* simplify read/write methods

* move direct functions to direct keymanager

* further move direct km specific funcs

* cleanup

* simplify the direct tests

* fixed tests

* lint

* further simplify

* tidy

* fix config write

* fixed test

Co-authored-by: prylabs-bulldozer[bot] <58059840+prylabs-bulldozer[bot]@users.noreply.github.com>
* conflict resolution
* trim
2020-07-23 03:10:23 +00:00

107 lines
3.3 KiB
Go

package v2
import (
"context"
"crypto/rand"
"fmt"
"math/big"
"os"
"path/filepath"
"strings"
"testing"
"github.com/prysmaticlabs/prysm/shared/testutil"
"github.com/prysmaticlabs/prysm/shared/testutil/require"
v2keymanager "github.com/prysmaticlabs/prysm/validator/keymanager/v2"
"github.com/prysmaticlabs/prysm/validator/keymanager/v2/direct"
)
func TestZipAndUnzip(t *testing.T) {
walletDir, passwordsDir := setupWalletAndPasswordsDir(t)
randPath, err := rand.Int(rand.Reader, big.NewInt(1000000))
require.NoError(t, err, "Could not generate random file path")
exportDir := filepath.Join(testutil.TempDir(), fmt.Sprintf("/%d", randPath), "export")
importDir := filepath.Join(testutil.TempDir(), fmt.Sprintf("/%d", randPath), "import")
t.Cleanup(func() {
require.NoError(t, os.RemoveAll(exportDir), "Failed to remove directory")
require.NoError(t, os.RemoveAll(importDir), "Failed to remove directory")
})
cliCtx := setupWalletCtx(t, &testWalletConfig{
walletDir: walletDir,
passwordsDir: passwordsDir,
exportDir: exportDir,
keymanagerKind: v2keymanager.Direct,
})
wallet, err := NewWallet(cliCtx)
require.NoError(t, err)
ctx := context.Background()
keymanager, err := direct.NewKeymanager(
ctx,
wallet,
direct.DefaultConfig(),
)
require.NoError(t, err)
_, err = keymanager.CreateAccount(ctx, password)
require.NoError(t, err)
accounts, err := keymanager.ValidatingAccountNames()
require.NoError(t, err)
if len(accounts) == 0 {
t.Fatal("Expected more accounts, received 0")
}
err = wallet.zipAccounts(accounts, exportDir)
require.NoError(t, err)
if _, err := os.Stat(filepath.Join(exportDir, archiveFilename)); os.IsNotExist(err) {
t.Fatal("Expected file to exist")
}
importedAccounts, err := unzipArchiveToTarget(exportDir, importDir)
require.NoError(t, err)
allAccountsStr := strings.Join(accounts, " ")
for _, importedAccount := range importedAccounts {
if !strings.Contains(allAccountsStr, importedAccount) {
t.Fatalf("Expected %s to be in %s", importedAccount, allAccountsStr)
}
}
}
func TestExport_Noninteractive(t *testing.T) {
walletDir, passwordsDir := setupWalletAndPasswordsDir(t)
randPath, err := rand.Int(rand.Reader, big.NewInt(1000000))
require.NoError(t, err, "Could not generate random file path")
exportDir := filepath.Join(testutil.TempDir(), fmt.Sprintf("/%d", randPath), "export")
t.Cleanup(func() {
require.NoError(t, os.RemoveAll(exportDir), "Failed to remove directory")
})
accounts := "all"
cliCtx := setupWalletCtx(t, &testWalletConfig{
walletDir: walletDir,
passwordsDir: passwordsDir,
exportDir: exportDir,
accountsToExport: accounts,
keymanagerKind: v2keymanager.Direct,
})
wallet, err := NewWallet(cliCtx)
require.NoError(t, err)
ctx := context.Background()
keymanagerCfg := direct.DefaultConfig()
encodedCfg, err := direct.MarshalConfigFile(ctx, keymanagerCfg)
require.NoError(t, err)
require.NoError(t, wallet.WriteKeymanagerConfigToDisk(ctx, encodedCfg))
keymanager, err := direct.NewKeymanager(
ctx,
wallet,
keymanagerCfg,
)
require.NoError(t, err)
_, err = keymanager.CreateAccount(ctx, password)
require.NoError(t, err)
require.NoError(t, ExportAccount(cliCtx))
if _, err := os.Stat(filepath.Join(exportDir, archiveFilename)); os.IsNotExist(err) {
t.Fatal("Expected file to exist")
}
}