2020-12-18 12:52:27 +00:00
|
|
|
package audit_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2023-03-07 13:38:26 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/innerring/processors/audit"
|
|
|
|
cid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container/id"
|
|
|
|
cidtest "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container/id/test"
|
2020-12-18 12:52:27 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestSelect(t *testing.T) {
|
|
|
|
cids := generateContainers(10)
|
|
|
|
|
|
|
|
t.Run("invalid input", func(t *testing.T) {
|
|
|
|
require.Empty(t, audit.Select(cids, 0, 0, 0))
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("even split", func(t *testing.T) {
|
|
|
|
const irSize = 5 // every node takes two audit nodes
|
|
|
|
|
|
|
|
m := hitMap(cids)
|
|
|
|
|
|
|
|
for i := 0; i < irSize; i++ {
|
|
|
|
s := audit.Select(cids, 0, uint64(i), irSize)
|
|
|
|
require.Equal(t, len(cids)/irSize, len(s))
|
|
|
|
|
|
|
|
for _, id := range s {
|
2022-05-31 17:00:41 +00:00
|
|
|
n, ok := m[id.EncodeToString()]
|
2020-12-18 12:52:27 +00:00
|
|
|
require.True(t, ok)
|
|
|
|
require.Equal(t, 0, n)
|
2022-05-31 17:00:41 +00:00
|
|
|
m[id.EncodeToString()] = 1
|
2020-12-18 12:52:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
require.True(t, allHit(m))
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("odd split", func(t *testing.T) {
|
|
|
|
const irSize = 3
|
|
|
|
|
|
|
|
m := hitMap(cids)
|
|
|
|
|
|
|
|
for i := 0; i < irSize; i++ {
|
|
|
|
s := audit.Select(cids, 0, uint64(i), irSize)
|
|
|
|
|
|
|
|
for _, id := range s {
|
2022-05-31 17:00:41 +00:00
|
|
|
n, ok := m[id.EncodeToString()]
|
2020-12-18 12:52:27 +00:00
|
|
|
require.True(t, ok)
|
|
|
|
require.Equal(t, 0, n)
|
2022-05-31 17:00:41 +00:00
|
|
|
m[id.EncodeToString()] = 1
|
2020-12-18 12:52:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
require.True(t, allHit(m))
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("epoch shift", func(t *testing.T) {
|
|
|
|
const irSize = 4
|
|
|
|
|
|
|
|
m := hitMap(cids)
|
|
|
|
|
|
|
|
for i := 0; i < irSize; i++ {
|
|
|
|
s := audit.Select(cids, uint64(i), 0, irSize)
|
|
|
|
|
|
|
|
for _, id := range s {
|
2022-05-31 17:00:41 +00:00
|
|
|
n, ok := m[id.EncodeToString()]
|
2020-12-18 12:52:27 +00:00
|
|
|
require.True(t, ok)
|
|
|
|
require.Equal(t, 0, n)
|
2022-05-31 17:00:41 +00:00
|
|
|
m[id.EncodeToString()] = 1
|
2020-12-18 12:52:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
require.True(t, allHit(m))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-05-31 17:00:41 +00:00
|
|
|
func generateContainers(n int) []cid.ID {
|
|
|
|
result := make([]cid.ID, n)
|
2020-12-18 12:52:27 +00:00
|
|
|
|
|
|
|
for i := 0; i < n; i++ {
|
2022-05-31 17:00:41 +00:00
|
|
|
result[i] = cidtest.ID()
|
2020-12-18 12:52:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2022-05-31 17:00:41 +00:00
|
|
|
func hitMap(ids []cid.ID) map[string]int {
|
2020-12-18 12:52:27 +00:00
|
|
|
result := make(map[string]int, len(ids))
|
|
|
|
|
|
|
|
for _, id := range ids {
|
2022-05-31 17:00:41 +00:00
|
|
|
result[id.EncodeToString()] = 0
|
2020-12-18 12:52:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
func allHit(m map[string]int) bool {
|
|
|
|
for _, v := range m {
|
|
|
|
if v == 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|