Skip to content

Commit

Permalink
chore(tests/rpc): refactor getResponse
Browse files Browse the repository at this point in the history
- not test aware
- get passed target interface
- push test assertion/skip to actual test
  • Loading branch information
qdm12 committed Apr 6, 2022
1 parent 65af058 commit f02da5d
Show file tree
Hide file tree
Showing 10 changed files with 94 additions and 27 deletions.
28 changes: 11 additions & 17 deletions tests/rpc/rpc_00_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@ package rpc

import (
"context"
"reflect"
"testing"
"fmt"

"github.com/ChainSafe/gossamer/tests/utils/rpc"
"github.com/stretchr/testify/require"
)

var (
Expand All @@ -24,22 +22,18 @@ type testCase struct {
skip bool
}

func getResponse(ctx context.Context, t *testing.T, test *testCase) interface{} {
if test.skip {
t.Skip("RPC endpoint not yet implemented")
return nil
}

func getResponse(ctx context.Context, method, params string, target interface{}) (err error) {
const currentPort = "8540"
endpoint := rpc.NewEndpoint(currentPort)
respBody, err := rpc.Post(ctx, endpoint, test.method, test.params)
require.NoError(t, err)

target := reflect.New(reflect.TypeOf(test.expected)).Interface()
err = rpc.Decode(respBody, target)
require.NoError(t, err)
respBody, err := rpc.Post(ctx, endpoint, method, params)
if err != nil {
return fmt.Errorf("cannot RPC post: %w", err)
}

require.NotNil(t, target)
err = rpc.Decode(respBody, &target)
if err != nil {
return fmt.Errorf("cannot decode RPC response: %w", err)
}

return target
return nil
}
9 changes: 8 additions & 1 deletion tests/rpc/rpc_01-system_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package rpc

import (
"context"
"reflect"
"testing"
"time"

Expand Down Expand Up @@ -102,9 +103,15 @@ func TestSystemRPC(t *testing.T) {

for _, test := range testCases {
t.Run(test.description, func(t *testing.T) {
if test.skip {
t.SkipNow()
}

getResponseCtx, getResponseCancel := context.WithTimeout(ctx, time.Second)
defer getResponseCancel()
target := getResponse(getResponseCtx, t, test)
target := reflect.New(reflect.TypeOf(test.expected)).Interface()
err := getResponse(getResponseCtx, test.method, test.params, target)
require.NoError(t, err)

switch v := target.(type) {
case *modules.SystemHealthResponse:
Expand Down
9 changes: 8 additions & 1 deletion tests/rpc/rpc_02-author_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"bytes"
"context"
"fmt"
"reflect"
"testing"
"time"

Expand Down Expand Up @@ -143,9 +144,15 @@ func TestAuthorRPC(t *testing.T) {

for _, test := range testCases {
t.Run(test.description, func(t *testing.T) {
if test.skip {
t.SkipNow()
}

getResponseCtx, getResponseCancel := context.WithTimeout(ctx, time.Second)
defer getResponseCancel()
_ = getResponse(getResponseCtx, t, test)
target := reflect.New(reflect.TypeOf(test.expected)).Interface()
err := getResponse(getResponseCtx, test.method, test.params, target)
require.NoError(t, err)
})
}
}
10 changes: 8 additions & 2 deletions tests/rpc/rpc_03-chain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package rpc
import (
"context"
"log"
"reflect"
"testing"
"time"

Expand Down Expand Up @@ -71,8 +72,10 @@ func TestChainRPC(t *testing.T) {

chainBlockHeaderHash := ""
for _, test := range testCases {

t.Run(test.description, func(t *testing.T) {
if test.skip {
t.SkipNow()
}

// set params for chain_getBlock from previous chain_getHeader call
if chainBlockHeaderHash != "" {
Expand All @@ -81,7 +84,10 @@ func TestChainRPC(t *testing.T) {

getResponseCtx, getResponseCancel := context.WithTimeout(ctx, time.Second)
defer getResponseCancel()
target := getResponse(getResponseCtx, t, test)

target := reflect.New(reflect.TypeOf(test.expected)).Interface()
err := getResponse(getResponseCtx, test.method, test.params, target)
require.NoError(t, err)

switch v := target.(type) {
case *modules.ChainBlockHeaderResponse:
Expand Down
11 changes: 10 additions & 1 deletion tests/rpc/rpc_04-offchain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ package rpc

import (
"context"
"reflect"
"testing"
"time"

libutils "github.com/ChainSafe/gossamer/lib/utils"
"github.com/ChainSafe/gossamer/tests/utils"
"github.com/ChainSafe/gossamer/tests/utils/config"
"github.com/ChainSafe/gossamer/tests/utils/node"
"github.com/stretchr/testify/require"
)

func TestOffchainRPC(t *testing.T) {
Expand Down Expand Up @@ -49,9 +51,16 @@ func TestOffchainRPC(t *testing.T) {

for _, test := range testCases {
t.Run(test.description, func(t *testing.T) {
if test.skip {
t.SkipNow()
}

getResponseCtx, getResponseCancel := context.WithTimeout(ctx, time.Second)
defer getResponseCancel()
_ = getResponse(getResponseCtx, t, test)

target := reflect.New(reflect.TypeOf(test.expected)).Interface()
err := getResponse(getResponseCtx, test.method, test.params, target)
require.NoError(t, err)
})
}
}
10 changes: 9 additions & 1 deletion tests/rpc/rpc_05-state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package rpc
import (
"context"
"fmt"
"reflect"
"testing"
"time"

Expand Down Expand Up @@ -121,9 +122,16 @@ func TestStateRPCResponseValidation(t *testing.T) {

for _, test := range testCases {
t.Run(test.description, func(t *testing.T) {
if test.skip {
t.SkipNow()
}

getResponseCtx, getResponseCancel := context.WithTimeout(ctx, time.Second)
defer getResponseCancel()
_ = getResponse(getResponseCtx, t, test)

target := reflect.New(reflect.TypeOf(test.expected)).Interface()
err := getResponse(getResponseCtx, test.method, test.params, target)
require.NoError(t, err)
})
}

Expand Down
11 changes: 10 additions & 1 deletion tests/rpc/rpc_06-engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ package rpc

import (
"context"
"reflect"
"testing"
"time"

libutils "github.com/ChainSafe/gossamer/lib/utils"
"github.com/ChainSafe/gossamer/tests/utils"
"github.com/ChainSafe/gossamer/tests/utils/config"
"github.com/ChainSafe/gossamer/tests/utils/node"
"github.com/stretchr/testify/require"
)

func TestEngineRPC(t *testing.T) {
Expand Down Expand Up @@ -44,9 +46,16 @@ func TestEngineRPC(t *testing.T) {

for _, test := range testCases {
t.Run(test.description, func(t *testing.T) {
if test.skip {
t.SkipNow()
}

getResponseCtx, getResponseCancel := context.WithTimeout(ctx, time.Second)
defer getResponseCancel()
_ = getResponse(getResponseCtx, t, test)

target := reflect.New(reflect.TypeOf(test.expected)).Interface()
err := getResponse(getResponseCtx, test.method, test.params, target)
require.NoError(t, err)
})
}
}
11 changes: 10 additions & 1 deletion tests/rpc/rpc_07-payment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ package rpc

import (
"context"
"reflect"
"testing"
"time"

libutils "github.com/ChainSafe/gossamer/lib/utils"
"github.com/ChainSafe/gossamer/tests/utils"
"github.com/ChainSafe/gossamer/tests/utils/config"
"github.com/ChainSafe/gossamer/tests/utils/node"
"github.com/stretchr/testify/require"
)

func TestPaymentRPC(t *testing.T) {
Expand Down Expand Up @@ -39,9 +41,16 @@ func TestPaymentRPC(t *testing.T) {

for _, test := range testCases {
t.Run(test.description, func(t *testing.T) {
if test.skip {
t.SkipNow()
}

getResponseCtx, getResponseCancel := context.WithTimeout(ctx, time.Second)
defer getResponseCancel()
_ = getResponse(getResponseCtx, t, test)

target := reflect.New(reflect.TypeOf(test.expected)).Interface()
err := getResponse(getResponseCtx, test.method, test.params, target)
require.NoError(t, err)
})
}
}
11 changes: 10 additions & 1 deletion tests/rpc/rpc_08-contracts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ package rpc

import (
"context"
"reflect"
"testing"
"time"

libutils "github.com/ChainSafe/gossamer/lib/utils"
"github.com/ChainSafe/gossamer/tests/utils"
"github.com/ChainSafe/gossamer/tests/utils/config"
"github.com/ChainSafe/gossamer/tests/utils/node"
"github.com/stretchr/testify/require"
)

func TestContractsRPC(t *testing.T) {
Expand Down Expand Up @@ -44,9 +46,16 @@ func TestContractsRPC(t *testing.T) {

for _, test := range testCases {
t.Run(test.description, func(t *testing.T) {
if test.skip {
t.SkipNow()
}

getResponseCtx, getResponseCancel := context.WithTimeout(ctx, time.Second)
defer getResponseCancel()
_ = getResponse(getResponseCtx, t, test)

target := reflect.New(reflect.TypeOf(test.expected)).Interface()
err := getResponse(getResponseCtx, test.method, test.params, target)
require.NoError(t, err)
})
}
}
11 changes: 10 additions & 1 deletion tests/rpc/rpc_09-babe_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ package rpc

import (
"context"
"reflect"
"testing"
"time"

libutils "github.com/ChainSafe/gossamer/lib/utils"
"github.com/ChainSafe/gossamer/tests/utils"
"github.com/ChainSafe/gossamer/tests/utils/config"
"github.com/ChainSafe/gossamer/tests/utils/node"
"github.com/stretchr/testify/require"
)

func TestBabeRPC(t *testing.T) {
Expand Down Expand Up @@ -39,9 +41,16 @@ func TestBabeRPC(t *testing.T) {

for _, test := range testCases {
t.Run(test.description, func(t *testing.T) {
if test.skip {
t.SkipNow()
}

getResponseCtx, cancel := context.WithTimeout(ctx, time.Second)
defer cancel()
_ = getResponse(getResponseCtx, t, test)

target := reflect.New(reflect.TypeOf(test.expected)).Interface()
err := getResponse(getResponseCtx, test.method, test.params, target)
require.NoError(t, err)
})
}
}

0 comments on commit f02da5d

Please sign in to comment.