2021-01-21 15:47:00 +00:00
|
|
|
package container_test
|
|
|
|
|
|
|
|
import (
|
2021-01-28 13:01:07 +00:00
|
|
|
"crypto/sha256"
|
2021-01-21 15:47:00 +00:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/nspcc-dev/neofs-api-go/pkg/container"
|
2021-05-31 12:09:04 +00:00
|
|
|
cid "github.com/nspcc-dev/neofs-api-go/pkg/container/id"
|
|
|
|
cidtest "github.com/nspcc-dev/neofs-api-go/pkg/container/id/test"
|
2021-06-08 11:38:38 +00:00
|
|
|
containertest "github.com/nspcc-dev/neofs-api-go/pkg/container/test"
|
2021-01-21 15:47:00 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/v2/refs"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestAnnouncement(t *testing.T) {
|
2021-01-26 18:35:26 +00:00
|
|
|
const epoch, usedSpace uint64 = 10, 100
|
2021-01-21 15:47:00 +00:00
|
|
|
|
2021-05-31 12:09:04 +00:00
|
|
|
cidValue := [sha256.Size]byte{1, 2, 3}
|
|
|
|
id := cidtest.GenerateWithChecksum(cidValue)
|
2021-01-21 15:47:00 +00:00
|
|
|
|
|
|
|
a := container.NewAnnouncement()
|
2021-01-26 18:35:26 +00:00
|
|
|
a.SetEpoch(epoch)
|
2021-05-31 12:09:04 +00:00
|
|
|
a.SetContainerID(id)
|
2021-01-21 15:47:00 +00:00
|
|
|
a.SetUsedSpace(usedSpace)
|
|
|
|
|
2021-01-26 18:35:26 +00:00
|
|
|
require.Equal(t, epoch, a.Epoch())
|
2021-01-21 15:47:00 +00:00
|
|
|
require.Equal(t, usedSpace, a.UsedSpace())
|
2021-05-31 12:09:04 +00:00
|
|
|
require.Equal(t, id, a.ContainerID())
|
2021-01-21 15:47:00 +00:00
|
|
|
|
|
|
|
t.Run("test v2", func(t *testing.T) {
|
2021-01-26 18:35:26 +00:00
|
|
|
const newEpoch, newUsedSpace uint64 = 20, 200
|
2021-01-21 15:47:00 +00:00
|
|
|
|
|
|
|
newCidValue := [32]byte{4, 5, 6}
|
|
|
|
newCID := new(refs.ContainerID)
|
|
|
|
newCID.SetValue(newCidValue[:])
|
|
|
|
|
|
|
|
v2 := a.ToV2()
|
|
|
|
require.Equal(t, usedSpace, v2.GetUsedSpace())
|
2021-01-26 18:35:26 +00:00
|
|
|
require.Equal(t, epoch, v2.GetEpoch())
|
2021-01-21 15:47:00 +00:00
|
|
|
require.Equal(t, cidValue[:], v2.GetContainerID().GetValue())
|
|
|
|
|
2021-01-26 18:35:26 +00:00
|
|
|
v2.SetEpoch(newEpoch)
|
2021-01-21 15:47:00 +00:00
|
|
|
v2.SetUsedSpace(newUsedSpace)
|
|
|
|
v2.SetContainerID(newCID)
|
|
|
|
|
|
|
|
newA := container.NewAnnouncementFromV2(v2)
|
|
|
|
|
2021-01-26 18:35:26 +00:00
|
|
|
require.Equal(t, newEpoch, newA.Epoch())
|
2021-01-21 15:47:00 +00:00
|
|
|
require.Equal(t, newUsedSpace, newA.UsedSpace())
|
2021-05-31 12:09:04 +00:00
|
|
|
require.Equal(t, cid.NewFromV2(newCID), newA.ContainerID())
|
2021-01-21 15:47:00 +00:00
|
|
|
})
|
|
|
|
}
|
2021-01-28 13:01:07 +00:00
|
|
|
|
|
|
|
func TestUsedSpaceEncoding(t *testing.T) {
|
2021-06-08 11:38:38 +00:00
|
|
|
a := containertest.UsedSpaceAnnouncement()
|
2021-01-28 13:01:07 +00:00
|
|
|
|
|
|
|
t.Run("binary", func(t *testing.T) {
|
|
|
|
data, err := a.Marshal()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
a2 := container.NewAnnouncement()
|
|
|
|
require.NoError(t, a2.Unmarshal(data))
|
|
|
|
|
|
|
|
require.Equal(t, a, a2)
|
|
|
|
})
|
|
|
|
}
|