You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

179 lines
5.0 KiB

8 years ago
7 years ago
8 years ago
  1. package mock_test
  2. import (
  3. "fmt"
  4. "testing"
  5. "github.com/pkg/errors"
  6. "github.com/stretchr/testify/assert"
  7. "github.com/stretchr/testify/require"
  8. "github.com/tendermint/abci/example/dummy"
  9. abci "github.com/tendermint/abci/types"
  10. data "github.com/tendermint/go-wire/data"
  11. "github.com/tendermint/tendermint/rpc/client"
  12. "github.com/tendermint/tendermint/rpc/client/mock"
  13. ctypes "github.com/tendermint/tendermint/rpc/core/types"
  14. "github.com/tendermint/tendermint/types"
  15. )
  16. func TestABCIMock(t *testing.T) {
  17. assert, require := assert.New(t), require.New(t)
  18. key, value := []byte("foo"), []byte("bar")
  19. height := uint64(10)
  20. goodTx := types.Tx{0x01, 0xff}
  21. badTx := types.Tx{0x12, 0x21}
  22. m := mock.ABCIMock{
  23. Info: mock.Call{Error: errors.New("foobar")},
  24. Query: mock.Call{Response: abci.ResponseQuery{
  25. Key: key,
  26. Value: value,
  27. Height: height,
  28. }},
  29. // Broadcast commit depends on call
  30. BroadcastCommit: mock.Call{
  31. Args: goodTx,
  32. Response: &ctypes.ResultBroadcastTxCommit{
  33. CheckTx: abci.ResponseCheckTx{Data: data.Bytes("stand")},
  34. DeliverTx: abci.ResponseDeliverTx{Data: data.Bytes("deliver")},
  35. },
  36. Error: errors.New("bad tx"),
  37. },
  38. Broadcast: mock.Call{Error: errors.New("must commit")},
  39. }
  40. // now, let's try to make some calls
  41. _, err := m.ABCIInfo()
  42. require.NotNil(err)
  43. assert.Equal("foobar", err.Error())
  44. // query always returns the response
  45. query, err := m.ABCIQueryWithOptions("/", nil, client.ABCIQueryOptions{Trusted: true})
  46. require.Nil(err)
  47. require.NotNil(query)
  48. assert.EqualValues(key, query.Key)
  49. assert.EqualValues(value, query.Value)
  50. assert.Equal(height, query.Height)
  51. // non-commit calls always return errors
  52. _, err = m.BroadcastTxSync(goodTx)
  53. require.NotNil(err)
  54. assert.Equal("must commit", err.Error())
  55. _, err = m.BroadcastTxAsync(goodTx)
  56. require.NotNil(err)
  57. assert.Equal("must commit", err.Error())
  58. // commit depends on the input
  59. _, err = m.BroadcastTxCommit(badTx)
  60. require.NotNil(err)
  61. assert.Equal("bad tx", err.Error())
  62. bres, err := m.BroadcastTxCommit(goodTx)
  63. require.Nil(err, "%+v", err)
  64. assert.EqualValues(0, bres.CheckTx.Code)
  65. assert.EqualValues("stand", bres.CheckTx.Data)
  66. assert.EqualValues("deliver", bres.DeliverTx.Data)
  67. }
  68. func TestABCIRecorder(t *testing.T) {
  69. assert, require := assert.New(t), require.New(t)
  70. // This mock returns errors on everything but Query
  71. m := mock.ABCIMock{
  72. Info: mock.Call{Response: abci.ResponseInfo{
  73. Data: "data",
  74. Version: "v0.9.9",
  75. }},
  76. Query: mock.Call{Error: errors.New("query")},
  77. Broadcast: mock.Call{Error: errors.New("broadcast")},
  78. BroadcastCommit: mock.Call{Error: errors.New("broadcast_commit")},
  79. }
  80. r := mock.NewABCIRecorder(m)
  81. require.Equal(0, len(r.Calls))
  82. _, err := r.ABCIInfo()
  83. assert.Nil(err, "expected no err on info")
  84. _, err = r.ABCIQueryWithOptions("path", data.Bytes("data"), client.ABCIQueryOptions{Trusted: false})
  85. assert.NotNil(err, "expected error on query")
  86. require.Equal(2, len(r.Calls))
  87. info := r.Calls[0]
  88. assert.Equal("abci_info", info.Name)
  89. assert.Nil(info.Error)
  90. assert.Nil(info.Args)
  91. require.NotNil(info.Response)
  92. ir, ok := info.Response.(*ctypes.ResultABCIInfo)
  93. require.True(ok)
  94. assert.Equal("data", ir.Response.Data)
  95. assert.Equal("v0.9.9", ir.Response.Version)
  96. query := r.Calls[1]
  97. assert.Equal("abci_query", query.Name)
  98. assert.Nil(query.Response)
  99. require.NotNil(query.Error)
  100. assert.Equal("query", query.Error.Error())
  101. require.NotNil(query.Args)
  102. qa, ok := query.Args.(mock.QueryArgs)
  103. require.True(ok)
  104. assert.Equal("path", qa.Path)
  105. assert.EqualValues("data", qa.Data)
  106. assert.False(qa.Trusted)
  107. // now add some broadcasts (should all err)
  108. txs := []types.Tx{{1}, {2}, {3}}
  109. _, err = r.BroadcastTxCommit(txs[0])
  110. assert.NotNil(err, "expected err on broadcast")
  111. _, err = r.BroadcastTxSync(txs[1])
  112. assert.NotNil(err, "expected err on broadcast")
  113. _, err = r.BroadcastTxAsync(txs[2])
  114. assert.NotNil(err, "expected err on broadcast")
  115. require.Equal(5, len(r.Calls))
  116. bc := r.Calls[2]
  117. assert.Equal("broadcast_tx_commit", bc.Name)
  118. assert.Nil(bc.Response)
  119. require.NotNil(bc.Error)
  120. assert.EqualValues(bc.Args, txs[0])
  121. bs := r.Calls[3]
  122. assert.Equal("broadcast_tx_sync", bs.Name)
  123. assert.Nil(bs.Response)
  124. require.NotNil(bs.Error)
  125. assert.EqualValues(bs.Args, txs[1])
  126. ba := r.Calls[4]
  127. assert.Equal("broadcast_tx_async", ba.Name)
  128. assert.Nil(ba.Response)
  129. require.NotNil(ba.Error)
  130. assert.EqualValues(ba.Args, txs[2])
  131. }
  132. func TestABCIApp(t *testing.T) {
  133. assert, require := assert.New(t), require.New(t)
  134. app := dummy.NewDummyApplication()
  135. m := mock.ABCIApp{app}
  136. // get some info
  137. info, err := m.ABCIInfo()
  138. require.Nil(err)
  139. assert.Equal(`{"size":0}`, info.Response.GetData())
  140. // add a key
  141. key, value := "foo", "bar"
  142. tx := fmt.Sprintf("%s=%s", key, value)
  143. res, err := m.BroadcastTxCommit(types.Tx(tx))
  144. require.Nil(err)
  145. assert.True(res.CheckTx.Code.IsOK())
  146. require.NotNil(res.DeliverTx)
  147. assert.True(res.DeliverTx.Code.IsOK())
  148. // check the key
  149. qres, err := m.ABCIQueryWithOptions("/key", data.Bytes(key), client.ABCIQueryOptions{Trusted: true})
  150. require.Nil(err)
  151. assert.EqualValues(value, qres.Value)
  152. }