2020-08-21 18:36:08 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"os"
|
2021-11-17 11:14:22 +00:00
|
|
|
"path/filepath"
|
2020-08-21 18:36:08 +00:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/config"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/config/netmode"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/storage"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/network/metrics"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/rpc"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"github.com/urfave/cli"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
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) {
|
|
|
|
set := flag.NewFlagSet("flagSet", flag.ExitOnError)
|
|
|
|
set.String("config-path", "../../config", "")
|
|
|
|
set.Bool("testnet", true, "")
|
|
|
|
ctx := cli.NewContext(cli.NewApp(), set, nil)
|
|
|
|
cfg, err := getConfigFromContext(ctx)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, netmode.TestNet, cfg.ProtocolConfiguration.Magic)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestHandleLoggingParams(t *testing.T) {
|
2021-11-17 13:23:20 +00:00
|
|
|
// This test is failing on Windows, see https://github.com/nspcc-dev/neo-go/issues/2269
|
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
|
|
|
|
|
|
|
t.Run("default", func(t *testing.T) {
|
|
|
|
set := flag.NewFlagSet("flagSet", flag.ExitOnError)
|
|
|
|
ctx := cli.NewContext(cli.NewApp(), set, nil)
|
|
|
|
cfg := config.ApplicationConfiguration{
|
2021-08-25 19:17:37 +00:00
|
|
|
LogPath: testLog,
|
2020-08-21 18:36:08 +00:00
|
|
|
}
|
|
|
|
logger, err := handleLoggingParams(ctx, cfg)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.True(t, logger.Core().Enabled(zap.InfoLevel))
|
|
|
|
require.False(t, logger.Core().Enabled(zap.DebugLevel))
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("debug", func(t *testing.T) {
|
|
|
|
set := flag.NewFlagSet("flagSet", flag.ExitOnError)
|
|
|
|
set.Bool("debug", true, "")
|
|
|
|
ctx := cli.NewContext(cli.NewApp(), set, nil)
|
|
|
|
cfg := config.ApplicationConfiguration{
|
2021-08-25 19:17:37 +00:00
|
|
|
LogPath: testLog,
|
2020-08-21 18:36:08 +00:00
|
|
|
}
|
|
|
|
logger, err := handleLoggingParams(ctx, cfg)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.True(t, logger.Core().Enabled(zap.InfoLevel))
|
|
|
|
require.True(t, logger.Core().Enabled(zap.DebugLevel))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
cfg, err := getConfigFromContext(ctx)
|
|
|
|
require.NoError(t, err)
|
|
|
|
logger, err := handleLoggingParams(ctx, cfg.ApplicationConfiguration)
|
2020-12-28 14:31:50 +00:00
|
|
|
require.NoError(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
|
|
|
|
|
|
|
//dump first
|
|
|
|
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)
|
|
|
|
|
|
|
|
// and then restore
|
|
|
|
set.String("in", testDump, "")
|
|
|
|
set.String("dump", saveDump, "")
|
|
|
|
require.NoError(t, restoreDB(ctx))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestConfigureAddresses(t *testing.T) {
|
|
|
|
defaultAddress := "http://127.0.0.1:10333"
|
|
|
|
customAddress := "http://127.0.0.1:10334"
|
|
|
|
|
|
|
|
t.Run("default addresses", func(t *testing.T) {
|
|
|
|
cfg := &config.ApplicationConfiguration{
|
|
|
|
Address: defaultAddress,
|
|
|
|
}
|
|
|
|
configureAddresses(cfg)
|
|
|
|
require.Equal(t, defaultAddress, cfg.RPC.Address)
|
|
|
|
require.Equal(t, defaultAddress, cfg.Prometheus.Address)
|
|
|
|
require.Equal(t, defaultAddress, cfg.Pprof.Address)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("custom RPC address", func(t *testing.T) {
|
|
|
|
cfg := &config.ApplicationConfiguration{
|
|
|
|
Address: defaultAddress,
|
|
|
|
RPC: rpc.Config{
|
|
|
|
Address: customAddress,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
configureAddresses(cfg)
|
|
|
|
require.Equal(t, cfg.RPC.Address, customAddress)
|
|
|
|
require.Equal(t, cfg.Prometheus.Address, defaultAddress)
|
|
|
|
require.Equal(t, cfg.Pprof.Address, defaultAddress)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("custom Pprof address", func(t *testing.T) {
|
|
|
|
cfg := &config.ApplicationConfiguration{
|
|
|
|
Address: defaultAddress,
|
|
|
|
Pprof: metrics.Config{
|
|
|
|
Address: customAddress,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
configureAddresses(cfg)
|
|
|
|
require.Equal(t, cfg.RPC.Address, defaultAddress)
|
|
|
|
require.Equal(t, cfg.Prometheus.Address, defaultAddress)
|
|
|
|
require.Equal(t, cfg.Pprof.Address, customAddress)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("custom Prometheus address", func(t *testing.T) {
|
|
|
|
cfg := &config.ApplicationConfiguration{
|
|
|
|
Address: defaultAddress,
|
|
|
|
Prometheus: metrics.Config{
|
|
|
|
Address: customAddress,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
configureAddresses(cfg)
|
|
|
|
require.Equal(t, cfg.RPC.Address, defaultAddress)
|
|
|
|
require.Equal(t, cfg.Prometheus.Address, customAddress)
|
|
|
|
require.Equal(t, cfg.Pprof.Address, defaultAddress)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestInitBlockChain(t *testing.T) {
|
|
|
|
t.Run("bad storage", func(t *testing.T) {
|
|
|
|
_, err := initBlockChain(config.Config{}, nil)
|
|
|
|
require.Error(t, err)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("empty logger", func(t *testing.T) {
|
|
|
|
_, err := initBlockChain(config.Config{
|
|
|
|
ApplicationConfiguration: config.ApplicationConfiguration{
|
|
|
|
DBConfiguration: storage.DBConfiguration{
|
|
|
|
Type: "inmemory",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, nil)
|
|
|
|
require.Error(t, err)
|
|
|
|
})
|
|
|
|
}
|