Skip to content

Commit

Permalink
chore(tests): cache runtimes to work offline (#2577)
Browse files Browse the repository at this point in the history
- Download runtimes to ${TMP}/gossamer/runtimes/ depending on environment
- Do not remove runtime files and re-use them if found
- Merge `GetRuntimeVars` and `GetRuntimeBlob` into `GetRuntime`
- Improve HTTP fetching code for runtime wasms
- Remove `TestMain`-used `GetRuntimes` since we already have calls to `GetRuntime` wherever a runtime blob is needed.
  • Loading branch information
qdm12 authored Jun 9, 2022
1 parent e5c8cf5 commit 8582cb2
Show file tree
Hide file tree
Showing 13 changed files with 125 additions and 187 deletions.
5 changes: 2 additions & 3 deletions cmd/gossamer/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package main

import (
"context"
"encoding/hex"
"encoding/json"
"errors"
Expand Down Expand Up @@ -941,9 +942,7 @@ func TestGlobalNodeName_WhenNodeAlreadyHasStoredName(t *testing.T) {
cfg := newTestConfig(t)
cfg.Global.Name = globalName

runtimeFilePath := filepath.Join(t.TempDir(), "runtime")
_, testRuntimeURL := runtime.GetRuntimeVars(runtime.NODE_RUNTIME)
err := runtime.GetRuntimeBlob(runtimeFilePath, testRuntimeURL)
runtimeFilePath, err := runtime.GetRuntime(context.Background(), runtime.NODE_RUNTIME)
require.NoError(t, err)
runtimeData, err := os.ReadFile(runtimeFilePath)
require.NoError(t, err)
Expand Down
23 changes: 7 additions & 16 deletions dot/core/service_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package core

import (
"context"
"fmt"
"math/big"
"os"
Expand Down Expand Up @@ -134,20 +135,6 @@ func generateTestValidRemarkTxns(t *testing.T, pubKey []byte, accInfo types.Acco
return extBytes, rt
}

func TestMain(m *testing.M) {
wasmFilePaths, err := runtime.GenerateRuntimeWasmFile()
if err != nil {
log.Errorf("failed to generate runtime wasm file: %s", err)
os.Exit(1)
}

// Start all tests
code := m.Run()

runtime.RemoveFiles(wasmFilePaths)
os.Exit(code)
}

func TestStartService(t *testing.T) {
s := NewTestService(t, nil)

Expand Down Expand Up @@ -695,7 +682,9 @@ func TestService_HandleRuntimeChanges(t *testing.T) {
func TestService_HandleCodeSubstitutes(t *testing.T) {
s := NewTestService(t, nil)

testRuntime, err := os.ReadFile(runtime.POLKADOT_RUNTIME_FP)
runtimeFilepath, err := runtime.GetRuntime(context.Background(), runtime.POLKADOT_RUNTIME)
require.NoError(t, err)
testRuntime, err := os.ReadFile(runtimeFilepath)
require.NoError(t, err)

// hash for known test code substitution
Expand Down Expand Up @@ -745,7 +734,9 @@ func TestService_HandleRuntimeChangesAfterCodeSubstitutes(t *testing.T) {
require.NoError(t, err)
require.Equal(t, codeHashBefore, parentRt.GetCodeHash()) // codeHash should remain unchanged after code substitute

testRuntime, err := os.ReadFile(runtime.POLKADOT_RUNTIME_FP)
runtimeFilepath, err := runtime.GetRuntime(context.Background(), runtime.POLKADOT_RUNTIME)
require.NoError(t, err)
testRuntime, err := os.ReadFile(runtimeFilepath)
require.NoError(t, err)

ts, err = s.storageState.TrieState(nil)
Expand Down
5 changes: 2 additions & 3 deletions dot/node_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package dot

import (
"context"
"encoding/hex"
"encoding/json"
"os"
Expand Down Expand Up @@ -271,9 +272,7 @@ func TestNode_PersistGlobalName_WhenInitialize(t *testing.T) {
// newTestGenesisAndRuntime create a new test runtime and a new test genesis
// file with the test runtime stored in raw data and returns the genesis file
func newTestGenesisAndRuntime(t *testing.T) (filename string) {
runtimeFilePath := filepath.Join(t.TempDir(), "runtime")
_, testRuntimeURL := runtime.GetRuntimeVars(runtime.NODE_RUNTIME)
err := runtime.GetRuntimeBlob(runtimeFilePath, testRuntimeURL)
runtimeFilePath, err := runtime.GetRuntime(context.Background(), runtime.NODE_RUNTIME)
require.NoError(t, err)
runtimeData, err := os.ReadFile(runtimeFilePath)
require.NoError(t, err)
Expand Down
22 changes: 2 additions & 20 deletions dot/rpc/modules/author_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package modules

import (
"context"
"errors"
"fmt"
"os"
Expand Down Expand Up @@ -53,16 +54,11 @@ func useInstanceFromGenesis(t *testing.T, rtStorage *storage.TrieState) (instanc
}

func useInstanceFromRuntimeV0910(t *testing.T, rtStorage *storage.TrieState) (instance runtime.Instance) {
testRuntimeFilePath, testRuntimeURL := runtime.GetRuntimeVars(runtime.POLKADOT_RUNTIME_v0910)
err := runtime.GetRuntimeBlob(testRuntimeFilePath, testRuntimeURL)
testRuntimeFilePath, err := runtime.GetRuntime(context.Background(), runtime.POLKADOT_RUNTIME_v0910)
require.NoError(t, err)

bytes, err := os.ReadFile(testRuntimeFilePath)
require.NoError(t, err)

err = runtime.RemoveFiles([]string{testRuntimeFilePath})
require.NoError(t, err)

rtStorage.Set(common.CodeKey, bytes)

cfg := &wasmer.Config{}
Expand All @@ -82,20 +78,6 @@ func useInstanceFromRuntimeV0910(t *testing.T, rtStorage *storage.TrieState) (in
return runtimeInstance
}

func TestMain(m *testing.M) {
wasmFilePaths, err := runtime.GenerateRuntimeWasmFile()
if err != nil {
log.Errorf("failed to generate runtime wasm file: %s", err)
os.Exit(1)
}

// Start all tests
code := m.Run()

runtime.RemoveFiles(wasmFilePaths)
os.Exit(code)
}

func TestAuthorModule_Pending_Integration(t *testing.T) {
t.Parallel()
integrationTestController := setupStateAndRuntime(t, t.TempDir(), nil)
Expand Down
8 changes: 3 additions & 5 deletions dot/rpc/subscription/listeners_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
package subscription

import (
"context"
"encoding/json"
"fmt"
"log"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"strings"
"testing"
"time"
Expand Down Expand Up @@ -352,11 +352,9 @@ func TestRuntimeChannelListener_Listen(t *testing.T) {
expectedInitialResponse.Params.Result = expectedInitialVersion

instance := wasmer.NewTestInstance(t, runtime.NODE_RUNTIME)
err := runtime.GetRuntimeBlob(runtime.POLKADOT_RUNTIME_FP, runtime.POLKADOT_RUNTIME_URL)
polkadotRuntimeFilepath, err := runtime.GetRuntime(context.Background(), runtime.POLKADOT_RUNTIME)
require.NoError(t, err)
fp, err := filepath.Abs(runtime.POLKADOT_RUNTIME_FP)
require.NoError(t, err)
code, err := os.ReadFile(fp)
code, err := os.ReadFile(polkadotRuntimeFilepath)
require.NoError(t, err)
version, err := instance.CheckRuntimeVersion(code)
require.NoError(t, err)
Expand Down
15 changes: 0 additions & 15 deletions dot/sync/syncer_integeration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ package sync

import (
"errors"
"os"
"path/filepath"
"testing"

Expand All @@ -28,20 +27,6 @@ import (
"github.com/stretchr/testify/require"
)

func TestMain(m *testing.M) {
wasmFilePaths, err := runtime.GenerateRuntimeWasmFile()
if err != nil {
log.Errorf("failed to generate runtime wasm file: %s", err)
os.Exit(1)
}

// Start all tests
code := m.Run()

runtime.RemoveFiles(wasmFilePaths)
os.Exit(code)
}

func newMockFinalityGadget() *mocks.FinalityGadget {
m := new(mocks.FinalityGadget)
// using []uint8 instead of []byte: https://github.com/stretchr/testify/pull/969
Expand Down
17 changes: 0 additions & 17 deletions lib/babe/babe_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
package babe

import (
"os"
"path/filepath"
"testing"
"time"
Expand Down Expand Up @@ -153,22 +152,6 @@ func createTestService(t *testing.T, cfg *ServiceConfig) *Service {
return babeService
}

func TestMain(m *testing.M) {
wasmFilePaths, err := runtime.GenerateRuntimeWasmFile()
if err != nil {
log.Errorf("failed to generate runtime wasm file: %s", err)
os.Exit(1)
}

logger = log.NewFromGlobal(log.SetLevel(defaultTestLogLvl))

// Start all tests
code := m.Run()

runtime.RemoveFiles(wasmFilePaths)
os.Exit(code)
}

func newTestServiceSetupParameters(t *testing.T) (*Service, *state.EpochState, *types.BabeConfiguration) {
ctrl := gomock.NewController(t)
telemetryMock := NewMockClient(ctrl)
Expand Down
2 changes: 0 additions & 2 deletions lib/runtime/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import (
"github.com/ChainSafe/gossamer/lib/common"
)

var runtimes = []string{HOST_API_TEST_RUNTIME, POLKADOT_RUNTIME, POLKADOT_RUNTIME_v0917, NODE_RUNTIME, DEV_RUNTIME}

//nolint:revive
const (
// v0.9 substrate runtime
Expand Down
22 changes: 8 additions & 14 deletions lib/runtime/life/test_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
package life

import (
"path/filepath"
"context"
"testing"

"github.com/ChainSafe/gossamer/internal/log"
Expand All @@ -20,30 +20,24 @@ var DefaultTestLogLvl = log.Info

// newTestInstance will create a new runtime instance using the given target runtime
func newTestInstance(t *testing.T, targetRuntime string) *Instance {
return newTestInstanceWithTrie(t, runtime.HOST_API_TEST_RUNTIME, nil, DefaultTestLogLvl)
return newTestInstanceWithTrie(t, targetRuntime, nil, DefaultTestLogLvl)
}

// newTestInstanceWithTrie will create a new runtime instance with the supplied trie as the storage
func newTestInstanceWithTrie(t *testing.T, targetRuntime string, tt *trie.Trie, lvl log.Level) *Instance {
fp, cfg := setupConfig(t, targetRuntime, tt, lvl, 0)
r, err := NewInstanceFromFile(fp, cfg)
testRuntimeFilePath, err := runtime.GetRuntime(context.Background(), targetRuntime)
require.NoError(t, err)
cfg := setupConfig(t, tt, lvl, 0)
r, err := NewInstanceFromFile(testRuntimeFilePath, cfg)
require.NoError(t, err, "Got error when trying to create new VM", "targetRuntime", targetRuntime)
require.NotNil(t, r, "Could not create new VM instance", "targetRuntime", targetRuntime)
return r
}

func setupConfig(t *testing.T, targetRuntime string, tt *trie.Trie, lvl log.Level, role byte) (string, *Config) {
testRuntimeFilePath, testRuntimeURL := runtime.GetRuntimeVars(targetRuntime)

err := runtime.GetRuntimeBlob(testRuntimeFilePath, testRuntimeURL)
require.Nil(t, err, "Fail: could not get runtime", "targetRuntime", targetRuntime)

func setupConfig(t *testing.T, tt *trie.Trie, lvl log.Level, role byte) *Config {
s, err := storage.NewTrieState(tt)
require.NoError(t, err)

fp, err := filepath.Abs(testRuntimeFilePath)
require.Nil(t, err, "could not create testRuntimeFilePath", "targetRuntime", targetRuntime)

ns := runtime.NodeStorage{
LocalStorage: runtime.NewInMemoryDB(t),
PersistentStorage: runtime.NewInMemoryDB(t), // we're using a local storage here since this is a test runtime
Expand All @@ -56,5 +50,5 @@ func setupConfig(t *testing.T, targetRuntime string, tt *trie.Trie, lvl log.Leve
cfg.Network = new(runtime.TestRuntimeNetwork)
cfg.Role = role
cfg.Resolver = new(Resolver)
return fp, cfg
return cfg
}
Loading

0 comments on commit 8582cb2

Please sign in to comment.