2022-10-05 06:44:10 +00:00
|
|
|
package server_test
|
2020-12-01 10:26:53 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
2021-11-17 11:14:22 +00:00
|
|
|
"path/filepath"
|
2022-01-31 13:20:14 +00:00
|
|
|
"strconv"
|
2020-12-01 10:26:53 +00:00
|
|
|
"testing"
|
|
|
|
|
2022-10-05 06:44:10 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/internal/testcli"
|
2020-12-01 10:26:53 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/config"
|
|
|
|
"github.com/stretchr/testify/require"
|
2021-09-25 10:09:55 +00:00
|
|
|
"gopkg.in/yaml.v3"
|
2020-12-01 10:26:53 +00:00
|
|
|
)
|
|
|
|
|
2022-01-31 13:20:14 +00:00
|
|
|
func TestDBRestoreDump(t *testing.T) {
|
2021-08-25 19:17:37 +00:00
|
|
|
tmpDir := t.TempDir()
|
2020-12-01 10:26:53 +00:00
|
|
|
|
2022-01-31 13:20:14 +00:00
|
|
|
loadConfig := func(t *testing.T) config.Config {
|
|
|
|
chainPath := filepath.Join(tmpDir, "neogotestchain")
|
2022-10-05 06:44:10 +00:00
|
|
|
cfg, err := config.LoadFile(filepath.Join("..", "..", "config", "protocol.unit_testnet.yml"))
|
2022-01-31 13:20:14 +00:00
|
|
|
require.NoError(t, err, "could not load config")
|
|
|
|
cfg.ApplicationConfiguration.DBConfiguration.Type = "leveldb"
|
|
|
|
cfg.ApplicationConfiguration.DBConfiguration.LevelDBOptions.DataDirectoryPath = chainPath
|
|
|
|
return cfg
|
|
|
|
}
|
2020-12-01 10:26:53 +00:00
|
|
|
|
2022-01-31 13:20:14 +00:00
|
|
|
cfg := loadConfig(t)
|
2020-12-01 10:26:53 +00:00
|
|
|
out, err := yaml.Marshal(cfg)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-11-17 11:14:22 +00:00
|
|
|
cfgPath := filepath.Join(tmpDir, "protocol.unit_testnet.yml")
|
2022-02-22 16:27:32 +00:00
|
|
|
require.NoError(t, os.WriteFile(cfgPath, out, os.ModePerm))
|
2020-12-01 10:26:53 +00:00
|
|
|
|
2022-10-05 06:44:10 +00:00
|
|
|
// generated via `go run ./scripts/gendump/main.go --out ./cli/server/testdata/chain50x2.acc --blocks 50 --txs 2`
|
2020-12-01 10:26:53 +00:00
|
|
|
const inDump = "./testdata/chain50x2.acc"
|
2022-10-05 06:44:10 +00:00
|
|
|
e := testcli.NewExecutor(t, false)
|
2022-01-31 13:20:14 +00:00
|
|
|
|
2021-11-17 11:14:22 +00:00
|
|
|
stateDump := filepath.Join(tmpDir, "neogo.teststate")
|
2020-12-01 10:26:53 +00:00
|
|
|
baseArgs := []string{"neo-go", "db", "restore", "--unittest",
|
|
|
|
"--config-path", tmpDir, "--in", inDump, "--dump", stateDump}
|
|
|
|
|
2022-08-05 10:32:37 +00:00
|
|
|
t.Run("excessive restore parameters", func(t *testing.T) {
|
|
|
|
e.RunWithError(t, append(baseArgs, "something")...)
|
|
|
|
})
|
2020-12-01 10:26:53 +00:00
|
|
|
// First 15 blocks.
|
|
|
|
e.Run(t, append(baseArgs, "--count", "15")...)
|
|
|
|
|
|
|
|
// Big count.
|
2021-07-16 07:45:34 +00:00
|
|
|
e.RunWithError(t, append(baseArgs, "--count", "1000")...)
|
2020-12-01 10:26:53 +00:00
|
|
|
|
|
|
|
// Continue 15..25
|
2021-07-16 07:45:34 +00:00
|
|
|
e.Run(t, append(baseArgs, "--count", "10")...)
|
2020-12-01 10:26:53 +00:00
|
|
|
|
|
|
|
// Continue till end.
|
2021-07-16 07:45:34 +00:00
|
|
|
e.Run(t, baseArgs...)
|
2020-12-01 10:26:53 +00:00
|
|
|
|
|
|
|
// Dump and compare.
|
2021-11-17 11:14:22 +00:00
|
|
|
dumpPath := filepath.Join(tmpDir, "testdump.acc")
|
2022-01-31 13:20:14 +00:00
|
|
|
|
|
|
|
t.Run("missing config", func(t *testing.T) {
|
|
|
|
e.RunWithError(t, "neo-go", "db", "dump", "--privnet",
|
|
|
|
"--config-path", tmpDir, "--out", dumpPath)
|
|
|
|
})
|
|
|
|
t.Run("bad logger config", func(t *testing.T) {
|
|
|
|
badConfigDir := t.TempDir()
|
|
|
|
logfile := filepath.Join(badConfigDir, "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 = loadConfig(t)
|
|
|
|
cfg.ApplicationConfiguration.LogPath = filepath.Join(logfile, "file.log")
|
|
|
|
out, err = yaml.Marshal(cfg)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
cfgPath = filepath.Join(badConfigDir, "protocol.unit_testnet.yml")
|
2022-02-22 16:27:32 +00:00
|
|
|
require.NoError(t, os.WriteFile(cfgPath, out, os.ModePerm))
|
2022-01-31 13:20:14 +00:00
|
|
|
|
|
|
|
e.RunWithError(t, "neo-go", "db", "dump", "--unittest",
|
|
|
|
"--config-path", badConfigDir, "--out", dumpPath)
|
|
|
|
})
|
|
|
|
t.Run("bad storage config", func(t *testing.T) {
|
|
|
|
badConfigDir := t.TempDir()
|
|
|
|
logfile := filepath.Join(badConfigDir, "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 = loadConfig(t)
|
|
|
|
cfg.ApplicationConfiguration.DBConfiguration.Type = ""
|
|
|
|
out, err = yaml.Marshal(cfg)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
cfgPath = filepath.Join(badConfigDir, "protocol.unit_testnet.yml")
|
2022-02-22 16:27:32 +00:00
|
|
|
require.NoError(t, os.WriteFile(cfgPath, out, os.ModePerm))
|
2022-01-31 13:20:14 +00:00
|
|
|
|
|
|
|
e.RunWithError(t, "neo-go", "db", "dump", "--unittest",
|
|
|
|
"--config-path", badConfigDir, "--out", dumpPath)
|
|
|
|
})
|
|
|
|
|
|
|
|
baseCmd := []string{"neo-go", "db", "dump", "--unittest",
|
|
|
|
"--config-path", tmpDir, "--out", dumpPath}
|
|
|
|
|
|
|
|
t.Run("invalid start/count", func(t *testing.T) {
|
|
|
|
e.RunWithError(t, append(baseCmd, "--start", "5", "--count", strconv.Itoa(50-5+1+1))...)
|
|
|
|
})
|
2022-08-05 10:32:37 +00:00
|
|
|
t.Run("excessive dump parameters", func(t *testing.T) {
|
|
|
|
e.RunWithError(t, append(baseCmd, "something")...)
|
|
|
|
})
|
2022-01-31 13:20:14 +00:00
|
|
|
|
|
|
|
e.Run(t, baseCmd...)
|
2020-12-01 10:26:53 +00:00
|
|
|
|
2022-02-22 16:27:32 +00:00
|
|
|
d1, err := os.ReadFile(inDump)
|
2020-12-01 10:26:53 +00:00
|
|
|
require.NoError(t, err)
|
2022-02-22 16:27:32 +00:00
|
|
|
d2, err := os.ReadFile(dumpPath)
|
2020-12-01 10:26:53 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, d1, d2, "dumps differ")
|
|
|
|
}
|