From e99e6ea0c7d913a8484b642d222044a47565d65a Mon Sep 17 00:00:00 2001 From: Adrian Brink Date: Thu, 4 May 2017 11:29:25 +0200 Subject: [PATCH 1/2] Extend abci-cli to allow integration tests This commit adds the basic test command 'abci-cli test' that will allow developers of server for their own language to continuously test their implementation. --- README.md | 44 +++++++++++++++++ client/grpc_client.go | 5 +- cmd/abci-cli/abci-cli.go | 51 +++++++++++++++++++ tests/server/client.go | 103 +++++++++++++++++++++++++++++++++++++++ tests/test_cli/test.sh | 0 tests/tests.go | 1 + 6 files changed, 201 insertions(+), 3 deletions(-) create mode 100644 tests/server/client.go mode change 100644 => 100755 tests/test_cli/test.sh create mode 100644 tests/tests.go diff --git a/README.md b/README.md index 1b7f89007..3c7453a26 100644 --- a/README.md +++ b/README.md @@ -192,3 +192,47 @@ Here, we describe the requests and responses as function arguments and return va #### Flush * __Usage__:
* Signals that messages queued on the client should be flushed to the server. It is called periodically by the client implementation to ensure asynchronous requests are actually sent, and is called immediately to make a synchronous request, which returns when the Flush response comes back. + +# Implementation + +We provide three implementations of the ABCI in Go: + +1. ABCI-socket +2. GRPC +3. Golang in-process + +## Socket + +ABCI is best implemented as a streaming protocol. +The socket implementation provides for asynchronous, ordered message passing over unix or tcp. +Messages are serialized using Protobuf3 and length-prefixed. +Protobuf3 doesn't have an official length-prefix standard, so we use our own. The first byte represents the length of the big-endian encoded length. + +For example, if the Protobuf3 encoded ABCI message is `0xDEADBEEF` (4 bytes), the length-prefixed message is `0x0104DEADBEEF`. If the Protobuf3 encoded ABCI message is 65535 bytes long, the length-prefixed message would be like `0x02FFFF...`. + +## GRPC + +GRPC is an rpc framework native to Protocol Buffers with support in many languages. +Implementing the ABCI using GRPC can allow for faster prototyping, but is expected to be much slower than +the ordered, asynchronous socket protocol. + +Note the length-prefixing used in the socket implementation does not apply for GRPC. + +## In Process + +The simplest implementation just uses function calls within Go. +This means ABCI applications written in Golang can be compiled with TendermintCore and run as a single binary. + + +# Tools and Apps + +The `abci-cli` tool wraps any ABCI client and can be used for probing/testing an ABCI application. +See the [guide](https://tendermint.readthedocs.io/en/master/abci-cli.html) for more details. + +Multiple example apps are included: +- the `counter` application, which illustrates nonce checking in txs +- the `dummy` application, which illustrates a simple key-value merkle tree +- the `dummy --persistent` application, which augments the dummy with persistence and validator set changes + +When you are developing an implementation of the ABCI server in your favourite language, we provide the `abci-cli test` +command that allows you to run our tests against your own ABCI server. Please make sure that your server supports at least the TSP protocol, and optionally gRPC. diff --git a/client/grpc_client.go b/client/grpc_client.go index 9328fa326..f277e1d77 100644 --- a/client/grpc_client.go +++ b/client/grpc_client.go @@ -48,7 +48,6 @@ func (cli *grpcClient) OnStart() error { return err } RETRY_LOOP: - for { conn, err := grpc.Dial(cli.addr, grpc.WithInsecure(), grpc.WithDialer(dialerFunc)) if err != nil { @@ -83,8 +82,8 @@ func (cli *grpcClient) OnStop() { cli.mtx.Lock() defer cli.mtx.Unlock() // TODO: how to close conn? its not a net.Conn and grpc doesn't expose a Close() - /*if cli.conn != nil { - cli.conn.Close() + /*if cli.client.conn != nil { + cli.client.conn.Close() }*/ } diff --git a/cmd/abci-cli/abci-cli.go b/cmd/abci-cli/abci-cli.go index e7fae52ee..9e8e86b97 100644 --- a/cmd/abci-cli/abci-cli.go +++ b/cmd/abci-cli/abci-cli.go @@ -19,6 +19,7 @@ import ( "github.com/tendermint/abci/example/counter" "github.com/tendermint/abci/example/dummy" "github.com/tendermint/abci/server" + servertest "github.com/tendermint/abci/tests/server" "github.com/tendermint/abci/types" "github.com/tendermint/abci/version" ) @@ -141,6 +142,7 @@ func addCommands() { RootCmd.AddCommand(checkTxCmd) RootCmd.AddCommand(commitCmd) RootCmd.AddCommand(versionCmd) + RootCmd.AddCommand(testCmd) addQueryFlags() RootCmd.AddCommand(queryCmd) @@ -271,6 +273,16 @@ var dummyCmd = &cobra.Command{ }, } +var testCmd = &cobra.Command{ + Use: "test", + Short: "Run integration tests", + Long: "", + Args: cobra.ExactArgs(0), + RunE: func(cmd *cobra.Command, args []string) error { + return cmdTest(cmd, args) + }, +} + // Generates new Args array based off of previous call args to maintain flag persistence func persistentArgs(line []byte) []string { @@ -287,6 +299,45 @@ func persistentArgs(line []byte) []string { //-------------------------------------------------------------------------------- +func cmdTest(cmd *cobra.Command, args []string) error { + fmt.Println("Running tests") + + var err error + + err = servertest.InitChain(client) + fmt.Println("") + err = servertest.SetOption(client, "serial", "on") + fmt.Println("") + err = servertest.Commit(client, nil) + fmt.Println("") + err = servertest.DeliverTx(client, []byte("abc"), types.CodeType_BadNonce, nil) + fmt.Println("") + err = servertest.Commit(client, nil) + fmt.Println("") + err = servertest.DeliverTx(client, []byte{0x00}, types.CodeType_OK, nil) + fmt.Println("") + err = servertest.Commit(client, []byte{0, 0, 0, 0, 0, 0, 0, 1}) + fmt.Println("") + err = servertest.DeliverTx(client, []byte{0x00}, types.CodeType_BadNonce, nil) + fmt.Println("") + err = servertest.DeliverTx(client, []byte{0x01}, types.CodeType_OK, nil) + fmt.Println("") + err = servertest.DeliverTx(client, []byte{0x00, 0x02}, types.CodeType_OK, nil) + fmt.Println("") + err = servertest.DeliverTx(client, []byte{0x00, 0x03}, types.CodeType_OK, nil) + fmt.Println("") + err = servertest.DeliverTx(client, []byte{0x00, 0x00, 0x04}, types.CodeType_OK, nil) + fmt.Println("") + err = servertest.DeliverTx(client, []byte{0x00, 0x00, 0x06}, types.CodeType_BadNonce, nil) + fmt.Println("") + err = servertest.Commit(client, []byte{0, 0, 0, 0, 0, 0, 0, 5}) + + if err != nil { + return errors.New("Some checks didn't pass, please use the cli to see the exact failures.") + } + return nil +} + func cmdBatch(cmd *cobra.Command, args []string) error { bufReader := bufio.NewReader(os.Stdin) for { diff --git a/tests/server/client.go b/tests/server/client.go new file mode 100644 index 000000000..502e4e8e6 --- /dev/null +++ b/tests/server/client.go @@ -0,0 +1,103 @@ +package testsuite + +import ( + "bytes" + "errors" + "fmt" + + abcicli "github.com/tendermint/abci/client" + "github.com/tendermint/abci/types" + crypto "github.com/tendermint/go-crypto" + cmn "github.com/tendermint/tmlibs/common" +) + +func InitChain(client abcicli.Client) error { + total := 10 + vals := make([]*types.Validator, total) + for i := 0; i < total; i++ { + pubkey := crypto.GenPrivKeyEd25519FromSecret([]byte(cmn.Fmt("test%d", i))).PubKey().Bytes() + power := cmn.RandInt() + vals[i] = &types.Validator{pubkey, uint64(power)} + } + err := client.InitChainSync(vals) + if err != nil { + fmt.Println("Failed test: InitChain - %v", err) + return err + } + fmt.Println("Passed test: InitChain") + return nil +} + +func SetOption(client abcicli.Client, key, value string) error { + res := client.SetOptionSync(key, value) + _, _, log := res.Code, res.Data, res.Log + if res.IsErr() { + fmt.Println("Failed test: SetOption") + fmt.Printf("setting %v=%v: \nlog: %v", key, value, log) + fmt.Println("Failed test: SetOption") + return errors.New(res.Error()) + } + fmt.Println("Passed test: SetOption") + return nil +} + +func Commit(client abcicli.Client, hashExp []byte) error { + res := client.CommitSync() + _, data, log := res.Code, res.Data, res.Log + if res.IsErr() { + fmt.Println("Failed test: Commit") + fmt.Printf("committing %v\nlog: %v", log) + return errors.New(res.Error()) + } + if !bytes.Equal(res.Data, hashExp) { + fmt.Println("Failed test: Commit") + fmt.Printf("Commit hash was unexpected. Got %X expected %X", + data, hashExp) + return errors.New("CommitTx failed") + } + fmt.Println("Passed test: Commit") + return nil +} + +func DeliverTx(client abcicli.Client, txBytes []byte, codeExp types.CodeType, dataExp []byte) error { + res := client.DeliverTxSync(txBytes) + code, data, log := res.Code, res.Data, res.Log + if code != codeExp { + fmt.Println("Failed test: DeliverTx") + fmt.Printf("DeliverTx response code was unexpected. Got %v expected %v. Log: %v", + code, codeExp, log) + return errors.New("DeliverTx error") + } + if !bytes.Equal(data, dataExp) { + fmt.Println("Failed test: DeliverTx") + fmt.Printf("DeliverTx response data was unexpected. Got %X expected %X", + data, dataExp) + return errors.New("DeliverTx error") + } + fmt.Println("Passed test: DeliverTx") + return nil +} + +func CheckTx(client abcicli.Client, txBytes []byte, codeExp types.CodeType, dataExp []byte) error { + res := client.CheckTxSync(txBytes) + code, data, log := res.Code, res.Data, res.Log + if res.IsErr() { + fmt.Println("Failed test: CheckTx") + fmt.Printf("checking tx %X: %v\nlog: %v", txBytes, log) + return errors.New(res.Error()) + } + if code != codeExp { + fmt.Println("Failed test: CheckTx") + fmt.Printf("CheckTx response code was unexpected. Got %v expected %v. Log: %v", + code, codeExp, log) + return errors.New("CheckTx") + } + if !bytes.Equal(data, dataExp) { + fmt.Println("Failed test: CheckTx") + fmt.Printf("CheckTx response data was unexpected. Got %X expected %X", + data, dataExp) + return errors.New("CheckTx") + } + fmt.Println("Passed test: CheckTx") + return nil +} diff --git a/tests/test_cli/test.sh b/tests/test_cli/test.sh old mode 100644 new mode 100755 diff --git a/tests/tests.go b/tests/tests.go new file mode 100644 index 000000000..ca8701d29 --- /dev/null +++ b/tests/tests.go @@ -0,0 +1 @@ +package tests From 8357326db011b1b30bc13392423e953a815d95cf Mon Sep 17 00:00:00 2001 From: Krzysztof Jurewicz Date: Fri, 20 Oct 2017 18:44:37 +0200 Subject: [PATCH 2/2] Fix test command --- cmd/abci-cli/abci-cli.go | 43 ++++++++++++++++++++++++---------------- tests/server/client.go | 39 ++++++++++++++++-------------------- 2 files changed, 43 insertions(+), 39 deletions(-) diff --git a/cmd/abci-cli/abci-cli.go b/cmd/abci-cli/abci-cli.go index 9e8e86b97..3ee454aa6 100644 --- a/cmd/abci-cli/abci-cli.go +++ b/cmd/abci-cli/abci-cli.go @@ -16,6 +16,7 @@ import ( "github.com/tendermint/tmlibs/log" abcicli "github.com/tendermint/abci/client" + "github.com/tendermint/abci/example/code" "github.com/tendermint/abci/example/counter" "github.com/tendermint/abci/example/dummy" "github.com/tendermint/abci/server" @@ -274,10 +275,10 @@ var dummyCmd = &cobra.Command{ } var testCmd = &cobra.Command{ - Use: "test", + Use: "test", Short: "Run integration tests", - Long: "", - Args: cobra.ExactArgs(0), + Long: "", + Args: cobra.ExactArgs(0), RunE: func(cmd *cobra.Command, args []string) error { return cmdTest(cmd, args) }, @@ -299,6 +300,14 @@ func persistentArgs(line []byte) []string { //-------------------------------------------------------------------------------- +func or(err1 error, err2 error) error { + if err1 == nil { + return err2 + } else { + return err1 + } +} + func cmdTest(cmd *cobra.Command, args []string) error { fmt.Println("Running tests") @@ -306,34 +315,34 @@ func cmdTest(cmd *cobra.Command, args []string) error { err = servertest.InitChain(client) fmt.Println("") - err = servertest.SetOption(client, "serial", "on") + err = or(err, servertest.SetOption(client, "serial", "on")) fmt.Println("") - err = servertest.Commit(client, nil) + err = or(err, servertest.Commit(client, nil)) fmt.Println("") - err = servertest.DeliverTx(client, []byte("abc"), types.CodeType_BadNonce, nil) + err = or(err, servertest.DeliverTx(client, []byte("abc"), code.CodeTypeBadNonce, nil)) fmt.Println("") - err = servertest.Commit(client, nil) + err = or(err, servertest.Commit(client, nil)) fmt.Println("") - err = servertest.DeliverTx(client, []byte{0x00}, types.CodeType_OK, nil) + err = or(err, servertest.DeliverTx(client, []byte{0x00}, code.CodeTypeOK, nil)) fmt.Println("") - err = servertest.Commit(client, []byte{0, 0, 0, 0, 0, 0, 0, 1}) + err = or(err, servertest.Commit(client, []byte{0, 0, 0, 0, 0, 0, 0, 1})) fmt.Println("") - err = servertest.DeliverTx(client, []byte{0x00}, types.CodeType_BadNonce, nil) + err = or(err, servertest.DeliverTx(client, []byte{0x00}, code.CodeTypeBadNonce, nil)) fmt.Println("") - err = servertest.DeliverTx(client, []byte{0x01}, types.CodeType_OK, nil) + err = or(err, servertest.DeliverTx(client, []byte{0x01}, code.CodeTypeOK, nil)) fmt.Println("") - err = servertest.DeliverTx(client, []byte{0x00, 0x02}, types.CodeType_OK, nil) + err = or(err, servertest.DeliverTx(client, []byte{0x00, 0x02}, code.CodeTypeOK, nil)) fmt.Println("") - err = servertest.DeliverTx(client, []byte{0x00, 0x03}, types.CodeType_OK, nil) + err = or(err, servertest.DeliverTx(client, []byte{0x00, 0x03}, code.CodeTypeOK, nil)) fmt.Println("") - err = servertest.DeliverTx(client, []byte{0x00, 0x00, 0x04}, types.CodeType_OK, nil) + err = or(err, servertest.DeliverTx(client, []byte{0x00, 0x00, 0x04}, code.CodeTypeOK, nil)) fmt.Println("") - err = servertest.DeliverTx(client, []byte{0x00, 0x00, 0x06}, types.CodeType_BadNonce, nil) + err = or(err, servertest.DeliverTx(client, []byte{0x00, 0x00, 0x06}, code.CodeTypeBadNonce, nil)) fmt.Println("") - err = servertest.Commit(client, []byte{0, 0, 0, 0, 0, 0, 0, 5}) + err = or(err, servertest.Commit(client, []byte{0, 0, 0, 0, 0, 0, 0, 5})) if err != nil { - return errors.New("Some checks didn't pass, please use the cli to see the exact failures.") + return errors.New("Some checks didn't pass, please inspect stdout to see the exact failures.") } return nil } diff --git a/tests/server/client.go b/tests/server/client.go index 502e4e8e6..f1757fe15 100644 --- a/tests/server/client.go +++ b/tests/server/client.go @@ -17,9 +17,9 @@ func InitChain(client abcicli.Client) error { for i := 0; i < total; i++ { pubkey := crypto.GenPrivKeyEd25519FromSecret([]byte(cmn.Fmt("test%d", i))).PubKey().Bytes() power := cmn.RandInt() - vals[i] = &types.Validator{pubkey, uint64(power)} + vals[i] = &types.Validator{pubkey, int64(power)} } - err := client.InitChainSync(vals) + _, err := client.InitChainSync(types.RequestInitChain{Validators: vals}) if err != nil { fmt.Println("Failed test: InitChain - %v", err) return err @@ -29,38 +29,38 @@ func InitChain(client abcicli.Client) error { } func SetOption(client abcicli.Client, key, value string) error { - res := client.SetOptionSync(key, value) - _, _, log := res.Code, res.Data, res.Log - if res.IsErr() { + res, err := client.SetOptionSync(types.RequestSetOption{Key: key, Value: value}) + log := res.GetLog() + if err != nil { fmt.Println("Failed test: SetOption") fmt.Printf("setting %v=%v: \nlog: %v", key, value, log) fmt.Println("Failed test: SetOption") - return errors.New(res.Error()) + return err } fmt.Println("Passed test: SetOption") return nil } func Commit(client abcicli.Client, hashExp []byte) error { - res := client.CommitSync() - _, data, log := res.Code, res.Data, res.Log - if res.IsErr() { + res, err := client.CommitSync() + _, data := res.Code, res.Data + if err != nil { fmt.Println("Failed test: Commit") - fmt.Printf("committing %v\nlog: %v", log) - return errors.New(res.Error()) + fmt.Printf("committing %v\nlog: %v", res.GetLog()) + return err } - if !bytes.Equal(res.Data, hashExp) { + if !bytes.Equal(data, hashExp) { fmt.Println("Failed test: Commit") fmt.Printf("Commit hash was unexpected. Got %X expected %X", - data, hashExp) + data.Bytes(), hashExp) return errors.New("CommitTx failed") } fmt.Println("Passed test: Commit") return nil } -func DeliverTx(client abcicli.Client, txBytes []byte, codeExp types.CodeType, dataExp []byte) error { - res := client.DeliverTxSync(txBytes) +func DeliverTx(client abcicli.Client, txBytes []byte, codeExp uint32, dataExp []byte) error { + res, _ := client.DeliverTxSync(txBytes) code, data, log := res.Code, res.Data, res.Log if code != codeExp { fmt.Println("Failed test: DeliverTx") @@ -78,14 +78,9 @@ func DeliverTx(client abcicli.Client, txBytes []byte, codeExp types.CodeType, da return nil } -func CheckTx(client abcicli.Client, txBytes []byte, codeExp types.CodeType, dataExp []byte) error { - res := client.CheckTxSync(txBytes) +func CheckTx(client abcicli.Client, txBytes []byte, codeExp uint32, dataExp []byte) error { + res, _ := client.CheckTxSync(txBytes) code, data, log := res.Code, res.Data, res.Log - if res.IsErr() { - fmt.Println("Failed test: CheckTx") - fmt.Printf("checking tx %X: %v\nlog: %v", txBytes, log) - return errors.New(res.Error()) - } if code != codeExp { fmt.Println("Failed test: CheckTx") fmt.Printf("CheckTx response code was unexpected. Got %v expected %v. Log: %v",