From c102b71abe13e9d008f207154f5867afa6d2d474 Mon Sep 17 00:00:00 2001 From: Kishan Mohanbhai Sagathiya Date: Tue, 4 Jul 2023 16:37:22 +0530 Subject: [PATCH 1/2] Use TempDir instead of home dir in tests --- dot/build_spec_integration_test.go | 6 ++-- dot/build_spec_test.go | 6 ++-- dot/import_integration_test.go | 19 +++--------- dot/node_integration_test.go | 33 ++++++++------------ dot/node_test.go | 15 ++++++--- dot/services_integration_test.go | 49 ++++++++++++------------------ dot/services_test.go | 8 ++--- dot/utils_integration_test.go | 5 +-- dot/utils_test.go | 32 ++----------------- 9 files changed, 55 insertions(+), 118 deletions(-) diff --git a/dot/build_spec_integration_test.go b/dot/build_spec_integration_test.go index 2dfd337059..4a8dc3e76e 100644 --- a/dot/build_spec_integration_test.go +++ b/dot/build_spec_integration_test.go @@ -11,8 +11,6 @@ import ( "path/filepath" "testing" - westenddev "github.com/ChainSafe/gossamer/chain/westend-dev" - "github.com/ChainSafe/gossamer/lib/genesis" "github.com/ChainSafe/gossamer/lib/utils" "github.com/stretchr/testify/require" @@ -22,7 +20,7 @@ import ( const codeHex = "0x3a636f6465" func TestWriteGenesisSpecFile_Integration(t *testing.T) { - config := westenddev.DefaultConfig() + config := DefaultTestWestendDevConfig(t) config.ChainSpec = utils.GetWestendDevRawGenesisPath(t) expected, err := genesis.NewGenesisFromJSONRaw(config.ChainSpec) @@ -61,7 +59,7 @@ func TestWriteGenesisSpecFile_Integration(t *testing.T) { func TestBuildFromDB_Integration(t *testing.T) { // setup expected - config := westenddev.DefaultConfig() + config := DefaultTestWestendDevConfig(t) config.ChainSpec = utils.GetWestendDevRawGenesisPath(t) expected, err := genesis.NewGenesisFromJSONRaw(config.ChainSpec) require.NoError(t, err) diff --git a/dot/build_spec_test.go b/dot/build_spec_test.go index f7c512ce51..eee6456c33 100644 --- a/dot/build_spec_test.go +++ b/dot/build_spec_test.go @@ -10,8 +10,6 @@ import ( "strings" "testing" - westenddev "github.com/ChainSafe/gossamer/chain/westend-dev" - "github.com/ChainSafe/gossamer/lib/genesis" "github.com/ChainSafe/gossamer/lib/utils" "github.com/stretchr/testify/assert" @@ -124,9 +122,9 @@ func TestBuildSpec_ToJSON(t *testing.T) { func TestBuildFromDB(t *testing.T) { // initialise node (initialise state database and load genesis data) - config := westenddev.DefaultConfig() + config := DefaultTestWestendDevConfig(t) + config.ChainSpec = utils.GetWestendDevRawGenesisPath(t) - config.BasePath = t.TempDir() builder := nodeBuilder{} err := builder.initNode(config) require.NoError(t, err) diff --git a/dot/import_integration_test.go b/dot/import_integration_test.go index c89148e2cf..534f50b4cf 100644 --- a/dot/import_integration_test.go +++ b/dot/import_integration_test.go @@ -7,11 +7,8 @@ package dot import ( "errors" - "os" "testing" - westend_dev "github.com/ChainSafe/gossamer/chain/westend-dev" - "github.com/ChainSafe/gossamer/dot/state" "github.com/ChainSafe/gossamer/dot/types" "github.com/ChainSafe/gossamer/internal/log" @@ -84,10 +81,7 @@ func TestNewHeaderFromFile(t *testing.T) { } func TestImportState_Integration(t *testing.T) { - basepath := os.TempDir() - - config := westend_dev.DefaultConfig() - config.BasePath = basepath + config := DefaultTestWestendDevConfig(t) genFile := NewTestGenesisRawFile(t, config) @@ -99,11 +93,11 @@ func TestImportState_Integration(t *testing.T) { headerFP := setupHeaderFile(t) const firstSlot = uint64(262493679) - err = ImportState(basepath, stateFP, headerFP, firstSlot) + err = ImportState(config.BasePath, stateFP, headerFP, firstSlot) require.NoError(t, err) // confirm data is imported into db stateConfig := state.Config{ - Path: basepath, + Path: config.BasePath, LogLevel: log.Info, } srv := state.NewService(stateConfig) @@ -119,10 +113,7 @@ func TestImportState_Integration(t *testing.T) { func TestImportState(t *testing.T) { t.Parallel() - basepath := t.TempDir() - - config := westend_dev.DefaultConfig() - config.BasePath = basepath + config := DefaultTestWestendDevConfig(t) config.ChainSpec = NewTestGenesisRawFile(t, config) nodeInstance := nodeBuilder{} @@ -150,7 +141,7 @@ func TestImportState(t *testing.T) { { name: "working_example", args: args{ - basepath: basepath, + basepath: config.BasePath, stateFP: stateFP, headerFP: headerFP, firstSlot: 262493679, diff --git a/dot/node_integration_test.go b/dot/node_integration_test.go index 2d7c158727..691f281fda 100644 --- a/dot/node_integration_test.go +++ b/dot/node_integration_test.go @@ -15,7 +15,6 @@ import ( "testing" "github.com/ChainSafe/gossamer/chain/westend" - westenddev "github.com/ChainSafe/gossamer/chain/westend-dev" cfg "github.com/ChainSafe/gossamer/config" "github.com/ChainSafe/gossamer/dot/core" @@ -163,7 +162,8 @@ func TestNewNode(t *testing.T) { } func Test_nodeBuilder_loadRuntime(t *testing.T) { - config := westenddev.DefaultConfig() + config := DefaultTestWestendDevConfig(t) + type args struct { config *cfg.Config ns *runtime.NodeStorage @@ -205,12 +205,11 @@ func Test_nodeBuilder_loadRuntime(t *testing.T) { } func TestInitNode_Integration(t *testing.T) { - config := westenddev.DefaultConfig() + config := DefaultTestWestendDevConfig(t) genFile := NewTestGenesisRawFile(t, config) config.ChainSpec = genFile - config.BasePath = t.TempDir() err := InitNode(config) require.NoError(t, err) @@ -222,12 +221,11 @@ func TestInitNode_Integration(t *testing.T) { } func TestInitNode_GenesisSpec(t *testing.T) { - config := westenddev.DefaultConfig() + config := DefaultTestWestendDevConfig(t) - genFile := newTestGenesisFile(t, config) + genFile := NewTestGenesisRawFile(t, config) config.ChainSpec = genFile - config.BasePath = t.TempDir() err := InitNode(config) require.NoError(t, err) @@ -238,12 +236,11 @@ func TestInitNode_GenesisSpec(t *testing.T) { } func TestNodeInitializedIntegration(t *testing.T) { - config := westenddev.DefaultConfig() + config := DefaultTestWestendDevConfig(t) genFile := NewTestGenesisRawFile(t, config) config.ChainSpec = genFile - config.BasePath = t.TempDir() result := IsNodeInitialised(config.BasePath) require.False(t, result) @@ -256,12 +253,11 @@ func TestNodeInitializedIntegration(t *testing.T) { } func TestNewNodeIntegration(t *testing.T) { - config := westenddev.DefaultConfig() + config := DefaultTestWestendDevConfig(t) genFile := NewTestGenesisRawFile(t, config) config.ChainSpec = genFile - config.BasePath = t.TempDir() err := InitNode(config) require.NoError(t, err) @@ -288,12 +284,11 @@ func TestNewNodeIntegration(t *testing.T) { } func TestNewNode_Authority(t *testing.T) { - config := westenddev.DefaultConfig() + config := DefaultTestWestendDevConfig(t) genFile := NewTestGenesisRawFile(t, config) config.ChainSpec = genFile - config.BasePath = t.TempDir() err := InitNode(config) require.NoError(t, err) @@ -323,12 +318,11 @@ func TestNewNode_Authority(t *testing.T) { } func TestStartStopNode(t *testing.T) { - config := westenddev.DefaultConfig() + config := DefaultTestWestendDevConfig(t) genFile := NewTestGenesisRawFile(t, config) config.ChainSpec = genFile - config.BasePath = t.TempDir() config.Core.GrandpaAuthority = false config.Core.BabeAuthority = false @@ -361,7 +355,7 @@ func TestStartStopNode(t *testing.T) { } func TestInitNode_LoadStorageRoot(t *testing.T) { - config := westenddev.DefaultConfig() + config := DefaultTestWestendDevConfig(t) genPath := newTestGenesisAndRuntime(t) @@ -369,7 +363,6 @@ func TestInitNode_LoadStorageRoot(t *testing.T) { config.Core.BabeAuthority = false config.Core.GrandpaAuthority = false config.ChainSpec = genPath - config.BasePath = t.TempDir() gen, err := genesis.NewGenesisFromJSONRaw(genPath) require.NoError(t, err) @@ -410,7 +403,7 @@ func balanceKey(t *testing.T, publicKey [32]byte) (storageTrieKey []byte) { } func TestInitNode_LoadBalances(t *testing.T) { - config := westenddev.DefaultConfig() + config := DefaultTestWestendDevConfig(t) genPath := newTestGenesisAndRuntime(t) @@ -418,7 +411,6 @@ func TestInitNode_LoadBalances(t *testing.T) { config.Core.BabeAuthority = false config.Core.GrandpaAuthority = false config.ChainSpec = genPath - config.BasePath = t.TempDir() err := InitNode(config) require.NoError(t, err) @@ -451,14 +443,13 @@ func TestInitNode_LoadBalances(t *testing.T) { func TestNode_PersistGlobalName_WhenInitialize(t *testing.T) { globalName := RandomNodeName() - config := westenddev.DefaultConfig() + config := DefaultTestWestendDevConfig(t) config.Name = globalName config.Core.Role = common.FullNodeRole config.Core.BabeAuthority = false config.Core.GrandpaAuthority = false config.ChainSpec = newTestGenesisAndRuntime(t) - config.BasePath = t.TempDir() err := InitNode(config) require.NoError(t, err) diff --git a/dot/node_test.go b/dot/node_test.go index fb9b6f7560..a566f8ee33 100644 --- a/dot/node_test.go +++ b/dot/node_test.go @@ -11,7 +11,7 @@ import ( "testing" "time" - westend_dev "github.com/ChainSafe/gossamer/chain/westend-dev" + westenddev "github.com/ChainSafe/gossamer/chain/westend-dev" cfg "github.com/ChainSafe/gossamer/config" "github.com/ChainSafe/gossamer/dot/network" @@ -27,10 +27,16 @@ import ( "github.com/stretchr/testify/require" ) +func DefaultTestWestendDevConfig(t *testing.T) *cfg.Config { + config := westenddev.DefaultConfig() + config.BasePath = t.TempDir() + + return config +} + func TestInitNode(t *testing.T) { - config := westend_dev.DefaultConfig() + config := DefaultTestWestendDevConfig(t) config.ChainSpec = NewTestGenesisRawFile(t, config) - config.BasePath = t.TempDir() tests := []struct { name string config *cfg.Config @@ -129,10 +135,9 @@ func setConfigTestDefaults(t *testing.T, cfg *network.Config) { } func TestNodeInitialized(t *testing.T) { - config := westend_dev.DefaultConfig() + config := DefaultTestWestendDevConfig(t) genFile := NewTestGenesisRawFile(t, config) config.ChainSpec = genFile - config.BasePath = t.TempDir() nodeInstance := nodeBuilder{} err := nodeInstance.initNode(config) diff --git a/dot/services_integration_test.go b/dot/services_integration_test.go index 6750742428..510f2f746d 100644 --- a/dot/services_integration_test.go +++ b/dot/services_integration_test.go @@ -10,7 +10,6 @@ import ( "testing" "time" - westend_dev "github.com/ChainSafe/gossamer/chain/westend-dev" cfg "github.com/ChainSafe/gossamer/config" core "github.com/ChainSafe/gossamer/dot/core" @@ -39,7 +38,7 @@ func Test_nodeBuilder_createBABEService(t *testing.T) { ctrl := gomock.NewController(t) - config := westend_dev.DefaultConfig() + config := DefaultTestWestendDevConfig(t) ks := keystore.NewGlobalKeystore() ks2 := keystore.NewGlobalKeystore() @@ -159,7 +158,7 @@ func Test_nodeBuilder_createCoreService(t *testing.T) { t.Run(tt.name, func(t *testing.T) { t.Parallel() - config := westend_dev.DefaultConfig() + config := DefaultTestWestendDevConfig(t) ctrl := gomock.NewController(t) stateSrvc := newStateService(t, ctrl) @@ -200,7 +199,7 @@ func Test_nodeBuilder_createNetworkService(t *testing.T) { t.Parallel() ctrl := gomock.NewController(t) - config := westend_dev.DefaultConfig() + config := DefaultTestWestendDevConfig(t) stateSrvc := newStateService(t, ctrl) no := nodeBuilder{} got, err := no.createNetworkService(config, stateSrvc, nil) @@ -235,7 +234,7 @@ func Test_nodeBuilder_createRPCService(t *testing.T) { t.Run(tt.name, func(t *testing.T) { t.Parallel() - config := westend_dev.DefaultConfig() + config := DefaultTestWestendDevConfig(t) ctrl := gomock.NewController(t) stateSrvc := newStateService(t, ctrl) no := nodeBuilder{} @@ -289,7 +288,7 @@ func Test_nodeBuilder_createGRANDPAService(t *testing.T) { t.Run(tt.name, func(t *testing.T) { t.Parallel() - config := westend_dev.DefaultConfig() + config := DefaultTestWestendDevConfig(t) ctrl := gomock.NewController(t) stateSrvc := newStateService(t, ctrl) networkConfig := &network.Config{ @@ -316,7 +315,7 @@ func Test_nodeBuilder_createGRANDPAService(t *testing.T) { func Test_createRuntime(t *testing.T) { t.Parallel() - config := westend_dev.DefaultConfig() + config := DefaultTestWestendDevConfig(t) type args struct { config *cfg.Config @@ -394,7 +393,7 @@ func Test_nodeBuilder_newSyncService(t *testing.T) { t.Run(tt.name, func(t *testing.T) { t.Parallel() - config := westend_dev.DefaultConfig() + config := DefaultTestWestendDevConfig(t) ctrl := gomock.NewController(t) stateSrvc := newStateService(t, ctrl) no := nodeBuilder{} @@ -411,11 +410,10 @@ func Test_nodeBuilder_newSyncService(t *testing.T) { } func TestCreateStateService(t *testing.T) { - config := westend_dev.DefaultConfig() + config := DefaultTestWestendDevConfig(t) genFile := NewTestGenesisRawFile(t, config) config.ChainSpec = genFile - config.BasePath = t.TempDir() err := InitNode(config) require.NoError(t, err) @@ -475,14 +473,13 @@ func newStateServiceWithoutMock(t *testing.T) *state.Service { } func TestCreateCoreService(t *testing.T) { - config := westend_dev.DefaultConfig() + config := DefaultTestWestendDevConfig(t) genFile := NewTestGenesisRawFile(t, config) config.Core.Role = common.FullNodeRole config.Core.BabeAuthority = false config.Core.GrandpaAuthority = false config.ChainSpec = genFile - config.BasePath = t.TempDir() err := InitNode(config) require.NoError(t, err) @@ -506,12 +503,11 @@ func TestCreateCoreService(t *testing.T) { } func TestCreateBlockVerifier(t *testing.T) { - config := westend_dev.DefaultConfig() + config := DefaultTestWestendDevConfig(t) - genFile := newTestGenesisFile(t, config) + genFile := NewTestGenesisRawFile(t, config) config.ChainSpec = genFile - config.BasePath = t.TempDir() err := InitNode(config) require.NoError(t, err) @@ -525,12 +521,11 @@ func TestCreateBlockVerifier(t *testing.T) { } func TestCreateSyncService(t *testing.T) { - config := westend_dev.DefaultConfig() + config := DefaultTestWestendDevConfig(t) - genFile := newTestGenesisFile(t, config) + genFile := NewTestGenesisRawFile(t, config) config.ChainSpec = genFile - config.BasePath = t.TempDir() err := InitNode(config) require.NoError(t, err) @@ -554,12 +549,11 @@ func TestCreateSyncService(t *testing.T) { } func TestCreateNetworkService(t *testing.T) { - config := westend_dev.DefaultConfig() + config := DefaultTestWestendDevConfig(t) genFile := NewTestGenesisRawFile(t, config) config.ChainSpec = genFile - config.BasePath = t.TempDir() err := InitNode(config) require.NoError(t, err) @@ -573,7 +567,7 @@ func TestCreateNetworkService(t *testing.T) { } func TestCreateRPCService(t *testing.T) { - config := westend_dev.DefaultConfig() + config := DefaultTestWestendDevConfig(t) genFile := NewTestGenesisRawFile(t, config) @@ -581,7 +575,6 @@ func TestCreateRPCService(t *testing.T) { config.Core.BabeAuthority = false config.Core.GrandpaAuthority = false config.ChainSpec = genFile - config.BasePath = t.TempDir() err := InitNode(config) require.NoError(t, err) @@ -627,13 +620,12 @@ func TestCreateRPCService(t *testing.T) { } func TestCreateBABEService_Integration(t *testing.T) { - config := westend_dev.DefaultConfig() + config := DefaultTestWestendDevConfig(t) genFile := NewTestGenesisRawFile(t, config) config.Core.Role = common.FullNodeRole config.ChainSpec = genFile - config.BasePath = t.TempDir() err := InitNode(config) require.NoError(t, err) @@ -663,13 +655,12 @@ func TestCreateBABEService_Integration(t *testing.T) { } func TestCreateGrandpaService(t *testing.T) { - config := westend_dev.DefaultConfig() + config := DefaultTestWestendDevConfig(t) genFile := NewTestGenesisRawFile(t, config) config.Core.Role = common.AuthorityRole config.ChainSpec = genFile - config.BasePath = t.TempDir() err := InitNode(config) require.NoError(t, err) @@ -731,7 +722,7 @@ func TestNewWebSocketServer(t *testing.T) { expected: []byte(`{"jsonrpc":"2.0","result":2,"id":4}` + "\n")}, } - config := westend_dev.DefaultConfig() + config := DefaultTestWestendDevConfig(t) genFile := NewTestGenesisRawFile(t, config) @@ -743,7 +734,6 @@ func TestNewWebSocketServer(t *testing.T) { config.RPC.WSPort = 9546 config.RPC.WSExternal = true config.System.SystemName = "gossamer" - config.BasePath = t.TempDir() err := InitNode(config) require.NoError(t, err) @@ -830,13 +820,12 @@ func Test_createPprofService(t *testing.T) { } func Test_createDigestHandler(t *testing.T) { - config := westend_dev.DefaultConfig() + config := DefaultTestWestendDevConfig(t) genFile := NewTestGenesisRawFile(t, config) config.Core.Role = common.AuthorityRole config.ChainSpec = genFile - config.BasePath = t.TempDir() err := InitNode(config) require.NoError(t, err) diff --git a/dot/services_test.go b/dot/services_test.go index 9340a07975..ff34806185 100644 --- a/dot/services_test.go +++ b/dot/services_test.go @@ -6,8 +6,6 @@ package dot import ( "testing" - westend_dev "github.com/ChainSafe/gossamer/chain/westend-dev" - "github.com/ChainSafe/gossamer/dot/state" "github.com/ChainSafe/gossamer/dot/types" "github.com/ChainSafe/gossamer/internal/log" @@ -20,10 +18,9 @@ import ( ) func Test_createRuntimeStorage(t *testing.T) { - config := westend_dev.DefaultConfig() + config := DefaultTestWestendDevConfig(t) config.ChainSpec = NewTestGenesisRawFile(t, config) - config.BasePath = t.TempDir() builder := nodeBuilder{} err := builder.initNode(config) @@ -56,10 +53,9 @@ func Test_createRuntimeStorage(t *testing.T) { } func Test_createSystemService(t *testing.T) { - config := westend_dev.DefaultConfig() + config := DefaultTestWestendDevConfig(t) config.ChainSpec = NewTestGenesisRawFile(t, config) - config.BasePath = t.TempDir() builder := nodeBuilder{} err := builder.initNode(config) diff --git a/dot/utils_integration_test.go b/dot/utils_integration_test.go index 0998df26f8..7f4e0a67a3 100644 --- a/dot/utils_integration_test.go +++ b/dot/utils_integration_test.go @@ -8,18 +8,15 @@ package dot import ( "testing" - westend_dev "github.com/ChainSafe/gossamer/chain/westend-dev" - "github.com/ChainSafe/gossamer/lib/genesis" "github.com/ChainSafe/gossamer/lib/trie" "github.com/stretchr/testify/require" ) func TestTrieSnapshot(t *testing.T) { - config := westend_dev.DefaultConfig() + config := DefaultTestWestendDevConfig(t) genRawFile := NewTestGenesisRawFile(t, config) - config.BasePath = t.TempDir() genRaw, err := genesis.NewGenesisFromJSONRaw(genRawFile) require.NoError(t, err) diff --git a/dot/utils_test.go b/dot/utils_test.go index e74c79003d..43e344b4cd 100644 --- a/dot/utils_test.go +++ b/dot/utils_test.go @@ -5,13 +5,11 @@ package dot import ( "crypto/sha256" - "encoding/json" "fmt" "os" "path/filepath" "testing" - westend_dev "github.com/ChainSafe/gossamer/chain/westend-dev" cfg "github.com/ChainSafe/gossamer/config" "github.com/ChainSafe/gossamer/lib/genesis" "github.com/ChainSafe/gossamer/lib/utils" @@ -19,32 +17,6 @@ import ( "github.com/stretchr/testify/require" ) -// newTestGenesisFile returns a human-readable test genesis file using "westend-dev" human readable data -func newTestGenesisFile(t *testing.T, config *cfg.Config) (filename string) { - t.Helper() - - fp := utils.GetWestendDevRawGenesisPath(t) - westendDevGenesis, err := genesis.NewGenesisFromJSONRaw(fp) - require.NoError(t, err) - - gen := &genesis.Genesis{ - Name: config.Name, - ID: config.ID, - Bootnodes: config.Network.Bootnodes, - ProtocolID: config.Network.ProtocolID, - Genesis: westendDevGenesis.GenesisFields(), - } - - b, err := json.Marshal(gen) - require.NoError(t, err) - - filename = filepath.Join(t.TempDir(), "genesis.json") - err = os.WriteFile(filename, b, os.ModePerm) - require.NoError(t, err) - - return filename -} - func TestCreateJSONRawFile(t *testing.T) { type args struct { bs *BuildSpec @@ -91,14 +63,14 @@ func TestNewTestGenesisFile(t *testing.T) { name: "working_example", args: args{ t: t, - config: westend_dev.DefaultConfig(), + config: DefaultTestWestendDevConfig(t), }, want: &os.File{}, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - got := newTestGenesisFile(tt.args.t, tt.args.config) + got := NewTestGenesisRawFile(tt.args.t, tt.args.config) if tt.want != nil { assert.NotNil(t, got) } From d338bf77cf66835ac3c71175240b318c8cf946d8 Mon Sep 17 00:00:00 2001 From: Kishan Mohanbhai Sagathiya Date: Wed, 5 Jul 2023 02:41:23 +0530 Subject: [PATCH 2/2] fixing one more test --- dot/services_integration_test.go | 1 + 1 file changed, 1 insertion(+) diff --git a/dot/services_integration_test.go b/dot/services_integration_test.go index 63bc1dd6cf..3669d8b5e0 100644 --- a/dot/services_integration_test.go +++ b/dot/services_integration_test.go @@ -554,6 +554,7 @@ func TestCreateSyncService(t *testing.T) { networkService, err := network.NewService(&network.Config{ BlockState: stateSrvc.Block, + BasePath: config.BasePath, }) require.NoError(t, err)