2019-05-22 15:50:22 +01:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package audit_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2019-07-02 16:16:25 +01:00
|
|
|
"github.com/stretchr/testify/assert"
|
2019-05-22 15:50:22 +01:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
2019-12-27 11:48:47 +00:00
|
|
|
"storj.io/common/pkcrypto"
|
|
|
|
"storj.io/common/testcontext"
|
|
|
|
"storj.io/common/testrand"
|
2019-11-14 19:46:15 +00:00
|
|
|
"storj.io/storj/private/testplanet"
|
2019-07-28 06:55:36 +01:00
|
|
|
"storj.io/storj/satellite/audit"
|
|
|
|
"storj.io/storj/satellite/overlay"
|
2019-05-22 15:50:22 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestContainIncrementAndGet(t *testing.T) {
|
|
|
|
testplanet.Run(t, testplanet.Config{
|
2019-07-02 16:16:25 +01:00
|
|
|
SatelliteCount: 1, StorageNodeCount: 2,
|
2019-05-22 15:50:22 +01:00
|
|
|
}, func(t *testing.T, ctx *testcontext.Context, planet *testplanet.Planet) {
|
2019-07-02 16:16:25 +01:00
|
|
|
containment := planet.Satellites[0].DB.Containment()
|
|
|
|
cache := planet.Satellites[0].DB.OverlayCache()
|
|
|
|
|
2019-05-22 15:50:22 +01:00
|
|
|
input := &audit.PendingAudit{
|
|
|
|
NodeID: planet.StorageNodes[0].ID(),
|
2019-06-26 11:38:51 +01:00
|
|
|
ExpectedShareHash: pkcrypto.SHA256Hash(testrand.Bytes(10)),
|
2019-05-22 15:50:22 +01:00
|
|
|
}
|
|
|
|
|
2019-07-02 16:16:25 +01:00
|
|
|
err := containment.IncrementPending(ctx, input)
|
2019-05-22 15:50:22 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2019-07-02 16:16:25 +01:00
|
|
|
output, err := containment.Get(ctx, input.NodeID)
|
2019-05-22 15:50:22 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2019-07-02 16:16:25 +01:00
|
|
|
assert.Equal(t, input, output)
|
2019-05-22 15:50:22 +01:00
|
|
|
|
|
|
|
// check contained flag set to true
|
2019-07-02 16:16:25 +01:00
|
|
|
node, err := cache.Get(ctx, input.NodeID)
|
2019-05-22 15:50:22 +01:00
|
|
|
require.NoError(t, err)
|
2019-07-02 16:16:25 +01:00
|
|
|
assert.True(t, node.Contained)
|
2019-05-22 15:50:22 +01:00
|
|
|
|
|
|
|
nodeID1 := planet.StorageNodes[1].ID()
|
2019-07-02 16:16:25 +01:00
|
|
|
_, err = containment.Get(ctx, nodeID1)
|
2019-08-21 17:30:29 +01:00
|
|
|
require.Error(t, err, audit.ErrContainedNotFound.New("%v", nodeID1))
|
2019-07-02 16:16:25 +01:00
|
|
|
assert.True(t, audit.ErrContainedNotFound.Has(err))
|
2019-05-22 15:50:22 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestContainIncrementPendingEntryExists(t *testing.T) {
|
|
|
|
testplanet.Run(t, testplanet.Config{
|
2019-07-02 16:16:25 +01:00
|
|
|
SatelliteCount: 1, StorageNodeCount: 1,
|
2019-05-22 15:50:22 +01:00
|
|
|
}, func(t *testing.T, ctx *testcontext.Context, planet *testplanet.Planet) {
|
2019-07-02 16:16:25 +01:00
|
|
|
containment := planet.Satellites[0].DB.Containment()
|
|
|
|
|
2019-05-22 15:50:22 +01:00
|
|
|
info1 := &audit.PendingAudit{
|
|
|
|
NodeID: planet.StorageNodes[0].ID(),
|
2019-06-26 11:38:51 +01:00
|
|
|
ExpectedShareHash: pkcrypto.SHA256Hash(testrand.Bytes(10)),
|
2019-05-22 15:50:22 +01:00
|
|
|
}
|
|
|
|
|
2019-07-02 16:16:25 +01:00
|
|
|
err := containment.IncrementPending(ctx, info1)
|
2019-05-22 15:50:22 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
info2 := &audit.PendingAudit{
|
|
|
|
NodeID: info1.NodeID,
|
|
|
|
StripeIndex: 1,
|
|
|
|
ShareSize: 1,
|
2019-06-26 11:38:51 +01:00
|
|
|
ExpectedShareHash: pkcrypto.SHA256Hash(testrand.Bytes(10)),
|
2019-05-22 15:50:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// expect failure when an entry with the same nodeID but different expected share data already exists
|
2019-07-02 16:16:25 +01:00
|
|
|
err = containment.IncrementPending(ctx, info2)
|
|
|
|
assert.True(t, audit.ErrAlreadyExists.Has(err))
|
2019-05-22 15:50:22 +01:00
|
|
|
|
|
|
|
// expect reverify count for an entry to be 0 after first IncrementPending call
|
2019-07-02 16:16:25 +01:00
|
|
|
pending, err := containment.Get(ctx, info1.NodeID)
|
2019-05-22 15:50:22 +01:00
|
|
|
require.NoError(t, err)
|
2019-07-02 16:16:25 +01:00
|
|
|
assert.EqualValues(t, 0, pending.ReverifyCount)
|
2019-05-22 15:50:22 +01:00
|
|
|
|
|
|
|
// expect reverify count to be 1 after second IncrementPending call
|
2019-07-02 16:16:25 +01:00
|
|
|
err = containment.IncrementPending(ctx, info1)
|
2019-05-22 15:50:22 +01:00
|
|
|
require.NoError(t, err)
|
2019-07-02 16:16:25 +01:00
|
|
|
pending, err = containment.Get(ctx, info1.NodeID)
|
2019-05-22 15:50:22 +01:00
|
|
|
require.NoError(t, err)
|
2019-07-02 16:16:25 +01:00
|
|
|
assert.EqualValues(t, 1, pending.ReverifyCount)
|
2019-05-22 15:50:22 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestContainDelete(t *testing.T) {
|
|
|
|
testplanet.Run(t, testplanet.Config{
|
2019-07-02 16:16:25 +01:00
|
|
|
SatelliteCount: 1, StorageNodeCount: 1,
|
2019-05-22 15:50:22 +01:00
|
|
|
}, func(t *testing.T, ctx *testcontext.Context, planet *testplanet.Planet) {
|
2019-07-02 16:16:25 +01:00
|
|
|
containment := planet.Satellites[0].DB.Containment()
|
|
|
|
cache := planet.Satellites[0].DB.OverlayCache()
|
|
|
|
|
2019-05-22 15:50:22 +01:00
|
|
|
info1 := &audit.PendingAudit{
|
|
|
|
NodeID: planet.StorageNodes[0].ID(),
|
2019-06-26 11:38:51 +01:00
|
|
|
ExpectedShareHash: pkcrypto.SHA256Hash(testrand.Bytes(10)),
|
2019-05-22 15:50:22 +01:00
|
|
|
}
|
|
|
|
|
2019-07-02 16:16:25 +01:00
|
|
|
err := containment.IncrementPending(ctx, info1)
|
2019-05-22 15:50:22 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2019-07-02 16:16:25 +01:00
|
|
|
// delete the node from containment db
|
|
|
|
isDeleted, err := containment.Delete(ctx, info1.NodeID)
|
2019-05-22 15:50:22 +01:00
|
|
|
require.NoError(t, err)
|
2019-07-02 16:16:25 +01:00
|
|
|
assert.True(t, isDeleted)
|
2019-05-22 15:50:22 +01:00
|
|
|
|
|
|
|
// check contained flag set to false
|
2019-07-02 16:16:25 +01:00
|
|
|
node, err := cache.Get(ctx, info1.NodeID)
|
2019-05-22 15:50:22 +01:00
|
|
|
require.NoError(t, err)
|
2019-07-02 16:16:25 +01:00
|
|
|
assert.False(t, node.Contained)
|
2019-05-22 15:50:22 +01:00
|
|
|
|
|
|
|
// get pending audit that doesn't exist
|
2019-07-02 16:16:25 +01:00
|
|
|
_, err = containment.Get(ctx, info1.NodeID)
|
2019-08-21 17:30:29 +01:00
|
|
|
assert.Error(t, err, audit.ErrContainedNotFound.New("%v", info1.NodeID))
|
2019-07-02 16:16:25 +01:00
|
|
|
assert.True(t, audit.ErrContainedNotFound.Has(err))
|
2019-05-22 15:50:22 +01:00
|
|
|
|
|
|
|
// delete pending audit that doesn't exist
|
2019-07-02 16:16:25 +01:00
|
|
|
isDeleted, err = containment.Delete(ctx, info1.NodeID)
|
2019-05-22 15:50:22 +01:00
|
|
|
require.NoError(t, err)
|
2019-07-02 16:16:25 +01:00
|
|
|
assert.False(t, isDeleted)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestContainUpdateStats(t *testing.T) {
|
|
|
|
testplanet.Run(t, testplanet.Config{
|
|
|
|
SatelliteCount: 1, StorageNodeCount: 1,
|
|
|
|
}, func(t *testing.T, ctx *testcontext.Context, planet *testplanet.Planet) {
|
|
|
|
containment := planet.Satellites[0].DB.Containment()
|
|
|
|
cache := planet.Satellites[0].DB.OverlayCache()
|
|
|
|
|
|
|
|
info1 := &audit.PendingAudit{
|
|
|
|
NodeID: planet.StorageNodes[0].ID(),
|
|
|
|
ExpectedShareHash: pkcrypto.SHA256Hash(testrand.Bytes(10)),
|
|
|
|
}
|
|
|
|
|
|
|
|
err := containment.IncrementPending(ctx, info1)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// update node stats
|
2020-05-20 20:57:53 +01:00
|
|
|
_, err = planet.Satellites[0].Overlay.Service.BatchUpdateStats(ctx, []*overlay.UpdateRequest{{NodeID: info1.NodeID}})
|
2019-07-02 16:16:25 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// check contained flag set to false
|
|
|
|
node, err := cache.Get(ctx, info1.NodeID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.False(t, node.Contained)
|
|
|
|
|
|
|
|
// get pending audit that doesn't exist
|
|
|
|
_, err = containment.Get(ctx, info1.NodeID)
|
2019-08-21 17:30:29 +01:00
|
|
|
assert.Error(t, err, audit.ErrContainedNotFound.New("%v", info1.NodeID))
|
2019-07-02 16:16:25 +01:00
|
|
|
assert.True(t, audit.ErrContainedNotFound.Has(err))
|
2019-05-22 15:50:22 +01:00
|
|
|
})
|
|
|
|
}
|