Fix --clear-db on Windows (#7474)

* fix with text

* fix also for validator and add test

* handle error

* fix another test

* handle error

* gazelle

* fix bug in slasher as well

* gazelle
This commit is contained in:
dv8silencer 2020-10-09 04:28:35 -05:00 committed by GitHub
parent 6b5265d2d4
commit c944f29c7c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 86 additions and 1 deletions

View File

@ -57,6 +57,7 @@ go_test(
embed = [":go_default_library"],
deps = [
"//beacon-chain/core/feed/state:go_default_library",
"//shared/cmd:go_default_library",
"//shared/testutil:go_default_library",
"//shared/testutil/assert:go_default_library",
"//shared/testutil/require:go_default_library",

View File

@ -316,6 +316,9 @@ func (b *BeaconNode) startDB(cliCtx *cli.Context) error {
}
if clearDBConfirmed || forceClearDB {
log.Warning("Removing database")
if err := d.Close(); err != nil {
return errors.Wrap(err, "could not close db prior to clearing")
}
if err := d.ClearDB(); err != nil {
return errors.Wrap(err, "could not clear database")
}

View File

@ -1,13 +1,17 @@
package node
import (
"crypto/rand"
"flag"
"fmt"
"io/ioutil"
"math/big"
"os"
"path/filepath"
"testing"
statefeed "github.com/prysmaticlabs/prysm/beacon-chain/core/feed/state"
"github.com/prysmaticlabs/prysm/shared/cmd"
"github.com/prysmaticlabs/prysm/shared/testutil"
"github.com/prysmaticlabs/prysm/shared/testutil/assert"
"github.com/prysmaticlabs/prysm/shared/testutil/require"
@ -65,3 +69,25 @@ func TestBootStrapNodeFile(t *testing.T) {
assert.Equal(t, sampleNode1[2:], nodeList[1], "Unexpected nodes")
assert.Equal(t, sampleNode2[2:], nodeList[2], "Unexpected nodes")
}
// TestClearDB tests clearing the database
func TestClearDB(t *testing.T) {
hook := logTest.NewGlobal()
randPath, err := rand.Int(rand.Reader, big.NewInt(1000000))
require.NoError(t, err, "Could not generate random number for file path")
tmp := filepath.Join(testutil.TempDir(), fmt.Sprintf("datadirtest%d", randPath))
require.NoError(t, os.RemoveAll(tmp))
app := cli.App{}
set := flag.NewFlagSet("test", 0)
set.String("datadir", tmp, "node data directory")
set.Bool(cmd.ForceClearDB.Name, true, "force clear db")
context := cli.NewContext(&app, set, nil)
_, err = NewBeaconNode(context)
require.NoError(t, err)
require.LogsContain(t, hook, "Removing database")
require.NoError(t, os.RemoveAll(tmp))
}

View File

@ -34,6 +34,7 @@ go_test(
srcs = ["node_test.go"],
embed = [":go_default_library"],
deps = [
"//shared/cmd:go_default_library",
"//shared/testutil:go_default_library",
"//shared/testutil/require:go_default_library",
"@com_github_sirupsen_logrus//:go_default_library",

View File

@ -194,6 +194,9 @@ func (s *SlasherNode) startDB() error {
}
if clearDBConfirmed || forceClearDB {
log.Warning("Removing database")
if err := d.Close(); err != nil {
return errors.Wrap(err, "could not close db prior to clearing")
}
if err := d.ClearDB(); err != nil {
return err
}

View File

@ -1,12 +1,16 @@
package node
import (
"crypto/rand"
"flag"
"fmt"
"io/ioutil"
"math/big"
"os"
"path/filepath"
"testing"
"github.com/prysmaticlabs/prysm/shared/cmd"
"github.com/prysmaticlabs/prysm/shared/testutil"
"github.com/prysmaticlabs/prysm/shared/testutil/require"
"github.com/sirupsen/logrus"
@ -43,3 +47,27 @@ func TestNodeClose_OK(t *testing.T) {
require.LogsContain(t, hook, "Stopping hash slinging slasher")
require.NoError(t, os.RemoveAll(tmp))
}
// TestClearDB tests clearing the database
func TestClearDB(t *testing.T) {
hook := logTest.NewGlobal()
randPath, err := rand.Int(rand.Reader, big.NewInt(1000000))
require.NoError(t, err, "Could not generate random number for file path")
tmp := filepath.Join(testutil.TempDir(), fmt.Sprintf("datadirtest%d", randPath))
require.NoError(t, os.RemoveAll(tmp))
app := cli.App{}
set := flag.NewFlagSet("test", 0)
set.String("datadir", tmp, "node data directory")
set.Bool(cmd.ForceClearDB.Name, true, "force clear db")
context := cli.NewContext(&app, set, nil)
slasherNode, err := NewSlasherNode(context)
require.NoError(t, err)
require.LogsContain(t, hook, "Removing database")
err = slasherNode.db.Close()
require.NoError(t, err)
require.NoError(t, os.RemoveAll(tmp))
}

View File

@ -11,6 +11,7 @@ go_test(
"//shared/testutil/assert:go_default_library",
"//shared/testutil/require:go_default_library",
"//validator/accounts/v1:go_default_library",
"@com_github_sirupsen_logrus//hooks/test:go_default_library",
"@com_github_urfave_cli_v2//:go_default_library",
],
)

View File

@ -516,6 +516,9 @@ func clearDB(dataDir string, force bool) error {
if err != nil {
return errors.Wrapf(err, "Could not create DB in dir %s", dataDir)
}
if err := valDB.Close(); err != nil {
return errors.Wrapf(err, "could not close DB in dir %s", dataDir)
}
log.Warning("Removing database")
if err := valDB.ClearDB(); err != nil {

View File

@ -1,14 +1,19 @@
package node
import (
"crypto/rand"
"flag"
"fmt"
"math/big"
"os"
"path/filepath"
"testing"
"github.com/prysmaticlabs/prysm/shared/testutil"
"github.com/prysmaticlabs/prysm/shared/testutil/assert"
"github.com/prysmaticlabs/prysm/shared/testutil/require"
v1 "github.com/prysmaticlabs/prysm/validator/accounts/v1"
logTest "github.com/sirupsen/logrus/hooks/test"
"github.com/urfave/cli/v2"
)
@ -31,6 +36,20 @@ func TestNode_Builds(t *testing.T) {
context := cli.NewContext(&app, set, nil)
require.NoError(t, v1.NewValidatorAccount(dir, "1234"), "Could not create validator account")
_, err := NewValidatorClient(context)
valClient, err := NewValidatorClient(context)
require.NoError(t, err, "Failed to create ValidatorClient")
err = valClient.db.Close()
require.NoError(t, err)
}
// TestClearDB tests clearing the database
func TestClearDB(t *testing.T) {
hook := logTest.NewGlobal()
randPath, err := rand.Int(rand.Reader, big.NewInt(1000000))
require.NoError(t, err, "Could not generate random number for file path")
tmp := filepath.Join(testutil.TempDir(), fmt.Sprintf("datadirtest%d", randPath))
require.NoError(t, os.RemoveAll(tmp))
err = clearDB(tmp, true)
require.NoError(t, err)
require.LogsContain(t, hook, "Removing database")
}