2020-08-21 18:36:08 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2022-01-31 13:20:14 +00:00
|
|
|
"encoding/binary"
|
2020-08-21 18:36:08 +00:00
|
|
|
"flag"
|
|
|
|
"os"
|
2021-11-17 11:14:22 +00:00
|
|
|
"path/filepath"
|
2024-02-09 17:39:37 +00:00
|
|
|
"runtime"
|
2020-08-21 18:36:08 +00:00
|
|
|
"testing"
|
|
|
|
|
2022-10-07 11:51:51 +00:00
|
|
|
"go.uber.org/zap/zapcore"
|
|
|
|
|
2022-10-03 12:05:34 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/cli/options"
|
2020-08-21 18:36:08 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/config"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/config/netmode"
|
2022-07-08 16:42:06 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/storage/dbconfig"
|
2020-08-21 18:36:08 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"github.com/urfave/cli"
|
2021-09-25 10:09:55 +00:00
|
|
|
"gopkg.in/yaml.v3"
|
2020-08-21 18:36:08 +00:00
|
|
|
)
|
|
|
|
|
2021-08-25 19:17:37 +00:00
|
|
|
// serverTestWD is the default working directory for server tests.
|
|
|
|
var serverTestWD string
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
var err error
|
|
|
|
serverTestWD, err = os.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
panic("can't get current working directory")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-21 18:36:08 +00:00
|
|
|
func TestGetConfigFromContext(t *testing.T) {
|
2023-05-12 13:53:57 +00:00
|
|
|
t.Run("config-path", func(t *testing.T) {
|
|
|
|
set := flag.NewFlagSet("flagSet", flag.ExitOnError)
|
|
|
|
set.String("config-path", "../../config", "")
|
|
|
|
set.Bool("testnet", true, "")
|
|
|
|
ctx := cli.NewContext(cli.NewApp(), set, nil)
|
|
|
|
cfg, err := options.GetConfigFromContext(ctx)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, netmode.TestNet, cfg.ProtocolConfiguration.Magic)
|
|
|
|
})
|
|
|
|
t.Run("config-file", func(t *testing.T) {
|
|
|
|
set := flag.NewFlagSet("flagSet", flag.ExitOnError)
|
|
|
|
set.String("config-path", "../../config", "")
|
|
|
|
set.Bool("testnet", true, "")
|
|
|
|
set.String("config-file", "../../config/protocol.testnet.yml", "")
|
|
|
|
ctx := cli.NewContext(cli.NewApp(), set, nil)
|
|
|
|
cfg, err := options.GetConfigFromContext(ctx)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, netmode.TestNet, cfg.ProtocolConfiguration.Magic)
|
|
|
|
})
|
2024-02-09 17:39:37 +00:00
|
|
|
t.Run("relative-path windows", func(t *testing.T) {
|
|
|
|
if runtime.GOOS != "windows" {
|
|
|
|
t.Skip("skipping Windows specific test")
|
|
|
|
}
|
|
|
|
|
|
|
|
set := flag.NewFlagSet("flagSet", flag.ExitOnError)
|
|
|
|
set.String("relative-path", "..\\..\\config", "")
|
|
|
|
set.Bool("testnet", true, "")
|
|
|
|
set.String("config-file", ".\\testdata\\protocol.testnet.windows.yml", "")
|
|
|
|
ctx := cli.NewContext(cli.NewApp(), set, nil)
|
|
|
|
cfg, err := options.GetConfigFromContext(ctx)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, filepath.Join("..", "..", "config", "chains", "testnet"), cfg.ApplicationConfiguration.DBConfiguration.LevelDBOptions.DataDirectoryPath)
|
|
|
|
require.Equal(t, "C:\\someFolder\\cn_wallet.json", cfg.ApplicationConfiguration.Consensus.UnlockWallet.Path)
|
|
|
|
require.Equal(t, "C:\\someFolder\\notary_wallet.json", cfg.ApplicationConfiguration.P2PNotary.UnlockWallet.Path)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("relative-path non-windows", func(t *testing.T) {
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
t.Skip("skipping non-Windows specific test")
|
|
|
|
}
|
|
|
|
|
2023-11-23 17:41:50 +00:00
|
|
|
set := flag.NewFlagSet("flagSet", flag.ExitOnError)
|
|
|
|
set.String("relative-path", "../../config", "")
|
|
|
|
set.Bool("testnet", true, "")
|
|
|
|
set.String("config-file", "../../config/protocol.testnet.yml", "")
|
|
|
|
ctx := cli.NewContext(cli.NewApp(), set, nil)
|
|
|
|
cfg, err := options.GetConfigFromContext(ctx)
|
|
|
|
require.NoError(t, err)
|
2024-02-09 17:39:37 +00:00
|
|
|
require.Equal(t, filepath.Join("..", "..", "config", "chains", "testnet"), cfg.ApplicationConfiguration.DBConfiguration.LevelDBOptions.DataDirectoryPath)
|
2023-11-23 17:41:50 +00:00
|
|
|
require.Equal(t, "/cn_wallet.json", cfg.ApplicationConfiguration.Consensus.UnlockWallet.Path)
|
|
|
|
require.Equal(t, "/notary_wallet.json", cfg.ApplicationConfiguration.P2PNotary.UnlockWallet.Path)
|
|
|
|
})
|
2020-08-21 18:36:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestHandleLoggingParams(t *testing.T) {
|
2021-08-25 19:17:37 +00:00
|
|
|
d := t.TempDir()
|
2021-11-17 11:14:22 +00:00
|
|
|
testLog := filepath.Join(d, "file.log")
|
2020-08-21 18:36:08 +00:00
|
|
|
|
2022-01-31 13:20:14 +00:00
|
|
|
t.Run("logdir is a file", func(t *testing.T) {
|
|
|
|
logfile := filepath.Join(d, "logdir")
|
2022-02-22 16:27:32 +00:00
|
|
|
require.NoError(t, os.WriteFile(logfile, []byte{1, 2, 3}, os.ModePerm))
|
2022-01-31 13:20:14 +00:00
|
|
|
cfg := config.ApplicationConfiguration{
|
|
|
|
LogPath: filepath.Join(logfile, "file.log"),
|
|
|
|
}
|
2022-12-05 12:43:55 +00:00
|
|
|
_, lvl, closer, err := options.HandleLoggingParams(false, cfg)
|
2022-01-31 13:20:14 +00:00
|
|
|
require.Error(t, err)
|
2022-12-05 12:43:55 +00:00
|
|
|
require.Nil(t, lvl)
|
2022-02-07 15:51:54 +00:00
|
|
|
require.Nil(t, closer)
|
2022-01-31 13:20:14 +00:00
|
|
|
})
|
|
|
|
|
2022-12-05 11:58:16 +00:00
|
|
|
t.Run("broken level", func(t *testing.T) {
|
|
|
|
cfg := config.ApplicationConfiguration{
|
|
|
|
LogPath: testLog,
|
|
|
|
LogLevel: "qwerty",
|
|
|
|
}
|
2022-12-05 12:43:55 +00:00
|
|
|
_, lvl, closer, err := options.HandleLoggingParams(false, cfg)
|
2022-12-05 11:58:16 +00:00
|
|
|
require.Error(t, err)
|
2022-12-05 12:43:55 +00:00
|
|
|
require.Nil(t, lvl)
|
2022-12-05 11:58:16 +00:00
|
|
|
require.Nil(t, closer)
|
|
|
|
})
|
|
|
|
|
2020-08-21 18:36:08 +00:00
|
|
|
t.Run("default", func(t *testing.T) {
|
|
|
|
cfg := config.ApplicationConfiguration{
|
2021-08-25 19:17:37 +00:00
|
|
|
LogPath: testLog,
|
2020-08-21 18:36:08 +00:00
|
|
|
}
|
2022-12-05 12:43:55 +00:00
|
|
|
logger, lvl, closer, err := options.HandleLoggingParams(false, cfg)
|
|
|
|
require.NotNil(t, lvl)
|
2020-08-21 18:36:08 +00:00
|
|
|
require.NoError(t, err)
|
2022-02-07 15:51:54 +00:00
|
|
|
t.Cleanup(func() {
|
|
|
|
if closer != nil {
|
|
|
|
require.NoError(t, closer())
|
|
|
|
}
|
|
|
|
})
|
2022-12-05 12:43:55 +00:00
|
|
|
require.Equal(t, zapcore.InfoLevel, lvl.Level())
|
2022-10-07 11:51:51 +00:00
|
|
|
require.True(t, logger.Core().Enabled(zapcore.InfoLevel))
|
|
|
|
require.False(t, logger.Core().Enabled(zapcore.DebugLevel))
|
2020-08-21 18:36:08 +00:00
|
|
|
})
|
|
|
|
|
2022-12-05 11:58:16 +00:00
|
|
|
t.Run("warn", func(t *testing.T) {
|
|
|
|
cfg := config.ApplicationConfiguration{
|
|
|
|
LogPath: testLog,
|
|
|
|
LogLevel: "warn",
|
|
|
|
}
|
2022-12-05 12:43:55 +00:00
|
|
|
logger, lvl, closer, err := options.HandleLoggingParams(false, cfg)
|
2022-12-05 11:58:16 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
t.Cleanup(func() {
|
|
|
|
if closer != nil {
|
|
|
|
require.NoError(t, closer())
|
|
|
|
}
|
|
|
|
})
|
2022-12-05 12:43:55 +00:00
|
|
|
require.Equal(t, zapcore.WarnLevel, lvl.Level())
|
2022-12-05 11:58:16 +00:00
|
|
|
require.True(t, logger.Core().Enabled(zapcore.WarnLevel))
|
|
|
|
require.False(t, logger.Core().Enabled(zapcore.InfoLevel))
|
|
|
|
})
|
|
|
|
|
2020-08-21 18:36:08 +00:00
|
|
|
t.Run("debug", func(t *testing.T) {
|
|
|
|
cfg := config.ApplicationConfiguration{
|
2021-08-25 19:17:37 +00:00
|
|
|
LogPath: testLog,
|
2020-08-21 18:36:08 +00:00
|
|
|
}
|
2022-12-05 12:43:55 +00:00
|
|
|
logger, lvl, closer, err := options.HandleLoggingParams(true, cfg)
|
2020-08-21 18:36:08 +00:00
|
|
|
require.NoError(t, err)
|
2022-02-07 15:51:54 +00:00
|
|
|
t.Cleanup(func() {
|
|
|
|
if closer != nil {
|
|
|
|
require.NoError(t, closer())
|
|
|
|
}
|
|
|
|
})
|
2022-12-05 12:43:55 +00:00
|
|
|
require.Equal(t, zapcore.DebugLevel, lvl.Level())
|
2022-10-07 11:51:51 +00:00
|
|
|
require.True(t, logger.Core().Enabled(zapcore.InfoLevel))
|
|
|
|
require.True(t, logger.Core().Enabled(zapcore.DebugLevel))
|
2020-08-21 18:36:08 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestInitBCWithMetrics(t *testing.T) {
|
2021-08-25 19:17:37 +00:00
|
|
|
d := t.TempDir()
|
|
|
|
err := os.Chdir(d)
|
2021-05-12 18:34:07 +00:00
|
|
|
require.NoError(t, err)
|
2021-08-25 19:17:37 +00:00
|
|
|
t.Cleanup(func() { require.NoError(t, os.Chdir(serverTestWD)) })
|
2020-08-21 18:36:08 +00:00
|
|
|
|
|
|
|
set := flag.NewFlagSet("flagSet", flag.ExitOnError)
|
2021-11-17 11:14:22 +00:00
|
|
|
set.String("config-path", filepath.Join(serverTestWD, "..", "..", "config"), "")
|
2020-08-21 18:36:08 +00:00
|
|
|
set.Bool("testnet", true, "")
|
|
|
|
set.Bool("debug", true, "")
|
|
|
|
ctx := cli.NewContext(cli.NewApp(), set, nil)
|
2022-10-03 12:05:34 +00:00
|
|
|
cfg, err := options.GetConfigFromContext(ctx)
|
2020-08-21 18:36:08 +00:00
|
|
|
require.NoError(t, err)
|
2022-12-05 12:43:55 +00:00
|
|
|
logger, _, closer, err := options.HandleLoggingParams(true, cfg.ApplicationConfiguration)
|
2020-12-28 14:31:50 +00:00
|
|
|
require.NoError(t, err)
|
2022-02-07 15:51:54 +00:00
|
|
|
t.Cleanup(func() {
|
|
|
|
if closer != nil {
|
|
|
|
require.NoError(t, closer())
|
|
|
|
}
|
|
|
|
})
|
2022-01-31 13:20:14 +00:00
|
|
|
|
|
|
|
t.Run("bad store", func(t *testing.T) {
|
|
|
|
_, _, _, err = initBCWithMetrics(config.Config{}, logger)
|
|
|
|
require.Error(t, err)
|
|
|
|
})
|
|
|
|
|
2020-08-21 18:36:08 +00:00
|
|
|
chain, prometheus, pprof, err := initBCWithMetrics(cfg, logger)
|
|
|
|
require.NoError(t, err)
|
2021-03-01 11:14:15 +00:00
|
|
|
t.Cleanup(func() {
|
|
|
|
chain.Close()
|
|
|
|
prometheus.ShutDown()
|
|
|
|
pprof.ShutDown()
|
|
|
|
})
|
2020-08-21 18:36:08 +00:00
|
|
|
require.Equal(t, netmode.TestNet, chain.GetConfig().Magic)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDumpDB(t *testing.T) {
|
2021-08-25 19:17:37 +00:00
|
|
|
testDump := "file.acc"
|
|
|
|
|
2020-08-21 18:36:08 +00:00
|
|
|
t.Run("too low chain", func(t *testing.T) {
|
2021-08-25 19:17:37 +00:00
|
|
|
d := t.TempDir()
|
|
|
|
err := os.Chdir(d)
|
2020-08-21 18:36:08 +00:00
|
|
|
require.NoError(t, err)
|
2021-08-25 19:17:37 +00:00
|
|
|
t.Cleanup(func() { require.NoError(t, os.Chdir(serverTestWD)) })
|
2020-08-21 18:36:08 +00:00
|
|
|
set := flag.NewFlagSet("flagSet", flag.ExitOnError)
|
2021-11-17 11:14:22 +00:00
|
|
|
set.String("config-path", filepath.Join(serverTestWD, "..", "..", "config"), "")
|
2020-08-21 18:36:08 +00:00
|
|
|
set.Bool("privnet", true, "")
|
|
|
|
set.Bool("debug", true, "")
|
|
|
|
set.Int("start", 0, "")
|
|
|
|
set.Int("count", 5, "")
|
|
|
|
set.String("out", testDump, "")
|
|
|
|
ctx := cli.NewContext(cli.NewApp(), set, nil)
|
|
|
|
err = dumpDB(ctx)
|
|
|
|
require.Error(t, err)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("positive", func(t *testing.T) {
|
2021-08-25 19:17:37 +00:00
|
|
|
d := t.TempDir()
|
|
|
|
err := os.Chdir(d)
|
2021-05-12 18:34:07 +00:00
|
|
|
require.NoError(t, err)
|
2021-08-25 19:17:37 +00:00
|
|
|
t.Cleanup(func() { require.NoError(t, os.Chdir(serverTestWD)) })
|
2020-08-21 18:36:08 +00:00
|
|
|
set := flag.NewFlagSet("flagSet", flag.ExitOnError)
|
2021-11-17 11:14:22 +00:00
|
|
|
set.String("config-path", filepath.Join(serverTestWD, "..", "..", "config"), "")
|
2020-08-21 18:36:08 +00:00
|
|
|
set.Bool("privnet", true, "")
|
|
|
|
set.Bool("debug", true, "")
|
|
|
|
set.Int("start", 0, "")
|
|
|
|
set.Int("count", 1, "")
|
|
|
|
set.String("out", testDump, "")
|
|
|
|
ctx := cli.NewContext(cli.NewApp(), set, nil)
|
|
|
|
err = dumpDB(ctx)
|
|
|
|
require.NoError(t, err)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRestoreDB(t *testing.T) {
|
2021-08-25 19:17:37 +00:00
|
|
|
d := t.TempDir()
|
2020-08-21 18:36:08 +00:00
|
|
|
testDump := "file1.acc"
|
|
|
|
saveDump := "file2.acc"
|
2021-08-25 19:17:37 +00:00
|
|
|
err := os.Chdir(d)
|
2021-05-12 18:34:07 +00:00
|
|
|
require.NoError(t, err)
|
2021-08-25 19:17:37 +00:00
|
|
|
t.Cleanup(func() { require.NoError(t, os.Chdir(serverTestWD)) })
|
2020-08-21 18:36:08 +00:00
|
|
|
|
2022-01-31 13:20:14 +00:00
|
|
|
// dump first
|
2020-08-21 18:36:08 +00:00
|
|
|
set := flag.NewFlagSet("flagSet", flag.ExitOnError)
|
2022-01-31 13:20:14 +00:00
|
|
|
goodCfg := filepath.Join(serverTestWD, "..", "..", "config")
|
|
|
|
cfgPath := set.String("config-path", goodCfg, "")
|
2020-08-21 18:36:08 +00:00
|
|
|
set.Bool("privnet", true, "")
|
|
|
|
set.Bool("debug", true, "")
|
|
|
|
set.Int("start", 0, "")
|
|
|
|
set.Int("count", 1, "")
|
|
|
|
set.String("out", testDump, "")
|
|
|
|
ctx := cli.NewContext(cli.NewApp(), set, nil)
|
|
|
|
err = dumpDB(ctx)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// and then restore
|
2022-01-31 13:20:14 +00:00
|
|
|
t.Run("invalid config", func(t *testing.T) {
|
|
|
|
*cfgPath = filepath.Join(serverTestWD, "..", "..", "config_invalid")
|
|
|
|
require.Error(t, restoreDB(ctx))
|
|
|
|
})
|
|
|
|
t.Run("invalid logger path", func(t *testing.T) {
|
|
|
|
badCfgDir := t.TempDir()
|
|
|
|
logfile := filepath.Join(badCfgDir, "logdir")
|
2022-02-22 16:27:32 +00:00
|
|
|
require.NoError(t, os.WriteFile(logfile, []byte{1, 2, 3}, os.ModePerm))
|
2022-01-31 13:20:14 +00:00
|
|
|
cfg, err := config.LoadFile(filepath.Join(goodCfg, "protocol.privnet.yml"))
|
|
|
|
require.NoError(t, err, "could not load config")
|
|
|
|
cfg.ApplicationConfiguration.LogPath = filepath.Join(logfile, "file.log")
|
|
|
|
out, err := yaml.Marshal(cfg)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
badCfgPath := filepath.Join(badCfgDir, "protocol.privnet.yml")
|
2022-02-22 16:27:32 +00:00
|
|
|
require.NoError(t, os.WriteFile(badCfgPath, out, os.ModePerm))
|
2022-01-31 13:20:14 +00:00
|
|
|
|
|
|
|
*cfgPath = badCfgDir
|
|
|
|
require.Error(t, restoreDB(ctx))
|
|
|
|
|
|
|
|
*cfgPath = goodCfg
|
|
|
|
})
|
|
|
|
t.Run("invalid bc config", func(t *testing.T) {
|
|
|
|
badCfgDir := t.TempDir()
|
|
|
|
cfg, err := config.LoadFile(filepath.Join(goodCfg, "protocol.privnet.yml"))
|
|
|
|
require.NoError(t, err, "could not load config")
|
|
|
|
cfg.ApplicationConfiguration.DBConfiguration.Type = ""
|
|
|
|
out, err := yaml.Marshal(cfg)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
badCfgPath := filepath.Join(badCfgDir, "protocol.privnet.yml")
|
2022-02-22 16:27:32 +00:00
|
|
|
require.NoError(t, os.WriteFile(badCfgPath, out, os.ModePerm))
|
2022-01-31 13:20:14 +00:00
|
|
|
|
|
|
|
*cfgPath = badCfgDir
|
|
|
|
require.Error(t, restoreDB(ctx))
|
|
|
|
|
|
|
|
*cfgPath = goodCfg
|
|
|
|
})
|
|
|
|
|
|
|
|
in := set.String("in", testDump, "")
|
|
|
|
incremental := set.Bool("incremental", false, "")
|
|
|
|
t.Run("invalid in", func(t *testing.T) {
|
|
|
|
*in = "unknown-file"
|
|
|
|
require.Error(t, restoreDB(ctx))
|
|
|
|
|
|
|
|
*in = testDump
|
|
|
|
})
|
|
|
|
t.Run("corrupted in: invalid block count", func(t *testing.T) {
|
|
|
|
inPath := filepath.Join(t.TempDir(), "file3.acc")
|
2022-02-22 16:27:32 +00:00
|
|
|
require.NoError(t, os.WriteFile(inPath, []byte{1, 2, 3}, // file is expected to start from uint32
|
2022-01-31 13:20:14 +00:00
|
|
|
os.ModePerm))
|
|
|
|
*in = inPath
|
|
|
|
require.Error(t, restoreDB(ctx))
|
|
|
|
|
|
|
|
*in = testDump
|
|
|
|
})
|
|
|
|
t.Run("corrupted in: corrupted block", func(t *testing.T) {
|
|
|
|
inPath := filepath.Join(t.TempDir(), "file3.acc")
|
2022-02-22 16:27:32 +00:00
|
|
|
b, err := os.ReadFile(testDump)
|
2022-01-31 13:20:14 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
b[5] = 0xff // file is expected to start from uint32 (4 bytes) followed by the first block, so corrupt the first block bytes
|
2022-02-22 16:27:32 +00:00
|
|
|
require.NoError(t, os.WriteFile(inPath, b, os.ModePerm))
|
2022-01-31 13:20:14 +00:00
|
|
|
*in = inPath
|
|
|
|
require.Error(t, restoreDB(ctx))
|
|
|
|
|
|
|
|
*in = testDump
|
|
|
|
})
|
|
|
|
t.Run("incremental dump", func(t *testing.T) {
|
|
|
|
inPath := filepath.Join(t.TempDir(), "file1_incremental.acc")
|
2022-02-22 16:27:32 +00:00
|
|
|
b, err := os.ReadFile(testDump)
|
2022-01-31 13:20:14 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
start := make([]byte, 4)
|
|
|
|
t.Run("good", func(t *testing.T) {
|
|
|
|
binary.LittleEndian.PutUint32(start, 1) // start from the first block
|
2022-02-22 16:27:32 +00:00
|
|
|
require.NoError(t, os.WriteFile(inPath, append(start, b...),
|
2022-01-31 13:20:14 +00:00
|
|
|
os.ModePerm))
|
|
|
|
*in = inPath
|
|
|
|
*incremental = true
|
|
|
|
|
|
|
|
require.NoError(t, restoreDB(ctx))
|
|
|
|
})
|
|
|
|
t.Run("dump is too high", func(t *testing.T) {
|
|
|
|
binary.LittleEndian.PutUint32(start, 2) // start from the second block
|
2022-02-22 16:27:32 +00:00
|
|
|
require.NoError(t, os.WriteFile(inPath, append(start, b...),
|
2022-01-31 13:20:14 +00:00
|
|
|
os.ModePerm))
|
|
|
|
*in = inPath
|
|
|
|
*incremental = true
|
|
|
|
|
|
|
|
require.Error(t, restoreDB(ctx))
|
|
|
|
})
|
|
|
|
|
|
|
|
*in = testDump
|
|
|
|
*incremental = false
|
|
|
|
})
|
|
|
|
|
2020-08-21 18:36:08 +00:00
|
|
|
set.String("dump", saveDump, "")
|
|
|
|
require.NoError(t, restoreDB(ctx))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestInitBlockChain(t *testing.T) {
|
|
|
|
t.Run("bad storage", func(t *testing.T) {
|
2022-11-14 07:22:52 +00:00
|
|
|
_, _, err := initBlockChain(config.Config{}, nil)
|
2020-08-21 18:36:08 +00:00
|
|
|
require.Error(t, err)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("empty logger", func(t *testing.T) {
|
2022-11-14 07:22:52 +00:00
|
|
|
_, _, err := initBlockChain(config.Config{
|
2020-08-21 18:36:08 +00:00
|
|
|
ApplicationConfiguration: config.ApplicationConfiguration{
|
2022-07-08 16:42:06 +00:00
|
|
|
DBConfiguration: dbconfig.DBConfiguration{
|
2022-10-05 06:05:36 +00:00
|
|
|
Type: dbconfig.InMemoryDB,
|
2020-08-21 18:36:08 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}, nil)
|
|
|
|
require.Error(t, err)
|
|
|
|
})
|
|
|
|
}
|
2022-10-20 10:59:35 +00:00
|
|
|
|
|
|
|
func TestResetDB(t *testing.T) {
|
|
|
|
d := t.TempDir()
|
|
|
|
err := os.Chdir(d)
|
|
|
|
require.NoError(t, err)
|
|
|
|
t.Cleanup(func() { require.NoError(t, os.Chdir(serverTestWD)) })
|
|
|
|
set := flag.NewFlagSet("flagSet", flag.ExitOnError)
|
|
|
|
set.String("config-path", filepath.Join(serverTestWD, "..", "..", "config"), "")
|
|
|
|
set.Bool("privnet", true, "")
|
|
|
|
set.Bool("debug", true, "")
|
|
|
|
set.Int("height", 0, "")
|
|
|
|
ctx := cli.NewContext(cli.NewApp(), set, nil)
|
|
|
|
err = resetDB(ctx)
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|