2021-05-19 13:50:48 +00:00
|
|
|
package stores_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/filecoin-project/go-state-types/abi"
|
|
|
|
"github.com/filecoin-project/lotus/extern/sector-storage/partialfile"
|
|
|
|
"github.com/filecoin-project/lotus/extern/sector-storage/stores"
|
|
|
|
"github.com/filecoin-project/lotus/extern/sector-storage/stores/mocks"
|
|
|
|
"github.com/filecoin-project/lotus/extern/sector-storage/storiface"
|
|
|
|
"github.com/filecoin-project/specs-storage/storage"
|
2021-05-20 05:08:22 +00:00
|
|
|
"github.com/golang/mock/gomock"
|
2021-05-19 13:50:48 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"golang.org/x/xerrors"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestRemoteGetAllocated(t *testing.T) {
|
|
|
|
|
|
|
|
emptyPartialFile := &partialfile.PartialFile{}
|
|
|
|
pfPath := "path"
|
|
|
|
expectedSectorRef := storage.SectorRef{
|
|
|
|
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)
|
2021-05-20 05:08:22 +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,
|
2021-05-20 05:08:22 +00:00
|
|
|
storeFnc: func(l *mocks.MockStore) {
|
|
|
|
|
|
|
|
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,
|
2021-05-20 05:08:22 +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
|
|
|
},
|
|
|
|
},
|
|
|
|
"fails when partial file is not found locally": {
|
|
|
|
expectedStatusCode: http.StatusInternalServerError,
|
2021-05-20 05:08:22 +00:00
|
|
|
storeFnc: func(l *mocks.MockStore) {
|
2021-05-19 13:50:48 +00:00
|
|
|
// will return emppty paths
|
|
|
|
|
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
|
|
|
},
|
|
|
|
|
2021-05-20 05:08:22 +00:00
|
|
|
pfFunc: func(pf *mocks.MockpartialFileHandler) {
|
2021-05-19 13:50:48 +00:00
|
|
|
//OpenPartialFile(maxPieceSize abi.PaddedPieceSize, path string)
|
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,
|
2021-05-20 05:08:22 +00:00
|
|
|
storeFnc: func(l *mocks.MockStore) {
|
2021-05-19 13:50:48 +00:00
|
|
|
// will return emppty paths
|
|
|
|
|
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
|
|
|
},
|
|
|
|
|
2021-05-20 05:08:22 +00:00
|
|
|
pfFunc: func(pf *mocks.MockpartialFileHandler) {
|
|
|
|
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,
|
2021-05-20 05:08:22 +00:00
|
|
|
storeFnc: func(l *mocks.MockStore) {
|
2021-05-19 13:50:48 +00:00
|
|
|
// will return emppty paths
|
|
|
|
|
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
|
|
|
},
|
|
|
|
|
2021-05-20 05:08:22 +00:00
|
|
|
pfFunc: func(pf *mocks.MockpartialFileHandler) {
|
|
|
|
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,
|
2021-05-20 05:08:22 +00:00
|
|
|
storeFnc: func(l *mocks.MockStore) {
|
2021-05-19 13:50:48 +00:00
|
|
|
// will return emppty paths
|
|
|
|
|
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
|
|
|
},
|
|
|
|
|
2021-05-20 05:08:22 +00:00
|
|
|
pfFunc: func(pf *mocks.MockpartialFileHandler) {
|
|
|
|
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()
|
|
|
|
|
|
|
|
lstore := mocks.NewMockStore(mockCtrl)
|
|
|
|
pfhandler := mocks.NewMockpartialFileHandler(mockCtrl)
|
2021-05-19 13:50:48 +00:00
|
|
|
|
|
|
|
handler := &stores.FetchHandler{
|
|
|
|
lstore,
|
|
|
|
pfhandler,
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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()
|
|
|
|
expectedSectorRef := storage.SectorRef{
|
|
|
|
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)
|
2021-05-20 05:08:22 +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": {
|
2021-05-20 05:08:22 +00:00
|
|
|
storeFnc: func(l *mocks.MockStore, _ string) {
|
|
|
|
|
|
|
|
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,
|
2021-05-20 05:08:22 +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,
|
2021-05-20 05:08:22 +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": {
|
2021-05-20 05:08:22 +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": {
|
2021-05-20 05:08:22 +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()
|
|
|
|
lstore := mocks.NewMockStore(mockCtrl)
|
|
|
|
pfhandler := mocks.NewMockpartialFileHandler(mockCtrl)
|
|
|
|
|
2021-05-19 13:50:48 +00:00
|
|
|
var path string
|
|
|
|
|
|
|
|
if !tc.isDir {
|
|
|
|
// create file
|
|
|
|
tempFile, err := ioutil.TempFile("", "TestRemoteGetSector-")
|
|
|
|
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
|
|
|
|
tempFile2, err := ioutil.TempFile("", "TestRemoteGetSector-")
|
|
|
|
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)
|
|
|
|
tempDir, err := ioutil.TempDir("", "TestRemoteGetSector-")
|
|
|
|
require.NoError(t, err)
|
2021-05-19 14:09:37 +00:00
|
|
|
|
|
|
|
defer func() {
|
|
|
|
_ = os.RemoveAll(tempDir)
|
|
|
|
}()
|
|
|
|
|
2021-05-19 13:50:48 +00:00
|
|
|
require.NoError(t, os.Rename(tempFile2.Name(), filepath.Join(tempDir, stat.Name())))
|
|
|
|
|
|
|
|
path = tempDir
|
|
|
|
}
|
|
|
|
|
|
|
|
handler := &stores.FetchHandler{
|
|
|
|
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
|
|
|
|
|
|
|
bz, err := ioutil.ReadAll(resp.Body)
|
|
|
|
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"))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|