storj/satellite/metainfo/objectdeletion/report_test.go
Yingrong Zhao 2b2cb468bf satellite/metainfo: implement DeleteObjectPieces with objectdeletion package
Use new objectdeletion package for deleting pointers.

In the best case scenario, it will make on database call to fetch
information about the number of segments. And another request to delete
and fetch information about other segments.

This PR also changes our object deletion API to return no error when an
object is not found but instead consider such operation as success. This
behavior is asligned with S3 API and makes the code less complex.

Change-Id: I280c56e8b5d815a8c4dafe8227689467e899775a
2020-08-06 16:10:13 -04:00

80 lines
2.0 KiB
Go

// Copyright (C) 2020 Storj Labs, Inc.
// See LICENSE for copying information.
package objectdeletion_test
import (
"strconv"
"testing"
"github.com/stretchr/testify/require"
"github.com/zeebo/errs"
"go.uber.org/zap/zaptest"
"storj.io/common/pb"
"storj.io/common/testcontext"
"storj.io/common/testrand"
"storj.io/storj/satellite/metainfo/objectdeletion"
)
func TestReport(t *testing.T) {
logger := zaptest.NewLogger(t)
var testCases = []struct {
description string
numRequests int
numDeletedPaths int
expectedFailure bool
}{
{"has-failure", 2, 1, true},
{"all-deleted", 2, 2, false},
}
for _, tt := range testCases {
tt := tt
t.Run(tt.description, func(t *testing.T) {
ctx := testcontext.New(t)
defer ctx.Cleanup()
requests := createRequests(tt.numRequests)
paths, pointers, err := createDeletedItems(requests, tt.numDeletedPaths)
require.NoError(t, err)
report := objectdeletion.GenerateReport(ctx, logger, requests, paths, pointers)
require.Equal(t, tt.expectedFailure, report.HasFailures())
})
}
}
func createDeletedItems(requests []*objectdeletion.ObjectIdentifier, numDeleted int) ([][]byte, []*pb.Pointer, error) {
if numDeleted > len(requests) {
return nil, nil, errs.New("invalid argument")
}
paths := make([][]byte, 0, numDeleted)
pointers := make([]*pb.Pointer, 0, numDeleted)
for i := 0; i < numDeleted; i++ {
path, err := requests[i].SegmentPath(int64(testrand.Intn(10)))
if err != nil {
return nil, nil, err
}
paths = append(paths, path)
pointers = append(pointers, &pb.Pointer{})
}
return paths, pointers, nil
}
func createRequests(numRequests int) []*objectdeletion.ObjectIdentifier {
requests := make([]*objectdeletion.ObjectIdentifier, 0, numRequests)
for i := 0; i < numRequests; i++ {
obj := objectdeletion.ObjectIdentifier{
ProjectID: testrand.UUID(),
Bucket: []byte("test"),
EncryptedPath: []byte(strconv.Itoa(i) + "test"),
}
requests = append(requests, &obj)
}
return requests
}