test: chain stat-obj cli command

Test expected output with respect to the --base flag
This commit is contained in:
Nikola Divic 2022-02-09 16:22:52 +01:00
parent c0f47e5eed
commit a923d7c884
2 changed files with 55 additions and 3 deletions

View File

@ -264,6 +264,7 @@ var ChainStatObjCmd = &cli.Command{
}, },
}, },
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
@ -289,8 +290,8 @@ var ChainStatObjCmd = &cli.Command{
return err return err
} }
fmt.Printf("Links: %d\n", stats.Links) afmt.Printf("Links: %d\n", stats.Links)
fmt.Printf("Size: %s (%d)\n", types.SizeStr(types.NewInt(stats.Size)), stats.Size) afmt.Printf("Size: %s (%d)\n", types.SizeStr(types.NewInt(stats.Size)), stats.Size)
return nil return nil
}, },
} }

View File

@ -6,6 +6,7 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"regexp" "regexp"
"strings"
"testing" "testing"
"github.com/filecoin-project/lotus/api" "github.com/filecoin-project/lotus/api"
@ -130,7 +131,7 @@ func TestChainDeleteObj(t *testing.T) {
assert.Error(t, err) assert.Error(t, err)
}) })
// given a force flag, API delete should be called // given a force flag, it calls API delete
t.Run("really-do-it", func(t *testing.T) { t.Run("really-do-it", func(t *testing.T) {
app, mockApi, buf, done := newMockAppWithFullAPI(t, cmd) app, mockApi, buf, done := newMockAppWithFullAPI(t, cmd)
defer done() defer done()
@ -148,3 +149,53 @@ func TestChainDeleteObj(t *testing.T) {
assert.Contains(t, buf.String(), block.Cid().String()) assert.Contains(t, buf.String(), block.Cid().String())
}) })
} }
func TestChainStatObj(t *testing.T) {
cmd := WithCategory("chain", ChainStatObjCmd)
block := mock.MkBlock(nil, 0, 0)
stat := api.ObjStat{Size: 123, Links: 321}
checkOutput := func(buf *bytes.Buffer) {
out := buf.String()
outSplit := strings.Split(out, "\n")
assert.Contains(t, outSplit[0], fmt.Sprintf("%d", stat.Links))
assert.Contains(t, outSplit[1], fmt.Sprintf("%d", stat.Size))
}
// given no --base flag, it calls ChainStatObj with base=cid.Undef
t.Run("no-base", func(t *testing.T) {
app, mockApi, buf, done := newMockAppWithFullAPI(t, cmd)
defer done()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
gomock.InOrder(
mockApi.EXPECT().ChainStatObj(ctx, block.Cid(), cid.Undef).Return(stat, nil),
)
err := app.Run([]string{"chain", "stat-obj", block.Cid().String()})
assert.NoError(t, err)
checkOutput(buf)
})
// given a --base flag, it calls ChainStatObj with that base
t.Run("base", func(t *testing.T) {
app, mockApi, buf, done := newMockAppWithFullAPI(t, cmd)
defer done()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
gomock.InOrder(
mockApi.EXPECT().ChainStatObj(ctx, block.Cid(), block.Cid()).Return(stat, nil),
)
err := app.Run([]string{"chain", "stat-obj", fmt.Sprintf("-base=%s", block.Cid().String()), block.Cid().String()})
assert.NoError(t, err)
checkOutput(buf)
})
}