Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

testing(dot): create dot unit tests #2446

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/integration-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ jobs:
matrix:
packages:
[
github.com/ChainSafe/gossamer/dot,
github.com/ChainSafe/gossamer/dot/core,
github.com/ChainSafe/gossamer/dot/rpc/modules,
github.com/ChainSafe/gossamer/lib/babe,
Expand Down
77 changes: 70 additions & 7 deletions cmd/gossamer/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@
package main

import (
"encoding/hex"
"encoding/json"
"errors"
"io"
"os"
"path/filepath"
"testing"
"time"

Expand All @@ -17,8 +21,8 @@ import (
"github.com/ChainSafe/gossamer/dot/types"
"github.com/ChainSafe/gossamer/internal/log"
"github.com/ChainSafe/gossamer/lib/genesis"
"github.com/ChainSafe/gossamer/lib/runtime"
"github.com/ChainSafe/gossamer/lib/utils"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/urfave/cli"
Expand Down Expand Up @@ -767,7 +771,7 @@ func TestRPCConfigFromFlags(t *testing.T) {
// TestUpdateConfigFromGenesisJSON tests updateDotConfigFromGenesisJSON
func TestUpdateConfigFromGenesisJSON(t *testing.T) {
testCfg, testCfgFile := newTestConfigWithFile(t)
genFile := dot.NewTestGenesisRawFile(t, testCfg)
genFile := newTestGenesisRawFile(t, testCfg)

ctx, err := newTestContext(
t.Name(),
Expand Down Expand Up @@ -871,7 +875,7 @@ func TestUpdateConfigFromGenesisJSON_Default(t *testing.T) {

func TestUpdateConfigFromGenesisData(t *testing.T) {
testCfg, testCfgFile := newTestConfigWithFile(t)
genFile := dot.NewTestGenesisRawFile(t, testCfg)
genFile := newTestGenesisRawFile(t, testCfg)

ctx, err := newTestContext(
t.Name(),
Expand Down Expand Up @@ -948,19 +952,50 @@ func TestGlobalNodeName_WhenNodeAlreadyHasStoredName(t *testing.T) {
// Initialise a node with a random name
globalName := dot.RandomNodeName()

cfg := dot.NewTestConfig(t)
cfg := newTestConfig(t)
cfg.Global.Name = globalName
require.NotNil(t, cfg)

genPath := dot.NewTestGenesisAndRuntime(t)
require.NotNil(t, genPath)
runtimeFilePath := filepath.Join(t.TempDir(), "runtime")
_, testRuntimeURL := runtime.GetRuntimeVars(runtime.NODE_RUNTIME)
err := runtime.GetRuntimeBlob(runtimeFilePath, testRuntimeURL)
require.NoError(t, err)
runtimeData, err := os.ReadFile(runtimeFilePath)
require.NoError(t, err)

fp := utils.GetGssmrGenesisRawPathTest(t)

gssmrGen, err := genesis.NewGenesisFromJSONRaw(fp)
require.NoError(t, err)

gen := &genesis.Genesis{
Name: "test",
ID: "test",
Bootnodes: []string(nil),
ProtocolID: "/gossamer/test/0",
Genesis: gssmrGen.GenesisFields(),
}

gen.Genesis.Raw = map[string]map[string]string{
"top": {
"0x3a636f6465": "0x" + hex.EncodeToString(runtimeData),
"0xcf722c0832b5231d35e29f319ff27389f5032bfc7bfc3ba5ed7839f2042fb99f": "0x0000000000000001",
},
}

genData, err := json.Marshal(gen)
require.NoError(t, err)

genPath := filepath.Join(t.TempDir(), "genesis.json")
err = os.WriteFile(genPath, genData, os.ModePerm)
require.NoError(t, err)

cfg.Core.Roles = types.FullNodeRole
cfg.Core.BabeAuthority = false
cfg.Core.GrandpaAuthority = false
cfg.Init.Genesis = genPath

err := dot.InitNode(cfg)
err = dot.InitNode(cfg)
require.NoError(t, err)

// call another command and test the name
Expand Down Expand Up @@ -1302,3 +1337,31 @@ func Test_setLogConfig(t *testing.T) {
})
}
}

// newTestGenesisRawFile returns a test genesis file using "gssmr" raw data
func newTestGenesisRawFile(t *testing.T, cfg *dot.Config) (filename string) {
t.Helper()

filename = filepath.Join(t.TempDir(), "genesis.json")

fp := utils.GetGssmrGenesisRawPathTest(t)

gssmrGen, err := genesis.NewGenesisFromJSONRaw(fp)
require.NoError(t, err)

gen := &genesis.Genesis{
Name: cfg.Global.Name,
ID: cfg.Global.ID,
Bootnodes: cfg.Network.Bootnodes,
ProtocolID: cfg.Network.ProtocolID,
Genesis: gssmrGen.GenesisFields(),
}

b, err := json.Marshal(gen)
require.NoError(t, err)

err = os.WriteFile(filename, b, os.ModePerm)
require.NoError(t, err)

return filename
}
2 changes: 1 addition & 1 deletion cmd/gossamer/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
func TestExportCommand(t *testing.T) {
testCfg, testConfigFile := newTestConfigWithFile(t)
testDir := testCfg.Global.BasePath
genFile := dot.NewTestGenesisRawFile(t, testCfg)
genFile := newTestGenesisRawFile(t, testCfg)

testApp := cli.NewApp()
testApp.Writer = io.Discard
Expand Down
4 changes: 1 addition & 3 deletions cmd/gossamer/flags_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,14 @@ import (
"testing"

"github.com/ChainSafe/gossamer/chain/dev"
"github.com/ChainSafe/gossamer/dot"

"github.com/stretchr/testify/require"
"github.com/urfave/cli"
)

// TestFixFlagOrder tests the FixFlagOrder method
func TestFixFlagOrder(t *testing.T) {
testCfg, testConfig := newTestConfigWithFile(t)
genFile := dot.NewTestGenesisRawFile(t, testCfg)
genFile := newTestGenesisRawFile(t, testCfg)

testApp := cli.NewApp()
testApp.Writer = io.Discard
Expand Down
2 changes: 1 addition & 1 deletion cmd/gossamer/toml_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func TestLoadConfig(t *testing.T) {
cfg, cfgFile := newTestConfigWithFile(t)
require.NotNil(t, cfg)

genFile := dot.NewTestGenesisRawFile(t, cfg)
genFile := newTestGenesisRawFile(t, cfg)

cfg.Init.Genesis = genFile

Expand Down
3 changes: 1 addition & 2 deletions dot/build_spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,7 @@ func WriteGenesisSpecFile(data []byte, fp string) error {
return err
}

writeConfig(data, fp)
return nil
return os.WriteFile(fp, data, 0600)
}

// BuildFromDB builds a BuildSpec from the DB located at path
Expand Down
28 changes: 7 additions & 21 deletions dot/build_spec_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ package dot

import (
"encoding/json"
"fmt"
"os"
"path/filepath"
"testing"
Expand All @@ -20,7 +19,7 @@ import (
// hex encoding for ":code", used as key for code is raw genesis files.
const codeHex = "0x3a636f6465"

func TestBuildFromGenesis(t *testing.T) {
func TestBuildFromGenesis_Integration(t *testing.T) {
t.Parallel()

file := genesis.CreateTestGenesisJSONFile(t, false)
Expand Down Expand Up @@ -69,20 +68,7 @@ func TestBuildFromGenesis_WhenGenesisDoesNotExists(t *testing.T) {
require.ErrorIs(t, err, os.ErrNotExist)
}

func TestWriteGenesisSpecFileWhenFileAlreadyExists(t *testing.T) {
t.Parallel()

filePath := filepath.Join(t.TempDir(), "genesis.raw")
someBytes := []byte("Testing some bytes")
err := WriteGenesisSpecFile(someBytes, filePath)

require.EqualError(t, err,
fmt.Sprintf("file %s already exists, rename to avoid overwriting", filePath))
}

func TestWriteGenesisSpecFile(t *testing.T) {
t.Parallel()

func TestWriteGenesisSpecFile_Integration(t *testing.T) {
cfg := NewTestConfig(t)
cfg.Init.Genesis = utils.GetGssmrGenesisRawPathTest(t)

Expand All @@ -101,11 +87,13 @@ func TestWriteGenesisSpecFile(t *testing.T) {
tmpFile := filepath.Join(t.TempDir(), "unique-raw-genesis.json")
err = WriteGenesisSpecFile(data, tmpFile)
require.NoError(t, err)
require.FileExists(t, tmpFile)

file, err := os.Open(tmpFile)
require.NoError(t, err)
defer file.Close()
t.Cleanup(func() {
err := file.Close()
require.NoError(t, err)
})

gen := new(genesis.Genesis)

Expand All @@ -118,9 +106,7 @@ func TestWriteGenesisSpecFile(t *testing.T) {

}

func TestBuildFromDB(t *testing.T) {
t.Parallel()

func TestBuildFromDB_Integration(t *testing.T) {
// setup expected
cfg := NewTestConfig(t)
cfg.Init.Genesis = utils.GetGssmrGenesisRawPathTest(t)
Expand Down
Loading