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 #2112

Merged
merged 12 commits into from
Apr 4, 2022
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
45 changes: 40 additions & 5 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 @@ -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
1 change: 0 additions & 1 deletion cmd/gossamer/flags_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (

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

"github.com/stretchr/testify/require"
"github.com/urfave/cli"
)
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