github.com/iotexproject/iotex-core@v1.14.1-rc1/ioctl/newcmd/node/nodeprobationlist_test.go (about) 1 // Copyright (c) 2022 IoTeX Foundation 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 node 7 8 import ( 9 "strconv" 10 "testing" 11 12 "github.com/golang/mock/gomock" 13 "github.com/iotexproject/iotex-proto/golang/iotexapi" 14 "github.com/iotexproject/iotex-proto/golang/iotexapi/mock_iotexapi" 15 "github.com/iotexproject/iotex-proto/golang/iotextypes" 16 "github.com/pkg/errors" 17 "github.com/stretchr/testify/require" 18 19 "github.com/iotexproject/iotex-core/action/protocol/vote" 20 "github.com/iotexproject/iotex-core/ioctl/config" 21 "github.com/iotexproject/iotex-core/ioctl/util" 22 "github.com/iotexproject/iotex-core/test/mock/mock_ioctlclient" 23 ) 24 25 func TestNewNodeProbationlistCmd(t *testing.T) { 26 require := require.New(t) 27 ctrl := gomock.NewController(t) 28 client := mock_ioctlclient.NewMockClient(ctrl) 29 apiServiceClient := mock_iotexapi.NewMockAPIServiceClient(ctrl) 30 31 client.EXPECT().SelectTranslation(gomock.Any()).Return("", config.English).Times(12) 32 client.EXPECT().APIServiceClient().Return(apiServiceClient, nil).AnyTimes() 33 34 t.Run("failed to get chain meta", func(t *testing.T) { 35 expectedErr := errors.New("failed to get chain meta") 36 apiServiceClient.EXPECT().GetChainMeta(gomock.Any(), gomock.Any()).Return(nil, expectedErr).Times(1) 37 38 cmd := NewNodeProbationlistCmd(client) 39 _, err := util.ExecuteCmd(cmd) 40 require.Contains(err.Error(), "failed to get chain meta") 41 }) 42 43 var testBlockProducersInfo = []*iotexapi.BlockProducerInfo{ 44 {Address: "io1kr8c6krd7dhxaaqwdkr6erqgu4z0scug3drgja", Votes: "109510794521770016955545668", Active: true, Production: 30}, 45 {Address: "io13q2am9nedrd3n746lsj6qan4pymcpgm94vvx2c", Votes: "81497052527306018062463878", Active: false, Production: 0}, 46 } 47 48 chainMetaResponse := &iotexapi.GetChainMetaResponse{ChainMeta: &iotextypes.ChainMeta{Epoch: &iotextypes.EpochData{Num: 7000}}} 49 epochMetaResponse := &iotexapi.GetEpochMetaResponse{EpochData: &iotextypes.EpochData{Num: 7000, Height: 3223081}, TotalBlocks: 720, BlockProducersInfo: testBlockProducersInfo} 50 51 apiServiceClient.EXPECT().GetChainMeta(gomock.Any(), gomock.Any()).Return(chainMetaResponse, nil).AnyTimes() 52 apiServiceClient.EXPECT().GetEpochMeta(gomock.Any(), gomock.Any()).Return(epochMetaResponse, nil).Times(3) 53 54 t.Run("query probation list", func(t *testing.T) { 55 probationList := &iotexapi.ReadStateResponse{} 56 57 apiServiceClient.EXPECT().ReadState(gomock.Any(), gomock.Any()).Return(probationList, nil).Times(1) 58 59 cmd := NewNodeProbationlistCmd(client) 60 result, err := util.ExecuteCmd(cmd) 61 require.NoError(err) 62 require.Contains(result, "ProbationList : []") 63 }) 64 65 t.Run("epochNum > 0", func(t *testing.T) { 66 d, _ := vote.NewProbationList(1).Serialize() 67 probationList := &iotexapi.ReadStateResponse{ 68 Data: d, 69 } 70 71 apiServiceClient.EXPECT().ReadState(gomock.Any(), gomock.Any()).Return(probationList, nil).Times(1) 72 73 cmd := NewNodeProbationlistCmd(client) 74 result, err := util.ExecuteCmd(cmd, "-e", "1") 75 require.NoError(err) 76 require.Contains(result, "ProbationList : []") 77 }) 78 79 t.Run("failed to get probation list", func(t *testing.T) { 80 apiServiceClient.EXPECT().ReadState(gomock.Any(), &iotexapi.ReadStateRequest{ 81 ProtocolID: []byte("poll"), 82 MethodName: []byte("ProbationListByEpoch"), 83 Arguments: [][]byte{[]byte("7000")}, 84 Height: strconv.FormatUint(3223081, 10), 85 }).Return(&iotexapi.ReadStateResponse{ 86 Data: []byte("0")}, 87 nil) 88 89 cmd := NewNodeProbationlistCmd(client) 90 _, err := util.ExecuteCmd(cmd) 91 require.Contains(err.Error(), "failed to get probation list") 92 }) 93 }