lotus/node/impl/client/import_test.go

134 lines
3.7 KiB
Go
Raw Normal View History

2021-07-06 07:10:57 +00:00
package client
import (
"context"
"io"
"io/ioutil"
"os"
"strings"
"testing"
"github.com/filecoin-project/go-fil-markets/stores"
2021-07-06 07:10:57 +00:00
"github.com/filecoin-project/lotus/node/repo/importmgr"
"github.com/ipfs/go-blockservice"
"github.com/ipfs/go-cid"
offline "github.com/ipfs/go-ipfs-exchange-offline"
files "github.com/ipfs/go-ipfs-files"
"github.com/ipfs/go-merkledag"
unixfile "github.com/ipfs/go-unixfs/file"
2021-07-21 04:00:33 +00:00
carv2 "github.com/ipld/go-car/v2"
2021-07-06 07:10:57 +00:00
"github.com/ipld/go-car/v2/blockstore"
"github.com/stretchr/testify/require"
)
// This test uses a full "dense" CARv2, and not a filestore (positional mapping).
func TestRoundtripUnixFS_Dense(t *testing.T) {
2021-07-06 07:10:57 +00:00
ctx := context.Background()
inputPath, inputContents := genInputFile(t)
2021-07-06 07:10:57 +00:00
defer os.Remove(inputPath) //nolint:errcheck
carv2File := newTmpFile(t)
2021-07-06 07:10:57 +00:00
defer os.Remove(carv2File) //nolint:errcheck
// import a file to a Unixfs DAG using a CARv2 read/write blockstore.
path, err := blockstore.OpenReadWrite(carv2File, nil,
carv2.ZeroLengthSectionAsEOF(true),
blockstore.UseWholeCIDs(true))
2021-07-06 07:10:57 +00:00
require.NoError(t, err)
bsvc := blockservice.New(path, offline.Exchange(path))
dags := merkledag.NewDAGService(bsvc)
root, err := buildUnixFS(ctx, inputPath, dags)
2021-07-06 07:10:57 +00:00
require.NoError(t, err)
require.NotEqual(t, cid.Undef, root)
require.NoError(t, path.Finalize())
2021-07-06 07:10:57 +00:00
// reconstruct the file.
readOnly, err := blockstore.OpenReadOnly(carv2File,
carv2.ZeroLengthSectionAsEOF(true),
blockstore.UseWholeCIDs(true))
2021-07-06 07:10:57 +00:00
require.NoError(t, err)
defer readOnly.Close() //nolint:errcheck
dags = merkledag.NewDAGService(blockservice.New(readOnly, offline.Exchange(readOnly)))
nd, err := dags.Get(ctx, root)
2021-07-06 07:10:57 +00:00
require.NoError(t, err)
file, err := unixfile.NewUnixfsFile(ctx, dags, nd)
2021-07-06 07:10:57 +00:00
require.NoError(t, err)
tmpOutput := newTmpFile(t)
2021-07-06 07:10:57 +00:00
defer os.Remove(tmpOutput) //nolint:errcheck
require.NoError(t, files.WriteTo(file, tmpOutput))
// ensure contents of the initial input file and the output file are identical.
fo, err := os.Open(tmpOutput)
require.NoError(t, err)
bz2, err := ioutil.ReadAll(fo)
require.NoError(t, err)
require.NoError(t, fo.Close())
require.Equal(t, inputContents, bz2)
}
func TestRoundtripUnixFS_Filestore(t *testing.T) {
2021-07-06 07:10:57 +00:00
ctx := context.Background()
a := &API{
Imports: &importmgr.Mgr{},
}
inputFilePath, inputContents := genInputFile(t)
2021-07-06 07:10:57 +00:00
defer os.Remove(inputFilePath) //nolint:errcheck
path := newTmpFile(t)
defer os.Remove(path) //nolint:errcheck
2021-07-06 07:10:57 +00:00
root, err := a.doImport(ctx, inputFilePath, path)
2021-07-06 07:10:57 +00:00
require.NoError(t, err)
require.NotEqual(t, cid.Undef, root)
// convert the CARv2 to a normal file again and ensure the contents match
fs, err := stores.ReadOnlyFilestore(path)
2021-07-06 07:10:57 +00:00
require.NoError(t, err)
defer fs.Close() //nolint:errcheck
2021-07-06 07:10:57 +00:00
dags := merkledag.NewDAGService(blockservice.New(fs, offline.Exchange(fs)))
nd, err := dags.Get(ctx, root)
2021-07-06 07:10:57 +00:00
require.NoError(t, err)
file, err := unixfile.NewUnixfsFile(ctx, dags, nd)
2021-07-06 07:10:57 +00:00
require.NoError(t, err)
tmpOutput := newTmpFile(t)
2021-07-06 07:10:57 +00:00
defer os.Remove(tmpOutput) //nolint:errcheck
require.NoError(t, files.WriteTo(file, tmpOutput))
// ensure contents of the initial input file and the output file are identical.
fo, err := os.Open(tmpOutput)
require.NoError(t, err)
bz2, err := ioutil.ReadAll(fo)
require.NoError(t, err)
require.NoError(t, fo.Close())
require.Equal(t, inputContents, bz2)
}
func newTmpFile(t *testing.T) string {
2021-07-06 07:10:57 +00:00
f, err := os.CreateTemp("", "")
require.NoError(t, err)
require.NoError(t, f.Close())
return f.Name()
}
func genInputFile(t *testing.T) (filepath string, contents []byte) {
2021-07-06 07:10:57 +00:00
s := strings.Repeat("abcde", 100)
tmp, err := os.CreateTemp("", "")
require.NoError(t, err)
_, err = io.Copy(tmp, strings.NewReader(s))
require.NoError(t, err)
require.NoError(t, tmp.Close())
return tmp.Name(), []byte(s)
}