Skip to content

Commit 9bf3ff7

Browse files
authored
Merge PR #6489: Test Network Testing Framework
1 parent 3df4dd0 commit 9bf3ff7

File tree

19 files changed

+883
-115
lines changed

19 files changed

+883
-115
lines changed

.github/workflows/test.yml

+4-28
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ jobs:
6060
if: "env.GIT_DIFF != ''"
6161
- name: test & coverage report creation
6262
run: |
63-
cat xaa.txt | xargs go test -mod=readonly -timeout 8m -race -coverprofile=coverage.txt -covermode=atomic -tags='ledger test_ledger_mock'
63+
cat xaa.txt | xargs go test -mod=readonly -timeout 8m -coverprofile=coverage.txt -covermode=atomic -tags='ledger test_ledger_mock'
6464
if: "env.GIT_DIFF != ''"
6565
- name: filter out DONTCOVER
6666
run: |
@@ -98,7 +98,7 @@ jobs:
9898
if: "env.GIT_DIFF != ''"
9999
- name: test & coverage report creation
100100
run: |
101-
cat xab.txt | xargs go test -mod=readonly -timeout 6m -race -coverprofile=coverage.txt -covermode=atomic -tags='ledger test_ledger_mock'
101+
cat xab.txt | xargs go test -mod=readonly -timeout 6m -coverprofile=coverage.txt -covermode=atomic -tags='ledger test_ledger_mock'
102102
if: "env.GIT_DIFF != ''"
103103
- name: filter out DONTCOVER
104104
run: |
@@ -136,7 +136,7 @@ jobs:
136136
if: "env.GIT_DIFF != ''"
137137
- name: test & coverage report creation
138138
run: |
139-
cat xac.txt | xargs go test -mod=readonly -timeout 6m -race -coverprofile=coverage.txt -covermode=atomic -tags='ledger test_ledger_mock'
139+
cat xac.txt | xargs go test -mod=readonly -timeout 6m -coverprofile=coverage.txt -covermode=atomic -tags='ledger test_ledger_mock'
140140
if: "env.GIT_DIFF != ''"
141141
- name: filter out DONTCOVER
142142
run: |
@@ -174,7 +174,7 @@ jobs:
174174
if: "env.GIT_DIFF != ''"
175175
- name: test & coverage report creation
176176
run: |
177-
cat xad.txt | xargs go test -mod=readonly -timeout 6m -race -coverprofile=coverage.txt -covermode=atomic -tags='ledger test_ledger_mock'
177+
cat xad.txt | xargs go test -mod=readonly -timeout 6m -coverprofile=coverage.txt -covermode=atomic -tags='ledger test_ledger_mock'
178178
if: "env.GIT_DIFF != ''"
179179
- name: filter out DONTCOVER
180180
run: |
@@ -213,27 +213,3 @@ jobs:
213213
run: |
214214
make test-integration
215215
if: "env.GIT_DIFF != ''"
216-
217-
liveness-test:
218-
runs-on: ubuntu-latest
219-
timeout-minutes: 10
220-
steps:
221-
- uses: actions/checkout@v2
222-
- uses: technote-space/get-diff-action@v1
223-
id: git_diff
224-
with:
225-
SUFFIX_FILTER: |
226-
.go
227-
.mod
228-
.sum
229-
- name: build image
230-
run: |
231-
make build-docker-local-simapp
232-
- name: start localnet
233-
run: |
234-
make clean build-sim-linux localnet-start
235-
if: "env.GIT_DIFF != ''"
236-
- name: test liveness
237-
run: |
238-
./contrib/localnet_liveness.sh 100 5 50 localhost
239-
if: "env.GIT_DIFF != ''"

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ be used to retrieve the actual proposal `Content`. Also the `NewMsgSubmitProposa
146146

147147
### Features
148148

149+
* (tests) [\#6489](https://github.com/cosmos/cosmos-sdk/pull/6489) Introduce package `testutil`, new in-process testing network framework for use in integration and unit tests.
149150
* (crypto/multisig) [\#6241](https://github.com/cosmos/cosmos-sdk/pull/6241) Add Multisig type directly to the repo. Previously this was in tendermint.
150151
* (rest) [\#6167](https://github.com/cosmos/cosmos-sdk/pull/6167) Support `max-body-bytes` CLI flag for the REST service.
151152
* (x/ibc) [\#5588](https://github.com/cosmos/cosmos-sdk/pull/5588) Add [ICS 024 - Host State Machine Requirements](https://github.com/cosmos/ics/tree/master/spec/ics-024-host-requirements) subpackage to `x/ibc` module.

contrib/localnet_liveness.sh

-55
This file was deleted.

go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ require (
3939
github.com/tendermint/iavl v0.14.0-rc1
4040
github.com/tendermint/tendermint v0.33.5
4141
github.com/tendermint/tm-db v0.5.1
42+
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e
4243
google.golang.org/grpc v1.30.0
4344
gopkg.in/yaml.v2 v2.3.0
4445
)

go.sum

+1
Original file line numberDiff line numberDiff line change
@@ -605,6 +605,7 @@ golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJ
605605
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
606606
golang.org/x/sync v0.0.0-20190227155943-e225da77a7e6/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
607607
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
608+
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e h1:vcxGaoTs7kV8m5Np9uUNQin4BrLOthgV7252N8V+FwY=
608609
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
609610
golang.org/x/sys v0.0.0-20180823144017-11551d06cbcc/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
610611
golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=

server/api/server.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"fmt"
55
"net"
66
"net/http"
7-
"os"
87
"strings"
98
"time"
109

@@ -33,11 +32,11 @@ type Server struct {
3332
listener net.Listener
3433
}
3534

36-
func New(clientCtx client.Context) *Server {
35+
func New(clientCtx client.Context, logger log.Logger) *Server {
3736
return &Server{
3837
Router: mux.NewRouter(),
3938
ClientCtx: clientCtx,
40-
logger: log.NewTMLogger(log.NewSyncWriter(os.Stdout)).With("module", "api-server"),
39+
logger: logger,
4140
}
4241
}
4342

server/start.go

+16-4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"fmt"
77
"os"
88
"runtime/pprof"
9+
"time"
910

1011
"github.com/spf13/cobra"
1112
"github.com/spf13/viper"
@@ -199,8 +200,9 @@ func startInProcess(ctx *Context, cdc codec.JSONMarshaler, appCreator AppCreator
199200
return err
200201
}
201202

202-
config := config.GetConfig()
203203
var apiSrv *api.Server
204+
205+
config := config.GetConfig()
204206
if config.API.Enable {
205207
genDoc, err := genDocProvider()
206208
if err != nil {
@@ -210,18 +212,28 @@ func startInProcess(ctx *Context, cdc codec.JSONMarshaler, appCreator AppCreator
210212
// TODO: Since this is running in process, do we need to provide a verifier
211213
// and set TrustNode=false? If so, we need to add additional logic that
212214
// waits for a block to be committed first before starting the API server.
213-
ctx := client.Context{}.
215+
clientCtx := client.Context{}.
214216
WithHomeDir(home).
215217
WithChainID(genDoc.ChainID).
216218
WithJSONMarshaler(cdc).
217219
WithClient(local.New(tmNode)).
218220
WithTrustNode(true)
219221

220-
apiSrv = api.New(ctx)
222+
apiSrv = api.New(clientCtx, ctx.Logger.With("module", "api-server"))
221223
app.RegisterAPIRoutes(apiSrv)
222224

223-
if err := apiSrv.Start(config); err != nil {
225+
errCh := make(chan error)
226+
227+
go func() {
228+
if err := apiSrv.Start(config); err != nil {
229+
errCh <- err
230+
}
231+
}()
232+
233+
select {
234+
case err := <-errCh:
224235
return err
236+
case <-time.After(5 * time.Second): // assume server started successfully
225237
}
226238
}
227239

testutil/doc.go

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
Package testutil implements and exposes a fully operational in-process Tendermint
3+
test network that consists of at least one or potentially many validators. This
4+
test network can be used primarily for integration tests or unit test suites.
5+
6+
The testnetwork utilizes SimApp as the ABCI application and uses all the modules
7+
defined in the Cosmos SDK. An in-process test network can be configured with any
8+
number of validators as well as account funds and even custom genesis state.
9+
10+
When creating a test network, a series of Validator objects are returned. Each
11+
Validator object has useful information such as their address and pubkey. A
12+
Validator will also provide its RPC, P2P, and API addresses that can be useful
13+
for integration testing. In addition, a Tendermint local RPC client is also provided
14+
which can be handy for making direct RPC calls to Tendermint.
15+
16+
Note, due to limitations in concurrency and the design of the RPC layer in
17+
Tendermint, only the first Validator object will have an RPC and API client
18+
exposed. Due to this exact same limitation, only a single test network can exist
19+
at a time. A caller must be certain it calls Cleanup after it no longer needs
20+
the network.
21+
22+
A typical testing flow might look like the following:
23+
24+
type IntegrationTestSuite struct {
25+
suite.Suite
26+
27+
cfg testutil.Config
28+
network *testutil.Network
29+
}
30+
31+
func (s *IntegrationTestSuite) SetupSuite() {
32+
s.T().Log("setting up integration test suite")
33+
34+
cfg := testutil.DefaultConfig()
35+
cfg.NumValidators = 1
36+
37+
s.cfg = cfg
38+
s.network = testutil.NewTestNetwork(s.T(), cfg)
39+
40+
_, err := s.network.WaitForHeight(1)
41+
s.Require().NoError(err)
42+
}
43+
44+
func (s *IntegrationTestSuite) TearDownSuite() {
45+
s.T().Log("tearing down integration test suite")
46+
47+
// This is important and must be called to ensure other tests can create
48+
// a network!
49+
s.network.Cleanup()
50+
}
51+
52+
func (s *IntegrationTestSuite) TestQueryBalancesRequestHandlerFn() {
53+
val := s.network.Validators[0]
54+
baseURL := val.APIAddress
55+
56+
// Use baseURL to make API HTTP requests or use val.RPCClient to make direct
57+
// Tendermint RPC calls.
58+
// ...
59+
}
60+
61+
func TestIntegrationTestSuite(t *testing.T) {
62+
suite.Run(t, new(IntegrationTestSuite))
63+
}
64+
*/
65+
package testutil

0 commit comments

Comments
 (0)