test(systemtests): fix gRPC tests for v1 & v2 (#22774)

This commit is contained in:
Julien Robert
2024-12-06 11:03:12 +00:00
committed by GitHub
parent 3a2a0ac666
commit 4caac04c23
8 changed files with 233 additions and 170 deletions
+4
View File
@@ -36,6 +36,10 @@ Ref: https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.j
## [Unreleased]
## [v1.0.0-rc.3] - 2024-12-05
* [#22774](https://github.com/cosmos/cosmos-sdk/pull/22774) Add greater than or equal support in Rest test suite
## [v1.0.0-rc.2] - 2024-11-26
* [#22577](https://github.com/cosmos/cosmos-sdk/pull/22577) Support invalid RPC response for CometBFT v1
+79 -20
View File
@@ -7,15 +7,14 @@ import (
"testing"
"github.com/stretchr/testify/require"
"github.com/cosmos/cosmos-sdk/testutil"
)
type RestTestCase struct {
Name string
Url string
ExpCode int
ExpOut string
Name string
Url string
ExpCode int
ExpCodeGTE int
ExpOut string
}
// RunRestQueries runs given Rest testcases by making requests and
@@ -25,33 +24,71 @@ func RunRestQueries(t *testing.T, testCases ...RestTestCase) {
for _, tc := range testCases {
t.Run(tc.Name, func(t *testing.T) {
resp := GetRequestWithHeaders(t, tc.Url, nil, tc.ExpCode)
if tc.ExpCodeGTE > 0 && tc.ExpCode > 0 {
require.Fail(t, "only one of ExpCode or ExpCodeGTE should be set")
}
var resp []byte
if tc.ExpCodeGTE > 0 {
resp = GetRequestWithHeadersGreaterThanOrEqual(t, tc.Url, nil, tc.ExpCodeGTE)
} else {
resp = GetRequestWithHeaders(t, tc.Url, nil, tc.ExpCode)
}
require.JSONEq(t, tc.ExpOut, string(resp))
})
}
}
// TestRestQueryIgnoreNumbers runs given rest testcases by making requests and
// RunRestQueriesIgnoreNumbers runs given rest testcases by making requests and
// checking response with expected output ignoring number values
// This method is used when number values in response are non-deterministic
func TestRestQueryIgnoreNumbers(t *testing.T, testCases ...RestTestCase) {
func RunRestQueriesIgnoreNumbers(t *testing.T, testCases ...RestTestCase) {
t.Helper()
// regex for standalone quoted numbers (e.g., "-3" or "0.02")
standaloneQuotedNumberRegex := regexp.MustCompile(`"(-?\d+(\.\d+)?)"`)
// regex for numbers in escaped strings (e.g., \"-3\")
escapedNumberRegex := regexp.MustCompile(`\\\"(-?\d+(\.\d+)?)\\\"`)
// regex for unquoted numbers (e.g., 2, -1, 0.02,)
unquotedNumberRegex := regexp.MustCompile(`\b-?\d+(\.\d+)?\b,`)
replaceNumber := func(input string) string {
// handle numbers in escaped strings
result := escapedNumberRegex.ReplaceAllStringFunc(input, func(match string) string {
// replace with escaped "NUMBER"
return `\"NUMBER\"`
})
// handle standalone quoted numbers
result = standaloneQuotedNumberRegex.ReplaceAllStringFunc(result, func(match string) string {
// replace with "NUMBER" (quotes preserved)
return `"NUMBER"`
})
// handle unquoted numbers
result = unquotedNumberRegex.ReplaceAllStringFunc(result, func(match string) string {
// replace with "NUMBER" (add quotes to ensure json validity)
return `"NUMBER",`
})
return result
}
for _, tc := range testCases {
t.Run(tc.Name, func(t *testing.T) {
resp, err := testutil.GetRequest(tc.Url)
require.NoError(t, err)
if tc.ExpCodeGTE > 0 && tc.ExpCode > 0 {
require.Fail(t, "only one of ExpCode or ExpCodeGTE should be set")
}
// regular expression pattern to match any numeric value in the JSON
numberRegexPattern := `"\d+(\.\d+)?"`
var resp []byte
if tc.ExpCodeGTE > 0 {
resp = GetRequestWithHeadersGreaterThanOrEqual(t, tc.Url, nil, tc.ExpCodeGTE)
} else {
resp = GetRequestWithHeaders(t, tc.Url, nil, tc.ExpCode)
}
// compile the regex
r, err := regexp.Compile(numberRegexPattern)
require.NoError(t, err)
// replace all numeric values in the above JSONs with `NUMBER` text
expectedJSON := r.ReplaceAllString(tc.ExpOut, `"NUMBER"`)
actualJSON := r.ReplaceAllString(string(resp), `"NUMBER"`)
expectedJSON := replaceNumber(tc.ExpOut)
actualJSON := replaceNumber(string(resp))
// compare two jsons
require.JSONEq(t, expectedJSON, actualJSON)
@@ -85,3 +122,25 @@ func GetRequestWithHeaders(t *testing.T, url string, headers map[string]string,
return body
}
func GetRequestWithHeadersGreaterThanOrEqual(t *testing.T, url string, headers map[string]string, expCode int) []byte {
t.Helper()
req, err := http.NewRequest("GET", url, nil)
require.NoError(t, err)
for key, value := range headers {
req.Header.Set(key, value)
}
httpClient := &http.Client{}
res, err := httpClient.Do(req)
require.NoError(t, err)
defer func() {
_ = res.Body.Close()
}()
body, err := io.ReadAll(res.Body)
require.NoError(t, err)
require.GreaterOrEqual(t, res.StatusCode, expCode, "status code should be greater or equal to %d, got: %d, %s", expCode, res.StatusCode, body)
return body
}