2021-11-08 12:25:13 +00:00
|
|
|
package container_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/google/uuid"
|
2022-03-23 15:35:44 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/v2/refs"
|
2021-11-08 12:25:13 +00:00
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/container"
|
|
|
|
containertest "github.com/nspcc-dev/neofs-sdk-go/container/test"
|
|
|
|
netmaptest "github.com/nspcc-dev/neofs-sdk-go/netmap/test"
|
2022-04-11 06:30:22 +00:00
|
|
|
usertest "github.com/nspcc-dev/neofs-sdk-go/user/test"
|
2022-02-09 12:19:05 +00:00
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/version"
|
2021-11-08 12:25:13 +00:00
|
|
|
versiontest "github.com/nspcc-dev/neofs-sdk-go/version/test"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestNewContainer(t *testing.T) {
|
|
|
|
c := container.New()
|
|
|
|
|
|
|
|
nonce := uuid.New()
|
|
|
|
|
2022-04-11 06:30:22 +00:00
|
|
|
ownerID := usertest.ID()
|
2021-11-08 12:25:13 +00:00
|
|
|
policy := netmaptest.PlacementPolicy()
|
|
|
|
|
2022-06-16 06:17:41 +00:00
|
|
|
c.SetBasicACL(container.BasicACLPublicRW)
|
2021-11-08 12:25:13 +00:00
|
|
|
|
|
|
|
attrs := containertest.Attributes()
|
|
|
|
c.SetAttributes(attrs)
|
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
c.SetPlacementPolicy(&policy)
|
2021-11-08 12:25:13 +00:00
|
|
|
c.SetNonceUUID(nonce)
|
|
|
|
c.SetOwnerID(ownerID)
|
|
|
|
|
|
|
|
ver := versiontest.Version()
|
2022-03-23 15:35:44 +00:00
|
|
|
c.SetVersion(&ver)
|
2021-11-08 12:25:13 +00:00
|
|
|
|
|
|
|
v2 := c.ToV2()
|
|
|
|
newContainer := container.NewContainerFromV2(v2)
|
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
require.EqualValues(t, newContainer.PlacementPolicy(), &policy)
|
2021-11-08 12:25:13 +00:00
|
|
|
require.EqualValues(t, newContainer.Attributes(), attrs)
|
2022-06-16 06:17:41 +00:00
|
|
|
require.EqualValues(t, newContainer.BasicACL(), container.BasicACLPublicRW)
|
2021-11-08 12:25:13 +00:00
|
|
|
|
|
|
|
newNonce, err := newContainer.NonceUUID()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
require.EqualValues(t, newNonce, nonce)
|
|
|
|
require.EqualValues(t, newContainer.OwnerID(), ownerID)
|
2022-03-23 15:35:44 +00:00
|
|
|
require.EqualValues(t, *newContainer.Version(), ver)
|
2021-11-08 12:25:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestContainerEncoding(t *testing.T) {
|
|
|
|
c := containertest.Container()
|
|
|
|
|
|
|
|
t.Run("binary", func(t *testing.T) {
|
|
|
|
data, err := c.Marshal()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
c2 := container.New()
|
|
|
|
require.NoError(t, c2.Unmarshal(data))
|
|
|
|
|
|
|
|
require.Equal(t, c, c2)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("json", func(t *testing.T) {
|
|
|
|
data, err := c.MarshalJSON()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
c2 := container.New()
|
|
|
|
require.NoError(t, c2.UnmarshalJSON(data))
|
|
|
|
|
|
|
|
require.Equal(t, c, c2)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestContainer_ToV2(t *testing.T) {
|
|
|
|
t.Run("nil", func(t *testing.T) {
|
|
|
|
var x *container.Container
|
|
|
|
|
|
|
|
require.Nil(t, x.ToV2())
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("default values", func(t *testing.T) {
|
|
|
|
cnt := container.New()
|
|
|
|
|
|
|
|
// check initial values
|
|
|
|
require.Nil(t, cnt.Attributes())
|
|
|
|
require.Nil(t, cnt.PlacementPolicy())
|
|
|
|
require.Nil(t, cnt.OwnerID())
|
|
|
|
|
2022-06-16 06:17:41 +00:00
|
|
|
require.EqualValues(t, container.BasicACLPrivate, cnt.BasicACL())
|
2022-03-23 15:35:44 +00:00
|
|
|
require.Equal(t, version.Current(), *cnt.Version())
|
2021-11-08 12:25:13 +00:00
|
|
|
|
|
|
|
nonce, err := cnt.NonceUUID()
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, nonce)
|
|
|
|
|
|
|
|
// convert to v2 message
|
|
|
|
cntV2 := cnt.ToV2()
|
|
|
|
|
|
|
|
nonceV2, err := uuid.FromBytes(cntV2.GetNonce())
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
require.Equal(t, nonce.String(), nonceV2.String())
|
|
|
|
|
|
|
|
require.Nil(t, cntV2.GetAttributes())
|
|
|
|
require.Nil(t, cntV2.GetPlacementPolicy())
|
|
|
|
require.Nil(t, cntV2.GetOwnerID())
|
|
|
|
|
2022-06-16 06:17:41 +00:00
|
|
|
require.EqualValues(t, 0x1C8C8CCC, cntV2.GetBasicACL())
|
2022-03-23 15:35:44 +00:00
|
|
|
|
|
|
|
var verV2 refs.Version
|
|
|
|
version.Current().WriteToV2(&verV2)
|
|
|
|
require.Equal(t, verV2, *cntV2.GetVersion())
|
2021-11-08 12:25:13 +00:00
|
|
|
})
|
|
|
|
}
|