prysm-pulse/validator/accounts/v2/import_test.go
Raul Jordan 7c52ef8c2b
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>
2020-07-22 21:12:51 -05:00

70 lines
2.4 KiB
Go

package v2
import (
"context"
"crypto/rand"
"fmt"
"io/ioutil"
"math/big"
"os"
"path"
"path/filepath"
"testing"
"github.com/prysmaticlabs/prysm/shared/testutil"
"github.com/prysmaticlabs/prysm/shared/testutil/assert"
"github.com/prysmaticlabs/prysm/shared/testutil/require"
v2keymanager "github.com/prysmaticlabs/prysm/validator/keymanager/v2"
"github.com/prysmaticlabs/prysm/validator/keymanager/v2/direct"
)
func TestImport_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 := path.Join(testutil.TempDir(), fmt.Sprintf("/%d", randPath), "export")
importDir := path.Join(testutil.TempDir(), fmt.Sprintf("/%d", randPath), "import")
importPasswordDir := path.Join(testutil.TempDir(), fmt.Sprintf("/%d", randPath), "importpassword")
t.Cleanup(func() {
require.NoError(t, os.RemoveAll(exportDir), "Failed to remove directory")
require.NoError(t, os.RemoveAll(importDir), "Failed to remove directory")
require.NoError(t, os.RemoveAll(importPasswordDir), "Failed to remove directory")
})
require.NoError(t, os.MkdirAll(importPasswordDir, os.ModePerm))
passwordFilePath := filepath.Join(importPasswordDir, passwordFileName)
require.NoError(t, ioutil.WriteFile(passwordFilePath, []byte(password), os.ModePerm))
cliCtx := setupWalletCtx(t, &testWalletConfig{
walletDir: walletDir,
passwordsDir: passwordsDir,
exportDir: exportDir,
keymanagerKind: v2keymanager.Direct,
passwordFile: passwordFilePath,
})
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)
accounts, err := keymanager.ValidatingAccountNames()
require.NoError(t, err)
assert.Equal(t, len(accounts), 1)
require.NoError(t, wallet.zipAccounts(accounts, exportDir))
if _, err := os.Stat(filepath.Join(exportDir, archiveFilename)); os.IsNotExist(err) {
t.Fatal("Expected file to exist")
}
require.NoError(t, ImportAccount(cliCtx))
}