From 10693a3427603b2479d1585aa11d9585ecaa418b Mon Sep 17 00:00:00 2001 From: mariko Date: Tue, 6 Dec 2022 13:05:46 +0900 Subject: [PATCH 1/6] feat: Get validator pubkey considering KMS --- server/oc_cmds.go | 80 ++++++++++++++++++++++++++++++++++------------- 1 file changed, 58 insertions(+), 22 deletions(-) diff --git a/server/oc_cmds.go b/server/oc_cmds.go index 65e7d852e7..009ac06c38 100644 --- a/server/oc_cmds.go +++ b/server/oc_cmds.go @@ -4,16 +4,17 @@ package server import ( "fmt" - + sdk "github.com/line/lbm-sdk/types" + cfg "github.com/line/ostracon/config" + osjson "github.com/line/ostracon/libs/json" + ostos "github.com/line/ostracon/libs/os" + "github.com/line/ostracon/node" "github.com/line/ostracon/p2p" pvm "github.com/line/ostracon/privval" + "github.com/line/ostracon/types" ostversion "github.com/line/ostracon/version" "github.com/spf13/cobra" yaml "gopkg.in/yaml.v2" - - "github.com/line/lbm-sdk/client" - cryptocodec "github.com/line/lbm-sdk/crypto/codec" - sdk "github.com/line/lbm-sdk/types" ) // ShowNodeIDCmd - ported from Ostracon, dump node ID to stdout @@ -43,29 +44,64 @@ func ShowValidatorCmd() *cobra.Command { RunE: func(cmd *cobra.Command, args []string) error { serverCtx := GetServerContextFromCmd(cmd) cfg := serverCtx.Config - - privValidator := pvm.LoadFilePV(cfg.PrivValidatorKeyFile(), cfg.PrivValidatorStateFile()) - pk, err := privValidator.GetPubKey() - if err != nil { - return err - } - sdkPK, err := cryptocodec.FromOcPubKeyInterface(pk) - if err != nil { - return err - } - clientCtx := client.GetClientContextFromCmd(cmd) - bz, err := clientCtx.Codec.MarshalInterfaceJSON(sdkPK) - if err != nil { - return err - } - fmt.Println(string(bz)) - return nil + return showValidator(cmd, args, cfg) }, } return &cmd } +func showValidator(cmd *cobra.Command, args []string, config *cfg.Config) error { + var pv types.PrivValidator + if config.PrivValidatorListenAddr != "" { + chainID, err := loadChainID(config) + if err != nil { + return err + } + serverCtx := GetServerContextFromCmd(cmd) + log := serverCtx.Logger + pv, err = node.CreateAndStartPrivValidatorSocketClient(config.PrivValidatorListenAddr, chainID, log) + if err != nil { + return err + } + } else { + keyFilePath := config.PrivValidatorKeyFile() + if !ostos.FileExists(keyFilePath) { + return fmt.Errorf("private validator file %s does not exist", keyFilePath) + } + pv = pvm.LoadFilePV(keyFilePath, config.PrivValidatorStateFile()) + } + + pubKey, err := pv.GetPubKey() + if err != nil { + return fmt.Errorf("can't get pubkey: %w", err) + } + + bz, err := osjson.Marshal(pubKey) + if err != nil { + return fmt.Errorf("failed to marshal private validator pubkey: %w", err) + } + + fmt.Println(string(bz)) + return nil +} + +func loadChainID(config *cfg.Config) (string, error) { + stateDB, err := node.DefaultDBProvider(&node.DBContext{ID: "state", Config: config}) + if err != nil { + return "", err + } + defer func() { + var _ = stateDB.Close() + }() + genesisDocProvider := node.DefaultGenesisDocProviderFunc(config) + _, genDoc, err := node.LoadStateFromDBOrGenesisDocProvider(stateDB, genesisDocProvider) + if err != nil { + return "", err + } + return genDoc.ChainID, nil +} + // ShowAddressCmd - show this node's validator address func ShowAddressCmd() *cobra.Command { cmd := &cobra.Command{ From 7ee53105f36182423ce80549473c697abb6c0d69 Mon Sep 17 00:00:00 2001 From: mariko Date: Tue, 6 Dec 2022 13:21:39 +0900 Subject: [PATCH 2/6] fix: lint error --- server/oc_cmds.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/server/oc_cmds.go b/server/oc_cmds.go index 009ac06c38..659f359301 100644 --- a/server/oc_cmds.go +++ b/server/oc_cmds.go @@ -4,6 +4,7 @@ package server import ( "fmt" + sdk "github.com/line/lbm-sdk/types" cfg "github.com/line/ostracon/config" osjson "github.com/line/ostracon/libs/json" @@ -44,14 +45,14 @@ func ShowValidatorCmd() *cobra.Command { RunE: func(cmd *cobra.Command, args []string) error { serverCtx := GetServerContextFromCmd(cmd) cfg := serverCtx.Config - return showValidator(cmd, args, cfg) + return showValidator(cmd, cfg) }, } return &cmd } -func showValidator(cmd *cobra.Command, args []string, config *cfg.Config) error { +func showValidator(cmd *cobra.Command, config *cfg.Config) error { var pv types.PrivValidator if config.PrivValidatorListenAddr != "" { chainID, err := loadChainID(config) From 858b05a5db7a402b34b6a0e46e26470bb2a4cd7d Mon Sep 17 00:00:00 2001 From: kekeshiM0chi Date: Sun, 11 Dec 2022 21:46:03 +0900 Subject: [PATCH 3/6] test: Add test for `ShowValidatorCmd()` for KMS --- server/oc_cmds.go | 4 +- server/oc_cmds_test.go | 232 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 234 insertions(+), 2 deletions(-) create mode 100644 server/oc_cmds_test.go diff --git a/server/oc_cmds.go b/server/oc_cmds.go index 659f359301..1ffe80a070 100644 --- a/server/oc_cmds.go +++ b/server/oc_cmds.go @@ -55,7 +55,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 } @@ -87,7 +87,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 diff --git a/server/oc_cmds_test.go b/server/oc_cmds_test.go new file mode 100644 index 0000000000..7b4fcb96cb --- /dev/null +++ b/server/oc_cmds_test.go @@ -0,0 +1,232 @@ +package server_test + +import ( + "bytes" + "context" + "fmt" + "github.com/line/lbm-sdk/server" + cfg "github.com/line/ostracon/config" + "github.com/line/ostracon/crypto" + tmjson "github.com/line/ostracon/libs/json" + "github.com/line/ostracon/libs/log" + tmos "github.com/line/ostracon/libs/os" + tmrand "github.com/line/ostracon/libs/rand" + "github.com/line/ostracon/p2p" + "github.com/line/ostracon/privval" + "github.com/line/ostracon/types" + tmtime "github.com/line/ostracon/types/time" + "github.com/stretchr/testify/require" + "io/ioutil" + "os" + "testing" +) + +var ( + logger = log.NewOCLogger(log.NewSyncWriter(os.Stdout)) +) + +func TestShowValidator(t *testing.T) { + testCommon := newPrecedenceCommon(t) + + serverCtx := &server.Context{} + ctx := context.WithValue(context.Background(), server.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 must match the locally stored priv_validator key + privKey := loadFilePVKey(t, serverCtx.Config.PrivValidatorKeyFile()) + bz, err := tmjson.Marshal(privKey.PubKey) + require.NoError(t, err) + require.Equal(t, string(bz), output) +} + +func TestShowValidatorWithKMS(t *testing.T) { + testCommon := newPrecedenceCommon(t) + + serverCtx := &server.Context{} + ctx := context.WithValue(context.Background(), server.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) + + chainID, err := server.LoadChainID(serverCtx.Config) + require.NoError(t, err) + + // remove config file + if tmos.FileExists(serverCtx.Config.PrivValidatorKeyFile()) { + err := os.Remove(serverCtx.Config.PrivValidatorKeyFile()) + require.NoError(t, err) + } + + 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) }) + require.NoError(t, err) + + // output must contains the KMS public key + bz, err := tmjson.Marshal(privKey.PubKey()) + require.NoError(t, err) + expected := string(bz) + require.Contains(t, output, expected) + }) +} + +func TestShowValidatorWithInefficientKMSAddress(t *testing.T) { + testCommon := newPrecedenceCommon(t) + + serverCtx := &server.Context{} + ctx := context.WithValue(context.Background(), server.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) + + // remove config file + if tmos.FileExists(serverCtx.Config.PrivValidatorKeyFile()) { + err := os.Remove(serverCtx.Config.PrivValidatorKeyFile()) + require.NoError(t, err) + } + + serverCtx.Config.PrivValidatorListenAddr = "127.0.0.1:inefficient" + err := server.ShowValidatorCmd().ExecuteContext(ctx) + require.Error(t, err) +} + +func TestLoadChainID(t *testing.T) { + expected := "c57861" + config := cfg.ResetTestRootWithChainID("TestLoadChainID", expected) + defer func() { + var _ = os.RemoveAll(config.RootDir) + }() + + require.FileExists(t, config.GenesisFile()) + genDoc, err := types.GenesisDocFromFile(config.GenesisFile()) + require.NoError(t, err) + require.Equal(t, expected, genDoc.ChainID) + + chainID, err := server.LoadChainID(config) + require.NoError(t, err) + require.Equal(t, expected, chainID) +} + +func TestLoadChainIDWithoutStateDB(t *testing.T) { + expected := "c34091" + config := cfg.ResetTestRootWithChainID("TestLoadChainID", expected) + defer func() { + var _ = os.RemoveAll(config.RootDir) + }() + + config.DBBackend = "goleveldb" + config.DBPath = "/../path with containing chars that cannot be used\\/:*?\"<>|\x00" + + _, err := server.LoadChainID(config) + require.Error(t, err) +} + +func initFilesWithConfig(config *cfg.Config) error { + // private validator + privValKeyFile := config.PrivValidatorKeyFile() + privValStateFile := config.PrivValidatorStateFile() + privKeyType := config.PrivValidatorKeyType() + var pv *privval.FilePV + if tmos.FileExists(privValKeyFile) { + pv = privval.LoadFilePV(privValKeyFile, privValStateFile) + logger.Info("Found private validator", "keyFile", privValKeyFile, + "stateFile", privValStateFile) + } else { + var err error + pv, err = privval.GenFilePV(privValKeyFile, privValStateFile, privKeyType) + if err != nil { + return err + } + if pv != nil { + pv.Save() + } + logger.Info("Generated private validator", "keyFile", privValKeyFile, + "stateFile", privValStateFile) + } + + nodeKeyFile := config.NodeKeyFile() + if tmos.FileExists(nodeKeyFile) { + logger.Info("Found node key", "path", nodeKeyFile) + } else { + if _, err := p2p.LoadOrGenNodeKey(nodeKeyFile); err != nil { + return err + } + logger.Info("Generated node key", "path", nodeKeyFile) + } + + // genesis file + genFile := config.GenesisFile() + if tmos.FileExists(genFile) { + logger.Info("Found genesis file", "path", genFile) + } else { + genDoc := types.GenesisDoc{ + ChainID: fmt.Sprintf("test-chain-%v", tmrand.Str(6)), + GenesisTime: tmtime.Now(), + ConsensusParams: types.DefaultConsensusParams(), + VoterParams: types.DefaultVoterParams(), + } + pubKey, err := pv.GetPubKey() + if err != nil { + return fmt.Errorf("can't get pubkey: %w", err) + } + genDoc.Validators = []types.GenesisValidator{{ + Address: pubKey.Address(), + PubKey: pubKey, + Power: 10, + }} + + if err := genDoc.SaveAs(genFile); err != nil { + return err + } + logger.Info("Generated genesis file", "path", genFile) + } + + return nil +} + +func loadFilePVKey(t *testing.T, file string) privval.FilePVKey { + // output must match the locally stored priv_validator key + keyJSONBytes, err := ioutil.ReadFile(file) + require.NoError(t, err) + privKey := privval.FilePVKey{} + err = tmjson.Unmarshal(keyJSONBytes, &privKey) + require.NoError(t, err) + return privKey +} + +func captureStdout(t *testing.T, fnc func()) string { + t.Helper() + backup := os.Stdout + defer func() { + os.Stdout = backup + }() + r, w, err := os.Pipe() + if err != nil { + t.Fatalf("fail pipe: %v", err) + } + os.Stdout = w + fnc() + w.Close() + var buffer bytes.Buffer + if n, err := buffer.ReadFrom(r); err != nil { + t.Fatalf("fail read buf: %v - number: %v", err, n) + } + output := buffer.String() + return output[:len(output)-1] +} From 2382021506e07c81bcf412aa33db301c59f6a9fe Mon Sep 17 00:00:00 2001 From: mariko Date: Mon, 12 Dec 2022 11:04:01 +0900 Subject: [PATCH 4/6] fix: Fix to sort imports by group --- server/oc_cmds.go | 8 +++++--- server/oc_cmds_test.go | 13 ++++++++----- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/server/oc_cmds.go b/server/oc_cmds.go index 1ffe80a070..199a1c2bb9 100644 --- a/server/oc_cmds.go +++ b/server/oc_cmds.go @@ -5,7 +5,9 @@ package server import ( "fmt" - sdk "github.com/line/lbm-sdk/types" + "github.com/spf13/cobra" + yaml "gopkg.in/yaml.v2" + cfg "github.com/line/ostracon/config" osjson "github.com/line/ostracon/libs/json" ostos "github.com/line/ostracon/libs/os" @@ -14,8 +16,8 @@ import ( pvm "github.com/line/ostracon/privval" "github.com/line/ostracon/types" ostversion "github.com/line/ostracon/version" - "github.com/spf13/cobra" - yaml "gopkg.in/yaml.v2" + + sdk "github.com/line/lbm-sdk/types" ) // ShowNodeIDCmd - ported from Ostracon, dump node ID to stdout diff --git a/server/oc_cmds_test.go b/server/oc_cmds_test.go index 7b4fcb96cb..90d48f08b6 100644 --- a/server/oc_cmds_test.go +++ b/server/oc_cmds_test.go @@ -4,7 +4,12 @@ import ( "bytes" "context" "fmt" - "github.com/line/lbm-sdk/server" + "io/ioutil" + "os" + "testing" + + "github.com/stretchr/testify/require" + cfg "github.com/line/ostracon/config" "github.com/line/ostracon/crypto" tmjson "github.com/line/ostracon/libs/json" @@ -15,10 +20,8 @@ import ( "github.com/line/ostracon/privval" "github.com/line/ostracon/types" tmtime "github.com/line/ostracon/types/time" - "github.com/stretchr/testify/require" - "io/ioutil" - "os" - "testing" + + "github.com/line/lbm-sdk/server" ) var ( From b8a1a9afed5f7c4e8a29efef1675a1d21c86cdf7 Mon Sep 17 00:00:00 2001 From: mariko Date: Mon, 12 Dec 2022 13:12:55 +0900 Subject: [PATCH 5/6] feat: Add changes for KMS support in `ShowValidatorCmd` --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5b6fd6c16e..afe2b9f97c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -41,6 +41,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ ### Improvements * (cosmovisor) [\#792](https://github.com/line/lbm-sdk/pull/792) Use upstream's cosmovisor +* (server) [\#821](https://github.com/line/lbm-sdk/pull/821) Get validator pubkey considering KMS ### Bug Fixes * (client) [\#817](https://github.com/line/lbm-sdk/pull/817) remove support for composite (BLS) type From a833aff39c6fbe40a3ab276667ea0312f62f053e Mon Sep 17 00:00:00 2001 From: mariko Date: Tue, 13 Dec 2022 11:30:03 +0900 Subject: [PATCH 6/6] fix: Change LoadchainID() to a private function, and change server_test to server to use a private function for testing --- server/oc_cmds.go | 4 +-- server/oc_cmds_test.go | 28 ++++++++++---------- server/util_test.go | 59 +++++++++++++++++++++--------------------- 3 files changed, 44 insertions(+), 47 deletions(-) diff --git a/server/oc_cmds.go b/server/oc_cmds.go index 199a1c2bb9..64e95cff0f 100644 --- a/server/oc_cmds.go +++ b/server/oc_cmds.go @@ -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 } @@ -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 diff --git a/server/oc_cmds_test.go b/server/oc_cmds_test.go index 90d48f08b6..1ea4bfc2c2 100644 --- a/server/oc_cmds_test.go +++ b/server/oc_cmds_test.go @@ -1,4 +1,4 @@ -package server_test +package server import ( "bytes" @@ -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 ( @@ -31,8 +29,8 @@ 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) @@ -40,7 +38,7 @@ func TestShowValidator(t *testing.T) { // 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()) @@ -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) @@ -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 @@ -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 @@ -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) @@ -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) } @@ -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) } @@ -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) } diff --git a/server/util_test.go b/server/util_test.go index 0f62fe85fe..c015a88d3a 100644 --- a/server/util_test.go +++ b/server/util_test.go @@ -1,4 +1,4 @@ -package server_test +package server import ( "context" @@ -13,7 +13,6 @@ 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") @@ -21,7 +20,7 @@ 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 } @@ -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) } @@ -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) @@ -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) @@ -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) @@ -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) @@ -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) } @@ -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) @@ -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 @@ -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) @@ -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) @@ -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) @@ -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) @@ -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) }