better comparision in test

This commit is contained in:
Aarsh Shah 2021-09-17 12:52:31 +04:00 committed by Dirk McCormick
parent 053317e334
commit 4d87f16e95

View File

@ -4,10 +4,13 @@ import (
"bufio" "bufio"
"context" "context"
"fmt" "fmt"
"io/ioutil"
"os" "os"
"testing" "testing"
"time" "time"
"github.com/ipld/go-car/v2/blockstore"
"github.com/filecoin-project/lotus/api" "github.com/filecoin-project/lotus/api"
ipld "github.com/ipfs/go-ipld-format" ipld "github.com/ipfs/go-ipld-format"
@ -128,25 +131,52 @@ func TestDealRetrieveByAnyCid(t *testing.T) {
require.NoError(t, err) require.NoError(t, err)
cidIndices := []int{1, 11, 27, 32, 47} cidIndices := []int{1, 11, 27, 32, 47}
for _, val := range cidIndices { for _, val := range cidIndices {
fmt.Println("performing retrieval for cid at index", val) fmt.Println("performing retrieval for cid at index", val)
targetCid := cids[val] targetCid := cids[val]
offer, err := client.ClientMinerQueryOffer(ctx, miner.ActorAddr, targetCid, &info.PieceCID) offer, err := client.ClientMinerQueryOffer(ctx, miner.ActorAddr, targetCid, &info.PieceCID)
require.NoError(t, err) require.NoError(t, err)
require.Empty(t, offer.Err) require.Empty(t, offer.Err)
outPath := dh.PerformRetrievalForOffer(ctx, true, offer) // retrieve in a CAR file and ensure roots match
_, err = os.Stat(outPath) outputCar := dh.PerformRetrievalForOffer(ctx, true, offer)
_, err = os.Stat(outputCar)
require.NoError(t, err) require.NoError(t, err)
f, err := os.Open(outputCar)
f, err := os.Open(outPath)
require.NoError(t, err) require.NoError(t, err)
defer f.Close()
ch, _, _ := car.ReadHeader(bufio.NewReader(f)) ch, _, _ := car.ReadHeader(bufio.NewReader(f))
require.EqualValues(t, ch.Roots[0], targetCid) require.EqualValues(t, ch.Roots[0], targetCid)
require.NoError(t, f.Close())
// create CAR from original file starting at targetCid and ensure it matches the retrieved CAR file.
tmp, err := os.CreateTemp(t.TempDir(), "randcarv1")
require.NoError(t, err)
rd, err := blockstore.OpenReadOnly(carv1FilePath, blockstore.UseWholeCIDs(true))
require.NoError(t, err)
err = car.NewSelectiveCar(
ctx,
rd,
[]car.Dag{{
Root: targetCid,
Selector: shared.AllSelector(),
}},
).Write(tmp)
require.NoError(t, tmp.Close())
require.NoError(t, rd.Close())
originalF, err := os.Open(tmp.Name())
require.NoError(t, err)
obz, err := ioutil.ReadAll(originalF)
require.NoError(t, err)
rf, err := os.Open(outputCar)
require.NoError(t, err)
bz, err := ioutil.ReadAll(rf)
require.NoError(t, err)
require.EqualValues(t, obz, bz)
fmt.Println("car files match")
} }
fmt.Println("finised test") fmt.Println("finished test")
} }