Skip to content

Commit

Permalink
fix: Change LoadchainID() to a private function, and change server_te…
Browse files Browse the repository at this point in the history
…st to server to use a private function for testing
  • Loading branch information
Kynea0b committed Dec 13, 2022
1 parent b8a1a9a commit a833aff
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 47 deletions.
4 changes: 2 additions & 2 deletions server/oc_cmds.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func ShowValidatorCmd() *cobra.Command {
func showValidator(cmd *cobra.Command, config *cfg.Config) error {
var pv types.PrivValidator
if config.PrivValidatorListenAddr != "" {
chainID, err := LoadChainID(config)
chainID, err := loadChainID(config)
if err != nil {
return err
}
Expand Down Expand Up @@ -89,7 +89,7 @@ func showValidator(cmd *cobra.Command, config *cfg.Config) error {
return nil
}

func LoadChainID(config *cfg.Config) (string, error) {
func loadChainID(config *cfg.Config) (string, error) {
stateDB, err := node.DefaultDBProvider(&node.DBContext{ID: "state", Config: config})
if err != nil {
return "", err
Expand Down
28 changes: 13 additions & 15 deletions server/oc_cmds_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package server_test
package server

import (
"bytes"
Expand All @@ -20,8 +20,6 @@ import (
"github.com/line/ostracon/privval"
"github.com/line/ostracon/types"
tmtime "github.com/line/ostracon/types/time"

"github.com/line/lbm-sdk/server"
)

var (
Expand All @@ -31,16 +29,16 @@ var (
func TestShowValidator(t *testing.T) {
testCommon := newPrecedenceCommon(t)

serverCtx := &server.Context{}
ctx := context.WithValue(context.Background(), server.ServerContextKey, serverCtx)
serverCtx := &Context{}
ctx := context.WithValue(context.Background(), ServerContextKey, serverCtx)

if err := testCommon.cmd.ExecuteContext(ctx); err != cancelledInPreRun {
t.Fatalf("function failed with [%T] %v", err, err)
}

// ostracon init & create the server config file
initFilesWithConfig(serverCtx.Config)
output := captureStdout(t, func() { server.ShowValidatorCmd().ExecuteContext(ctx) })
output := captureStdout(t, func() { ShowValidatorCmd().ExecuteContext(ctx) })

// output must match the locally stored priv_validator key
privKey := loadFilePVKey(t, serverCtx.Config.PrivValidatorKeyFile())
Expand All @@ -52,8 +50,8 @@ func TestShowValidator(t *testing.T) {
func TestShowValidatorWithKMS(t *testing.T) {
testCommon := newPrecedenceCommon(t)

serverCtx := &server.Context{}
ctx := context.WithValue(context.Background(), server.ServerContextKey, serverCtx)
serverCtx := &Context{}
ctx := context.WithValue(context.Background(), ServerContextKey, serverCtx)

if err := testCommon.cmd.ExecuteContext(ctx); err != cancelledInPreRun {
t.Fatalf("function failed with [%T] %v", err, err)
Expand All @@ -62,7 +60,7 @@ func TestShowValidatorWithKMS(t *testing.T) {
// ostracon init & create the server config file
initFilesWithConfig(serverCtx.Config)

chainID, err := server.LoadChainID(serverCtx.Config)
chainID, err := loadChainID(serverCtx.Config)
require.NoError(t, err)

// remove config file
Expand All @@ -74,7 +72,7 @@ func TestShowValidatorWithKMS(t *testing.T) {
privval.WithMockKMS(t, t.TempDir(), chainID, func(addr string, privKey crypto.PrivKey) {
serverCtx.Config.PrivValidatorListenAddr = addr
require.NoFileExists(t, serverCtx.Config.PrivValidatorKeyFile())
output := captureStdout(t, func() { server.ShowValidatorCmd().ExecuteContext(ctx) })
output := captureStdout(t, func() { ShowValidatorCmd().ExecuteContext(ctx) })
require.NoError(t, err)

// output must contains the KMS public key
Expand All @@ -88,8 +86,8 @@ func TestShowValidatorWithKMS(t *testing.T) {
func TestShowValidatorWithInefficientKMSAddress(t *testing.T) {
testCommon := newPrecedenceCommon(t)

serverCtx := &server.Context{}
ctx := context.WithValue(context.Background(), server.ServerContextKey, serverCtx)
serverCtx := &Context{}
ctx := context.WithValue(context.Background(), ServerContextKey, serverCtx)

if err := testCommon.cmd.ExecuteContext(ctx); err != cancelledInPreRun {
t.Fatalf("function failed with [%T] %v", err, err)
Expand All @@ -105,7 +103,7 @@ func TestShowValidatorWithInefficientKMSAddress(t *testing.T) {
}

serverCtx.Config.PrivValidatorListenAddr = "127.0.0.1:inefficient"
err := server.ShowValidatorCmd().ExecuteContext(ctx)
err := ShowValidatorCmd().ExecuteContext(ctx)
require.Error(t, err)
}

Expand All @@ -121,7 +119,7 @@ func TestLoadChainID(t *testing.T) {
require.NoError(t, err)
require.Equal(t, expected, genDoc.ChainID)

chainID, err := server.LoadChainID(config)
chainID, err := loadChainID(config)
require.NoError(t, err)
require.Equal(t, expected, chainID)
}
Expand All @@ -136,7 +134,7 @@ func TestLoadChainIDWithoutStateDB(t *testing.T) {
config.DBBackend = "goleveldb"
config.DBPath = "/../path with containing chars that cannot be used\\/:*?\"<>|\x00"

_, err := server.LoadChainID(config)
_, err := loadChainID(config)
require.Error(t, err)
}

Expand Down
59 changes: 29 additions & 30 deletions server/util_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package server_test
package server

import (
"context"
Expand All @@ -13,15 +13,14 @@ import (
"github.com/spf13/cobra"

"github.com/line/lbm-sdk/client/flags"
"github.com/line/lbm-sdk/server"
)

var cancelledInPreRun = errors.New("Cancelled in prerun")

// Used in each test to run the function under test via Cobra
// but to always halt the command
func preRunETestImpl(cmd *cobra.Command, args []string) error {
err := server.InterceptConfigsPreRunHandler(cmd, "", nil)
err := InterceptConfigsPreRunHandler(cmd, "", nil)
if err != nil {
return err
}
Expand All @@ -31,15 +30,15 @@ func preRunETestImpl(cmd *cobra.Command, args []string) error {

func TestInterceptConfigsPreRunHandlerCreatesConfigFilesWhenMissing(t *testing.T) {
tempDir := t.TempDir()
cmd := server.StartCmd(nil, "/foobar")
cmd := StartCmd(nil, "/foobar")
if err := cmd.Flags().Set(flags.FlagHome, tempDir); err != nil {
t.Fatalf("Could not set home flag [%T] %v", err, err)
}

cmd.PreRunE = preRunETestImpl

serverCtx := &server.Context{}
ctx := context.WithValue(context.Background(), server.ServerContextKey, serverCtx)
serverCtx := &Context{}
ctx := context.WithValue(context.Background(), ServerContextKey, serverCtx)
if err := cmd.ExecuteContext(ctx); err != cancelledInPreRun {
t.Fatalf("function failed with [%T] %v", err, err)
}
Expand Down Expand Up @@ -107,15 +106,15 @@ func TestInterceptConfigsPreRunHandlerReadsConfigToml(t *testing.T) {
t.Fatalf("Failed closing config.toml: %v", err)
}

cmd := server.StartCmd(nil, "/foobar")
cmd := StartCmd(nil, "/foobar")
if err := cmd.Flags().Set(flags.FlagHome, tempDir); err != nil {
t.Fatalf("Could not set home flag [%T] %v", err, err)
}

cmd.PreRunE = preRunETestImpl

serverCtx := &server.Context{}
ctx := context.WithValue(context.Background(), server.ServerContextKey, serverCtx)
serverCtx := &Context{}
ctx := context.WithValue(context.Background(), ServerContextKey, serverCtx)

if err := cmd.ExecuteContext(ctx); err != cancelledInPreRun {
t.Fatalf("function failed with [%T] %v", err, err)
Expand Down Expand Up @@ -147,12 +146,12 @@ func TestInterceptConfigsPreRunHandlerReadsAppToml(t *testing.T) {
if err := writer.Close(); err != nil {
t.Fatalf("Failed closing app.toml: %v", err)
}
cmd := server.StartCmd(nil, tempDir)
cmd := StartCmd(nil, tempDir)

cmd.PreRunE = preRunETestImpl

serverCtx := &server.Context{}
ctx := context.WithValue(context.Background(), server.ServerContextKey, serverCtx)
serverCtx := &Context{}
ctx := context.WithValue(context.Background(), ServerContextKey, serverCtx)

if err := cmd.ExecuteContext(ctx); err != cancelledInPreRun {
t.Fatalf("function failed with [%T] %v", err, err)
Expand All @@ -166,7 +165,7 @@ func TestInterceptConfigsPreRunHandlerReadsAppToml(t *testing.T) {
func TestInterceptConfigsPreRunHandlerReadsFlags(t *testing.T) {
const testAddr = "tcp://127.1.2.3:12345"
tempDir := t.TempDir()
cmd := server.StartCmd(nil, "/foobar")
cmd := StartCmd(nil, "/foobar")

if err := cmd.Flags().Set(flags.FlagHome, tempDir); err != nil {
t.Fatalf("Could not set home flag [%T] %v", err, err)
Expand All @@ -179,8 +178,8 @@ func TestInterceptConfigsPreRunHandlerReadsFlags(t *testing.T) {

cmd.PreRunE = preRunETestImpl

serverCtx := &server.Context{}
ctx := context.WithValue(context.Background(), server.ServerContextKey, serverCtx)
serverCtx := &Context{}
ctx := context.WithValue(context.Background(), ServerContextKey, serverCtx)

if err := cmd.ExecuteContext(ctx); err != cancelledInPreRun {
t.Fatalf("function failed with [%T] %v", err, err)
Expand All @@ -194,7 +193,7 @@ func TestInterceptConfigsPreRunHandlerReadsFlags(t *testing.T) {
func TestInterceptConfigsPreRunHandlerReadsEnvVars(t *testing.T) {
const testAddr = "tcp://127.1.2.3:12345"
tempDir := t.TempDir()
cmd := server.StartCmd(nil, "/foobar")
cmd := StartCmd(nil, "/foobar")
if err := cmd.Flags().Set(flags.FlagHome, tempDir); err != nil {
t.Fatalf("Could not set home flag [%T] %v", err, err)
}
Expand All @@ -214,8 +213,8 @@ func TestInterceptConfigsPreRunHandlerReadsEnvVars(t *testing.T) {

cmd.PreRunE = preRunETestImpl

serverCtx := &server.Context{}
ctx := context.WithValue(context.Background(), server.ServerContextKey, serverCtx)
serverCtx := &Context{}
ctx := context.WithValue(context.Background(), ServerContextKey, serverCtx)

if err := cmd.ExecuteContext(ctx); err != cancelledInPreRun {
t.Fatalf("function failed with [%T] %v", err, err)
Expand Down Expand Up @@ -280,7 +279,7 @@ func newPrecedenceCommon(t *testing.T) precedenceCommon {
})

// Set up the command object that is used in this test
retval.cmd = server.StartCmd(nil, tempDir)
retval.cmd = StartCmd(nil, tempDir)
retval.cmd.PreRunE = preRunETestImpl

return retval
Expand Down Expand Up @@ -318,8 +317,8 @@ func TestInterceptConfigsPreRunHandlerPrecedenceFlag(t *testing.T) {
testCommon := newPrecedenceCommon(t)
testCommon.setAll(t, &TestAddrExpected, &TestAddrNotExpected, &TestAddrNotExpected)

serverCtx := &server.Context{}
ctx := context.WithValue(context.Background(), server.ServerContextKey, serverCtx)
serverCtx := &Context{}
ctx := context.WithValue(context.Background(), ServerContextKey, serverCtx)

if err := testCommon.cmd.ExecuteContext(ctx); err != cancelledInPreRun {
t.Fatalf("function failed with [%T] %v", err, err)
Expand All @@ -334,8 +333,8 @@ func TestInterceptConfigsPreRunHandlerPrecedenceEnvVar(t *testing.T) {
testCommon := newPrecedenceCommon(t)
testCommon.setAll(t, nil, &TestAddrExpected, &TestAddrNotExpected)

serverCtx := &server.Context{}
ctx := context.WithValue(context.Background(), server.ServerContextKey, serverCtx)
serverCtx := &Context{}
ctx := context.WithValue(context.Background(), ServerContextKey, serverCtx)

if err := testCommon.cmd.ExecuteContext(ctx); err != cancelledInPreRun {
t.Fatalf("function failed with [%T] %v", err, err)
Expand All @@ -350,8 +349,8 @@ func TestInterceptConfigsPreRunHandlerPrecedenceConfigFile(t *testing.T) {
testCommon := newPrecedenceCommon(t)
testCommon.setAll(t, nil, nil, &TestAddrExpected)

serverCtx := &server.Context{}
ctx := context.WithValue(context.Background(), server.ServerContextKey, serverCtx)
serverCtx := &Context{}
ctx := context.WithValue(context.Background(), ServerContextKey, serverCtx)

if err := testCommon.cmd.ExecuteContext(ctx); err != cancelledInPreRun {
t.Fatalf("function failed with [%T] %v", err, err)
Expand All @@ -366,8 +365,8 @@ func TestInterceptConfigsPreRunHandlerPrecedenceConfigDefault(t *testing.T) {
testCommon := newPrecedenceCommon(t)
// Do not set anything

serverCtx := &server.Context{}
ctx := context.WithValue(context.Background(), server.ServerContextKey, serverCtx)
serverCtx := &Context{}
ctx := context.WithValue(context.Background(), ServerContextKey, serverCtx)

if err := testCommon.cmd.ExecuteContext(ctx); err != cancelledInPreRun {
t.Fatalf("function failed with [%T] %v", err, err)
Expand All @@ -387,15 +386,15 @@ func TestInterceptConfigsWithBadPermissions(t *testing.T) {
if err := os.Mkdir(subDir, 0o600); err != nil {
t.Fatalf("Failed to create sub directory: %v", err)
}
cmd := server.StartCmd(nil, "/foobar")
cmd := StartCmd(nil, "/foobar")
if err := cmd.Flags().Set(flags.FlagHome, subDir); err != nil {
t.Fatalf("Could not set home flag [%T] %v", err, err)
}

cmd.PreRunE = preRunETestImpl

serverCtx := &server.Context{}
ctx := context.WithValue(context.Background(), server.ServerContextKey, serverCtx)
serverCtx := &Context{}
ctx := context.WithValue(context.Background(), ServerContextKey, serverCtx)
if err := cmd.ExecuteContext(ctx); !os.IsPermission(err) {
t.Fatalf("Failed to catch permissions error, got: [%T] %v", err, err)
}
Expand Down

0 comments on commit a833aff

Please sign in to comment.