2019-08-21 16:49:27 +01:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package audit_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"math/rand"
|
2020-06-12 07:35:26 +01:00
|
|
|
"strconv"
|
2019-08-21 16:49:27 +01:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
2019-12-27 11:48:47 +00:00
|
|
|
"storj.io/common/memory"
|
|
|
|
"storj.io/common/testcontext"
|
|
|
|
"storj.io/common/testrand"
|
2019-11-14 19:46:15 +00:00
|
|
|
"storj.io/storj/private/testplanet"
|
2019-08-21 16:49:27 +01:00
|
|
|
"storj.io/storj/satellite/audit"
|
|
|
|
)
|
|
|
|
|
2020-12-14 12:54:22 +00:00
|
|
|
// TestAuditCollector does the following:
|
2019-08-21 16:49:27 +01:00
|
|
|
// - start testplanet with 5 nodes and a reservoir size of 3
|
|
|
|
// - upload 5 files
|
|
|
|
// - iterate over all the segments in satellite.Metainfo and store them in allPieces map
|
|
|
|
// - create a audit observer and call metaloop.Join(auditObs)
|
|
|
|
//
|
|
|
|
// Then for every node in testplanet:
|
|
|
|
// - expect that there is a reservoir for that node on the audit observer
|
|
|
|
// - that the reservoir size is <= 2 (the maxReservoirSize)
|
|
|
|
// - that every item in the reservoir is unique
|
2020-12-14 12:54:22 +00:00
|
|
|
func TestAuditCollector(t *testing.T) {
|
2019-08-21 16:49:27 +01:00
|
|
|
testplanet.Run(t, testplanet.Config{
|
|
|
|
SatelliteCount: 1, StorageNodeCount: 5, UplinkCount: 1,
|
2020-01-21 10:38:41 +00:00
|
|
|
Reconfigure: testplanet.Reconfigure{
|
|
|
|
Satellite: testplanet.ReconfigureRS(3, 4, 5, 5),
|
|
|
|
},
|
2019-08-21 16:49:27 +01:00
|
|
|
}, func(t *testing.T, ctx *testcontext.Context, planet *testplanet.Planet) {
|
|
|
|
satellite := planet.Satellites[0]
|
2019-09-11 23:37:01 +01:00
|
|
|
satellite.Audit.Worker.Loop.Pause()
|
2019-08-21 16:49:27 +01:00
|
|
|
|
|
|
|
ul := planet.Uplinks[0]
|
|
|
|
|
|
|
|
// upload 5 remote files with 1 segment
|
|
|
|
for i := 0; i < 5; i++ {
|
|
|
|
testData := testrand.Bytes(8 * memory.KiB)
|
2020-06-12 07:35:26 +01:00
|
|
|
path := "/some/remote/path/" + strconv.Itoa(i)
|
2020-01-21 10:38:41 +00:00
|
|
|
err := ul.Upload(ctx, satellite, "testbucket", path, testData)
|
2019-08-21 16:49:27 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
r := rand.New(rand.NewSource(time.Now().Unix()))
|
2020-12-14 12:54:22 +00:00
|
|
|
observer := audit.NewCollector(4, r)
|
2019-09-11 23:37:01 +01:00
|
|
|
err := satellite.Metainfo.Loop.Join(ctx, observer)
|
2019-08-21 16:49:27 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
for _, node := range planet.StorageNodes {
|
|
|
|
// expect a reservoir for every node
|
|
|
|
require.NotNil(t, observer.Reservoirs[node.ID()])
|
2020-12-14 12:54:22 +00:00
|
|
|
require.True(t, len(observer.Reservoirs[node.ID()].Segments) > 1)
|
2019-08-21 16:49:27 +01:00
|
|
|
|
2020-12-14 12:54:22 +00:00
|
|
|
// Require that len segments are <= 3 even though the Collector was instantiated with 4
|
2019-09-05 16:40:52 +01:00
|
|
|
// because the maxReservoirSize is currently 3.
|
2020-12-14 12:54:22 +00:00
|
|
|
require.True(t, len(observer.Reservoirs[node.ID()].Segments) <= 3)
|
2019-08-21 16:49:27 +01:00
|
|
|
|
2020-12-14 12:54:22 +00:00
|
|
|
repeats := make(map[audit.Segment]bool)
|
|
|
|
for _, segment := range observer.Reservoirs[node.ID()].Segments {
|
|
|
|
assert.False(t, repeats[segment], "expected every item in reservoir to be unique")
|
|
|
|
repeats[segment] = true
|
2019-08-21 16:49:27 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|