2022-06-14 18:25:52 +00:00
|
|
|
package paths_test
|
2021-05-19 13:50:48 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2023-03-29 19:24:07 +00:00
|
|
|
"io"
|
2021-05-19 13:50:48 +00:00
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
|
|
|
|
2022-06-14 15:00:51 +00:00
|
|
|
"github.com/golang/mock/gomock"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"golang.org/x/xerrors"
|
|
|
|
|
2021-05-19 13:50:48 +00:00
|
|
|
"github.com/filecoin-project/go-state-types/abi"
|
2022-06-14 15:00:51 +00:00
|
|
|
|
2022-06-14 18:25:52 +00:00
|
|
|
"github.com/filecoin-project/lotus/storage/paths"
|
2022-06-15 10:06:22 +00:00
|
|
|
"github.com/filecoin-project/lotus/storage/paths/mocks"
|
2022-06-14 18:03:38 +00:00
|
|
|
"github.com/filecoin-project/lotus/storage/sealer/partialfile"
|
2022-06-15 10:06:22 +00:00
|
|
|
"github.com/filecoin-project/lotus/storage/sealer/storiface"
|
2021-05-19 13:50:48 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestRemoteGetAllocated(t *testing.T) {
|
|
|
|
|
|
|
|
emptyPartialFile := &partialfile.PartialFile{}
|
|
|
|
pfPath := "path"
|
2022-06-17 11:31:05 +00:00
|
|
|
expectedSectorRef := storiface.SectorRef{
|
2021-05-19 13:50:48 +00:00
|
|
|
ID: abi.SectorID{
|
2021-05-19 14:09:37 +00:00
|
|
|
Miner: 123,
|
|
|
|
Number: 123,
|
2021-05-19 13:50:48 +00:00
|
|
|
},
|
|
|
|
ProofType: 0,
|
|
|
|
}
|
|
|
|
|
|
|
|
validSectorName := fmt.Sprintf("s-t0%d-%d", 123, 123)
|
|
|
|
validSectorFileType := storiface.FTUnsealed.String()
|
|
|
|
validSectorType := "1"
|
|
|
|
sectorSize := abi.SealProofInfos[1].SectorSize
|
|
|
|
|
|
|
|
validOffset := "100"
|
|
|
|
validOffsetInt := 100
|
|
|
|
|
|
|
|
validSize := "1000"
|
|
|
|
validSizeInt := 1000
|
|
|
|
|
|
|
|
type pieceInfo struct {
|
|
|
|
sectorName string
|
|
|
|
fileType string
|
|
|
|
sectorType string
|
|
|
|
|
|
|
|
// piece info
|
|
|
|
offset string
|
|
|
|
size string
|
|
|
|
}
|
|
|
|
validPieceInfo := pieceInfo{
|
|
|
|
sectorName: validSectorName,
|
|
|
|
fileType: validSectorFileType,
|
|
|
|
sectorType: validSectorType,
|
|
|
|
offset: validOffset,
|
|
|
|
size: validSize,
|
|
|
|
}
|
|
|
|
|
|
|
|
tcs := map[string]struct {
|
|
|
|
piFnc func(pi *pieceInfo)
|
2022-06-14 18:31:17 +00:00
|
|
|
storeFnc func(s *mocks.MockStore)
|
|
|
|
pfFunc func(s *mocks.MockPartialFileHandler)
|
2021-05-19 13:50:48 +00:00
|
|
|
|
|
|
|
// expectation
|
|
|
|
expectedStatusCode int
|
|
|
|
}{
|
|
|
|
"fails when sector name is invalid": {
|
|
|
|
piFnc: func(pi *pieceInfo) {
|
|
|
|
pi.sectorName = "invalid"
|
|
|
|
},
|
|
|
|
expectedStatusCode: http.StatusInternalServerError,
|
|
|
|
},
|
|
|
|
"fails when file type is invalid": {
|
|
|
|
piFnc: func(pi *pieceInfo) {
|
|
|
|
pi.fileType = "invalid"
|
|
|
|
},
|
|
|
|
expectedStatusCode: http.StatusInternalServerError,
|
|
|
|
},
|
|
|
|
"fails when sector proof type is invalid": {
|
|
|
|
piFnc: func(pi *pieceInfo) {
|
|
|
|
pi.sectorType = "invalid"
|
|
|
|
},
|
|
|
|
expectedStatusCode: http.StatusInternalServerError,
|
|
|
|
},
|
|
|
|
"fails when offset is invalid": {
|
|
|
|
piFnc: func(pi *pieceInfo) {
|
|
|
|
pi.offset = "invalid"
|
|
|
|
},
|
|
|
|
expectedStatusCode: http.StatusInternalServerError,
|
|
|
|
},
|
|
|
|
"fails when size is invalid": {
|
|
|
|
piFnc: func(pi *pieceInfo) {
|
|
|
|
pi.size = "invalid"
|
|
|
|
},
|
|
|
|
expectedStatusCode: http.StatusInternalServerError,
|
|
|
|
},
|
|
|
|
"fails when errors out during acquiring unsealed sector file": {
|
|
|
|
expectedStatusCode: http.StatusInternalServerError,
|
2022-06-14 18:31:17 +00:00
|
|
|
storeFnc: func(l *mocks.MockStore) {
|
2021-05-20 05:08:22 +00:00
|
|
|
|
|
|
|
l.EXPECT().AcquireSector(gomock.Any(), expectedSectorRef, storiface.FTUnsealed,
|
2021-05-19 13:50:48 +00:00
|
|
|
storiface.FTNone, storiface.PathStorage, storiface.AcquireMove).Return(storiface.SectorPaths{
|
|
|
|
Unsealed: "path",
|
|
|
|
},
|
2021-05-20 05:08:22 +00:00
|
|
|
storiface.SectorPaths{}, xerrors.New("some error")).Times(1)
|
2021-05-19 13:50:48 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
"fails when unsealed sector file is not found locally": {
|
|
|
|
expectedStatusCode: http.StatusInternalServerError,
|
2022-06-14 18:31:17 +00:00
|
|
|
storeFnc: func(l *mocks.MockStore) {
|
2021-05-19 13:50:48 +00:00
|
|
|
|
2021-05-20 05:08:22 +00:00
|
|
|
l.EXPECT().AcquireSector(gomock.Any(), expectedSectorRef, storiface.FTUnsealed,
|
2021-05-19 13:50:48 +00:00
|
|
|
storiface.FTNone, storiface.PathStorage, storiface.AcquireMove).Return(storiface.SectorPaths{},
|
2021-05-20 05:08:22 +00:00
|
|
|
storiface.SectorPaths{}, nil).Times(1)
|
2021-05-19 13:50:48 +00:00
|
|
|
},
|
|
|
|
},
|
2021-05-20 11:01:25 +00:00
|
|
|
"fails when error while opening partial file": {
|
2021-05-19 13:50:48 +00:00
|
|
|
expectedStatusCode: http.StatusInternalServerError,
|
2022-06-14 18:31:17 +00:00
|
|
|
storeFnc: func(l *mocks.MockStore) {
|
2021-05-19 13:50:48 +00:00
|
|
|
|
2021-05-20 05:08:22 +00:00
|
|
|
l.EXPECT().AcquireSector(gomock.Any(), expectedSectorRef, storiface.FTUnsealed,
|
2021-05-19 13:50:48 +00:00
|
|
|
storiface.FTNone, storiface.PathStorage, storiface.AcquireMove).Return(storiface.SectorPaths{
|
|
|
|
Unsealed: pfPath,
|
|
|
|
},
|
2021-05-20 05:08:22 +00:00
|
|
|
storiface.SectorPaths{}, nil).Times(1)
|
2021-05-19 13:50:48 +00:00
|
|
|
},
|
|
|
|
|
2022-06-14 18:31:17 +00:00
|
|
|
pfFunc: func(pf *mocks.MockPartialFileHandler) {
|
2021-05-20 05:08:22 +00:00
|
|
|
pf.EXPECT().OpenPartialFile(abi.PaddedPieceSize(sectorSize), pfPath).Return(&partialfile.PartialFile{},
|
|
|
|
xerrors.New("some error")).Times(1)
|
2021-05-19 13:50:48 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
"fails when determining partial file allocation returns an error": {
|
|
|
|
expectedStatusCode: http.StatusInternalServerError,
|
2022-06-14 18:31:17 +00:00
|
|
|
storeFnc: func(l *mocks.MockStore) {
|
2021-05-19 13:50:48 +00:00
|
|
|
|
2021-05-20 05:08:22 +00:00
|
|
|
l.EXPECT().AcquireSector(gomock.Any(), expectedSectorRef, storiface.FTUnsealed,
|
2021-05-19 13:50:48 +00:00
|
|
|
storiface.FTNone, storiface.PathStorage, storiface.AcquireMove).Return(storiface.SectorPaths{
|
|
|
|
Unsealed: pfPath,
|
|
|
|
},
|
2021-05-20 05:08:22 +00:00
|
|
|
storiface.SectorPaths{}, nil).Times(1)
|
2021-05-19 13:50:48 +00:00
|
|
|
},
|
|
|
|
|
2022-06-14 18:31:17 +00:00
|
|
|
pfFunc: func(pf *mocks.MockPartialFileHandler) {
|
2021-05-20 05:08:22 +00:00
|
|
|
pf.EXPECT().OpenPartialFile(abi.PaddedPieceSize(sectorSize), pfPath).Return(emptyPartialFile,
|
|
|
|
nil).Times(1)
|
|
|
|
|
|
|
|
pf.EXPECT().HasAllocated(emptyPartialFile, storiface.UnpaddedByteIndex(validOffsetInt),
|
|
|
|
abi.UnpaddedPieceSize(validSizeInt)).Return(true, xerrors.New("some error")).Times(1)
|
2021-05-19 13:50:48 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
"StatusRequestedRangeNotSatisfiable when piece is NOT allocated in partial file": {
|
|
|
|
expectedStatusCode: http.StatusRequestedRangeNotSatisfiable,
|
2022-06-14 18:31:17 +00:00
|
|
|
storeFnc: func(l *mocks.MockStore) {
|
2021-05-19 13:50:48 +00:00
|
|
|
|
2021-05-20 05:08:22 +00:00
|
|
|
l.EXPECT().AcquireSector(gomock.Any(), expectedSectorRef, storiface.FTUnsealed,
|
2021-05-19 13:50:48 +00:00
|
|
|
storiface.FTNone, storiface.PathStorage, storiface.AcquireMove).Return(storiface.SectorPaths{
|
|
|
|
Unsealed: pfPath,
|
|
|
|
},
|
2021-05-20 05:08:22 +00:00
|
|
|
storiface.SectorPaths{}, nil).Times(1)
|
2021-05-19 13:50:48 +00:00
|
|
|
},
|
|
|
|
|
2022-06-14 18:31:17 +00:00
|
|
|
pfFunc: func(pf *mocks.MockPartialFileHandler) {
|
2021-05-20 05:08:22 +00:00
|
|
|
pf.EXPECT().OpenPartialFile(abi.PaddedPieceSize(sectorSize), pfPath).Return(emptyPartialFile,
|
|
|
|
nil).Times(1)
|
|
|
|
|
|
|
|
pf.EXPECT().HasAllocated(emptyPartialFile, storiface.UnpaddedByteIndex(validOffsetInt),
|
|
|
|
abi.UnpaddedPieceSize(validSizeInt)).Return(false, nil).Times(1)
|
2021-05-19 13:50:48 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
"OK when piece is allocated in partial file": {
|
|
|
|
expectedStatusCode: http.StatusOK,
|
2022-06-14 18:31:17 +00:00
|
|
|
storeFnc: func(l *mocks.MockStore) {
|
2021-05-19 13:50:48 +00:00
|
|
|
|
2021-05-20 05:08:22 +00:00
|
|
|
l.EXPECT().AcquireSector(gomock.Any(), expectedSectorRef, storiface.FTUnsealed,
|
2021-05-19 13:50:48 +00:00
|
|
|
storiface.FTNone, storiface.PathStorage, storiface.AcquireMove).Return(storiface.SectorPaths{
|
|
|
|
Unsealed: pfPath,
|
|
|
|
},
|
2021-05-20 05:08:22 +00:00
|
|
|
storiface.SectorPaths{}, nil).Times(1)
|
2021-05-19 13:50:48 +00:00
|
|
|
},
|
|
|
|
|
2022-06-14 18:31:17 +00:00
|
|
|
pfFunc: func(pf *mocks.MockPartialFileHandler) {
|
2021-05-20 05:08:22 +00:00
|
|
|
pf.EXPECT().OpenPartialFile(abi.PaddedPieceSize(sectorSize), pfPath).Return(emptyPartialFile,
|
|
|
|
nil).Times(1)
|
|
|
|
|
|
|
|
pf.EXPECT().HasAllocated(emptyPartialFile, storiface.UnpaddedByteIndex(validOffsetInt),
|
|
|
|
abi.UnpaddedPieceSize(validSizeInt)).Return(true, nil).Times(1)
|
2021-05-19 13:50:48 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range tcs {
|
2021-05-19 14:09:37 +00:00
|
|
|
tc := tc
|
2021-05-19 13:50:48 +00:00
|
|
|
t.Run(name, func(t *testing.T) {
|
2021-05-20 05:08:22 +00:00
|
|
|
// create go mock controller here
|
|
|
|
mockCtrl := gomock.NewController(t)
|
|
|
|
// when test is done, assert expectations on all mock objects.
|
|
|
|
defer mockCtrl.Finish()
|
|
|
|
|
2022-06-14 18:31:17 +00:00
|
|
|
lstore := mocks.NewMockStore(mockCtrl)
|
|
|
|
pfhandler := mocks.NewMockPartialFileHandler(mockCtrl)
|
2021-05-19 13:50:48 +00:00
|
|
|
|
2022-06-14 18:25:52 +00:00
|
|
|
handler := &paths.FetchHandler{
|
2023-05-23 09:59:40 +00:00
|
|
|
Local: lstore,
|
|
|
|
PfHandler: pfhandler,
|
2021-05-19 13:50:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// run http server
|
|
|
|
ts := httptest.NewServer(handler)
|
|
|
|
defer ts.Close()
|
|
|
|
|
|
|
|
pi := validPieceInfo
|
2021-05-19 14:09:37 +00:00
|
|
|
|
2021-05-19 13:50:48 +00:00
|
|
|
if tc.piFnc != nil {
|
|
|
|
tc.piFnc(&pi)
|
|
|
|
}
|
|
|
|
|
|
|
|
if tc.storeFnc != nil {
|
|
|
|
tc.storeFnc(lstore)
|
|
|
|
}
|
|
|
|
if tc.pfFunc != nil {
|
|
|
|
tc.pfFunc(pfhandler)
|
|
|
|
}
|
|
|
|
|
|
|
|
// call remoteGetAllocated
|
|
|
|
url := fmt.Sprintf("%s/remote/%s/%s/%s/allocated/%s/%s",
|
|
|
|
ts.URL,
|
|
|
|
pi.fileType,
|
|
|
|
pi.sectorName,
|
|
|
|
pi.sectorType,
|
|
|
|
pi.offset,
|
|
|
|
pi.size)
|
|
|
|
resp, err := http.Get(url)
|
|
|
|
require.NoError(t, err)
|
2021-05-19 14:09:37 +00:00
|
|
|
defer func() {
|
|
|
|
_ = resp.Body.Close()
|
|
|
|
}()
|
2021-05-19 13:50:48 +00:00
|
|
|
|
|
|
|
// assert expected status code
|
|
|
|
require.Equal(t, tc.expectedStatusCode, resp.StatusCode)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRemoteGetSector(t *testing.T) {
|
|
|
|
str := "hello-world"
|
|
|
|
fileBytes := []byte(str)
|
|
|
|
|
|
|
|
validSectorName := fmt.Sprintf("s-t0%d-%d", 123, 123)
|
|
|
|
validSectorFileType := storiface.FTUnsealed.String()
|
2022-06-17 11:31:05 +00:00
|
|
|
expectedSectorRef := storiface.SectorRef{
|
2021-05-19 13:50:48 +00:00
|
|
|
ID: abi.SectorID{
|
2021-05-19 14:09:37 +00:00
|
|
|
Miner: 123,
|
|
|
|
Number: 123,
|
2021-05-19 13:50:48 +00:00
|
|
|
},
|
|
|
|
ProofType: 0,
|
|
|
|
}
|
|
|
|
|
|
|
|
type sectorInfo struct {
|
|
|
|
sectorName string
|
|
|
|
fileType string
|
|
|
|
}
|
|
|
|
validSectorInfo := sectorInfo{
|
|
|
|
sectorName: validSectorName,
|
|
|
|
fileType: validSectorFileType,
|
|
|
|
}
|
|
|
|
|
|
|
|
tcs := map[string]struct {
|
|
|
|
siFnc func(pi *sectorInfo)
|
2022-06-14 18:31:17 +00:00
|
|
|
storeFnc func(s *mocks.MockStore, path string)
|
2021-05-19 13:50:48 +00:00
|
|
|
|
|
|
|
// reading a file or a dir
|
|
|
|
isDir bool
|
|
|
|
|
|
|
|
// expectation
|
|
|
|
noResponseBytes bool
|
|
|
|
expectedContentType string
|
|
|
|
expectedStatusCode int
|
|
|
|
expectedResponseBytes []byte
|
|
|
|
}{
|
|
|
|
"fails when sector name is invalid": {
|
|
|
|
siFnc: func(si *sectorInfo) {
|
|
|
|
si.sectorName = "invalid"
|
|
|
|
},
|
|
|
|
expectedStatusCode: http.StatusInternalServerError,
|
|
|
|
noResponseBytes: true,
|
|
|
|
},
|
|
|
|
"fails when file type is invalid": {
|
|
|
|
siFnc: func(si *sectorInfo) {
|
|
|
|
si.fileType = "invalid"
|
|
|
|
},
|
|
|
|
expectedStatusCode: http.StatusInternalServerError,
|
|
|
|
noResponseBytes: true,
|
|
|
|
},
|
|
|
|
"fails when error while acquiring sector file": {
|
2022-06-14 18:31:17 +00:00
|
|
|
storeFnc: func(l *mocks.MockStore, _ string) {
|
2021-05-20 05:08:22 +00:00
|
|
|
|
|
|
|
l.EXPECT().AcquireSector(gomock.Any(), expectedSectorRef, storiface.FTUnsealed,
|
2021-05-19 13:50:48 +00:00
|
|
|
storiface.FTNone, storiface.PathStorage, storiface.AcquireMove).Return(storiface.SectorPaths{
|
|
|
|
Unsealed: "path",
|
|
|
|
},
|
2021-05-20 05:08:22 +00:00
|
|
|
storiface.SectorPaths{}, xerrors.New("some error")).Times(1)
|
2021-05-19 13:50:48 +00:00
|
|
|
},
|
|
|
|
expectedStatusCode: http.StatusInternalServerError,
|
|
|
|
noResponseBytes: true,
|
|
|
|
},
|
|
|
|
"fails when acquired sector file path is empty": {
|
|
|
|
expectedStatusCode: http.StatusInternalServerError,
|
2022-06-14 18:31:17 +00:00
|
|
|
storeFnc: func(l *mocks.MockStore, _ string) {
|
2021-05-19 13:50:48 +00:00
|
|
|
|
2021-05-20 05:08:22 +00:00
|
|
|
l.EXPECT().AcquireSector(gomock.Any(), expectedSectorRef, storiface.FTUnsealed,
|
2021-05-19 13:50:48 +00:00
|
|
|
storiface.FTNone, storiface.PathStorage, storiface.AcquireMove).Return(storiface.SectorPaths{},
|
2021-05-20 05:08:22 +00:00
|
|
|
storiface.SectorPaths{}, nil).Times(1)
|
2021-05-19 13:50:48 +00:00
|
|
|
},
|
|
|
|
noResponseBytes: true,
|
|
|
|
},
|
|
|
|
"fails when acquired file does not exist": {
|
|
|
|
expectedStatusCode: http.StatusInternalServerError,
|
2022-06-14 18:31:17 +00:00
|
|
|
storeFnc: func(l *mocks.MockStore, _ string) {
|
2021-05-19 13:50:48 +00:00
|
|
|
|
2021-05-20 05:08:22 +00:00
|
|
|
l.EXPECT().AcquireSector(gomock.Any(), expectedSectorRef, storiface.FTUnsealed,
|
2021-05-19 13:50:48 +00:00
|
|
|
storiface.FTNone, storiface.PathStorage, storiface.AcquireMove).Return(storiface.SectorPaths{
|
|
|
|
Unsealed: "path",
|
|
|
|
},
|
|
|
|
storiface.SectorPaths{}, nil)
|
|
|
|
},
|
|
|
|
noResponseBytes: true,
|
|
|
|
},
|
|
|
|
"successfully read a sector file": {
|
2022-06-14 18:31:17 +00:00
|
|
|
storeFnc: func(l *mocks.MockStore, path string) {
|
2021-05-19 13:50:48 +00:00
|
|
|
|
2021-05-20 05:08:22 +00:00
|
|
|
l.EXPECT().AcquireSector(gomock.Any(), expectedSectorRef, storiface.FTUnsealed,
|
2021-05-19 13:50:48 +00:00
|
|
|
storiface.FTNone, storiface.PathStorage, storiface.AcquireMove).Return(storiface.SectorPaths{
|
|
|
|
Unsealed: path,
|
|
|
|
},
|
|
|
|
storiface.SectorPaths{}, nil)
|
|
|
|
},
|
|
|
|
|
|
|
|
noResponseBytes: false,
|
|
|
|
expectedContentType: "application/octet-stream",
|
|
|
|
expectedStatusCode: 200,
|
|
|
|
expectedResponseBytes: fileBytes,
|
|
|
|
},
|
|
|
|
"successfully read a sector dir": {
|
2022-06-14 18:31:17 +00:00
|
|
|
storeFnc: func(l *mocks.MockStore, path string) {
|
2021-05-19 13:50:48 +00:00
|
|
|
|
2021-05-20 05:08:22 +00:00
|
|
|
l.EXPECT().AcquireSector(gomock.Any(), expectedSectorRef, storiface.FTUnsealed,
|
2021-05-19 13:50:48 +00:00
|
|
|
storiface.FTNone, storiface.PathStorage, storiface.AcquireMove).Return(storiface.SectorPaths{
|
|
|
|
Unsealed: path,
|
|
|
|
},
|
|
|
|
storiface.SectorPaths{}, nil)
|
|
|
|
},
|
|
|
|
|
|
|
|
isDir: true,
|
|
|
|
noResponseBytes: false,
|
|
|
|
expectedContentType: "application/x-tar",
|
|
|
|
expectedStatusCode: 200,
|
|
|
|
expectedResponseBytes: fileBytes,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range tcs {
|
2021-05-19 14:09:37 +00:00
|
|
|
tc := tc
|
2021-05-19 13:50:48 +00:00
|
|
|
t.Run(name, func(t *testing.T) {
|
2021-05-20 05:08:22 +00:00
|
|
|
mockCtrl := gomock.NewController(t)
|
|
|
|
// when test is done, assert expectations on all mock objects.
|
|
|
|
defer mockCtrl.Finish()
|
2022-06-14 18:31:17 +00:00
|
|
|
lstore := mocks.NewMockStore(mockCtrl)
|
|
|
|
pfhandler := mocks.NewMockPartialFileHandler(mockCtrl)
|
2021-05-20 05:08:22 +00:00
|
|
|
|
2021-05-19 13:50:48 +00:00
|
|
|
var path string
|
|
|
|
|
|
|
|
if !tc.isDir {
|
|
|
|
// create file
|
2023-03-29 19:24:07 +00:00
|
|
|
tempFile, err := os.CreateTemp("", "TestRemoteGetSector-")
|
2021-05-19 13:50:48 +00:00
|
|
|
require.NoError(t, err)
|
2021-05-19 14:09:37 +00:00
|
|
|
|
|
|
|
defer func() {
|
|
|
|
_ = os.Remove(tempFile.Name())
|
|
|
|
}()
|
|
|
|
|
2021-05-19 13:50:48 +00:00
|
|
|
_, err = tempFile.Write(fileBytes)
|
|
|
|
require.NoError(t, err)
|
|
|
|
path = tempFile.Name()
|
|
|
|
} else {
|
|
|
|
// create dir with a file
|
2023-03-29 19:24:07 +00:00
|
|
|
tempFile2, err := os.CreateTemp("", "TestRemoteGetSector-")
|
2021-05-19 13:50:48 +00:00
|
|
|
require.NoError(t, err)
|
2021-05-19 14:09:37 +00:00
|
|
|
defer func() {
|
|
|
|
_ = os.Remove(tempFile2.Name())
|
|
|
|
}()
|
|
|
|
|
2021-05-19 13:50:48 +00:00
|
|
|
stat, err := os.Stat(tempFile2.Name())
|
|
|
|
require.NoError(t, err)
|
2022-03-11 06:27:36 +00:00
|
|
|
tempDir := t.TempDir()
|
2021-05-19 14:09:37 +00:00
|
|
|
|
2021-05-19 13:50:48 +00:00
|
|
|
require.NoError(t, os.Rename(tempFile2.Name(), filepath.Join(tempDir, stat.Name())))
|
|
|
|
|
|
|
|
path = tempDir
|
|
|
|
}
|
|
|
|
|
2022-06-14 18:25:52 +00:00
|
|
|
handler := &paths.FetchHandler{
|
2021-05-19 13:50:48 +00:00
|
|
|
lstore,
|
|
|
|
pfhandler,
|
|
|
|
}
|
|
|
|
|
|
|
|
// run http server
|
|
|
|
ts := httptest.NewServer(handler)
|
|
|
|
defer ts.Close()
|
|
|
|
|
|
|
|
si := validSectorInfo
|
|
|
|
if tc.siFnc != nil {
|
|
|
|
tc.siFnc(&si)
|
|
|
|
}
|
|
|
|
|
|
|
|
if tc.storeFnc != nil {
|
|
|
|
tc.storeFnc(lstore, path)
|
|
|
|
}
|
|
|
|
|
|
|
|
// call remoteGetAllocated
|
|
|
|
url := fmt.Sprintf("%s/remote/%s/%s",
|
|
|
|
ts.URL,
|
|
|
|
si.fileType,
|
|
|
|
si.sectorName,
|
|
|
|
)
|
|
|
|
resp, err := http.Get(url)
|
|
|
|
require.NoError(t, err)
|
2021-05-19 14:09:37 +00:00
|
|
|
defer func() {
|
|
|
|
_ = resp.Body.Close()
|
|
|
|
}()
|
2021-05-19 13:50:48 +00:00
|
|
|
|
2023-03-29 19:24:07 +00:00
|
|
|
bz, err := io.ReadAll(resp.Body)
|
2021-05-19 13:50:48 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// assert expected status code
|
|
|
|
require.Equal(t, tc.expectedStatusCode, resp.StatusCode)
|
|
|
|
|
|
|
|
if !tc.noResponseBytes {
|
|
|
|
if !tc.isDir {
|
|
|
|
require.EqualValues(t, tc.expectedResponseBytes, bz)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
require.Equal(t, tc.expectedContentType, resp.Header.Get("Content-Type"))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|