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

test: Basic unit testing of node package #503

Merged
merged 5 commits into from
Jun 7, 2022
Merged
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
129 changes: 129 additions & 0 deletions node/node_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
// Copyright 2022 Democratized Data Foundation
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.

package node

import (
"context"
"testing"

badger "github.com/dgraph-io/badger/v3"
"github.com/sourcenetwork/defradb/client"
badgerds "github.com/sourcenetwork/defradb/datastore/badger/v3"
"github.com/sourcenetwork/defradb/db"
"github.com/stretchr/testify/assert"
"github.com/textileio/go-threads/broadcast"
)

// Node.Boostrap is not tested because the underlying, *ipfslite.Peer.Bootstrap is a best-effort function.

func FixtureNewMemoryDBWithBroadcaster(t *testing.T) (client.DB, *broadcast.Broadcaster) {
var database client.DB
var options []db.Option
var busBufferSize = 100
ctx := context.Background()
bs := broadcast.NewBroadcaster(busBufferSize)
options = append(options, db.WithBroadcaster(bs))
opts := badgerds.Options{Options: badger.DefaultOptions("").WithInMemory(true)}
rootstore, err := badgerds.NewDatastore("", &opts)
assert.NoError(t, err)
database, err = db.NewDB(ctx, rootstore, options...)
assert.NoError(t, err)
return database, bs
}

func TestNewNode(t *testing.T) {
db, bs := FixtureNewMemoryDBWithBroadcaster(t)
_, err := NewNode(
context.Background(),
db,
bs,
// DataPath() is a required option with the current implementation of key management
DataPath(t.TempDir()),
orpheuslummis marked this conversation as resolved.
Show resolved Hide resolved
)
assert.NoError(t, err)
}

func TestNewNodeNoPubSub(t *testing.T) {
db, bs := FixtureNewMemoryDBWithBroadcaster(t)
n, err := NewNode(
context.Background(),
db,
bs,
WithPubSub(false),
// DataPath() is a required option with the current implementation of key management
DataPath(t.TempDir()),
)
assert.NoError(t, err)
assert.Nil(t, n.pubsub)
}

func TestNewNodeWithPubSub(t *testing.T) {
db, bs := FixtureNewMemoryDBWithBroadcaster(t)
ctx := context.Background()
n, err := NewNode(
ctx,
db,
bs,
WithPubSub(true),
// DataPath() is a required option with the current implementation of key management
DataPath(t.TempDir()),
)
assert.NoError(t, err)
// overly simple check of validity of pubsub, avoiding the process of creating a PubSub
assert.NotNil(t, n.pubsub)
}

func TestNewNodeWithPubSubFailsWithoutDataPath(t *testing.T) {
db, bs := FixtureNewMemoryDBWithBroadcaster(t)
ctx := context.Background()
_, err := NewNode(
ctx,
db,
bs,
WithPubSub(true),
)
assert.EqualError(t, err, "1 error occurred:\n\t* mkdir : no such file or directory\n\n")
}

func TestNodeClose(t *testing.T) {
db, bs := FixtureNewMemoryDBWithBroadcaster(t)
n, err := NewNode(
context.Background(),
db,
bs,
// DataPath() is a required option with the current implementation of key management
DataPath(t.TempDir()),
)
assert.NoError(t, err)
err = n.Close()
assert.NoError(t, err)
}

func mergeOptions(nodeOpts ...NodeOpt) (Options, error) {
var options Options
var nodeOpt NodeOpt
for _, opt := range append(nodeOpts, nodeOpt) {
if opt == nil {
continue
}
if err := opt(&options); err != nil {
return options, err
}
}
return options, nil
}

func TestInvalidListenTCPAddrStrings(t *testing.T) {
opt := ListenTCPAddrStrings("/ip4/碎片整理")
options, err := mergeOptions(opt)
assert.EqualError(t, err, "failed to parse multiaddr \"/ip4/碎片整理\": invalid value \"碎片整理\" for protocol ip4: failed to parse ip4 addr: 碎片整理")
assert.Equal(t, Options{}, options)
}