test: chain read-obj

Simple test that checks if this CLI method prints the IPLD node referenced
by the given CID encoded in hexadecimal.
This commit is contained in:
Nikola Divic 2022-02-09 15:29:10 +01:00
parent 78649d45b9
commit 1cd590ace9
2 changed files with 29 additions and 2 deletions

View File

@ -185,6 +185,8 @@ var ChainReadObjCmd = &cli.Command{
Usage: "Read the raw bytes of an object", Usage: "Read the raw bytes of an object",
ArgsUsage: "[objectCid]", ArgsUsage: "[objectCid]",
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
@ -202,7 +204,7 @@ var ChainReadObjCmd = &cli.Command{
return err return err
} }
fmt.Printf("%x\n", obj) afmt.Printf("%x\n", obj)
return nil return nil
}, },
} }

View File

@ -4,6 +4,7 @@ import (
"bytes" "bytes"
"context" "context"
"encoding/json" "encoding/json"
"fmt"
"regexp" "regexp"
"testing" "testing"
@ -56,7 +57,7 @@ func TestChainHead(t *testing.T) {
assert.Regexp(t, regexp.MustCompile(ts.Cids()[0].String()), buf.String()) 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) { func TestGetBlock(t *testing.T) {
app, mockApi, buf, done := newMockAppWithFullAPI(t, WithCategory("chain", ChainGetBlock)) app, mockApi, buf, done := newMockAppWithFullAPI(t, WithCategory("chain", ChainGetBlock))
defer done() defer done()
@ -77,6 +78,7 @@ func TestGetBlock(t *testing.T) {
err := app.Run([]string{"chain", "getblock", block.Cid().String()}) err := app.Run([]string{"chain", "getblock", block.Cid().String()})
assert.NoError(t, err) assert.NoError(t, err)
// expected output format
out := struct { out := struct {
types.BlockHeader types.BlockHeader
BlsMessages []*types.Message BlsMessages []*types.Message
@ -90,3 +92,26 @@ func TestGetBlock(t *testing.T) {
assert.True(t, block.Cid().Equals(out.Cid())) 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()))
}