Skip to content

Commit 5b57dd7

Browse files
jimjbrettjtimwu20
authored andcommitted
testing (rpc/modules): migrate current tests to integration tests, write new unit tests (ChainSafe#1957)
* fix TestAuthorModule_InsertKey_Valid * fix TestAuthorModule_InsertKey_Valid_Gran_Keytype * fix rpc/module/author_integration_test * migrate rpc/module tests to integration tests TODO:write unit tests * remove skip author integration tests from golangci.yml * lint * rebase and fix errors * license * testing (rpc/modules): write new unit tests to increase code coverage (ChainSafe#1995) * migrate rpc/module tests to integration tests TODO:write unit tests * fix TestAuthorModule_InsertKey_Valid * fix TestAuthorModule_InsertKey_Valid_Gran_Keytype * migrate rpc/module tests to integration tests TODO:write unit tests * use mockery to generate BlockProducerAPI * WIP/test for dev control function * 100% test coverage for rpc/module/dev unit tests * test cov for getKeys in childState * 100% cover for getStorage size * 100% cover for getStorage hash * 100% coverage for childstate unit tests * format childState * lint * tests for offchain get * full coverage for offchain unit tests * unit tests for payment * 100% for grandpa proveFinality * grandpa unit tests * GenSyncSpec coverage * sync state unit tests * GetBlock * getBlockHash tests * chain tests * Wip/system tests * AccountNextIndex test * syncState test * tes localListenAndAddresses * test localPeerId * system tests * state getPairs test * getKeysPages test * test GetMetadata * test GetReadProof * test GetRuntime Version * test GetStorage * test GetStorageHash * test GetStorageSize * test queryStorage * state tests * remove subscribe func tests * format * rebase * fix naming and remove unnecessary variable inits * remove log * push test data * remove AnythingOfType from chain test * remove AnythingOfType from childState test * remove AnythingOfType from Grandpa test * remove AnythingOfType from offchain test * remove AnythingOfType from payment test * remove mocking anything in state test * remove mocking anything in sync state test * WIP/Finish removing mock.AnythingOfType * WIP/author test * author test * add exp cases to chain test * WIP/figure out error assertion issue * WIP/fix errors still * fix chain test error handling * author module tests fix * childstate test CR feedback * dev tests * wip/grandpa tests * grandpa tests * offchain tests * payment tests * syc state test updates * state test CR feedback * system tests * fix inconsistent test to use reflect on maps * use zip file for test data * delete test data hex file * fix up tests * close to final changes * finish CR feedback * clean up error checking * CR feedback TODO/Fix integration test error * move response init into subtests * remove panic from code * WIP/clean up tests * change res variable to be non pointer * WIP/add expected response check for error case * add response check for error case * finish first round of CR feedback * hasSessionKey tests * remove old author module tests * rename err to expErr * use system module constructor instead of fields * rename getter for test metadata * wip/CR feedback * separate out system getter tests * fix naming * clean up imports * wip/cr feedback * remove wantErr from rpc/module tests * finalize feedback * rebase * CR feedback * remove apimocks alias * final CR feedback * finalize CR changes * fix failing http test * fix LocalPeerId naming
1 parent 6f34b1b commit 5b57dd7

34 files changed

+5941
-2066
lines changed

.golangci.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ run:
2727
skip-files:
2828
- .*mock_.*\.go
2929
- .*mocks\/.*\.go
30-
- author_integration_test\.go # TODO remove once fixed
3130

3231
# all available settings of specific linters
3332
linters-settings:

dot/rpc/modules/api.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,8 @@ type RuntimeStorageAPI interface {
147147
GetPersistent(k []byte) ([]byte, error)
148148
}
149149

150+
//go:generate mockery --name SyncStateAPI --structname SyncStateAPI --case underscore --keeptree
151+
150152
// SyncStateAPI is the interface to interact with sync state.
151153
type SyncStateAPI interface {
152154
GenSyncSpec(raw bool) (*genesis.Genesis, error)

dot/rpc/modules/api_mocks.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ import (
1616
func NewMockStorageAPI() *modulesmocks.StorageAPI {
1717
m := new(modulesmocks.StorageAPI)
1818
m.On("GetStorage", mock.AnythingOfType("*common.Hash"), mock.AnythingOfType("[]uint8")).Return(nil, nil)
19-
m.On("GetStorageFromChild", mock.AnythingOfType("*common.Hash"), mock.AnythingOfType("[]uint8"), mock.AnythingOfType("[]uint8")).Return(nil, nil)
19+
m.On("GetStorageFromChild", mock.AnythingOfType("*common.Hash"), mock.AnythingOfType("[]uint8"),
20+
mock.AnythingOfType("[]uint8")).Return(nil, nil)
2021
m.On("Entries", mock.AnythingOfType("*common.Hash")).Return(nil, nil)
2122
m.On("GetStorageByBlockHash", mock.AnythingOfType("common.Hash"), mock.AnythingOfType("[]uint8")).Return(nil, nil)
2223
m.On("RegisterStorageObserver", mock.Anything)
@@ -41,7 +42,8 @@ func NewMockBlockAPI() *modulesmocks.BlockAPI {
4142
m.On("FreeFinalisedNotifierChannel", mock.AnythingOfType("chan *types.FinalisationInfo"))
4243
m.On("GetJustification", mock.AnythingOfType("common.Hash")).Return(make([]byte, 10), nil)
4344
m.On("HasJustification", mock.AnythingOfType("common.Hash")).Return(true, nil)
44-
m.On("SubChain", mock.AnythingOfType("common.Hash"), mock.AnythingOfType("common.Hash")).Return(make([]common.Hash, 0), nil)
45+
m.On("SubChain", mock.AnythingOfType("common.Hash"), mock.AnythingOfType("common.Hash")).
46+
Return(make([]common.Hash, 0), nil)
4547
m.On("RegisterRuntimeUpdatedChannel", mock.AnythingOfType("chan<- runtime.Version")).Return(uint32(0), nil)
4648

4749
return m

dot/rpc/modules/author.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ func (am *AuthorModule) PendingExtrinsics(r *http.Request, req *EmptyRequest, re
191191
}
192192

193193
// RemoveExtrinsic Remove given extrinsic from the pool and temporarily ban it to prevent reimporting
194-
func (am *AuthorModule) RemoveExtrinsic(r *http.Request, req *ExtrinsicOrHashRequest, res *RemoveExtrinsicsResponse) error {
194+
func (am *AuthorModule) RemoveExtrinsic(r *http.Request, _ *ExtrinsicOrHashRequest, _ *RemoveExtrinsicsResponse) error {
195195
return nil
196196
}
197197

dot/rpc/modules/author_integration_test.go

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ package modules
88

99
import (
1010
"fmt"
11+
"io"
1112
"os"
1213
"reflect"
1314
"testing"
@@ -16,6 +17,8 @@ import (
1617
"github.com/ChainSafe/gossamer/dot/types"
1718
"github.com/ChainSafe/gossamer/internal/log"
1819
"github.com/ChainSafe/gossamer/lib/common"
20+
"github.com/ChainSafe/gossamer/lib/crypto/ed25519"
21+
"github.com/ChainSafe/gossamer/lib/crypto/sr25519"
1922
"github.com/ChainSafe/gossamer/lib/keystore"
2023
"github.com/ChainSafe/gossamer/lib/runtime"
2124
"github.com/ChainSafe/gossamer/lib/runtime/wasmer"
@@ -24,7 +27,9 @@ import (
2427
)
2528

2629
// https://github.com/paritytech/substrate/blob/5420de3face1349a97eb954ae71c5b0b940c31de/core/transaction-pool/src/tests.rs#L95
27-
var testExt = common.MustHexToBytes("0x410284ffd43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d01f8efbe48487e57a22abf7e3acd491b7f3528a33a111b1298601554863d27eb129eaa4e718e1365414ff3d028b62bebc651194c6b5001e5c2839b982757e08a8c0000000600ff8eaf04151687736326c9fea17e25fc5287613693c912909cb226aa4794f26a480b00c465f14670")
30+
var testExt = common.MustHexToBytes("0x410284ffd43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d01f8e" +
31+
"fbe48487e57a22abf7e3acd491b7f3528a33a111b1298601554863d27eb129eaa4e718e1365414ff3d028b62bebc651194c6b5001e5c2839b98" +
32+
"2757e08a8c0000000600ff8eaf04151687736326c9fea17e25fc5287613693c912909cb226aa4794f26a480b00c465f14670")
2833

2934
// invalid transaction (above tx, with last byte changed)
3035
//nolint
@@ -33,7 +38,7 @@ var testInvalidExt = []byte{1, 212, 53, 147, 199, 21, 253, 211, 28, 97, 20, 26,
3338
func TestMain(m *testing.M) {
3439
wasmFilePaths, err := runtime.GenerateRuntimeWasmFile()
3540
if err != nil {
36-
log.Error("failed to generate runtime wasm file", err)
41+
log.Errorf("failed to generate runtime wasm file: %s", err)
3742
os.Exit(1)
3843
}
3944

@@ -46,7 +51,7 @@ func TestMain(m *testing.M) {
4651

4752
func TestAuthorModule_Pending(t *testing.T) {
4853
txQueue := state.NewTransactionState()
49-
auth := NewAuthorModule(nil, nil, nil, txQueue)
54+
auth := NewAuthorModule(log.New(log.SetWriter(io.Discard)), nil, txQueue)
5055

5156
res := new(PendingExtrinsicsResponse)
5257
err := auth.PendingExtrinsics(nil, nil, res)
@@ -178,35 +183,48 @@ func TestAuthorModule_SubmitExtrinsic_InQueue(t *testing.T) {
178183
}
179184

180185
func TestAuthorModule_InsertKey_Valid(t *testing.T) {
186+
seed := "0xb7e9185065667390d2ad952a5324e8c365c9bf503dcf97c67a5ce861afe97309"
187+
kp, err := sr25519.NewKeypairFromSeed(common.MustHexToBytes(seed))
188+
require.NoError(t, err)
189+
181190
auth := setupAuthModule(t, nil)
182-
req := &KeyInsertRequest{"babe", "0xb7e9185065667390d2ad952a5324e8c365c9bf503dcf97c67a5ce861afe97309", "0x6246ddf254e0b4b4e7dffefc8adf69d212b98ac2b579c362b473fec8c40b4c0a"}
191+
req := &KeyInsertRequest{"babe", seed, kp.Public().Hex()}
183192
res := &KeyInsertResponse{}
184-
err := auth.InsertKey(nil, req, res)
193+
err = auth.InsertKey(nil, req, res)
185194
require.Nil(t, err)
186195
require.Len(t, *res, 0) // zero len result on success
187196
}
188197

189-
func TestAuthorModule_InsertKey_Valid_gran_keytype(t *testing.T) {
198+
func TestAuthorModule_InsertKey_Valid_Gran_Keytype(t *testing.T) {
199+
seed := "0xb7e9185065667390d2ad952a5324e8c365c9bf503dcf97c67a5ce861afe97309"
200+
kp, err := ed25519.NewKeypairFromSeed(common.MustHexToBytes(seed))
201+
require.NoError(t, err)
202+
190203
auth := setupAuthModule(t, nil)
191-
req := &KeyInsertRequest{"gran", "0xb7e9185065667390d2ad952a5324e8c365c9bf503dcf97c67a5ce861afe97309b7e9185065667390d2ad952a5324e8c365c9bf503dcf97c67a5ce861afe97309", "0xb7e9185065667390d2ad952a5324e8c365c9bf503dcf97c67a5ce861afe97309"}
204+
req := &KeyInsertRequest{"gran", seed, kp.Public().Hex()}
192205
res := &KeyInsertResponse{}
193-
err := auth.InsertKey(nil, req, res)
206+
err = auth.InsertKey(nil, req, res)
194207
require.Nil(t, err)
195208

196209
require.Len(t, *res, 0) // zero len result on success
197210
}
198211

199212
func TestAuthorModule_InsertKey_InValid(t *testing.T) {
200213
auth := setupAuthModule(t, nil)
201-
req := &KeyInsertRequest{"babe", "0xb7e9185065667390d2ad952a5324e8c365c9bf503dcf97c67a5ce861afe97309", "0x0000000000000000000000000000000000000000000000000000000000000000"}
214+
req := &KeyInsertRequest{
215+
"babe",
216+
"0xb7e9185065667390d2ad952a5324e8c365c9bf503dcf97c67a5ce861afe97309",
217+
"0x0000000000000000000000000000000000000000000000000000000000000000"}
202218
res := &KeyInsertResponse{}
203219
err := auth.InsertKey(nil, req, res)
204220
require.EqualError(t, err, "generated public key does not equal provide public key")
205221
}
206222

207223
func TestAuthorModule_InsertKey_UnknownKeyType(t *testing.T) {
208224
auth := setupAuthModule(t, nil)
209-
req := &KeyInsertRequest{"mack", "0xb7e9185065667390d2ad952a5324e8c365c9bf503dcf97c67a5ce861afe97309", "0x6246ddf254e0b4b4e7dffefc8adf69d212b98ac2b579c362b473fec8c40b4c0a"}
225+
req := &KeyInsertRequest{"mack",
226+
"0xb7e9185065667390d2ad952a5324e8c365c9bf503dcf97c67a5ce861afe97309",
227+
"0x6246ddf254e0b4b4e7dffefc8adf69d212b98ac2b579c362b473fec8c40b4c0a"}
210228
res := &KeyInsertResponse{}
211229
err := auth.InsertKey(nil, req, res)
212230
require.EqualError(t, err, "cannot decode key: invalid key type")
@@ -267,5 +285,5 @@ func setupAuthModule(t *testing.T, txq *state.TransactionState) *AuthorModule {
267285
t.Cleanup(func() {
268286
rt.Stop()
269287
})
270-
return NewAuthorModule(nil, cs, rt, txq)
288+
return NewAuthorModule(log.New(log.SetWriter(io.Discard)), cs, txq)
271289
}

0 commit comments

Comments
 (0)