Skip to content

Commit 56cb095

Browse files
committed
Fix tests
1 parent cc25229 commit 56cb095

File tree

3 files changed

+32
-22
lines changed

3 files changed

+32
-22
lines changed

tests/systemtests/cometbft_client_test.go

+25-20
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package systemtests
55
import (
66
"context"
77
"fmt"
8+
"net/http"
89
"net/url"
910
"testing"
1011
"time"
@@ -82,6 +83,10 @@ func TestQueryBlockByHeight(t *testing.T) {
8283
}
8384

8485
func TestQueryLatestValidatorSet(t *testing.T) {
86+
if sut.NodesCount() < 2 {
87+
t.Skip("not enough nodes")
88+
return
89+
}
8590
baseurl := fmt.Sprintf("http://localhost:%d", apiPortStart)
8691
sut.ResetChain(t)
8792
sut.StartChain(t)
@@ -103,7 +108,7 @@ func TestQueryLatestValidatorSet(t *testing.T) {
103108
assert.NoError(t, err)
104109
assert.Equal(t, len(res.Validators), 2)
105110

106-
restRes := GetRequest(t, mustV(url.JoinPath(baseurl, "/cosmos/base/tendermint/v1beta1/validatorsets/latest?pagination.offset=0&pagination.limit=2")))
111+
restRes := GetRequest(t, fmt.Sprintf("%s/cosmos/base/tendermint/v1beta1/validatorsets/latest?pagination.offset=%d&pagination.limit=%d", baseurl, 0, 2))
107112
assert.Equal(t, len(gjson.GetBytes(restRes, "validators").Array()), 2)
108113
}
109114

@@ -161,13 +166,14 @@ func TestLatestValidatorSet_GRPCGateway(t *testing.T) {
161166
}
162167
for _, tc := range testCases {
163168
t.Run(tc.name, func(t *testing.T) {
164-
rsp := GetRequest(t, mustV(url.JoinPath(baseurl, tc.url)))
165169
if tc.expErr {
170+
rsp := GetRequestWithHeaders(t, baseurl+tc.url, nil, http.StatusBadRequest)
166171
errMsg := gjson.GetBytes(rsp, "message").String()
167172
assert.Contains(t, errMsg, tc.expErrMsg)
168-
} else {
169-
assert.Equal(t, len(vals), int(gjson.GetBytes(rsp, "pagination.total").Int()))
173+
return
170174
}
175+
rsp := GetRequest(t, baseurl+tc.url)
176+
assert.Equal(t, len(vals), int(gjson.GetBytes(rsp, "pagination.total").Int()))
171177
})
172178
}
173179
}
@@ -204,41 +210,40 @@ func TestValidatorSetByHeight(t *testing.T) {
204210
}
205211
}
206212

207-
func TestValidatorSetByHeight_GRPCGateway(t *testing.T) {
213+
func TestValidatorSetByHeight_GRPCRestGateway(t *testing.T) {
208214
sut.ResetChain(t)
209215
sut.StartChain(t)
210216

211217
vals := sut.RPCClient(t).Validators()
212218

213-
baseurl := fmt.Sprintf("http://localhost:%d", apiPortStart)
214-
219+
baseurl := sut.APIAddress()
215220
block := sut.AwaitNextBlock(t, time.Second*3)
216221
testCases := []struct {
217-
name string
218-
url string
219-
expErr bool
220-
expErrMsg string
222+
name string
223+
url string
224+
expErr bool
225+
expErrMsg string
226+
expHttpCode int
221227
}{
222-
{"invalid height", fmt.Sprintf("%s/cosmos/base/tendermint/v1beta1/validatorsets/%d", baseurl, -1), true, "height must be greater than 0"},
223-
{"no pagination", fmt.Sprintf("%s/cosmos/base/tendermint/v1beta1/validatorsets/%d", baseurl, block), false, ""},
224-
{"pagination invalid fields", fmt.Sprintf("%s/cosmos/base/tendermint/v1beta1/validatorsets/%d?pagination.offset=-1&pagination.limit=-2", baseurl, block), true, "strconv.ParseUint"},
225-
{"with pagination", fmt.Sprintf("%s/cosmos/base/tendermint/v1beta1/validatorsets/%d?pagination.limit=2", baseurl, 1), false, ""},
228+
{"invalid height", fmt.Sprintf("%s/cosmos/base/tendermint/v1beta1/validatorsets/%d", baseurl, -1), true, "height must be greater than 0", http.StatusInternalServerError},
229+
{"no pagination", fmt.Sprintf("%s/cosmos/base/tendermint/v1beta1/validatorsets/%d", baseurl, block), false, "", http.StatusOK},
230+
{"pagination invalid fields", fmt.Sprintf("%s/cosmos/base/tendermint/v1beta1/validatorsets/%d?pagination.offset=-1&pagination.limit=-2", baseurl, block), true, "strconv.ParseUint", http.StatusBadRequest},
231+
{"with pagination", fmt.Sprintf("%s/cosmos/base/tendermint/v1beta1/validatorsets/%d?pagination.limit=2", baseurl, 1), false, "", http.StatusOK},
226232
}
227233
for _, tc := range testCases {
228234
t.Run(tc.name, func(t *testing.T) {
229-
rsp := GetRequest(t, tc.url)
235+
rsp := GetRequestWithHeaders(t, tc.url, nil, tc.expHttpCode)
230236
if tc.expErr {
231237
errMsg := gjson.GetBytes(rsp, "message").String()
232238
assert.Contains(t, errMsg, tc.expErrMsg)
233-
} else {
234-
assert.Equal(t, len(vals), int(gjson.GetBytes(rsp, "pagination.total").Int()))
239+
return
235240
}
241+
assert.Equal(t, len(vals), int(gjson.GetBytes(rsp, "pagination.total").Int()))
236242
})
237243
}
238244
}
239245

240246
func TestABCIQuery(t *testing.T) {
241-
sut.ResetChain(t)
242247
sut.StartChain(t)
243248

244249
qc := cmtservice.NewServiceClient(sut.RPCClient(t))
@@ -312,7 +317,7 @@ func TestABCIQuery(t *testing.T) {
312317
} else {
313318
assert.NoError(t, err)
314319
assert.NotNil(t, res)
315-
assert.Equal(t, res.Code, tc.expectedCode)
320+
assert.Equal(t, tc.expectedCode, res.Code)
316321
}
317322

318323
if tc.validQuery {

tests/systemtests/rest_cli.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,9 @@ func GetRequestWithHeaders(t *testing.T, url string, headers map[string]string,
7979
defer func() {
8080
_ = res.Body.Close()
8181
}()
82-
require.Equal(t, expCode, res.StatusCode, "status code should be %d, got: %d", expCode, res.StatusCode)
83-
8482
body, err := io.ReadAll(res.Body)
8583
require.NoError(t, err)
84+
require.Equal(t, expCode, res.StatusCode, "status code should be %d, got: %d, %s", expCode, res.StatusCode, body)
85+
8686
return body
8787
}

tests/systemtests/system.go

+5
Original file line numberDiff line numberDiff line change
@@ -766,6 +766,11 @@ func (s *SystemUnderTest) CurrentHeight() int64 {
766766
return s.currentHeight.Load()
767767
}
768768

769+
// NodesCount returns the number of node instances used
770+
func (s *SystemUnderTest) NodesCount() int {
771+
return s.nodesCount
772+
}
773+
769774
type Node struct {
770775
ID string
771776
IP string

0 commit comments

Comments
 (0)