cli: add tests for server commands
Fixes: * Proper exit code should be returned on exit for server-related commands
This commit is contained in:
parent
613a23cc3f
commit
9de9bcb17c
5 changed files with 364 additions and 21 deletions
|
@ -1,7 +1,9 @@
|
|||
package server
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
"flag"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
@ -14,6 +16,7 @@ import (
|
|||
"github.com/stretchr/testify/require"
|
||||
"github.com/urfave/cli"
|
||||
"go.uber.org/zap"
|
||||
"gopkg.in/yaml.v2"
|
||||
)
|
||||
|
||||
// serverTestWD is the default working directory for server tests.
|
||||
|
@ -42,6 +45,18 @@ func TestHandleLoggingParams(t *testing.T) {
|
|||
d := t.TempDir()
|
||||
testLog := filepath.Join(d, "file.log")
|
||||
|
||||
t.Run("logdir is a file", func(t *testing.T) {
|
||||
logfile := filepath.Join(d, "logdir")
|
||||
require.NoError(t, os.WriteFile(logfile, []byte{1, 2, 3}, os.ModePerm))
|
||||
set := flag.NewFlagSet("flagSet", flag.ExitOnError)
|
||||
ctx := cli.NewContext(cli.NewApp(), set, nil)
|
||||
cfg := config.ApplicationConfiguration{
|
||||
LogPath: filepath.Join(logfile, "file.log"),
|
||||
}
|
||||
_, err := handleLoggingParams(ctx, cfg)
|
||||
require.Error(t, err)
|
||||
})
|
||||
|
||||
t.Run("default", func(t *testing.T) {
|
||||
set := flag.NewFlagSet("flagSet", flag.ExitOnError)
|
||||
ctx := cli.NewContext(cli.NewApp(), set, nil)
|
||||
|
@ -83,6 +98,12 @@ func TestInitBCWithMetrics(t *testing.T) {
|
|||
require.NoError(t, err)
|
||||
logger, err := handleLoggingParams(ctx, cfg.ApplicationConfiguration)
|
||||
require.NoError(t, err)
|
||||
|
||||
t.Run("bad store", func(t *testing.T) {
|
||||
_, _, _, err = initBCWithMetrics(config.Config{}, logger)
|
||||
require.Error(t, err)
|
||||
})
|
||||
|
||||
chain, prometheus, pprof, err := initBCWithMetrics(cfg, logger)
|
||||
require.NoError(t, err)
|
||||
t.Cleanup(func() {
|
||||
|
@ -139,9 +160,10 @@ func TestRestoreDB(t *testing.T) {
|
|||
require.NoError(t, err)
|
||||
t.Cleanup(func() { require.NoError(t, os.Chdir(serverTestWD)) })
|
||||
|
||||
//dump first
|
||||
// dump first
|
||||
set := flag.NewFlagSet("flagSet", flag.ExitOnError)
|
||||
set.String("config-path", filepath.Join(serverTestWD, "..", "..", "config"), "")
|
||||
goodCfg := filepath.Join(serverTestWD, "..", "..", "config")
|
||||
cfgPath := set.String("config-path", goodCfg, "")
|
||||
set.Bool("privnet", true, "")
|
||||
set.Bool("debug", true, "")
|
||||
set.Int("start", 0, "")
|
||||
|
@ -152,7 +174,101 @@ func TestRestoreDB(t *testing.T) {
|
|||
require.NoError(t, err)
|
||||
|
||||
// and then restore
|
||||
set.String("in", testDump, "")
|
||||
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")
|
||||
require.NoError(t, os.WriteFile(logfile, []byte{1, 2, 3}, os.ModePerm))
|
||||
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")
|
||||
require.NoError(t, ioutil.WriteFile(badCfgPath, out, os.ModePerm))
|
||||
|
||||
*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")
|
||||
require.NoError(t, ioutil.WriteFile(badCfgPath, out, os.ModePerm))
|
||||
|
||||
*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")
|
||||
require.NoError(t, os.WriteFile(inPath, []byte{1, 2, 3}, // file is expected to start from uint32
|
||||
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")
|
||||
b, err := os.ReadFile(testDump)
|
||||
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
|
||||
require.NoError(t, os.WriteFile(inPath, b, os.ModePerm))
|
||||
*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")
|
||||
b, err := os.ReadFile(testDump)
|
||||
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
|
||||
require.NoError(t, os.WriteFile(inPath, append(start, b...),
|
||||
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
|
||||
require.NoError(t, os.WriteFile(inPath, append(start, b...),
|
||||
os.ModePerm))
|
||||
*in = inPath
|
||||
*incremental = true
|
||||
|
||||
require.Error(t, restoreDB(ctx))
|
||||
})
|
||||
|
||||
*in = testDump
|
||||
*incremental = false
|
||||
})
|
||||
|
||||
set.String("dump", saveDump, "")
|
||||
require.NoError(t, restoreDB(ctx))
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue