test: chain bisect cli command

This commit is contained in:
Nikola Divic 2022-02-10 15:46:06 +01:00
parent 6bc2ee2735
commit b536dfa552
2 changed files with 48 additions and 13 deletions

View File

@ -932,6 +932,8 @@ var ChainBisectCmd = &cli.Command{
For special path elements see 'chain get' help For special path elements see 'chain get' help
`, `,
Action: func(cctx *cli.Context) error { Action: func(cctx *cli.Context) error {
afmt := NewAppFmt(cctx.App)
api, closer, err := GetFullNodeAPI(cctx) api, closer, err := GetFullNodeAPI(cctx)
if err != nil { if err != nil {
return err return err
@ -975,7 +977,7 @@ var ChainBisectCmd = &cli.Command{
} }
path := "/ipld/" + midTs.ParentState().String() + "/" + subPath path := "/ipld/" + midTs.ParentState().String() + "/" + subPath
fmt.Printf("* Testing %d (%d - %d) (%s): ", mid, start, end, path) afmt.Printf("* Testing %d (%d - %d) (%s): ", mid, start, end, path)
nd, err := api.ChainGetNode(ctx, path) nd, err := api.ChainGetNode(ctx, path)
if err != nil { if err != nil {
@ -1002,32 +1004,32 @@ var ChainBisectCmd = &cli.Command{
if strings.TrimSpace(out.String()) != "false" { if strings.TrimSpace(out.String()) != "false" {
end = mid end = mid
highest = midTs highest = midTs
fmt.Println("true") afmt.Println("true")
} else { } else {
start = mid start = mid
fmt.Printf("false (cli)\n") afmt.Printf("false (cli)\n")
} }
case *exec.ExitError: case *exec.ExitError:
if len(serr.String()) > 0 { if len(serr.String()) > 0 {
fmt.Println("error") afmt.Println("error")
fmt.Printf("> Command: %s\n---->\n", strings.Join(cctx.Args().Slice()[3:], " ")) afmt.Printf("> Command: %s\n---->\n", strings.Join(cctx.Args().Slice()[3:], " "))
fmt.Println(string(b)) afmt.Println(string(b))
fmt.Println("<----") afmt.Println("<----")
return xerrors.Errorf("error running bisect check: %s", serr.String()) return xerrors.Errorf("error running bisect check: %s", serr.String())
} }
start = mid start = mid
fmt.Println("false") afmt.Println("false")
default: default:
return err return err
} }
if start == end { if start == end {
if strings.TrimSpace(out.String()) == "true" { if strings.TrimSpace(out.String()) == "true" {
fmt.Println(midTs.Height()) afmt.Println(midTs.Height())
} else { } else {
fmt.Println(prev) afmt.Println(prev)
} }
return nil return nil
} }

View File

@ -368,8 +368,6 @@ func TestChainGet(t *testing.T) {
ts := mock.TipSet(blk) ts := mock.TipSet(blk)
cmd := WithCategory("chain", ChainGetCmd) cmd := WithCategory("chain", ChainGetCmd)
// lotus chain get /ipfs/[cid]/some/path
// given no -as-type flag & ipfs prefix, should print object as JSON if it's marshalable // given no -as-type flag & ipfs prefix, should print object as JSON if it's marshalable
t.Run("ipfs", func(t *testing.T) { t.Run("ipfs", func(t *testing.T) {
path := fmt.Sprintf("/ipfs/%s", blk.Cid().String()) path := fmt.Sprintf("/ipfs/%s", blk.Cid().String())
@ -437,7 +435,42 @@ func TestChainGet(t *testing.T) {
}) })
} }
func TestChainBisect(t *testing.T) {} func TestChainBisect(t *testing.T) {
blk1 := mock.MkBlock(nil, 0, 0)
blk1.Height = 0
ts1 := mock.TipSet(blk1)
blk2 := mock.MkBlock(ts1, 0, 0)
blk2.Height = 1
ts2 := mock.TipSet(blk2)
subpath := "whatever/its/mocked"
minHeight := uint64(0)
maxHeight := uint64(1)
shell := "echo"
path := fmt.Sprintf("/ipld/%s/%s", ts2.ParentState(), subpath)
cmd := WithCategory("chain", ChainBisectCmd)
app, mockApi, buf, done := NewMockAppWithFullAPI(t, cmd)
defer done()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
gomock.InOrder(
mockApi.EXPECT().ChainGetTipSetByHeight(ctx, abi.ChainEpoch(maxHeight), types.EmptyTSK).Return(ts2, nil),
mockApi.EXPECT().ChainGetTipSetByHeight(ctx, abi.ChainEpoch(maxHeight), ts2.Key()).Return(ts2, nil),
mockApi.EXPECT().ChainGetNode(ctx, path).Return(&api.IpldObject{Cid: blk2.Cid(), Obj: blk2}, nil),
)
err := app.Run([]string{"chain", "bisect", fmt.Sprintf("%d", minHeight), fmt.Sprintf("%d", maxHeight), subpath, shell})
assert.NoError(t, err)
out := buf.String()
assert.Contains(t, out, path)
}
func TestChainExport(t *testing.T) {} func TestChainExport(t *testing.T) {}