mirror of
https://github.com/OffchainLabs/prysm.git
synced 2026-01-09 13:28:01 -05:00
add happy/sad tests (#8279)
This commit is contained in:
@@ -27,7 +27,10 @@ go_library(
|
||||
|
||||
go_test(
|
||||
name = "go_default_test",
|
||||
srcs = ["restore_test.go"],
|
||||
srcs = [
|
||||
"migrate_test.go",
|
||||
"restore_test.go",
|
||||
],
|
||||
embed = [":go_default_library"],
|
||||
deps = [
|
||||
"//shared/cmd:go_default_library",
|
||||
@@ -35,6 +38,7 @@ go_test(
|
||||
"//shared/testutil/assert:go_default_library",
|
||||
"//shared/testutil/require:go_default_library",
|
||||
"//validator/db/kv:go_default_library",
|
||||
"//validator/db/testing:go_default_library",
|
||||
"@com_github_sirupsen_logrus//hooks/test:go_default_library",
|
||||
"@com_github_urfave_cli_v2//:go_default_library",
|
||||
],
|
||||
|
||||
@@ -14,8 +14,8 @@ import (
|
||||
func migrateUp(cliCtx *cli.Context) error {
|
||||
dataDir := cliCtx.String(cmd.DataDirFlag.Name)
|
||||
|
||||
if fileutil.FileExists(path.Join(dataDir, kv.ProtectionDbFileName)) {
|
||||
return errors.New("No validator db found at path, nothing to rollback")
|
||||
if !fileutil.FileExists(path.Join(dataDir, kv.ProtectionDbFileName)) {
|
||||
return errors.New("No validator db found at path, nothing to migrate")
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
@@ -29,7 +29,7 @@ func migrateUp(cliCtx *cli.Context) error {
|
||||
func migrateDown(cliCtx *cli.Context) error {
|
||||
dataDir := cliCtx.String(cmd.DataDirFlag.Name)
|
||||
|
||||
if fileutil.FileExists(path.Join(dataDir, kv.ProtectionDbFileName)) {
|
||||
if !fileutil.FileExists(path.Join(dataDir, kv.ProtectionDbFileName)) {
|
||||
return errors.New("No validator db found at path, nothing to rollback")
|
||||
}
|
||||
|
||||
|
||||
56
validator/db/migrate_test.go
Normal file
56
validator/db/migrate_test.go
Normal file
@@ -0,0 +1,56 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"testing"
|
||||
|
||||
"github.com/prysmaticlabs/prysm/shared/cmd"
|
||||
"github.com/prysmaticlabs/prysm/shared/testutil/assert"
|
||||
"github.com/prysmaticlabs/prysm/shared/testutil/require"
|
||||
dbtest "github.com/prysmaticlabs/prysm/validator/db/testing"
|
||||
"github.com/urfave/cli/v2"
|
||||
)
|
||||
|
||||
func Test_migrateUp_NoDBFound(t *testing.T) {
|
||||
app := cli.App{}
|
||||
set := flag.NewFlagSet("test", 0)
|
||||
set.String(cmd.DataDirFlag.Name, "", "")
|
||||
require.NoError(t, set.Set(cmd.DataDirFlag.Name, ""))
|
||||
cliCtx := cli.NewContext(&app, set, nil)
|
||||
err := migrateUp(cliCtx)
|
||||
assert.ErrorContains(t, "No validator db found at path", err)
|
||||
}
|
||||
|
||||
func Test_migrateUp_OK(t *testing.T) {
|
||||
validatorDB := dbtest.SetupDB(t, nil)
|
||||
dbPath := validatorDB.DatabasePath()
|
||||
require.NoError(t, validatorDB.Close())
|
||||
app := cli.App{}
|
||||
set := flag.NewFlagSet("test", 0)
|
||||
set.String(cmd.DataDirFlag.Name, dbPath, "")
|
||||
require.NoError(t, set.Set(cmd.DataDirFlag.Name, dbPath))
|
||||
cliCtx := cli.NewContext(&app, set, nil)
|
||||
assert.NoError(t, migrateUp(cliCtx))
|
||||
}
|
||||
|
||||
func Test_migrateDown_NoDBFound(t *testing.T) {
|
||||
app := cli.App{}
|
||||
set := flag.NewFlagSet("test", 0)
|
||||
set.String(cmd.DataDirFlag.Name, "", "")
|
||||
require.NoError(t, set.Set(cmd.DataDirFlag.Name, ""))
|
||||
cliCtx := cli.NewContext(&app, set, nil)
|
||||
err := migrateDown(cliCtx)
|
||||
assert.ErrorContains(t, "No validator db found at path", err)
|
||||
}
|
||||
|
||||
func Test_migrateDown_OK(t *testing.T) {
|
||||
validatorDB := dbtest.SetupDB(t, nil)
|
||||
dbPath := validatorDB.DatabasePath()
|
||||
require.NoError(t, validatorDB.Close())
|
||||
app := cli.App{}
|
||||
set := flag.NewFlagSet("test", 0)
|
||||
set.String(cmd.DataDirFlag.Name, dbPath, "")
|
||||
require.NoError(t, set.Set(cmd.DataDirFlag.Name, dbPath))
|
||||
cliCtx := cli.NewContext(&app, set, nil)
|
||||
assert.NoError(t, migrateDown(cliCtx))
|
||||
}
|
||||
@@ -7,7 +7,7 @@ go_library(
|
||||
importpath = "github.com/prysmaticlabs/prysm/validator/db/testing",
|
||||
visibility = ["//validator:__subpackages__"],
|
||||
deps = [
|
||||
"//validator/db:go_default_library",
|
||||
"//validator/db/iface:go_default_library",
|
||||
"//validator/db/kv:go_default_library",
|
||||
],
|
||||
)
|
||||
|
||||
@@ -4,12 +4,12 @@ import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/prysmaticlabs/prysm/validator/db"
|
||||
"github.com/prysmaticlabs/prysm/validator/db/iface"
|
||||
"github.com/prysmaticlabs/prysm/validator/db/kv"
|
||||
)
|
||||
|
||||
// SetupDB instantiates and returns a DB instance for the validator client.
|
||||
func SetupDB(t testing.TB, pubkeys [][48]byte) db.Database {
|
||||
func SetupDB(t testing.TB, pubkeys [][48]byte) iface.ValidatorDB {
|
||||
db, err := kv.NewKVStore(context.Background(), t.TempDir(), pubkeys)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to instantiate DB: %v", err)
|
||||
|
||||
Reference in New Issue
Block a user