From 3b329039d8b0dfaf5bfaf24724e2bd9d68d3c581 Mon Sep 17 00:00:00 2001 From: Ethan Buchman Date: Sat, 23 Jul 2016 18:54:58 -0400 Subject: [PATCH] better testing. cli test for tutorial --- Makefile | 2 +- README.md | 3 +- tests/test.sh | 8 +++ .../test_counter.go => test_app/app.go} | 62 ++++--------------- tests/test_app/main.go | 48 ++++++++++++++ tests/test_app/test.sh | 17 +++++ tests/test_cli/ex1.tmsp | 10 +++ tests/test_cli/ex1.tmsp.out | 31 ++++++++++ tests/test_cli/ex2.tmsp | 8 +++ tests/test_cli/ex2.tmsp.out | 26 ++++++++ tests/test_cli/test.sh | 29 +++++++++ tests/test_counter/test.sh | 14 ----- 12 files changed, 191 insertions(+), 67 deletions(-) create mode 100644 tests/test.sh rename tests/{test_counter/test_counter.go => test_app/app.go} (51%) create mode 100644 tests/test_app/main.go create mode 100755 tests/test_app/test.sh create mode 100644 tests/test_cli/ex1.tmsp create mode 100644 tests/test_cli/ex1.tmsp.out create mode 100644 tests/test_cli/ex2.tmsp create mode 100644 tests/test_cli/ex2.tmsp.out create mode 100644 tests/test_cli/test.sh delete mode 100755 tests/test_counter/test.sh diff --git a/Makefile b/Makefile index 0f8adc5b..b47dc0e2 100644 --- a/Makefile +++ b/Makefile @@ -10,7 +10,7 @@ install: get_deps test: go test github.com/tendermint/tmsp/... - bash tests/test_counter/test.sh + bash tests/test.sh get_deps: go get -d github.com/tendermint/tmsp/... diff --git a/README.md b/README.md index 11559c9a..ba751383 100644 --- a/README.md +++ b/README.md @@ -18,7 +18,7 @@ This repository holds a number of important pieces: - `types/types.proto` - the protobuf file definig TMSP message types, and the optional grpc interface. - - use `protoc --go_out=plugins=grpc:./types types/types.proto` to generate the `types/types.pb.go` file + - use `protoc --go_out=plugins=grpc:. types.proto` to from the `types` dir to generate the `types/types.pb.go` file - see `protoc --help` and [the grpc docs](www.grpc.io/docs) for examples and details of other languages - golang implementation of TMSP client and server @@ -29,6 +29,7 @@ This repository holds a number of important pieces: - `cmd/tmsp-cli` - command line tool wrapping the client for probing/testing a TMSP application + - use `tmsp-cli --version` to get the TMSP version - examples: - the `cmd/counter` application, which illustrates nonce checking in txs diff --git a/tests/test.sh b/tests/test.sh new file mode 100644 index 00000000..4087cdce --- /dev/null +++ b/tests/test.sh @@ -0,0 +1,8 @@ +#! /bin/bash + +# test the counter using a go test script +bash tests/test_app/test.sh + +# test the cli against the examples in the tutorial at tendermint.com +bash tests/test_cli/test.sh + diff --git a/tests/test_counter/test_counter.go b/tests/test_app/app.go similarity index 51% rename from tests/test_counter/test_counter.go rename to tests/test_app/app.go index 2f78b696..2a1bf15a 100644 --- a/tests/test_counter/test_counter.go +++ b/tests/test_app/app.go @@ -2,8 +2,6 @@ package main import ( "bytes" - "flag" - "fmt" "os" "time" @@ -13,57 +11,19 @@ import ( "github.com/tendermint/tmsp/types" ) -var tmspPtr = flag.String("tmsp", "socket", "socket or grpc") - -func main() { - flag.Parse() - - // Run tests - testBasic() - - fmt.Println("Success!") -} - -func testBasic() { - fmt.Println("Running basic tests") - appProc := startApp() - defer appProc.StopProcess(true) - client := startClient() - defer client.Stop() - - setOption(client, "serial", "on") - commit(client, nil) - appendTx(client, []byte("abc"), types.CodeType_BadNonce, nil) - commit(client, nil) - appendTx(client, []byte{0x00}, types.CodeType_OK, nil) - commit(client, []byte{0, 0, 0, 0, 0, 0, 0, 1}) - appendTx(client, []byte{0x00}, types.CodeType_BadNonce, nil) - appendTx(client, []byte{0x01}, types.CodeType_OK, nil) - appendTx(client, []byte{0x00, 0x02}, types.CodeType_OK, nil) - appendTx(client, []byte{0x00, 0x03}, types.CodeType_OK, nil) - appendTx(client, []byte{0x00, 0x00, 0x04}, types.CodeType_OK, nil) - appendTx(client, []byte{0x00, 0x00, 0x06}, types.CodeType_BadNonce, nil) - commit(client, []byte{0, 0, 0, 0, 0, 0, 0, 5}) -} - //---------------------------------------- -func startApp() *process.Process { - counterApp := os.Getenv("COUNTER_APP") - if counterApp == "" { - panic("No COUNTER_APP specified") - } - +func StartApp(tmspApp string) *process.Process { // Start the app //outBuf := NewBufferCloser(nil) - proc, err := process.StartProcess("counter_app", + proc, err := process.StartProcess("tmsp_app", "bash", - []string{"-c", counterApp}, + []string{"-c", tmspApp}, nil, os.Stdout, ) if err != nil { - panic("running counter_app: " + err.Error()) + panic("running tmsp_app: " + err.Error()) } // TODO a better way to handle this? @@ -72,16 +32,16 @@ func startApp() *process.Process { return proc } -func startClient() tmspcli.Client { +func StartClient(tmspType string) tmspcli.Client { // Start client - client, err := tmspcli.NewClient("tcp://127.0.0.1:46658", *tmspPtr, true) + client, err := tmspcli.NewClient("tcp://127.0.0.1:46658", tmspType, true) if err != nil { - panic("connecting to counter_app: " + err.Error()) + panic("connecting to tmsp_app: " + err.Error()) } return client } -func setOption(client tmspcli.Client, key, value string) { +func SetOption(client tmspcli.Client, key, value string) { res := client.SetOptionSync(key, value) _, _, log := res.Code, res.Data, res.Log if res.IsErr() { @@ -89,7 +49,7 @@ func setOption(client tmspcli.Client, key, value string) { } } -func commit(client tmspcli.Client, hashExp []byte) { +func Commit(client tmspcli.Client, hashExp []byte) { res := client.CommitSync() _, data, log := res.Code, res.Data, res.Log if res.IsErr() { @@ -101,7 +61,7 @@ func commit(client tmspcli.Client, hashExp []byte) { } } -func appendTx(client tmspcli.Client, txBytes []byte, codeExp types.CodeType, dataExp []byte) { +func AppendTx(client tmspcli.Client, txBytes []byte, codeExp types.CodeType, dataExp []byte) { res := client.AppendTxSync(txBytes) code, data, log := res.Code, res.Data, res.Log if code != codeExp { @@ -114,7 +74,7 @@ func appendTx(client tmspcli.Client, txBytes []byte, codeExp types.CodeType, dat } } -func checkTx(client tmspcli.Client, txBytes []byte, codeExp types.CodeType, dataExp []byte) { +func CheckTx(client tmspcli.Client, txBytes []byte, codeExp types.CodeType, dataExp []byte) { res := client.CheckTxSync(txBytes) code, data, log := res.Code, res.Data, res.Log if res.IsErr() { diff --git a/tests/test_app/main.go b/tests/test_app/main.go new file mode 100644 index 00000000..ada63718 --- /dev/null +++ b/tests/test_app/main.go @@ -0,0 +1,48 @@ +package main + +import ( + "fmt" + "os" + + "github.com/tendermint/tmsp/types" +) + +var tmspType string + +func init() { + tmspType = os.Getenv("TMSP") + if tmspType == "" { + tmspType = "socket" + } +} + +func main() { + testCounter() +} + +func testCounter() { + tmspApp := os.Getenv("TMSP_APP") + if tmspApp == "" { + panic("No TMSP_APP specified") + } + + fmt.Printf("Running %s test with tmsp=%s\n", tmspApp, tmspType) + appProc := StartApp(tmspApp) + defer appProc.StopProcess(true) + client := StartClient(tmspType) + defer client.Stop() + + SetOption(client, "serial", "on") + Commit(client, nil) + AppendTx(client, []byte("abc"), types.CodeType_BadNonce, nil) + Commit(client, nil) + AppendTx(client, []byte{0x00}, types.CodeType_OK, nil) + Commit(client, []byte{0, 0, 0, 0, 0, 0, 0, 1}) + AppendTx(client, []byte{0x00}, types.CodeType_BadNonce, nil) + AppendTx(client, []byte{0x01}, types.CodeType_OK, nil) + AppendTx(client, []byte{0x00, 0x02}, types.CodeType_OK, nil) + AppendTx(client, []byte{0x00, 0x03}, types.CodeType_OK, nil) + AppendTx(client, []byte{0x00, 0x00, 0x04}, types.CodeType_OK, nil) + AppendTx(client, []byte{0x00, 0x00, 0x06}, types.CodeType_BadNonce, nil) + Commit(client, []byte{0, 0, 0, 0, 0, 0, 0, 5}) +} diff --git a/tests/test_app/test.sh b/tests/test_app/test.sh new file mode 100755 index 00000000..4c28a831 --- /dev/null +++ b/tests/test_app/test.sh @@ -0,0 +1,17 @@ +#! /bin/bash +set -e + +# These tests spawn the counter app and server by execing the TMSP_APP command and run some simple client tests against it + +ROOT=$GOPATH/src/github.com/tendermint/tmsp/tests/test_app +cd $ROOT + +# test golang counter +TMSP_APP="counter" go run *.go + +# test golang counter via grpc +TMSP_APP="counter -tmsp=grpc" TMSP="grpc" go run *.go + +# test nodejs counter +# TODO: fix node app +#TMSP_APP="node $GOPATH/src/github.com/tendermint/js-tmsp/example/app.js" go test -test.run TestCounter diff --git a/tests/test_cli/ex1.tmsp b/tests/test_cli/ex1.tmsp new file mode 100644 index 00000000..de61c8b2 --- /dev/null +++ b/tests/test_cli/ex1.tmsp @@ -0,0 +1,10 @@ +echo hello +info +commit +append_tx abc +info +commit +query abc +append_tx def=xyz +commit +query def diff --git a/tests/test_cli/ex1.tmsp.out b/tests/test_cli/ex1.tmsp.out new file mode 100644 index 00000000..89bd61c2 --- /dev/null +++ b/tests/test_cli/ex1.tmsp.out @@ -0,0 +1,31 @@ +> echo hello +-> data: {hello} + +> info +-> data: {size:0} + +> commit + +> append_tx abc +-> code: OK + +> info +-> data: {size:1} + +> commit +-> data: {750502FC7E84BBD788ED589624F06CFA871845D1} + +> query abc +-> code: OK +-> data: {Index=0 value=abc exists=true} + +> append_tx def=xyz +-> code: OK + +> commit +-> data: {76393B8A182E450286B0694C629ECB51B286EFD5} + +> query def +-> code: OK +-> data: {Index=1 value=xyz exists=true} + diff --git a/tests/test_cli/ex2.tmsp b/tests/test_cli/ex2.tmsp new file mode 100644 index 00000000..e9550db8 --- /dev/null +++ b/tests/test_cli/ex2.tmsp @@ -0,0 +1,8 @@ +set_option serial on +check_tx 0x00 +check_tx 0xff +append_tx 0x00 +check_tx 0x00 +append_tx 0x01 +append_tx 0x04 +info diff --git a/tests/test_cli/ex2.tmsp.out b/tests/test_cli/ex2.tmsp.out new file mode 100644 index 00000000..3aa7744b --- /dev/null +++ b/tests/test_cli/ex2.tmsp.out @@ -0,0 +1,26 @@ +> set_option serial on +-> data: {serial=on} + +> check_tx 0x00 +-> code: OK + +> check_tx 0xff +-> code: OK + +> append_tx 0x00 +-> code: OK + +> check_tx 0x00 +-> code: BadNonce +-> log: Invalid nonce. Expected >= 1, got 0 + +> append_tx 0x01 +-> code: OK + +> append_tx 0x04 +-> code: BadNonce +-> log: Invalid nonce. Expected 2, got 4 + +> info +-> data: {hashes:0, txs:2} + diff --git a/tests/test_cli/test.sh b/tests/test_cli/test.sh new file mode 100644 index 00000000..f8920958 --- /dev/null +++ b/tests/test_cli/test.sh @@ -0,0 +1,29 @@ +#! /bin/bash + +function testExample() { + N=$1 + INPUT=$2 + APP=$3 + + echo "Example $N" + $APP &> /dev/null & + sleep 2 + tmsp-cli --verbose batch < $INPUT > "${INPUT}.out.new" + killall "$APP" > /dev/null + + pre=`shasum < "${INPUT}.out"` + post=`shasum < "${INPUT}.out.new"` + + if [[ "$pre" != "$post" ]]; then + echo "You broke the tutorial" + exit 1 + fi + + rm "${INPUT}".out.new +} + +testExample 1 tests/test_cli/ex1.tmsp dummy +testExample 2 tests/test_cli/ex2.tmsp counter + +echo "" +echo "PASS" diff --git a/tests/test_counter/test.sh b/tests/test_counter/test.sh deleted file mode 100755 index 4883a5e0..00000000 --- a/tests/test_counter/test.sh +++ /dev/null @@ -1,14 +0,0 @@ -# These tests spawn the counter app and server by execing the COUNTER_APP command and run some simple client tests against it - -ROOT=$GOPATH/src/github.com/tendermint/tmsp/tests/test_counter -cd $ROOT - -# test golang counter -COUNTER_APP="counter" go run test_counter.go - -# test golang counter via grpc -COUNTER_APP="counter -tmsp=grpc" go run test_counter.go -tmsp=grpc - -# test nodejs counter -# TODO: fix node app -#COUNTER_APP="node $GOPATH/src/github.com/tendermint/js-tmsp/example/app.js" go run test_counter.go