Skip to content
Open
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
5 changes: 4 additions & 1 deletion integration/benchmark/node/benchmark.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,10 @@ func GenerateConfig(testdataDir string) error {
}

func SetupNode(confPath string, factories ...NamedFactory) (*node.Node, error) {
fsc := node.NewWithConfPath(confPath)
fsc, err := node.NewWithConfPathE(confPath)
if err != nil {
return nil, err
}
if err := fsc.InstallSDK(viewsdk.NewSDK(fsc)); err != nil {
return nil, err
}
Expand Down
26 changes: 24 additions & 2 deletions node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,34 @@ type Node struct {

// New returns a new instance of Node from the default configuration path.
func New() *Node {
return NewWithConfPath("")
node, err := NewE()
if err != nil {
panic(err)
}
return node
}

// NewE returns a new instance of Node from the default configuration path.
func NewE() (*Node, error) {
return NewWithConfPathE("")
}

// NewWithConfPath returns a new instance of Node whose configuration is loaded from the passed path.
func NewWithConfPath(confPath string) *Node {
return newWithFSCNode(node.NewFromConfPath(confPath))
node, err := NewWithConfPathE(confPath)
if err != nil {
panic(err)
}
return node
}

// NewWithConfPathE returns a new instance of Node whose configuration is loaded from the passed path.
func NewWithConfPathE(confPath string) (*Node, error) {
fscNode, err := node.NewFromConfPathE(confPath)
if err != nil {
return nil, err
}
return newWithFSCNode(fscNode), nil
}

func newWithFSCNode(fscNode FSCNode) *Node {
Expand Down
35 changes: 35 additions & 0 deletions node/node_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
Copyright IBM Corp. All Rights Reserved.

SPDX-License-Identifier: Apache-2.0
*/

package node

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

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

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

dir := t.TempDir()
err := os.WriteFile(filepath.Join(dir, "core.yaml"), []byte("fsc:\n id: test-node\n"), 0o600)
require.NoError(t, err)

n, err := NewWithConfPathE(dir)
require.NoError(t, err)
require.NotNil(t, n)
require.Equal(t, "test-node", n.ID())
}

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

_, err := NewWithConfPathE("./does-not-exist")
require.Error(t, err)
}
14 changes: 11 additions & 3 deletions pkg/node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,20 +39,28 @@ type Node struct {
}

func NewFromConfPath(confPath string) *Node {
configService, err := config.NewProvider(confPath)
node, err := NewFromConfPathE(confPath)
if err != nil {
panic(err)
}
return node
}

func NewFromConfPathE(confPath string) (*Node, error) {
configService, err := config.NewProvider(confPath)
if err != nil {
return nil, err
}
registry := view.NewServiceProvider()
if err := registry.RegisterService(configService); err != nil {
panic(err)
return nil, err
}

return &Node{
sdks: []SDK{},
registry: registry,
id: configService.ID(),
}
}, nil
}

func (n *Node) AddSDK(sdk SDK) {
Expand Down
21 changes: 21 additions & 0 deletions pkg/node/node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ package node

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

"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -195,3 +197,22 @@ func TestNode_GetService_NotFound(t *testing.T) {
_, err := n.GetService((*mockSDK)(nil))
require.Error(t, err)
}

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

dir := t.TempDir()
err := os.WriteFile(filepath.Join(dir, "core.yaml"), []byte("fsc:\n id: test-node\n"), 0o600)
require.NoError(t, err)

n, err := NewFromConfPathE(dir)
require.NoError(t, err)
require.Equal(t, "test-node", n.ID())
}

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

_, err := NewFromConfPathE("./does-not-exist")
require.Error(t, err)
}
Loading