diff --git a/tests/systemtests/cometbft_client_test.go b/tests/systemtests/cometbft_client_test.go index 0c9fd62bb5bd2..cee97baa0c61f 100644 --- a/tests/systemtests/cometbft_client_test.go +++ b/tests/systemtests/cometbft_client_test.go @@ -5,6 +5,7 @@ package systemtests import ( "context" "fmt" + "net/http" "net/url" "testing" "time" @@ -82,6 +83,10 @@ func TestQueryBlockByHeight(t *testing.T) { } func TestQueryLatestValidatorSet(t *testing.T) { + if sut.NodesCount() < 2 { + t.Skip("not enough nodes") + return + } baseurl := fmt.Sprintf("http://localhost:%d", apiPortStart) sut.ResetChain(t) sut.StartChain(t) @@ -103,7 +108,7 @@ func TestQueryLatestValidatorSet(t *testing.T) { assert.NoError(t, err) assert.Equal(t, len(res.Validators), 2) - restRes := GetRequest(t, mustV(url.JoinPath(baseurl, "/cosmos/base/tendermint/v1beta1/validatorsets/latest?pagination.offset=0&pagination.limit=2"))) + restRes := GetRequest(t, fmt.Sprintf("%s/cosmos/base/tendermint/v1beta1/validatorsets/latest?pagination.offset=%d&pagination.limit=%d", baseurl, 0, 2)) assert.Equal(t, len(gjson.GetBytes(restRes, "validators").Array()), 2) } @@ -161,13 +166,14 @@ func TestLatestValidatorSet_GRPCGateway(t *testing.T) { } for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) { - rsp := GetRequest(t, mustV(url.JoinPath(baseurl, tc.url))) if tc.expErr { + rsp := GetRequestWithHeaders(t, baseurl+tc.url, nil, http.StatusBadRequest) errMsg := gjson.GetBytes(rsp, "message").String() assert.Contains(t, errMsg, tc.expErrMsg) - } else { - assert.Equal(t, len(vals), int(gjson.GetBytes(rsp, "pagination.total").Int())) + return } + rsp := GetRequest(t, baseurl+tc.url) + assert.Equal(t, len(vals), int(gjson.GetBytes(rsp, "pagination.total").Int())) }) } } @@ -204,41 +210,40 @@ func TestValidatorSetByHeight(t *testing.T) { } } -func TestValidatorSetByHeight_GRPCGateway(t *testing.T) { +func TestValidatorSetByHeight_GRPCRestGateway(t *testing.T) { sut.ResetChain(t) sut.StartChain(t) vals := sut.RPCClient(t).Validators() - baseurl := fmt.Sprintf("http://localhost:%d", apiPortStart) - + baseurl := sut.APIAddress() block := sut.AwaitNextBlock(t, time.Second*3) testCases := []struct { - name string - url string - expErr bool - expErrMsg string + name string + url string + expErr bool + expErrMsg string + expHttpCode int }{ - {"invalid height", fmt.Sprintf("%s/cosmos/base/tendermint/v1beta1/validatorsets/%d", baseurl, -1), true, "height must be greater than 0"}, - {"no pagination", fmt.Sprintf("%s/cosmos/base/tendermint/v1beta1/validatorsets/%d", baseurl, block), false, ""}, - {"pagination invalid fields", fmt.Sprintf("%s/cosmos/base/tendermint/v1beta1/validatorsets/%d?pagination.offset=-1&pagination.limit=-2", baseurl, block), true, "strconv.ParseUint"}, - {"with pagination", fmt.Sprintf("%s/cosmos/base/tendermint/v1beta1/validatorsets/%d?pagination.limit=2", baseurl, 1), false, ""}, + {"invalid height", fmt.Sprintf("%s/cosmos/base/tendermint/v1beta1/validatorsets/%d", baseurl, -1), true, "height must be greater than 0", http.StatusInternalServerError}, + {"no pagination", fmt.Sprintf("%s/cosmos/base/tendermint/v1beta1/validatorsets/%d", baseurl, block), false, "", http.StatusOK}, + {"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}, + {"with pagination", fmt.Sprintf("%s/cosmos/base/tendermint/v1beta1/validatorsets/%d?pagination.limit=2", baseurl, 1), false, "", http.StatusOK}, } for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) { - rsp := GetRequest(t, tc.url) + rsp := GetRequestWithHeaders(t, tc.url, nil, tc.expHttpCode) if tc.expErr { errMsg := gjson.GetBytes(rsp, "message").String() assert.Contains(t, errMsg, tc.expErrMsg) - } else { - assert.Equal(t, len(vals), int(gjson.GetBytes(rsp, "pagination.total").Int())) + return } + assert.Equal(t, len(vals), int(gjson.GetBytes(rsp, "pagination.total").Int())) }) } } func TestABCIQuery(t *testing.T) { - sut.ResetChain(t) sut.StartChain(t) qc := cmtservice.NewServiceClient(sut.RPCClient(t)) @@ -312,7 +317,7 @@ func TestABCIQuery(t *testing.T) { } else { assert.NoError(t, err) assert.NotNil(t, res) - assert.Equal(t, res.Code, tc.expectedCode) + assert.Equal(t, tc.expectedCode, res.Code) } if tc.validQuery { diff --git a/tests/systemtests/rest_cli.go b/tests/systemtests/rest_cli.go index a7609949387fe..3ad1e30dda685 100644 --- a/tests/systemtests/rest_cli.go +++ b/tests/systemtests/rest_cli.go @@ -79,9 +79,9 @@ func GetRequestWithHeaders(t *testing.T, url string, headers map[string]string, defer func() { _ = res.Body.Close() }() - require.Equal(t, expCode, res.StatusCode, "status code should be %d, got: %d", expCode, res.StatusCode) - body, err := io.ReadAll(res.Body) require.NoError(t, err) + require.Equal(t, expCode, res.StatusCode, "status code should be %d, got: %d, %s", expCode, res.StatusCode, body) + return body } diff --git a/tests/systemtests/system.go b/tests/systemtests/system.go index 346f39e761b49..f42bf96a79cdf 100644 --- a/tests/systemtests/system.go +++ b/tests/systemtests/system.go @@ -766,6 +766,11 @@ func (s *SystemUnderTest) CurrentHeight() int64 { return s.currentHeight.Load() } +// NodesCount returns the number of node instances used +func (s *SystemUnderTest) NodesCount() int { + return s.nodesCount +} + type Node struct { ID string IP string