frostfs-node/pkg/local_object_storage/metabase/containers_test.go
Ekaterina Lebedeva a685fcdc96 [] go.mod: Use range over int
Since Go 1.22 a "for" statement with a "range" clause is able
to iterate through integer values from zero to an upper limit.

gopatch script:
@@
var i, e expression
@@
-for i := 0; i <= e - 1; i++ {
+for i := range e {
    ...
}

@@
var i, e expression
@@
-for i := 0; i <= e; i++ {
+for i := range e + 1 {
    ...
}

@@
var i, e expression
@@
-for i := 0; i < e; i++ {
+for i := range e {
    ...
}

Signed-off-by: Ekaterina Lebedeva <ekaterina.lebedeva@yadro.com>
2024-09-03 13:00:54 +03:00

178 lines
3.7 KiB
Go

package meta_test
import (
"context"
"math/rand"
"testing"
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/core/object"
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/internal/testutil"
cid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container/id"
cidtest "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container/id/test"
objectSDK "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object"
oidtest "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object/id/test"
"github.com/stretchr/testify/require"
)
func TestDB_Containers(t *testing.T) {
t.Parallel()
db := newDB(t)
defer func() { require.NoError(t, db.Close()) }()
const N = 10
cids := make(map[string]int, N)
for range N {
obj := testutil.GenerateObject()
cnr, _ := obj.ContainerID()
cids[cnr.EncodeToString()] = 0
err := putBig(db, obj)
require.NoError(t, err)
}
lst, err := db.Containers(context.Background())
require.NoError(t, err)
for _, cnr := range lst {
i, ok := cids[cnr.EncodeToString()]
require.True(t, ok)
require.Equal(t, 0, i)
cids[cnr.EncodeToString()] = 1
}
// require.Contains not working since cnrs is a ptr slice
assertContains := func(cnrs []cid.ID, cnr cid.ID) {
found := false
for i := 0; !found && i < len(cnrs); i++ {
found = cnrs[i].Equals(cnr)
}
require.True(t, found)
}
t.Run("Inhume", func(t *testing.T) {
obj := testutil.GenerateObject()
require.NoError(t, putBig(db, obj))
cnrs, err := db.Containers(context.Background())
require.NoError(t, err)
cnr, _ := obj.ContainerID()
assertContains(cnrs, cnr)
require.NoError(t, metaInhume(db, object.AddressOf(obj), oidtest.Address()))
cnrs, err = db.Containers(context.Background())
require.NoError(t, err)
assertContains(cnrs, cnr)
})
}
func TestDB_ContainersCount(t *testing.T) {
t.Parallel()
db := newDB(t)
defer func() { require.NoError(t, db.Close()) }()
const R, T, SG, L = 10, 11, 12, 13 // amount of object per type
uploadObjects := [...]struct {
amount int
typ objectSDK.Type
}{
{R, objectSDK.TypeRegular},
{T, objectSDK.TypeTombstone},
{L, objectSDK.TypeLock},
}
expected := make([]cid.ID, 0, R+T+SG+L)
for _, upload := range uploadObjects {
for range upload.amount {
obj := testutil.GenerateObject()
obj.SetType(upload.typ)
err := putBig(db, obj)
require.NoError(t, err)
cnr, _ := obj.ContainerID()
expected = append(expected, cnr)
}
}
got, err := db.Containers(context.Background())
require.NoError(t, err)
require.ElementsMatch(t, expected, got)
}
func TestDB_ContainerSize(t *testing.T) {
t.Parallel()
db := newDB(t)
defer func() { require.NoError(t, db.Close()) }()
const (
C = 3
N = 5
)
cids := make(map[cid.ID]int, C)
objs := make(map[cid.ID][]*objectSDK.Object, C*N)
for range C {
cnr := cidtest.ID()
cids[cnr] = 0
for range N {
size := rand.Intn(1024)
parent := testutil.GenerateObjectWithCID(cnr)
parent.SetPayloadSize(uint64(size / 2))
obj := testutil.GenerateObjectWithCID(cnr)
obj.SetPayloadSize(uint64(size))
idParent, _ := parent.ID()
obj.SetParentID(idParent)
obj.SetParent(parent)
cids[cnr] += size
objs[cnr] = append(objs[cnr], obj)
err := putBig(db, obj)
require.NoError(t, err)
}
}
for cnr, volume := range cids {
n, err := db.ContainerSize(cnr)
require.NoError(t, err)
require.Equal(t, volume, int(n))
}
t.Run("Inhume", func(t *testing.T) {
for cnr, list := range objs {
volume := cids[cnr]
for _, obj := range list {
require.NoError(t, metaInhume(
db,
object.AddressOf(obj),
oidtest.Address(),
))
volume -= int(obj.PayloadSize())
n, err := db.ContainerSize(cnr)
require.NoError(t, err)
require.Equal(t, volume, int(n))
}
}
})
}