lotus/itests/kit/client.go

134 lines
3.6 KiB
Go
Raw Normal View History

2021-05-18 21:01:10 +00:00
package kit
import (
"context"
"fmt"
"io/ioutil"
2021-05-20 15:12:42 +00:00
"math/rand"
"os"
"path/filepath"
"regexp"
"strings"
"testing"
"time"
2021-05-20 15:12:42 +00:00
"github.com/filecoin-project/lotus/api"
"github.com/filecoin-project/lotus/build"
"github.com/filecoin-project/lotus/chain/types"
"github.com/filecoin-project/specs-actors/v2/actors/builtin"
"github.com/stretchr/testify/require"
lcli "github.com/urfave/cli/v2"
)
2021-05-18 21:01:10 +00:00
// RunClientTest exercises some of the Client CLI commands
func RunClientTest(t *testing.T, cmds []*lcli.Command, clientNode TestFullNode) {
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()
// Create mock CLI
2020-10-27 10:07:54 +00:00
mockCLI := NewMockCLI(ctx, t, cmds)
2020-10-26 11:01:33 +00:00
clientCLI := mockCLI.Client(clientNode.ListenAddr)
2021-05-18 21:01:10 +00:00
// Get the Miner address
addrs, err := clientNode.StateListMiners(ctx, types.EmptyTSK)
require.NoError(t, err)
require.Len(t, addrs, 1)
minerAddr := addrs[0]
fmt.Println("Miner:", minerAddr)
2021-05-18 21:01:10 +00:00
// Client query-ask <Miner addr>
out := clientCLI.RunCmd("client", "query-ask", minerAddr.String())
require.Regexp(t, regexp.MustCompile("Ask:"), out)
// Create a deal (non-interactive)
2021-05-18 21:01:10 +00:00
// Client deal --start-epoch=<start epoch> <cid> <Miner addr> 1000000attofil <duration>
2021-05-20 15:12:42 +00:00
res, _, err := CreateImportFile(ctx, clientNode, 1)
require.NoError(t, err)
startEpoch := fmt.Sprintf("--start-epoch=%d", 2<<12)
dataCid := res.Root
price := "1000000attofil"
duration := fmt.Sprintf("%d", build.MinDealDuration)
2021-05-18 21:01:10 +00:00
out = clientCLI.RunCmd("Client", "deal", startEpoch, dataCid.String(), minerAddr.String(), price, duration)
fmt.Println("Client deal", out)
// Create a deal (interactive)
2021-05-18 21:01:10 +00:00
// Client deal
// <cid>
// <duration> (in days)
2021-05-18 21:01:10 +00:00
// <Miner addr>
// "no" (verified Client)
// "yes" (confirm deal)
2021-05-20 15:12:42 +00:00
res, _, err = CreateImportFile(ctx, clientNode, 2)
require.NoError(t, err)
dataCid2 := res.Root
duration = fmt.Sprintf("%d", build.MinDealDuration/builtin.EpochsInDay)
2021-05-18 21:01:10 +00:00
cmd := []string{"Client", "deal"}
interactiveCmds := []string{
dataCid2.String(),
duration,
minerAddr.String(),
"no",
"yes",
}
2020-10-26 11:01:33 +00:00
out = clientCLI.RunInteractiveCmd(cmd, interactiveCmds)
2021-05-18 21:01:10 +00:00
fmt.Println("Client deal:\n", out)
// Wait for provider to start sealing deal
dealStatus := ""
2020-10-27 10:07:54 +00:00
for {
2021-05-18 21:01:10 +00:00
// Client list-deals
out = clientCLI.RunCmd("Client", "list-deals")
fmt.Println("list-deals:\n", out)
lines := strings.Split(out, "\n")
2020-12-01 10:48:14 +00:00
require.GreaterOrEqual(t, len(lines), 2)
re := regexp.MustCompile(`\s+`)
parts := re.Split(lines[1], -1)
if len(parts) < 4 {
require.Fail(t, "bad list-deals output format")
}
dealStatus = parts[3]
fmt.Println(" Deal status:", dealStatus)
st := CategorizeDealState(dealStatus)
require.NotEqual(t, TestDealStateFailed, st)
if st == TestDealStateComplete {
2020-10-27 10:07:54 +00:00
break
}
time.Sleep(time.Second)
}
2021-05-18 21:01:10 +00:00
// Retrieve the first file from the Miner
// Client retrieve <cid> <file path>
tmpdir, err := ioutil.TempDir(os.TempDir(), "test-cli-Client")
require.NoError(t, err)
path := filepath.Join(tmpdir, "outfile.dat")
2021-05-18 21:01:10 +00:00
out = clientCLI.RunCmd("Client", "retrieve", dataCid.String(), path)
fmt.Println("retrieve:\n", out)
require.Regexp(t, regexp.MustCompile("Success"), out)
}
2021-05-20 15:12:42 +00:00
func CreateImportFile(ctx context.Context, client api.FullNode, rseed int) (*api.ImportRes, []byte, error) {
data := make([]byte, 1600)
rand.New(rand.NewSource(int64(rseed))).Read(data)
dir, err := ioutil.TempDir(os.TempDir(), "test-make-deal-")
if err != nil {
return nil, nil, err
}
path := filepath.Join(dir, "sourcefile.dat")
err = ioutil.WriteFile(path, data, 0644)
if err != nil {
return nil, nil, err
}
res, err := client.ClientImport(ctx, api.FileRef{Path: path})
if err != nil {
return nil, nil, err
}
return res, data, nil
}