2019-04-02 15:55:58 +01:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package inspector_test
|
|
|
|
|
|
|
|
import (
|
2020-12-16 15:25:27 +00:00
|
|
|
"encoding/binary"
|
|
|
|
"errors"
|
2019-04-02 15:55:58 +01:00
|
|
|
"testing"
|
|
|
|
|
2020-12-16 15:25:27 +00:00
|
|
|
"github.com/btcsuite/btcutil/base58"
|
2019-04-02 15:55:58 +01:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
2020-12-16 15:25:27 +00:00
|
|
|
"storj.io/common/encryption"
|
2019-12-27 11:48:47 +00:00
|
|
|
"storj.io/common/memory"
|
2020-12-16 15:25:27 +00:00
|
|
|
"storj.io/common/paths"
|
|
|
|
"storj.io/common/pb"
|
2019-12-27 11:48:47 +00:00
|
|
|
"storj.io/common/storj"
|
|
|
|
"storj.io/common/testcontext"
|
|
|
|
"storj.io/common/testrand"
|
2019-11-14 19:46:15 +00:00
|
|
|
"storj.io/storj/private/testplanet"
|
2020-10-30 11:12:01 +00:00
|
|
|
"storj.io/storj/satellite/internalpb"
|
2021-04-21 13:42:57 +01:00
|
|
|
"storj.io/storj/satellite/metabase"
|
2020-02-21 14:07:29 +00:00
|
|
|
"storj.io/uplink/private/eestream"
|
2019-04-02 15:55:58 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestInspectorStats(t *testing.T) {
|
2019-04-22 10:07:50 +01:00
|
|
|
testplanet.Run(t, testplanet.Config{
|
|
|
|
SatelliteCount: 1, StorageNodeCount: 6, UplinkCount: 1,
|
|
|
|
}, func(t *testing.T, ctx *testcontext.Context, planet *testplanet.Planet) {
|
2020-12-16 15:25:27 +00:00
|
|
|
satellite := planet.Satellites[0]
|
|
|
|
upl := planet.Uplinks[0]
|
2019-06-26 11:38:51 +01:00
|
|
|
testData := testrand.Bytes(1 * memory.MiB)
|
2019-04-22 10:07:50 +01:00
|
|
|
|
|
|
|
bucket := "testbucket"
|
2020-12-16 15:25:27 +00:00
|
|
|
projectID := upl.Projects[0].ID
|
2019-04-22 10:07:50 +01:00
|
|
|
|
2020-12-16 15:25:27 +00:00
|
|
|
err := upl.Upload(ctx, planet.Satellites[0], bucket, "test/path", testData)
|
2019-04-22 10:07:50 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
healthEndpoint := planet.Satellites[0].Inspector.Endpoint
|
|
|
|
|
|
|
|
// Get path of random segment we just uploaded and check the health
|
2020-12-16 15:25:27 +00:00
|
|
|
access := upl.Access[satellite.ID()]
|
|
|
|
serializedAccess, err := access.Serialize()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
store, err := encryptionAccess(serializedAccess)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
encryptedPath, err := encryption.EncryptPathWithStoreCipher(bucket, paths.NewUnencrypted("test/path"), store)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
objectLocation := metabase.ObjectLocation{
|
|
|
|
ProjectID: projectID,
|
|
|
|
BucketName: "testbucket",
|
|
|
|
ObjectKey: metabase.ObjectKey(encryptedPath.Raw()),
|
|
|
|
}
|
|
|
|
|
2021-09-07 09:15:47 +01:00
|
|
|
segment, err := satellite.Metabase.DB.GetLatestObjectLastSegment(ctx, metabase.GetLatestObjectLastSegment{
|
2020-12-16 15:25:27 +00:00
|
|
|
ObjectLocation: objectLocation,
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
{ // Test Segment Health Request
|
|
|
|
req := &internalpb.SegmentHealthRequest{
|
|
|
|
ProjectId: projectID[:],
|
|
|
|
EncryptedPath: []byte(encryptedPath.Raw()),
|
|
|
|
Bucket: []byte(bucket),
|
|
|
|
SegmentIndex: int64(segment.Position.Encode()),
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := healthEndpoint.SegmentHealth(ctx, req)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
redundancy, err := eestream.NewRedundancyStrategyFromProto(resp.GetRedundancy())
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
require.Equal(t, 4, redundancy.TotalCount())
|
|
|
|
encodedPosition := binary.LittleEndian.Uint64(resp.GetHealth().GetSegment())
|
|
|
|
position := metabase.SegmentPositionFromEncoded(encodedPosition)
|
|
|
|
require.Equal(t, segment.Position, position)
|
|
|
|
}
|
|
|
|
|
|
|
|
{ // Test Object Health Request
|
|
|
|
objectHealthReq := &internalpb.ObjectHealthRequest{
|
|
|
|
ProjectId: projectID[:],
|
|
|
|
EncryptedPath: []byte(encryptedPath.Raw()),
|
|
|
|
Bucket: []byte(bucket),
|
|
|
|
StartAfterSegment: 0,
|
|
|
|
EndBeforeSegment: 0,
|
|
|
|
Limit: 0,
|
|
|
|
}
|
|
|
|
resp, err := healthEndpoint.ObjectHealth(ctx, objectHealthReq)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
segments := resp.GetSegments()
|
|
|
|
require.Len(t, segments, 1)
|
|
|
|
|
|
|
|
redundancy, err := eestream.NewRedundancyStrategyFromProto(resp.GetRedundancy())
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
require.Equal(t, 4, redundancy.TotalCount())
|
|
|
|
encodedPosition := binary.LittleEndian.Uint64(segments[0].GetSegment())
|
|
|
|
position := metabase.SegmentPositionFromEncoded(encodedPosition)
|
|
|
|
require.Equal(t, segment.Position, position)
|
|
|
|
}
|
|
|
|
|
2019-04-22 10:07:50 +01:00
|
|
|
})
|
2019-04-02 15:55:58 +01:00
|
|
|
}
|
2020-12-16 15:25:27 +00:00
|
|
|
|
|
|
|
func encryptionAccess(access string) (*encryption.Store, error) {
|
|
|
|
data, version, err := base58.CheckDecode(access)
|
|
|
|
if err != nil || version != 0 {
|
|
|
|
return nil, errors.New("invalid access grant format")
|
|
|
|
}
|
|
|
|
|
|
|
|
p := new(pb.Scope)
|
|
|
|
if err := pb.Unmarshal(data, p); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
key, err := storj.NewKey(p.EncryptionAccess.DefaultKey)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
store := encryption.NewStore()
|
|
|
|
store.SetDefaultKey(key)
|
|
|
|
store.SetDefaultPathCipher(storj.EncAESGCM)
|
|
|
|
|
|
|
|
return store, nil
|
|
|
|
}
|