github.com/nspcc-dev/neo-go@v0.105.2-0.20240517133400-6be757af3eba/cli/server/cli_dump_test.go (about)

     1  package server_test
     2  
     3  import (
     4  	"os"
     5  	"path/filepath"
     6  	"strconv"
     7  	"testing"
     8  
     9  	"github.com/nspcc-dev/neo-go/internal/testcli"
    10  	"github.com/nspcc-dev/neo-go/pkg/config"
    11  	"github.com/nspcc-dev/neo-go/pkg/core/storage/dbconfig"
    12  	"github.com/stretchr/testify/require"
    13  	"gopkg.in/yaml.v3"
    14  )
    15  
    16  // generated via `go run ./scripts/gendump/main.go --out ./cli/server/testdata/chain50x2.acc --blocks 50 --txs 2`.
    17  const inDump = "./testdata/chain50x2.acc"
    18  
    19  func TestDBRestoreDump(t *testing.T) {
    20  	tmpDir := t.TempDir()
    21  
    22  	loadConfig := func(t *testing.T) config.Config {
    23  		chainPath := filepath.Join(tmpDir, "neogotestchain")
    24  		cfg, err := config.LoadFile(filepath.Join("..", "..", "config", "protocol.unit_testnet.yml"))
    25  		require.NoError(t, err, "could not load config")
    26  		cfg.ApplicationConfiguration.DBConfiguration.Type = dbconfig.LevelDB
    27  		cfg.ApplicationConfiguration.DBConfiguration.LevelDBOptions.DataDirectoryPath = chainPath
    28  		return cfg
    29  	}
    30  
    31  	cfg := loadConfig(t)
    32  	out, err := yaml.Marshal(cfg)
    33  	require.NoError(t, err)
    34  
    35  	cfgPath := filepath.Join(tmpDir, "protocol.unit_testnet.yml")
    36  	require.NoError(t, os.WriteFile(cfgPath, out, os.ModePerm))
    37  
    38  	e := testcli.NewExecutor(t, false)
    39  
    40  	stateDump := filepath.Join(tmpDir, "neogo.teststate")
    41  	baseArgs := []string{"neo-go", "db", "restore", "--unittest",
    42  		"--config-path", tmpDir, "--in", inDump, "--dump", stateDump}
    43  
    44  	t.Run("excessive restore parameters", func(t *testing.T) {
    45  		e.RunWithError(t, append(baseArgs, "something")...)
    46  	})
    47  	// First 15 blocks.
    48  	e.Run(t, append(baseArgs, "--count", "15")...)
    49  
    50  	// Big count.
    51  	e.RunWithError(t, append(baseArgs, "--count", "1000")...)
    52  
    53  	// Continue 15..25
    54  	e.Run(t, append(baseArgs, "--count", "10")...)
    55  
    56  	// Continue till end.
    57  	e.Run(t, baseArgs...)
    58  
    59  	// Dump and compare.
    60  	dumpPath := filepath.Join(tmpDir, "testdump.acc")
    61  
    62  	t.Run("missing config", func(t *testing.T) {
    63  		e.RunWithError(t, "neo-go", "db", "dump", "--privnet",
    64  			"--config-path", tmpDir, "--out", dumpPath)
    65  	})
    66  	t.Run("bad logger config", func(t *testing.T) {
    67  		badConfigDir := t.TempDir()
    68  		logfile := filepath.Join(badConfigDir, "logdir")
    69  		require.NoError(t, os.WriteFile(logfile, []byte{1, 2, 3}, os.ModePerm))
    70  		cfg = loadConfig(t)
    71  		cfg.ApplicationConfiguration.LogPath = filepath.Join(logfile, "file.log")
    72  		out, err = yaml.Marshal(cfg)
    73  		require.NoError(t, err)
    74  
    75  		cfgPath = filepath.Join(badConfigDir, "protocol.unit_testnet.yml")
    76  		require.NoError(t, os.WriteFile(cfgPath, out, os.ModePerm))
    77  
    78  		e.RunWithError(t, "neo-go", "db", "dump", "--unittest",
    79  			"--config-path", badConfigDir, "--out", dumpPath)
    80  	})
    81  	t.Run("bad storage config", func(t *testing.T) {
    82  		badConfigDir := t.TempDir()
    83  		logfile := filepath.Join(badConfigDir, "logdir")
    84  		require.NoError(t, os.WriteFile(logfile, []byte{1, 2, 3}, os.ModePerm))
    85  		cfg = loadConfig(t)
    86  		cfg.ApplicationConfiguration.DBConfiguration.Type = ""
    87  		out, err = yaml.Marshal(cfg)
    88  		require.NoError(t, err)
    89  
    90  		cfgPath = filepath.Join(badConfigDir, "protocol.unit_testnet.yml")
    91  		require.NoError(t, os.WriteFile(cfgPath, out, os.ModePerm))
    92  
    93  		e.RunWithError(t, "neo-go", "db", "dump", "--unittest",
    94  			"--config-path", badConfigDir, "--out", dumpPath)
    95  	})
    96  
    97  	baseCmd := []string{"neo-go", "db", "dump", "--unittest",
    98  		"--config-path", tmpDir, "--out", dumpPath}
    99  
   100  	t.Run("invalid start/count", func(t *testing.T) {
   101  		e.RunWithError(t, append(baseCmd, "--start", "5", "--count", strconv.Itoa(50-5+1+1))...)
   102  	})
   103  	t.Run("excessive dump parameters", func(t *testing.T) {
   104  		e.RunWithError(t, append(baseCmd, "something")...)
   105  	})
   106  
   107  	e.Run(t, baseCmd...)
   108  
   109  	d1, err := os.ReadFile(inDump)
   110  	require.NoError(t, err)
   111  	d2, err := os.ReadFile(dumpPath)
   112  	require.NoError(t, err)
   113  	require.Equal(t, d1, d2, "dumps differ")
   114  }
   115  
   116  func TestDBDumpRestoreIncremental(t *testing.T) {
   117  	tmpDir := t.TempDir()
   118  	chainPath := filepath.Join(tmpDir, "neogotestchain")
   119  	nonincDump := filepath.Join(tmpDir, "nonincDump.acc")
   120  	incDump := filepath.Join(tmpDir, "incDump.acc")
   121  
   122  	cfg, err := config.LoadFile(filepath.Join("..", "..", "config", "protocol.unit_testnet.yml"))
   123  	require.NoError(t, err, "could not load config")
   124  	cfg.ApplicationConfiguration.DBConfiguration.Type = dbconfig.LevelDB
   125  	cfg.ApplicationConfiguration.DBConfiguration.LevelDBOptions.DataDirectoryPath = chainPath
   126  	out, err := yaml.Marshal(cfg)
   127  	require.NoError(t, err)
   128  
   129  	cfgPath := filepath.Join(tmpDir, "protocol.unit_testnet.yml")
   130  	require.NoError(t, os.WriteFile(cfgPath, out, os.ModePerm))
   131  
   132  	e := testcli.NewExecutor(t, false)
   133  
   134  	// Create DB from dump.
   135  	e.Run(t, "neo-go", "db", "restore", "--unittest", "--config-path", tmpDir, "--in", inDump)
   136  
   137  	// Create two dumps: non-incremental and incremental.
   138  	dumpBaseArgs := []string{"neo-go", "db", "dump", "--unittest",
   139  		"--config-path", tmpDir}
   140  
   141  	// Dump first 15 blocks to a non-incremental dump.
   142  	e.Run(t, append(dumpBaseArgs, "--out", nonincDump, "--count", "15")...)
   143  
   144  	// Dump second 15 blocks to an incremental dump.
   145  	e.Run(t, append(dumpBaseArgs, "--out", incDump, "--start", "15", "--count", "15")...)
   146  
   147  	// Clean the DB.
   148  	require.NoError(t, os.RemoveAll(chainPath))
   149  
   150  	// Restore chain from two dumps.
   151  	restoreBaseArgs := []string{"neo-go", "db", "restore", "--unittest", "--config-path", tmpDir}
   152  
   153  	// Restore first 15 blocks from non-incremental dump.
   154  	e.Run(t, append(restoreBaseArgs, "--in", nonincDump)...)
   155  
   156  	// Restore second 15 blocks from incremental dump.
   157  	e.Run(t, append(restoreBaseArgs, "--in", incDump, "-n", "--count", "15")...)
   158  }