2022-04-12 19:01:14 +01:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package reports_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"go.uber.org/zap/zaptest"
|
|
|
|
|
2022-05-26 00:31:42 +01:00
|
|
|
"storj.io/common/uuid"
|
2022-04-12 19:01:14 +01:00
|
|
|
"storj.io/storj/cmd/satellite/reports"
|
|
|
|
"storj.io/storj/satellite/attribution"
|
|
|
|
)
|
|
|
|
|
2022-05-26 00:31:42 +01:00
|
|
|
func TestProcessAttributions(t *testing.T) {
|
2022-04-12 19:01:14 +01:00
|
|
|
log := zaptest.NewLogger(t)
|
2022-05-26 00:31:42 +01:00
|
|
|
|
|
|
|
requireSum := func(total reports.Total, n int) {
|
|
|
|
require.Equal(t, float64(n), total.ByteHours)
|
|
|
|
require.Equal(t, float64(n), total.SegmentHours)
|
|
|
|
require.Equal(t, float64(n), total.ObjectHours)
|
|
|
|
require.Equal(t, float64(n), total.BucketHours)
|
|
|
|
require.Equal(t, int64(n), total.BytesEgress)
|
|
|
|
}
|
|
|
|
|
|
|
|
newUsage := func(userAgent string, projectID uuid.UUID, bucketName string) *attribution.BucketUsage {
|
|
|
|
return &attribution.BucketUsage{
|
|
|
|
UserAgent: []byte(userAgent),
|
|
|
|
ProjectID: projectID.Bytes(),
|
|
|
|
BucketName: []byte(bucketName),
|
2022-04-12 19:01:14 +01:00
|
|
|
ByteHours: 1,
|
|
|
|
SegmentHours: 1,
|
|
|
|
ObjectHours: 1,
|
|
|
|
Hours: 1,
|
|
|
|
EgressData: 1,
|
2022-05-26 00:31:42 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
id, err := uuid.New()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// test empty user agents
|
|
|
|
attributions := []*attribution.BucketUsage{
|
|
|
|
newUsage("", id, ""),
|
2022-04-12 19:01:14 +01:00
|
|
|
{
|
|
|
|
ByteHours: 1,
|
|
|
|
SegmentHours: 1,
|
|
|
|
ObjectHours: 1,
|
|
|
|
Hours: 1,
|
|
|
|
EgressData: 1,
|
|
|
|
},
|
|
|
|
}
|
2022-05-26 00:31:42 +01:00
|
|
|
totals := reports.ProcessAttributions(attributions, nil, log)
|
2022-04-12 19:01:14 +01:00
|
|
|
require.Equal(t, 0, len(totals))
|
|
|
|
|
|
|
|
// test user agent with additional entries and uppercase letters is summed with
|
|
|
|
// the first one
|
2022-04-25 21:56:11 +01:00
|
|
|
attributions = []*attribution.BucketUsage{
|
2022-05-26 00:31:42 +01:00
|
|
|
newUsage("teststorj", id, ""),
|
|
|
|
newUsage("TESTSTORJ/other", id, ""),
|
2022-04-12 19:01:14 +01:00
|
|
|
}
|
2022-05-26 00:31:42 +01:00
|
|
|
totals = reports.ProcessAttributions(attributions, nil, log)
|
2022-04-12 19:01:14 +01:00
|
|
|
require.Equal(t, 1, len(totals))
|
2022-05-26 00:31:42 +01:00
|
|
|
requireSum(totals[reports.AttributionTotalsIndex{"teststorj", id.String(), ""}], 2)
|
2022-04-12 19:01:14 +01:00
|
|
|
|
|
|
|
// test two user agents are summed separately
|
2022-04-25 21:56:11 +01:00
|
|
|
attributions = []*attribution.BucketUsage{
|
2022-05-26 00:31:42 +01:00
|
|
|
newUsage("teststorj1", id, ""),
|
|
|
|
newUsage("teststorj1", id, ""),
|
|
|
|
newUsage("teststorj2", id, ""),
|
|
|
|
newUsage("teststorj2", id, ""),
|
|
|
|
}
|
|
|
|
totals = reports.ProcessAttributions(attributions, nil, log)
|
|
|
|
require.Equal(t, 2, len(totals))
|
|
|
|
requireSum(totals[reports.AttributionTotalsIndex{"teststorj1", id.String(), ""}], 2)
|
|
|
|
requireSum(totals[reports.AttributionTotalsIndex{"teststorj2", id.String(), ""}], 2)
|
|
|
|
|
|
|
|
// Test that different project IDs are summed separately
|
|
|
|
id2, err := uuid.New()
|
|
|
|
require.NoError(t, err)
|
|
|
|
attributions = []*attribution.BucketUsage{
|
|
|
|
newUsage("teststorj1", id, ""),
|
|
|
|
newUsage("teststorj1", id, ""),
|
|
|
|
newUsage("teststorj1", id2, ""),
|
|
|
|
}
|
|
|
|
totals = reports.ProcessAttributions(attributions, nil, log)
|
|
|
|
require.Equal(t, 2, len(totals))
|
|
|
|
requireSum(totals[reports.AttributionTotalsIndex{"teststorj1", id.String(), ""}], 2)
|
|
|
|
requireSum(totals[reports.AttributionTotalsIndex{"teststorj1", id2.String(), ""}], 1)
|
|
|
|
|
|
|
|
// Test that different bucket names are summed separately
|
|
|
|
attributions = []*attribution.BucketUsage{
|
|
|
|
newUsage("teststorj1", id, "1"),
|
|
|
|
newUsage("teststorj1", id, "1"),
|
|
|
|
newUsage("teststorj1", id, "2"),
|
|
|
|
}
|
|
|
|
totals = reports.ProcessAttributions(attributions, nil, log)
|
|
|
|
require.Equal(t, 2, len(totals))
|
|
|
|
requireSum(totals[reports.AttributionTotalsIndex{"teststorj1", id.String(), "1"}], 2)
|
|
|
|
requireSum(totals[reports.AttributionTotalsIndex{"teststorj1", id.String(), "2"}], 1)
|
|
|
|
|
|
|
|
// Test that unspecified user agents are filtered out
|
|
|
|
attributions = []*attribution.BucketUsage{
|
|
|
|
newUsage("teststorj1", id, ""),
|
|
|
|
newUsage("teststorj2", id, ""),
|
|
|
|
newUsage("teststorj3", id, ""),
|
2022-04-12 19:01:14 +01:00
|
|
|
}
|
2022-05-26 00:31:42 +01:00
|
|
|
totals = reports.ProcessAttributions(attributions, []string{"teststorj1", "teststorj3"}, log)
|
2022-04-12 19:01:14 +01:00
|
|
|
require.Equal(t, 2, len(totals))
|
2022-05-26 00:31:42 +01:00
|
|
|
require.Contains(t, totals, reports.AttributionTotalsIndex{"teststorj1", id.String(), ""})
|
|
|
|
require.Contains(t, totals, reports.AttributionTotalsIndex{"teststorj3", id.String(), ""})
|
2022-04-12 19:01:14 +01:00
|
|
|
}
|