Enable and fix statdb tests (#2270)

This commit is contained in:
Maximillian von Briesen 2019-06-21 11:23:15 -04:00 committed by GitHub
parent 169fc9594c
commit bfcfe39313
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -19,13 +19,7 @@ import (
"storj.io/storj/satellite/satellitedb/satellitedbtest"
)
func getRatio(success, total int64) (ratio float64) {
ratio = float64(success) / float64(total)
return ratio
}
func TestStatDB(t *testing.T) {
t.Skip("team green skipped until V3-1843 is implemented")
satellitedbtest.Run(t, func(t *testing.T, db satellite.DB) {
ctx := testcontext.New(t)
defer ctx.Cleanup()
@ -35,57 +29,53 @@ func TestStatDB(t *testing.T) {
}
func testDatabase(ctx context.Context, t *testing.T, cache overlay.DB) {
nodeID := storj.NodeID{1, 2, 3, 4, 5}
currAuditSuccess := int64(4)
currAuditCount := int64(10)
currUptimeSuccess := int64(8)
currUptimeCount := int64(25)
{ // TestKnownUnreliableOrOffline
for _, tt := range []struct {
nodeID storj.NodeID
auditSuccessCount int64
auditCount int64
auditSuccessRatio float64
uptimeSuccessCount int64
uptimeCount int64
uptimeRatio float64
nodeID storj.NodeID
auditAlpha float64
auditBeta float64
uptimeAlpha float64
uptimeBeta float64
}{
{storj.NodeID{1}, 20, 20, 1.0, 20, 20, 1.0}, // good ratios => good
{storj.NodeID{2}, 5, 20, 0.25, 20, 20, 1}, // bad audit success, good uptime => bad
{storj.NodeID{3}, 20, 20, 1.0, 5, 20, 0.25}, // good audit success, bad uptime => bad
{storj.NodeID{4}, 0, 0, 0.0, 20, 20, 1.0}, // "bad" audit success, no audits => now considered bad
{storj.NodeID{5}, 20, 20, 1.0, 0, 0, 0.25}, // "bad" uptime success, no checks => now considered bad
{storj.NodeID{6}, 0, 1, 0.0, 5, 5, .01}, // bad audit success exactly one audit => bad
{storj.NodeID{7}, 0, 20, 0.0, 20, 20, 1.0}, // impossible math, but good ratios => good
{storj.NodeID{1}, 20, 0, 20, 0}, // good reputations => good
{storj.NodeID{2}, 0, 20, 20, 0}, // bad audit rep, good uptime rep => bad
{storj.NodeID{3}, 20, 0, 0, 20}, // good audit rep, bad uptime rep => bad
} {
nodeSelection := overlay.NodeSelectionConfig{
AuditSuccessRatio: tt.auditSuccessRatio,
UptimeRatio: tt.uptimeRatio,
AuditCount: tt.auditCount,
UptimeCount: tt.uptimeCount,
startingRep := overlay.NodeSelectionConfig{
AuditReputationAlpha0: tt.auditAlpha,
AuditReputationBeta0: tt.auditBeta,
UptimeReputationAlpha0: tt.uptimeAlpha,
UptimeReputationBeta0: tt.uptimeBeta,
}
err := cache.UpdateAddress(ctx, &pb.Node{Id: tt.nodeID}, nodeSelection)
err := cache.UpdateAddress(ctx, &pb.Node{Id: tt.nodeID}, startingRep)
require.NoError(t, err)
// update stats so node disqualification is triggered
_, err = cache.UpdateStats(ctx, &overlay.UpdateRequest{
NodeID: tt.nodeID,
AuditSuccess: true,
IsUp: true,
AuditLambda: 1, AuditWeight: 1,
UptimeLambda: 1, UptimeWeight: 1,
AuditDQ: 0.9, UptimeDQ: 0.9,
})
require.NoError(t, err)
}
nodeIds := storj.NodeIDList{
storj.NodeID{1}, storj.NodeID{2},
storj.NodeID{3}, storj.NodeID{4},
storj.NodeID{5}, storj.NodeID{6},
}
criteria := &overlay.NodeCriteria{OnlineWindow: time.Hour}
invalid, err := cache.KnownUnreliableOrOffline(ctx, criteria, nodeIds)
require.NoError(t, err)
assert.Contains(t, invalid, storj.NodeID{2})
assert.Contains(t, invalid, storj.NodeID{3})
assert.Contains(t, invalid, storj.NodeID{4})
assert.Contains(t, invalid, storj.NodeID{5})
assert.Contains(t, invalid, storj.NodeID{6})
assert.Len(t, invalid, 5)
require.Contains(t, invalid, storj.NodeID{2}) // bad audit
require.Contains(t, invalid, storj.NodeID{3}) // bad uptime
require.Contains(t, invalid, storj.NodeID{4}) // not in db
require.Len(t, invalid, 3)
}
{ // TestUpdateOperator
@ -99,16 +89,16 @@ func testDatabase(ctx context.Context, t *testing.T, cache overlay.DB) {
Email: "abc123@mail.test",
},
})
require.NoError(t, err)
assert.NotNil(t, update)
require.NotNil(t, update)
require.Equal(t, "0x1111111111111111111111111111111111111111", update.Operator.Wallet)
require.Equal(t, "abc123@mail.test", update.Operator.Email)
found, err := cache.Get(ctx, nodeID)
assert.NotNil(t, found)
require.NoError(t, err)
assert.Equal(t, "0x1111111111111111111111111111111111111111", update.Operator.Wallet)
assert.Equal(t, "abc123@mail.test", update.Operator.Email)
require.NotNil(t, found)
require.Equal(t, "0x1111111111111111111111111111111111111111", found.Operator.Wallet)
require.Equal(t, "abc123@mail.test", found.Operator.Email)
updateEmail, err := cache.UpdateNodeInfo(ctx, nodeID, &pb.InfoResponse{
Operator: &pb.NodeOperator{
@ -136,87 +126,86 @@ func testDatabase(ctx context.Context, t *testing.T, cache overlay.DB) {
}
{ // TestUpdateExists
auditSuccessRatio := getRatio(currAuditSuccess, currAuditCount)
uptimeRatio := getRatio(currUptimeSuccess, currUptimeCount)
nodeID := storj.NodeID{1}
node, err := cache.Get(ctx, nodeID)
require.NoError(t, err)
assert.EqualValues(t, currAuditCount, node.Reputation.AuditCount)
assert.EqualValues(t, currAuditSuccess, node.Reputation.AuditSuccessCount)
assert.EqualValues(t, auditSuccessRatio, node.Reputation.AuditSuccessRatio)
assert.EqualValues(t, currUptimeCount, node.Reputation.UptimeCount)
assert.EqualValues(t, currUptimeSuccess, node.Reputation.UptimeSuccessCount)
assert.EqualValues(t, uptimeRatio, node.Reputation.UptimeRatio)
auditAlpha := node.Reputation.AuditReputationAlpha
auditBeta := node.Reputation.AuditReputationBeta
uptimeAlpha := node.Reputation.UptimeReputationAlpha
uptimeBeta := node.Reputation.UptimeReputationBeta
updateReq := &overlay.UpdateRequest{
NodeID: nodeID,
AuditSuccess: true,
IsUp: false,
IsUp: true,
AuditLambda: 0.123, AuditWeight: 0.456,
UptimeLambda: 0.789, UptimeWeight: 0.876,
AuditDQ: 0, UptimeDQ: 0, // don't disqualify for any reason
}
stats, err := cache.UpdateStats(ctx, updateReq)
require.NoError(t, err)
currAuditSuccess++
currAuditCount++
currUptimeCount++
newAuditRatio := getRatio(currAuditSuccess, currAuditCount)
newUptimeRatio := getRatio(currUptimeSuccess, currUptimeCount)
expectedAuditAlpha := updateReq.AuditLambda*auditAlpha + updateReq.AuditWeight
expectedAuditBeta := updateReq.AuditLambda * auditBeta
expectedUptimeAlpha := updateReq.UptimeLambda*uptimeAlpha + updateReq.UptimeWeight
expectedUptimeBeta := updateReq.UptimeLambda * uptimeBeta
require.EqualValues(t, stats.AuditReputationAlpha, expectedAuditAlpha)
require.EqualValues(t, stats.AuditReputationBeta, expectedAuditBeta)
require.EqualValues(t, stats.UptimeReputationAlpha, expectedUptimeAlpha)
require.EqualValues(t, stats.UptimeReputationBeta, expectedUptimeBeta)
auditAlpha = expectedAuditAlpha
auditBeta = expectedAuditBeta
uptimeAlpha = expectedUptimeAlpha
uptimeBeta = expectedUptimeBeta
updateReq.AuditSuccess = false
updateReq.IsUp = false
stats, err = cache.UpdateStats(ctx, updateReq)
require.NoError(t, err)
expectedAuditAlpha = updateReq.AuditLambda * auditAlpha
expectedAuditBeta = updateReq.AuditLambda*auditBeta + updateReq.AuditWeight
expectedUptimeAlpha = updateReq.UptimeLambda * uptimeAlpha
expectedUptimeBeta = updateReq.UptimeLambda*uptimeBeta + updateReq.UptimeWeight
require.EqualValues(t, stats.AuditReputationAlpha, expectedAuditAlpha)
require.EqualValues(t, stats.AuditReputationBeta, expectedAuditBeta)
require.EqualValues(t, stats.UptimeReputationAlpha, expectedUptimeAlpha)
require.EqualValues(t, stats.UptimeReputationBeta, expectedUptimeBeta)
assert.EqualValues(t, newAuditRatio, stats.AuditSuccessRatio)
assert.EqualValues(t, newUptimeRatio, stats.UptimeRatio)
}
{ // TestUpdateUptimeExists
auditSuccessRatio := getRatio(currAuditSuccess, currAuditCount)
uptimeRatio := getRatio(currUptimeSuccess, currUptimeCount)
nodeID := storj.NodeID{1}
node, err := cache.Get(ctx, nodeID)
require.NoError(t, err)
assert.EqualValues(t, currAuditCount, node.Reputation.AuditCount)
assert.EqualValues(t, currAuditSuccess, node.Reputation.AuditSuccessCount)
assert.EqualValues(t, auditSuccessRatio, node.Reputation.AuditSuccessRatio)
assert.EqualValues(t, currUptimeCount, node.Reputation.UptimeCount)
assert.EqualValues(t, currUptimeSuccess, node.Reputation.UptimeSuccessCount)
assert.EqualValues(t, uptimeRatio, node.Reputation.UptimeRatio)
alpha := node.Reputation.UptimeReputationAlpha
beta := node.Reputation.UptimeReputationBeta
stats, err := cache.UpdateUptime(ctx, nodeID, false, 1, 1, 0.5)
lambda := 0.789
weight := 0.876
dq := float64(0) // don't disqualify for any reason
stats, err := cache.UpdateUptime(ctx, nodeID, false, lambda, weight, dq)
require.NoError(t, err)
currUptimeCount++
newUptimeRatio := getRatio(currUptimeSuccess, currUptimeCount)
assert.EqualValues(t, auditSuccessRatio, stats.AuditSuccessRatio)
assert.EqualValues(t, currAuditCount, stats.AuditCount)
assert.EqualValues(t, newUptimeRatio, stats.UptimeRatio)
}
expectedAlpha := lambda * alpha
expectedBeta := lambda*beta + weight
require.EqualValues(t, stats.UptimeReputationAlpha, expectedAlpha)
require.EqualValues(t, stats.UptimeReputationBeta, expectedBeta)
{ // TestUpdateStatsExists
auditSuccessRatio := getRatio(currAuditSuccess, currAuditCount)
uptimeRatio := getRatio(currUptimeSuccess, currUptimeCount)
alpha = expectedAlpha
beta = expectedBeta
node, err := cache.Get(ctx, nodeID)
stats, err = cache.UpdateUptime(ctx, nodeID, true, lambda, weight, dq)
require.NoError(t, err)
assert.EqualValues(t, currAuditCount, node.Reputation.AuditCount)
assert.EqualValues(t, currAuditSuccess, node.Reputation.AuditSuccessCount)
assert.EqualValues(t, auditSuccessRatio, node.Reputation.AuditSuccessRatio)
assert.EqualValues(t, currUptimeCount, node.Reputation.UptimeCount)
assert.EqualValues(t, currUptimeSuccess, node.Reputation.UptimeSuccessCount)
assert.EqualValues(t, uptimeRatio, node.Reputation.UptimeRatio)
stats, err := cache.UpdateStats(ctx, &overlay.UpdateRequest{
NodeID: nodeID,
IsUp: true,
AuditSuccess: false,
})
require.NoError(t, err)
currAuditCount++
newAuditRatio := getRatio(stats.AuditSuccessCount, stats.AuditCount)
assert.EqualValues(t, newAuditRatio, stats.AuditSuccessRatio)
assert.EqualValues(t, currAuditCount, stats.AuditCount)
newUptimeRatio := getRatio(stats.UptimeSuccessCount, stats.UptimeCount)
assert.EqualValues(t, newUptimeRatio, stats.UptimeRatio)
expectedAlpha = lambda*alpha + weight
expectedBeta = lambda * beta
require.EqualValues(t, stats.UptimeReputationAlpha, expectedAlpha)
require.EqualValues(t, stats.UptimeReputationBeta, expectedBeta)
}
}