From 1cd590ace93d627fee4d8d8d92ae130dd125f45e Mon Sep 17 00:00:00 2001 From: Nikola Divic Date: Wed, 9 Feb 2022 15:29:10 +0100 Subject: [PATCH] test: chain read-obj Simple test that checks if this CLI method prints the IPLD node referenced by the given CID encoded in hexadecimal. --- cli/chain.go | 4 +++- cli/chain_test.go | 27 ++++++++++++++++++++++++++- 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/cli/chain.go b/cli/chain.go index 4d03ac4f7..43d5ffb1f 100644 --- a/cli/chain.go +++ b/cli/chain.go @@ -185,6 +185,8 @@ var ChainReadObjCmd = &cli.Command{ Usage: "Read the raw bytes of an object", ArgsUsage: "[objectCid]", Action: func(cctx *cli.Context) error { + afmt := NewAppFmt(cctx.App) + api, closer, err := GetFullNodeAPI(cctx) if err != nil { return err @@ -202,7 +204,7 @@ var ChainReadObjCmd = &cli.Command{ return err } - fmt.Printf("%x\n", obj) + afmt.Printf("%x\n", obj) return nil }, } diff --git a/cli/chain_test.go b/cli/chain_test.go index eb98110f0..4a961d022 100644 --- a/cli/chain_test.go +++ b/cli/chain_test.go @@ -4,6 +4,7 @@ import ( "bytes" "context" "encoding/json" + "fmt" "regexp" "testing" @@ -56,7 +57,7 @@ func TestChainHead(t *testing.T) { assert.Regexp(t, regexp.MustCompile(ts.Cids()[0].String()), buf.String()) } -// TestGetBlock checks if "lotus chain getblock" returns the block information in the expected format +// TestGetBlock checks if "chain getblock" returns the block information in the expected format func TestGetBlock(t *testing.T) { app, mockApi, buf, done := newMockAppWithFullAPI(t, WithCategory("chain", ChainGetBlock)) defer done() @@ -77,6 +78,7 @@ func TestGetBlock(t *testing.T) { err := app.Run([]string{"chain", "getblock", block.Cid().String()}) assert.NoError(t, err) + // expected output format out := struct { types.BlockHeader BlsMessages []*types.Message @@ -90,3 +92,26 @@ func TestGetBlock(t *testing.T) { assert.True(t, block.Cid().Equals(out.Cid())) } + +// TestChainReadObj checks if "chain read-obj" prints the referenced IPLD node as hex, if exists +func TestReadOjb(t *testing.T) { + app, mockApi, buf, done := newMockAppWithFullAPI(t, WithCategory("chain", ChainReadObjCmd)) + defer done() + + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + + block := mock.MkBlock(nil, 0, 0) + obj := new(bytes.Buffer) + err := block.MarshalCBOR(obj) + assert.NoError(t, err) + + gomock.InOrder( + mockApi.EXPECT().ChainReadObj(ctx, block.Cid()).Return(obj.Bytes(), nil), + ) + + err = app.Run([]string{"chain", "read-obj", block.Cid().String()}) + assert.NoError(t, err) + + assert.Equal(t, buf.String(), fmt.Sprintf("%x\n", obj.Bytes())) +}