github.com/iotexproject/iotex-core@v1.14.1-rc1/ioctl/newcmd/bc/bcblock_test.go (about) 1 // Copyright (c) 2022 IoTeX 2 // This source code is provided 'as is' and no warranties are given as to title or non-infringement, merchantability 3 // or fitness for purpose and, to the extent permitted by law, all liability for your use of the code is disclaimed. 4 // This source code is governed by Apache License 2.0 that can be found in the LICENSE file. 5 6 package bc 7 8 import ( 9 "testing" 10 11 "github.com/golang/mock/gomock" 12 "github.com/iotexproject/iotex-proto/golang/iotexapi" 13 "github.com/iotexproject/iotex-proto/golang/iotexapi/mock_iotexapi" 14 "github.com/iotexproject/iotex-proto/golang/iotextypes" 15 "github.com/pkg/errors" 16 "github.com/stretchr/testify/require" 17 18 "github.com/iotexproject/iotex-core/ioctl/config" 19 "github.com/iotexproject/iotex-core/ioctl/util" 20 "github.com/iotexproject/iotex-core/test/mock/mock_ioctlclient" 21 ) 22 23 // test for bc info command 24 func TestNewBCBlockCmd(t *testing.T) { 25 require := require.New(t) 26 ctrl := gomock.NewController(t) 27 client := mock_ioctlclient.NewMockClient(ctrl) 28 cfg := config.Config{} 29 apiServiceClient := mock_iotexapi.NewMockAPIServiceClient(ctrl) 30 chainMetaResponse := &iotexapi.GetChainMetaResponse{ChainMeta: &iotextypes.ChainMeta{}} 31 blockMeta := []*iotextypes.BlockMeta{ 32 { 33 Hash: "abcd", 34 Height: 1, 35 }, 36 } 37 blockMetaResponse := &iotexapi.GetBlockMetasResponse{BlkMetas: blockMeta} 38 39 client.EXPECT().SelectTranslation(gomock.Any()).Return("", config.English).AnyTimes() 40 client.EXPECT().Config().Return(cfg).AnyTimes() 41 42 t.Run("failed to dial grpc connection", func(t *testing.T) { 43 expectedErr := errors.New("failed to dial grpc connection") 44 client.EXPECT().APIServiceClient().Return(nil, expectedErr).Times(1) 45 46 cmd := NewBCBlockCmd(client) 47 _, err := util.ExecuteCmd(cmd) 48 require.Error(err) 49 require.Equal(expectedErr, err) 50 }) 51 52 client.EXPECT().APIServiceClient().Return(apiServiceClient, nil).AnyTimes() 53 54 t.Run("failed to get chain meta", func(t *testing.T) { 55 expectedErr := errors.New("failed to get chain meta") 56 apiServiceClient.EXPECT().GetChainMeta(gomock.Any(), gomock.Any()).Return(nil, expectedErr).Times(1) 57 58 cmd := NewBCBlockCmd(client) 59 _, err := util.ExecuteCmd(cmd) 60 require.Error(err) 61 require.Contains(err.Error(), expectedErr.Error()) 62 }) 63 64 t.Run("failed to invoke GetBlockMetas api", func(t *testing.T) { 65 expectedErr := errors.New("failed to invoke GetBlockMetas api") 66 apiServiceClient.EXPECT().GetChainMeta(gomock.Any(), gomock.Any()).Return(chainMetaResponse, nil).Times(1) 67 apiServiceClient.EXPECT().GetBlockMetas(gomock.Any(), gomock.Any()).Return(nil, expectedErr).Times(1) 68 69 cmd := NewBCBlockCmd(client) 70 _, err := util.ExecuteCmd(cmd) 71 require.Error(err) 72 require.Contains(err.Error(), expectedErr.Error()) 73 }) 74 75 apiServiceClient.EXPECT().GetChainMeta(gomock.Any(), gomock.Any()).Return(chainMetaResponse, nil).Times(1) 76 apiServiceClient.EXPECT().GetBlockMetas(gomock.Any(), gomock.Any()).Return(blockMetaResponse, nil).Times(3) 77 78 t.Run("get blockchain block", func(t *testing.T) { 79 expectedValue := "Blockchain Node: \n{\n \"hash\": \"abcd\",\n \"height\": 1\n}\nnull\n" 80 81 cmd := NewBCBlockCmd(client) 82 result, err := util.ExecuteCmd(cmd) 83 require.NoError(err) 84 require.Equal(expectedValue, result) 85 }) 86 87 t.Run("get block meta by height", func(t *testing.T) { 88 expectedValue := "\"height\": 1" 89 90 cmd := NewBCBlockCmd(client) 91 result, err := util.ExecuteCmd(cmd, "1") 92 require.NoError(err) 93 require.Contains(result, expectedValue) 94 }) 95 96 t.Run("get block meta by hash", func(t *testing.T) { 97 expectedValue := "\"hash\": \"abcd\"" 98 99 cmd := NewBCBlockCmd(client) 100 result, err := util.ExecuteCmd(cmd, "abcd") 101 require.NoError(err) 102 require.Contains(result, expectedValue) 103 }) 104 105 t.Run("invalid height", func(t *testing.T) { 106 expectedErr := errors.New("invalid height") 107 108 cmd := NewBCBlockCmd(client) 109 _, err := util.ExecuteCmd(cmd, "0") 110 require.Error(err) 111 require.Contains(err.Error(), expectedErr.Error()) 112 }) 113 114 apiServiceClient.EXPECT().GetChainMeta(gomock.Any(), gomock.Any()).Return(chainMetaResponse, nil).Times(2) 115 apiServiceClient.EXPECT().GetBlockMetas(gomock.Any(), gomock.Any()).Return(blockMetaResponse, nil).Times(2) 116 117 t.Run("failed to get actions info", func(t *testing.T) { 118 expectedErr := errors.New("failed to get actions info") 119 apiServiceClient.EXPECT().GetRawBlocks(gomock.Any(), gomock.Any()).Return(nil, expectedErr).Times(1) 120 121 cmd := NewBCBlockCmd(client) 122 _, err := util.ExecuteCmd(cmd, "--verbose") 123 require.Error(err) 124 require.Contains(err.Error(), expectedErr.Error()) 125 }) 126 127 t.Run("verbose", func(t *testing.T) { 128 blockInfo := []*iotexapi.BlockInfo{ 129 { 130 Block: &iotextypes.Block{ 131 Body: &iotextypes.BlockBody{ 132 Actions: []*iotextypes.Action{ 133 &iotextypes.Action{ 134 Core: &iotextypes.ActionCore{ 135 Version: 1, 136 Nonce: 2, 137 GasLimit: 3, 138 GasPrice: "4", 139 }, 140 }, 141 &iotextypes.Action{ 142 Core: &iotextypes.ActionCore{ 143 Version: 5, 144 Nonce: 6, 145 GasLimit: 7, 146 GasPrice: "8", 147 }, 148 }, 149 }, 150 }, 151 }, 152 Receipts: []*iotextypes.Receipt{ 153 { 154 Status: 1, 155 BlkHeight: 1, 156 ActHash: []byte("02ae2a956d21e8d481c3a69e146633470cf625ec"), 157 GasConsumed: 1, 158 ContractAddress: "test", 159 Logs: []*iotextypes.Log{}, 160 }, 161 { 162 Status: 1, 163 BlkHeight: 1, 164 ActHash: []byte("02ae2a956d21e8d481c3a69e146633470cf625ec"), 165 GasConsumed: 1, 166 ContractAddress: "test", 167 Logs: []*iotextypes.Log{}, 168 }, 169 }, 170 }, 171 } 172 rawBlocksResponse := &iotexapi.GetRawBlocksResponse{Blocks: blockInfo} 173 expectedValue1 := "\"version\": 1,\n \"nonce\": 2,\n \"gasLimit\": 3,\n \"gasPrice\": \"4\"" 174 expectedValue2 := "\"version\": 5,\n \"nonce\": 6,\n \"gasLimit\": 7,\n \"gasPrice\": \"8\"" 175 apiServiceClient.EXPECT().GetRawBlocks(gomock.Any(), gomock.Any()).Return(rawBlocksResponse, nil).Times(1) 176 177 cmd := NewBCBlockCmd(client) 178 result, err := util.ExecuteCmd(cmd, "--verbose") 179 require.NoError(err) 180 require.Contains(result, expectedValue1) 181 require.Contains(result, expectedValue2) 182 }) 183 }