WIP: Morph: Add unit tests #2

Closed
dstepanov-yadro wants to merge 233 commits from TrueCloudLab/frostfs-node:master into object-3608-morph-unit-tests
5 changed files with 1 additions and 199 deletions
Showing only changes of commit 47b0ec33c3 - Show all commits

View file

@ -119,9 +119,7 @@ type (
)
const (
newEpochNotification = "NewEpoch"
addPeerNotification = "AddPeer"
updatePeerStateNotification = "UpdateState"
newEpochNotification = "NewEpoch"
)
// New creates network map contract processor instance.
@ -189,20 +187,6 @@ func (np *Processor) ListenerNotificationParsers() []event.NotificationParserInf
p.SetParser(netmapEvent.ParseNewEpoch)
parsers = append(parsers, p)
if !np.notaryDisabled {
return parsers
}
// new peer event
p.SetType(addPeerNotification)
p.SetParser(netmapEvent.ParseAddPeer)
parsers = append(parsers, p)
// update peer event
p.SetType(updatePeerStateNotification)
p.SetParser(netmapEvent.ParseUpdatePeer)
parsers = append(parsers, p)
return parsers
}
@ -219,20 +203,6 @@ func (np *Processor) ListenerNotificationHandlers() []event.NotificationHandlerI
i.SetHandler(np.handleNewEpoch)
handlers = append(handlers, i)
if !np.notaryDisabled {
return handlers
}
// new peer handler
i.SetType(addPeerNotification)
i.SetHandler(np.handleAddPeer)
handlers = append(handlers, i)
// update peer handler
i.SetType(updatePeerStateNotification)
i.SetHandler(np.handleUpdateState)
handlers = append(handlers, i)
return handlers
}

View file

@ -1,11 +1,6 @@
package netmap
import (
"fmt"
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/morph/client"
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/morph/event"
"github.com/nspcc-dev/neo-go/pkg/core/state"
"github.com/nspcc-dev/neo-go/pkg/network/payload"
)
@ -31,26 +26,3 @@ func (s AddPeer) NotaryRequest() *payload.P2PNotaryRequest {
}
const expectedItemNumAddPeer = 1
func ParseAddPeer(e *state.ContainedNotificationEvent) (event.Event, error) {
var (
ev AddPeer
err error
)
params, err := event.ParseStackArray(e)
if err != nil {
return nil, fmt.Errorf("could not parse stack items from notify event: %w", err)
}
if ln := len(params); ln != expectedItemNumAddPeer {
return nil, event.WrongNumberOfParameters(expectedItemNumAddPeer, ln)
}
ev.NodeBytes, err = client.BytesFromStackItem(params[0])
if err != nil {
return nil, fmt.Errorf("could not get raw nodeinfo: %w", err)
}
return ev, nil
}

View file

@ -1,47 +1,10 @@
package netmap
import (
"testing"
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/morph/event"
"github.com/nspcc-dev/neo-go/pkg/core/state"
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
"github.com/stretchr/testify/require"
)
func TestParseAddPeer(t *testing.T) {
t.Run("wrong number of parameters", func(t *testing.T) {
prms := []stackitem.Item{
stackitem.NewMap(),
stackitem.NewMap(),
}
_, err := ParseAddPeer(createNotifyEventFromItems(prms))
require.EqualError(t, err, event.WrongNumberOfParameters(1, len(prms)).Error())
})
t.Run("wrong first parameter type", func(t *testing.T) {
_, err := ParseAddPeer(createNotifyEventFromItems([]stackitem.Item{
stackitem.NewMap(),
}))
require.Error(t, err)
})
t.Run("correct behavior", func(t *testing.T) {
info := []byte{1, 2, 3}
ev, err := ParseAddPeer(createNotifyEventFromItems([]stackitem.Item{
stackitem.NewByteArray(info),
}))
require.NoError(t, err)
require.Equal(t, AddPeer{
NodeBytes: info,
}, ev)
})
}
func createNotifyEventFromItems(items []stackitem.Item) *state.ContainedNotificationEvent {
return &state.ContainedNotificationEvent{
NotificationEvent: state.NotificationEvent{

View file

@ -1,13 +1,9 @@
package netmap
import (
"crypto/elliptic"
"fmt"
"git.frostfs.info/TrueCloudLab/frostfs-contract/netmap"
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/morph/client"
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/morph/event"
"github.com/nspcc-dev/neo-go/pkg/core/state"
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
"github.com/nspcc-dev/neo-go/pkg/network/payload"
)
@ -60,43 +56,3 @@ func (s *UpdatePeer) decodeState(state int64) error {
}
const expectedItemNumUpdatePeer = 2
func ParseUpdatePeer(e *state.ContainedNotificationEvent) (event.Event, error) {
var (
ev UpdatePeer
err error
)
params, err := event.ParseStackArray(e)
if err != nil {
return nil, fmt.Errorf("could not parse stack items from notify event: %w", err)
}
if ln := len(params); ln != expectedItemNumUpdatePeer {
return nil, event.WrongNumberOfParameters(expectedItemNumUpdatePeer, ln)
}
// parse public key
key, err := client.BytesFromStackItem(params[1])
if err != nil {
return nil, fmt.Errorf("could not get public key: %w", err)
}
ev.PubKey, err = keys.NewPublicKeyFromBytes(key, elliptic.P256())
if err != nil {
return nil, fmt.Errorf("could not parse public key: %w", err)
}
// parse node status
st, err := client.IntFromStackItem(params[0])
if err != nil {
return nil, fmt.Errorf("could not get node status: %w", err)
}
err = ev.decodeState(st)
if err != nil {
return nil, err
}
return ev, nil
}

View file

@ -1,59 +0,0 @@
package netmap
import (
"math/big"
"testing"
"git.frostfs.info/TrueCloudLab/frostfs-contract/netmap"
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/morph/event"
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
"github.com/stretchr/testify/require"
)
func TestParseUpdatePeer(t *testing.T) {
priv, err := keys.NewPrivateKey()
require.NoError(t, err)
publicKey := priv.PublicKey()
t.Run("wrong number of parameters", func(t *testing.T) {
prms := []stackitem.Item{
stackitem.NewMap(),
}
_, err := ParseUpdatePeer(createNotifyEventFromItems(prms))
require.EqualError(t, err, event.WrongNumberOfParameters(2, len(prms)).Error())
})
t.Run("wrong first parameter type", func(t *testing.T) {
_, err := ParseUpdatePeer(createNotifyEventFromItems([]stackitem.Item{
stackitem.NewMap(),
}))
require.Error(t, err)
})
t.Run("wrong second parameter type", func(t *testing.T) {
_, err := ParseUpdatePeer(createNotifyEventFromItems([]stackitem.Item{
stackitem.NewByteArray(publicKey.Bytes()),
stackitem.NewMap(),
}))
require.Error(t, err)
})
t.Run("correct behavior", func(t *testing.T) {
const state = netmap.NodeStateMaintenance
ev, err := ParseUpdatePeer(createNotifyEventFromItems([]stackitem.Item{
stackitem.NewBigInteger(big.NewInt(int64(state))),
stackitem.NewByteArray(publicKey.Bytes()),
}))
require.NoError(t, err)
require.Equal(t, UpdatePeer{
PubKey: publicKey,
State: state,
}, ev)
})
}