2021-11-08 12:13:59 +00:00
|
|
|
package audit_test
|
|
|
|
|
|
|
|
import (
|
2022-03-14 11:30:07 +00:00
|
|
|
"bytes"
|
2021-11-08 12:13:59 +00:00
|
|
|
"testing"
|
|
|
|
|
2022-12-13 14:36:35 +00:00
|
|
|
"github.com/TrueCloudLab/frostfs-sdk-go/audit"
|
|
|
|
audittest "github.com/TrueCloudLab/frostfs-sdk-go/audit/test"
|
|
|
|
cidtest "github.com/TrueCloudLab/frostfs-sdk-go/container/id/test"
|
|
|
|
oid "github.com/TrueCloudLab/frostfs-sdk-go/object/id"
|
|
|
|
oidtest "github.com/TrueCloudLab/frostfs-sdk-go/object/id/test"
|
2021-11-08 12:13:59 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2022-03-14 11:30:07 +00:00
|
|
|
func TestResultData(t *testing.T) {
|
|
|
|
var r audit.Result
|
2021-11-08 12:13:59 +00:00
|
|
|
|
2022-03-14 11:30:07 +00:00
|
|
|
countSG := func(passed bool, f func(oid.ID)) int {
|
|
|
|
called := 0
|
2021-11-08 12:13:59 +00:00
|
|
|
|
2022-03-14 11:30:07 +00:00
|
|
|
ff := func(arg oid.ID) bool {
|
|
|
|
called++
|
2021-11-08 12:13:59 +00:00
|
|
|
|
2022-03-14 11:30:07 +00:00
|
|
|
if f != nil {
|
|
|
|
f(arg)
|
|
|
|
}
|
2021-11-08 12:13:59 +00:00
|
|
|
|
2022-03-14 11:30:07 +00:00
|
|
|
return true
|
|
|
|
}
|
2021-11-08 12:13:59 +00:00
|
|
|
|
2022-03-14 11:30:07 +00:00
|
|
|
if passed {
|
|
|
|
r.IteratePassedStorageGroups(ff)
|
|
|
|
} else {
|
|
|
|
r.IterateFailedStorageGroups(ff)
|
|
|
|
}
|
2021-11-08 12:13:59 +00:00
|
|
|
|
2022-03-14 11:30:07 +00:00
|
|
|
return called
|
|
|
|
}
|
2021-11-08 12:13:59 +00:00
|
|
|
|
2022-03-14 11:30:07 +00:00
|
|
|
countPassSG := func(f func(oid.ID)) int { return countSG(true, f) }
|
|
|
|
countFailSG := func(f func(oid.ID)) int { return countSG(false, f) }
|
2021-11-08 12:13:59 +00:00
|
|
|
|
2022-03-14 11:30:07 +00:00
|
|
|
countNodes := func(passed bool, f func([]byte)) int {
|
|
|
|
called := 0
|
2021-11-08 12:13:59 +00:00
|
|
|
|
2022-03-14 11:30:07 +00:00
|
|
|
ff := func(arg []byte) bool {
|
|
|
|
called++
|
2021-11-08 12:13:59 +00:00
|
|
|
|
2022-03-14 11:30:07 +00:00
|
|
|
if f != nil {
|
|
|
|
f(arg)
|
|
|
|
}
|
2021-11-08 12:13:59 +00:00
|
|
|
|
2022-03-14 11:30:07 +00:00
|
|
|
return true
|
|
|
|
}
|
2021-11-08 12:13:59 +00:00
|
|
|
|
2022-03-14 11:30:07 +00:00
|
|
|
if passed {
|
|
|
|
r.IteratePassedStorageNodes(ff)
|
|
|
|
} else {
|
|
|
|
r.IterateFailedStorageNodes(ff)
|
|
|
|
}
|
2021-11-08 12:13:59 +00:00
|
|
|
|
2022-03-14 11:30:07 +00:00
|
|
|
return called
|
|
|
|
}
|
2021-11-08 12:13:59 +00:00
|
|
|
|
2022-03-14 11:30:07 +00:00
|
|
|
countPassNodes := func(f func([]byte)) int { return countNodes(true, f) }
|
|
|
|
countFailNodes := func(f func([]byte)) int { return countNodes(false, f) }
|
2021-11-08 12:13:59 +00:00
|
|
|
|
2022-03-14 11:30:07 +00:00
|
|
|
require.Zero(t, r.Epoch())
|
2022-04-12 16:23:16 +00:00
|
|
|
_, set := r.Container()
|
|
|
|
require.False(t, set)
|
2022-03-14 11:30:07 +00:00
|
|
|
require.Nil(t, r.AuditorKey())
|
|
|
|
require.False(t, r.Completed())
|
|
|
|
require.Zero(t, r.RequestsPoR())
|
|
|
|
require.Zero(t, r.RetriesPoR())
|
|
|
|
require.Zero(t, countPassSG(nil))
|
|
|
|
require.Zero(t, countFailSG(nil))
|
|
|
|
require.Zero(t, countPassNodes(nil))
|
|
|
|
require.Zero(t, countFailNodes(nil))
|
2021-11-08 12:13:59 +00:00
|
|
|
|
2022-03-14 11:30:07 +00:00
|
|
|
epoch := uint64(13)
|
|
|
|
r.ForEpoch(epoch)
|
|
|
|
require.Equal(t, epoch, r.Epoch())
|
2021-11-08 12:13:59 +00:00
|
|
|
|
2022-03-14 11:30:07 +00:00
|
|
|
cnr := cidtest.ID()
|
2022-04-11 16:25:14 +00:00
|
|
|
r.ForContainer(cnr)
|
2022-04-12 16:23:16 +00:00
|
|
|
cID, set := r.Container()
|
|
|
|
require.True(t, set)
|
|
|
|
require.Equal(t, cnr, cID)
|
2021-11-08 12:13:59 +00:00
|
|
|
|
2022-03-14 11:30:07 +00:00
|
|
|
key := []byte{1, 2, 3}
|
|
|
|
r.SetAuditorKey(key)
|
|
|
|
require.Equal(t, key, r.AuditorKey())
|
2021-11-08 12:13:59 +00:00
|
|
|
|
2022-03-14 11:30:07 +00:00
|
|
|
r.Complete()
|
|
|
|
require.True(t, r.Completed())
|
2021-11-08 12:13:59 +00:00
|
|
|
|
2022-03-14 11:30:07 +00:00
|
|
|
requests := uint32(2)
|
|
|
|
r.SetRequestsPoR(requests)
|
|
|
|
require.Equal(t, requests, r.RequestsPoR())
|
2021-11-08 12:13:59 +00:00
|
|
|
|
2022-03-14 11:30:07 +00:00
|
|
|
retries := uint32(1)
|
|
|
|
r.SetRetriesPoR(retries)
|
|
|
|
require.Equal(t, retries, r.RetriesPoR())
|
|
|
|
|
2022-04-11 16:25:14 +00:00
|
|
|
passSG1, passSG2 := oidtest.ID(), oidtest.ID()
|
2022-03-14 11:30:07 +00:00
|
|
|
r.SubmitPassedStorageGroup(passSG1)
|
|
|
|
r.SubmitPassedStorageGroup(passSG2)
|
|
|
|
|
|
|
|
called1, called2 := false, false
|
|
|
|
|
|
|
|
require.EqualValues(t, 2, countPassSG(func(id oid.ID) {
|
2022-04-11 16:25:14 +00:00
|
|
|
if id.Equals(passSG1) {
|
2022-03-14 11:30:07 +00:00
|
|
|
called1 = true
|
2022-04-11 16:25:14 +00:00
|
|
|
} else if id.Equals(passSG2) {
|
2022-03-14 11:30:07 +00:00
|
|
|
called2 = true
|
|
|
|
}
|
|
|
|
}))
|
|
|
|
require.True(t, called1)
|
|
|
|
require.True(t, called2)
|
|
|
|
|
2022-04-11 16:25:14 +00:00
|
|
|
failSG1, failSG2 := oidtest.ID(), oidtest.ID()
|
2022-03-14 11:30:07 +00:00
|
|
|
r.SubmitFailedStorageGroup(failSG1)
|
|
|
|
r.SubmitFailedStorageGroup(failSG2)
|
|
|
|
|
|
|
|
called1, called2 = false, false
|
|
|
|
|
|
|
|
require.EqualValues(t, 2, countFailSG(func(id oid.ID) {
|
2022-04-11 16:25:14 +00:00
|
|
|
if id.Equals(failSG1) {
|
2022-03-14 11:30:07 +00:00
|
|
|
called1 = true
|
2022-04-11 16:25:14 +00:00
|
|
|
} else if id.Equals(failSG2) {
|
2022-03-14 11:30:07 +00:00
|
|
|
called2 = true
|
|
|
|
}
|
|
|
|
}))
|
|
|
|
require.True(t, called1)
|
|
|
|
require.True(t, called2)
|
2021-11-08 12:13:59 +00:00
|
|
|
|
2022-03-14 11:30:07 +00:00
|
|
|
hit := uint32(1)
|
|
|
|
r.SetHits(hit)
|
|
|
|
require.Equal(t, hit, r.Hits())
|
2021-11-08 12:13:59 +00:00
|
|
|
|
2022-03-14 11:30:07 +00:00
|
|
|
miss := uint32(2)
|
|
|
|
r.SetMisses(miss)
|
|
|
|
require.Equal(t, miss, r.Misses())
|
2021-11-08 12:13:59 +00:00
|
|
|
|
2022-03-14 11:30:07 +00:00
|
|
|
fail := uint32(3)
|
|
|
|
r.SetFailures(fail)
|
|
|
|
require.Equal(t, fail, r.Failures())
|
2021-11-08 12:13:59 +00:00
|
|
|
|
2022-03-14 11:30:07 +00:00
|
|
|
passNodes := [][]byte{{1}, {2}}
|
|
|
|
r.SubmitPassedStorageNodes(passNodes)
|
2021-11-08 12:13:59 +00:00
|
|
|
|
2022-03-14 11:30:07 +00:00
|
|
|
called1, called2 = false, false
|
2021-11-08 12:13:59 +00:00
|
|
|
|
2022-03-14 11:30:07 +00:00
|
|
|
require.EqualValues(t, 2, countPassNodes(func(arg []byte) {
|
|
|
|
if bytes.Equal(arg, passNodes[0]) {
|
|
|
|
called1 = true
|
|
|
|
} else if bytes.Equal(arg, passNodes[1]) {
|
|
|
|
called2 = true
|
|
|
|
}
|
|
|
|
}))
|
|
|
|
require.True(t, called1)
|
|
|
|
require.True(t, called2)
|
2021-11-08 12:13:59 +00:00
|
|
|
|
2022-03-14 11:30:07 +00:00
|
|
|
failNodes := [][]byte{{3}, {4}}
|
|
|
|
r.SubmitFailedStorageNodes(failNodes)
|
|
|
|
|
|
|
|
called1, called2 = false, false
|
|
|
|
|
|
|
|
require.EqualValues(t, 2, countFailNodes(func(arg []byte) {
|
|
|
|
if bytes.Equal(arg, failNodes[0]) {
|
|
|
|
called1 = true
|
|
|
|
} else if bytes.Equal(arg, failNodes[1]) {
|
|
|
|
called2 = true
|
|
|
|
}
|
|
|
|
}))
|
|
|
|
require.True(t, called1)
|
|
|
|
require.True(t, called2)
|
|
|
|
}
|
2021-11-08 12:13:59 +00:00
|
|
|
|
2022-03-14 11:30:07 +00:00
|
|
|
func TestResultEncoding(t *testing.T) {
|
|
|
|
r := *audittest.Result()
|
2021-11-08 12:13:59 +00:00
|
|
|
|
2022-03-14 11:30:07 +00:00
|
|
|
t.Run("binary", func(t *testing.T) {
|
|
|
|
data := r.Marshal()
|
2021-11-08 12:13:59 +00:00
|
|
|
|
2022-03-14 11:30:07 +00:00
|
|
|
var r2 audit.Result
|
|
|
|
require.NoError(t, r2.Unmarshal(data))
|
2021-11-08 12:13:59 +00:00
|
|
|
|
2022-03-14 11:30:07 +00:00
|
|
|
require.Equal(t, r, r2)
|
2021-11-08 12:13:59 +00:00
|
|
|
})
|
|
|
|
}
|