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

Closed
wants to merge 53 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
53 commits
Select commit Hold shift + click to select a range
6b61943
move build_spec_tests
edwardmack Nov 2, 2021
d12b7c2
added file
edwardmack Nov 2, 2021
2346cd9
move config_tests
edwardmack Nov 2, 2021
9ac48f4
move import_tests
edwardmack Nov 2, 2021
f10a4db
move node_tests
edwardmack Nov 2, 2021
8cafe33
move services_tests
edwardmack Nov 2, 2021
9042ce9
fix name
edwardmack Nov 2, 2021
1254342
move utils_tests
edwardmack Nov 2, 2021
035398c
Merge branch 'development' into ed/dot_tests_to_integration
edwardmack Nov 3, 2021
974b3b2
add build_spec unit tests
edwardmack Nov 3, 2021
e206c0c
Merge branch 'development' into ed/create_dot_unit_tests
edwardmack Nov 5, 2021
73d870a
refactor tests
edwardmack Nov 5, 2021
84321d7
lint
edwardmack Nov 5, 2021
28ce808
generate tests stubs
edwardmack Nov 6, 2021
6b3cd93
implement core config unit tests
edwardmack Nov 8, 2021
357dd19
add t.Parallel to tests
edwardmack Nov 8, 2021
b43f571
implement tests for import.go
edwardmack Nov 9, 2021
49c3add
add tests for node.go
edwardmack Nov 9, 2021
cecc641
implement service tests
edwardmack Nov 10, 2021
d5cee6c
Merge branch 'development' into ed/create_dot_unit_tests
edwardmack Nov 11, 2021
3cfdf93
implement services tests
edwardmack Nov 11, 2021
1c0afe5
implement utils tests
edwardmack Nov 12, 2021
0a95194
add to tests
edwardmack Nov 12, 2021
f1034a3
Merge branch 'development' into ed/create_dot_unit_tests
edwardmack Nov 12, 2021
8768a11
clean-up tests
edwardmack Nov 12, 2021
829a383
clean-up created test files
edwardmack Nov 12, 2021
5f871e3
address PR comments, clean-up
edwardmack Nov 12, 2021
487cc39
implement NewNodeB
edwardmack Nov 22, 2021
64f1d86
implement interface for new node
Nov 29, 2021
91dc01b
implement inteface functions for newNodeC
edwardmack Nov 29, 2021
3a952a6
Merge branch 'development' into ed/create_dot_unit_tests
edwardmack Dec 1, 2021
65f9df8
add mocks for newNodeIface
edwardmack Dec 2, 2021
bc693cb
Merge branch 'development' into ed/create_dot_unit_tests
edwardmack Dec 2, 2021
6f16011
Merge branch 'development' into ed/create_dot_unit_tests
edwardmack Dec 3, 2021
800e0e9
implement mocks for tests
edwardmack Dec 3, 2021
89366c3
chore(deps): bump actions/setup-node from 2.4.1 to 2.5.0 (#2104)
dependabot[bot] Dec 6, 2021
2a18498
lint
edwardmack Dec 6, 2021
2842ba7
feat(wasmer/crypto): move sig verifier to crypto pkg (#2057)
1garo Dec 6, 2021
219ca79
remove testing NewNodeB
edwardmack Dec 6, 2021
e651076
Merge branch 'development' into ed/create_dot_unit_tests
edwardmack Dec 6, 2021
d972258
change init keystore
edwardmack Dec 6, 2021
cdbe54c
remove newNodeC
edwardmack Dec 6, 2021
1d05a8d
remove gomocks.Any from tests
edwardmack Dec 7, 2021
eeb1115
remove naked return
edwardmack Dec 7, 2021
c84ca41
rename e to closeErr
edwardmack Dec 7, 2021
ecab259
Merge branch 'development' into ed/create_dot_unit_tests
edwardmack Dec 8, 2021
2830f7b
WIP save
edwardmack Dec 8, 2021
1ef3897
fix Test_createGRANDPAService test to use mocks
edwardmack Dec 8, 2021
29d8975
fix failing tests
edwardmack Dec 9, 2021
2880f52
add to comment
edwardmack Dec 9, 2021
c71c873
Merge branch 'development' into ed/create_dot_unit_tests
edwardmack Dec 9, 2021
5665356
test commit
edwardmack Dec 9, 2021
f3936af
test commit
edwardmack Dec 9, 2021
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
4 changes: 2 additions & 2 deletions dot/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ func NodeInitialized(basepath string) bool {
}

// LoadGlobalNodeName returns the stored global node name from database
func LoadGlobalNodeName(basepath string) (nodename string, err error) {
func LoadGlobalNodeName(basepath string) (string, error) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please let's use named returned values, it's really useful for readers/documentation.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated.

// initialise database using data directory
db, err := utils.SetupDatabase(basepath, false)
if err != nil {
Expand All @@ -196,7 +196,7 @@ func LoadGlobalNodeName(basepath string) (nodename string, err error) {
}()

basestate := state.NewBaseState(db)
nodename, err = basestate.LoadNodeGlobalName()
nodename, err := basestate.LoadNodeGlobalName()
if err != nil {
logger.Warn(
"failed to load global node name",
Expand Down
114 changes: 94 additions & 20 deletions dot/node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,13 @@
package dot

import (
"errors"
"fmt"
"reflect"
"github.com/ChainSafe/gossamer/dot/types"
"github.com/ChainSafe/gossamer/lib/common"
"github.com/ChainSafe/gossamer/lib/utils"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"sync"
"testing"

Expand All @@ -36,46 +41,98 @@ func TestInitNode(t *testing.T) {
tests := []struct {
name string
args args
wantErr bool
err error
}{
// TODO: Add test cases.
{
name: "no arguments",
args: args{cfg: GssmrConfig()},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := InitNode(tt.args.cfg); (err != nil) != tt.wantErr {
t.Errorf("InitNode() error = %v, wantErr %v", err, tt.wantErr)
// todo (ed) deal with file path to test this
fmt.Printf("gen %v\n", tt.args.cfg.Init.Genesis)
err := InitNode(tt.args.cfg)

if tt.err != nil {
assert.EqualError(t, err, tt.err.Error())
} else {
assert.NoError(t, err)
}
})
}
}

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

// initialise database using data directory
basePath := utils.NewTestBasePath(t, "tmpBase")
db, err := utils.SetupDatabase(basePath, false)
require.NoError(t, err)

basestate := state.NewBaseState(db)
basestate.Put(common.NodeNameKey, []byte(`nodeName`))

err = db.Close()
require.NoError(t, err)

type args struct {
basepath string
}
tests := []struct {
name string
args args
wantNodename string
wantErr bool
err error
}{
// TODO: Add test cases.
{
name: "working example",
args: args{basepath: basePath},
wantNodename: "nodeName",
},
{
name: "no arguments",
err: errors.New("Key not found"),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
gotNodename, err := LoadGlobalNodeName(tt.args.basepath)
if (err != nil) != tt.wantErr {
t.Errorf("LoadGlobalNodeName() error = %v, wantErr %v", err, tt.wantErr)
return
}
if gotNodename != tt.wantNodename {
t.Errorf("LoadGlobalNodeName() gotNodename = %v, want %v", gotNodename, tt.wantNodename)
if tt.err != nil {
assert.EqualError(t, err, tt.err.Error())
} else {
assert.NoError(t, err)
}

assert.Equal(t, tt.wantNodename, gotNodename)
})
}
}

func TestNewNode(t *testing.T) {
cfg := NewTestConfig(t)
require.NotNil(t, cfg)

genFile := NewTestGenesisRawFile(t, cfg)
require.NotNil(t, genFile)

defer utils.RemoveTestDir(t)

cfg.Init.Genesis = genFile.Name()

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

ks := keystore.NewGlobalKeystore()
err = keystore.LoadKeystore("alice", ks.Gran)
require.NoError(t, err)
err = keystore.LoadKeystore("alice", ks.Babe)
require.NoError(t, err)

cfg.Core.Roles = types.FullNodeRole


type args struct {
cfg *Config
ks *keystore.GlobalKeystore
Expand All @@ -85,19 +142,36 @@ func TestNewNode(t *testing.T) {
name string
args args
want *Node
wantErr bool
err error
}{
// TODO: Add test cases.
{
name: "missing keystore",
args: args{
cfg: cfg,
},
err: errors.New("failed to create core service: cannot have nil keystore"),
},
// todo (ed) this second test fails with; failed to create state service: failed to start state service: Cannot acquire directory lock on "/home/emack/projects/ChainSafe/gossamer/dot/test_data/TestNewNode/db". Another process is using this Badger database.: resource temporarily unavailable
{
name: "working example",
args: args{
cfg: cfg,
ks: ks,
},
want: &Node{Name: "Gossamer"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := NewNode(tt.args.cfg, tt.args.ks, tt.args.stopFunc)
if (err != nil) != tt.wantErr {
t.Errorf("NewNode() error = %v, wantErr %v", err, tt.wantErr)
return
if tt.err != nil {
assert.EqualError(t, err, tt.err.Error())
} else {
assert.NoError(t, err)
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("NewNode() got = %v, want %v", got, tt.want)

if tt.want != nil {
assert.Equal(t, tt.want.Name, got.Name)
}
})
}
Expand Down