2021-11-23 14:13:17 +00:00
|
|
|
package netmap_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2022-12-13 14:36:35 +00:00
|
|
|
"github.com/TrueCloudLab/frostfs-sdk-go/netmap"
|
|
|
|
subnetid "github.com/TrueCloudLab/frostfs-sdk-go/subnet/id"
|
2021-11-23 14:13:17 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestNodeInfoSubnets(t *testing.T) {
|
|
|
|
t.Run("enter subnet", func(t *testing.T) {
|
|
|
|
var id subnetid.ID
|
|
|
|
|
2022-06-15 07:12:35 +00:00
|
|
|
id.SetNumeric(13)
|
2021-11-23 14:13:17 +00:00
|
|
|
|
|
|
|
var node netmap.NodeInfo
|
|
|
|
|
|
|
|
node.EnterSubnet(id)
|
|
|
|
|
|
|
|
mIDs := make(map[string]struct{})
|
|
|
|
|
|
|
|
err := node.IterateSubnets(func(id subnetid.ID) error {
|
|
|
|
mIDs[id.String()] = struct{}{}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
_, ok := mIDs[id.String()]
|
|
|
|
require.True(t, ok)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("iterate with removal", func(t *testing.T) {
|
|
|
|
t.Run("not last", func(t *testing.T) {
|
|
|
|
var id, idrm subnetid.ID
|
|
|
|
|
2022-06-15 07:12:35 +00:00
|
|
|
id.SetNumeric(13)
|
|
|
|
idrm.SetNumeric(23)
|
2021-11-23 14:13:17 +00:00
|
|
|
|
|
|
|
var node netmap.NodeInfo
|
|
|
|
|
|
|
|
node.EnterSubnet(id)
|
|
|
|
node.EnterSubnet(idrm)
|
|
|
|
|
|
|
|
err := node.IterateSubnets(func(id subnetid.ID) error {
|
2022-06-15 07:12:35 +00:00
|
|
|
if subnetid.IsZero(id) || id.Equals(idrm) {
|
2021-11-23 14:13:17 +00:00
|
|
|
return netmap.ErrRemoveSubnet
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
mIDs := make(map[string]struct{})
|
|
|
|
|
|
|
|
err = node.IterateSubnets(func(id subnetid.ID) error {
|
|
|
|
mIDs[id.String()] = struct{}{}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
var zeroID subnetid.ID
|
|
|
|
|
|
|
|
_, ok := mIDs[zeroID.String()]
|
|
|
|
require.False(t, ok)
|
|
|
|
|
|
|
|
_, ok = mIDs[idrm.String()]
|
|
|
|
require.False(t, ok)
|
|
|
|
|
|
|
|
_, ok = mIDs[id.String()]
|
|
|
|
require.True(t, ok)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("last", func(t *testing.T) {
|
|
|
|
var node netmap.NodeInfo
|
|
|
|
|
|
|
|
err := node.IterateSubnets(func(id subnetid.ID) error {
|
|
|
|
return netmap.ErrRemoveSubnet
|
|
|
|
})
|
|
|
|
|
|
|
|
require.Error(t, err)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-11-26 12:31:36 +00:00
|
|
|
func TestEnterSubnet(t *testing.T) {
|
|
|
|
var (
|
|
|
|
id subnetid.ID
|
|
|
|
node netmap.NodeInfo
|
|
|
|
)
|
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
require.True(t, netmap.BelongsToSubnet(node, id))
|
2021-11-26 12:31:36 +00:00
|
|
|
|
|
|
|
node.EnterSubnet(id)
|
2022-06-07 08:25:34 +00:00
|
|
|
require.True(t, netmap.BelongsToSubnet(node, id))
|
2021-11-26 12:31:36 +00:00
|
|
|
|
|
|
|
node.ExitSubnet(id)
|
2022-06-07 08:25:34 +00:00
|
|
|
require.False(t, netmap.BelongsToSubnet(node, id))
|
2021-11-26 12:31:36 +00:00
|
|
|
|
2022-06-15 07:12:35 +00:00
|
|
|
id.SetNumeric(10)
|
2021-11-26 12:31:36 +00:00
|
|
|
node.EnterSubnet(id)
|
2022-06-07 08:25:34 +00:00
|
|
|
require.True(t, netmap.BelongsToSubnet(node, id))
|
|
|
|
require.False(t, netmap.BelongsToSubnet(node, subnetid.ID{}))
|
2021-11-26 12:31:36 +00:00
|
|
|
|
|
|
|
node.ExitSubnet(id)
|
2022-06-07 08:25:34 +00:00
|
|
|
require.False(t, netmap.BelongsToSubnet(node, id))
|
|
|
|
require.False(t, netmap.BelongsToSubnet(node, subnetid.ID{}))
|
2021-11-26 12:31:36 +00:00
|
|
|
}
|
|
|
|
|
2021-11-23 14:13:17 +00:00
|
|
|
func TestBelongsToSubnet(t *testing.T) {
|
|
|
|
var id, idMiss, idZero subnetid.ID
|
|
|
|
|
2022-06-15 07:12:35 +00:00
|
|
|
id.SetNumeric(13)
|
|
|
|
idMiss.SetNumeric(23)
|
2021-11-23 14:13:17 +00:00
|
|
|
|
|
|
|
var node netmap.NodeInfo
|
|
|
|
|
|
|
|
node.EnterSubnet(id)
|
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
require.True(t, netmap.BelongsToSubnet(node, idZero))
|
|
|
|
require.True(t, netmap.BelongsToSubnet(node, id))
|
|
|
|
require.False(t, netmap.BelongsToSubnet(node, idMiss))
|
2021-11-23 14:13:17 +00:00
|
|
|
}
|