github.com/Oyster-zx/tendermint@v0.34.24-fork/cmd/tendermint/commands/reindex_event_test.go (about) 1 package commands 2 3 import ( 4 "context" 5 "errors" 6 "testing" 7 8 "github.com/spf13/cobra" 9 "github.com/stretchr/testify/mock" 10 "github.com/stretchr/testify/require" 11 12 dbm "github.com/tendermint/tm-db" 13 14 abcitypes "github.com/tendermint/tendermint/abci/types" 15 tmcfg "github.com/tendermint/tendermint/config" 16 prototmstate "github.com/tendermint/tendermint/proto/tendermint/state" 17 blockmocks "github.com/tendermint/tendermint/state/indexer/mocks" 18 "github.com/tendermint/tendermint/state/mocks" 19 txmocks "github.com/tendermint/tendermint/state/txindex/mocks" 20 "github.com/tendermint/tendermint/types" 21 ) 22 23 const ( 24 height int64 = 10 25 base int64 = 2 26 ) 27 28 func setupReIndexEventCmd() *cobra.Command { 29 reIndexEventCmd := &cobra.Command{ 30 Use: ReIndexEventCmd.Use, 31 Run: func(cmd *cobra.Command, args []string) {}, 32 } 33 34 _ = reIndexEventCmd.ExecuteContext(context.Background()) 35 36 return reIndexEventCmd 37 } 38 39 func TestReIndexEventCheckHeight(t *testing.T) { 40 mockBlockStore := &mocks.BlockStore{} 41 mockBlockStore. 42 On("Base").Return(base). 43 On("Height").Return(height) 44 45 testCases := []struct { 46 startHeight int64 47 endHeight int64 48 validHeight bool 49 }{ 50 {0, 0, true}, 51 {0, base, true}, 52 {0, base - 1, false}, 53 {0, height, true}, 54 {0, height + 1, true}, 55 {0, 0, true}, 56 {base - 1, 0, false}, 57 {base, 0, true}, 58 {base, base, true}, 59 {base, base - 1, false}, 60 {base, height, true}, 61 {base, height + 1, true}, 62 {height, 0, true}, 63 {height, base, false}, 64 {height, height - 1, false}, 65 {height, height, true}, 66 {height, height + 1, true}, 67 {height + 1, 0, false}, 68 } 69 70 for _, tc := range testCases { 71 startHeight = tc.startHeight 72 endHeight = tc.endHeight 73 74 err := checkValidHeight(mockBlockStore) 75 if tc.validHeight { 76 require.NoError(t, err) 77 } else { 78 require.Error(t, err) 79 } 80 } 81 } 82 83 func TestLoadEventSink(t *testing.T) { 84 testCases := []struct { 85 sinks string 86 connURL string 87 loadErr bool 88 }{ 89 {"", "", true}, 90 {"NULL", "", true}, 91 {"KV", "", false}, 92 {"PSQL", "", true}, // true because empty connect url 93 // skip to test PSQL connect with correct url 94 {"UnsupportedSinkType", "wrongUrl", true}, 95 } 96 97 for idx, tc := range testCases { 98 cfg := tmcfg.TestConfig() 99 cfg.TxIndex.Indexer = tc.sinks 100 cfg.TxIndex.PsqlConn = tc.connURL 101 _, _, err := loadEventSinks(cfg) 102 if tc.loadErr { 103 require.Error(t, err, idx) 104 } else { 105 require.NoError(t, err, idx) 106 } 107 } 108 } 109 110 func TestLoadBlockStore(t *testing.T) { 111 cfg := tmcfg.TestConfig() 112 cfg.DBPath = t.TempDir() 113 _, _, err := loadStateAndBlockStore(cfg) 114 require.Error(t, err) 115 116 _, err = dbm.NewDB("blockstore", dbm.GoLevelDBBackend, cfg.DBDir()) 117 require.NoError(t, err) 118 119 // Get StateStore 120 _, err = dbm.NewDB("state", dbm.GoLevelDBBackend, cfg.DBDir()) 121 require.NoError(t, err) 122 123 bs, ss, err := loadStateAndBlockStore(cfg) 124 require.NoError(t, err) 125 require.NotNil(t, bs) 126 require.NotNil(t, ss) 127 128 } 129 130 func TestReIndexEvent(t *testing.T) { 131 mockBlockStore := &mocks.BlockStore{} 132 mockStateStore := &mocks.Store{} 133 mockBlockIndexer := &blockmocks.BlockIndexer{} 134 mockTxIndexer := &txmocks.TxIndexer{} 135 136 mockBlockStore. 137 On("Base").Return(base). 138 On("Height").Return(height). 139 On("LoadBlock", base).Return(nil).Once(). 140 On("LoadBlock", base).Return(&types.Block{Data: types.Data{Txs: types.Txs{make(types.Tx, 1)}}}). 141 On("LoadBlock", height).Return(&types.Block{Data: types.Data{Txs: types.Txs{make(types.Tx, 1)}}}) 142 143 dtx := abcitypes.ResponseDeliverTx{} 144 abciResp := &prototmstate.ABCIResponses{ 145 DeliverTxs: []*abcitypes.ResponseDeliverTx{&dtx}, 146 EndBlock: &abcitypes.ResponseEndBlock{}, 147 BeginBlock: &abcitypes.ResponseBeginBlock{}, 148 } 149 150 mockBlockIndexer. 151 On("Index", mock.AnythingOfType("types.EventDataNewBlockHeader")).Return(errors.New("")).Once(). 152 On("Index", mock.AnythingOfType("types.EventDataNewBlockHeader")).Return(nil) 153 154 mockTxIndexer. 155 On("AddBatch", mock.AnythingOfType("*txindex.Batch")).Return(errors.New("")).Once(). 156 On("AddBatch", mock.AnythingOfType("*txindex.Batch")).Return(nil) 157 158 mockStateStore. 159 On("LoadABCIResponses", base).Return(nil, errors.New("")).Once(). 160 On("LoadABCIResponses", base).Return(abciResp, nil). 161 On("LoadABCIResponses", height).Return(abciResp, nil) 162 163 testCases := []struct { 164 startHeight int64 165 endHeight int64 166 reIndexErr bool 167 }{ 168 {base, height, true}, // LoadBlock error 169 {base, height, true}, // LoadABCIResponses error 170 {base, height, true}, // index block event error 171 {base, height, true}, // index tx event error 172 {base, base, false}, 173 {height, height, false}, 174 } 175 176 for _, tc := range testCases { 177 args := eventReIndexArgs{ 178 startHeight: tc.startHeight, 179 endHeight: tc.endHeight, 180 blockIndexer: mockBlockIndexer, 181 txIndexer: mockTxIndexer, 182 blockStore: mockBlockStore, 183 stateStore: mockStateStore, 184 } 185 186 err := eventReIndex(setupReIndexEventCmd(), args) 187 if tc.reIndexErr { 188 require.Error(t, err) 189 } else { 190 require.NoError(t, err) 191 } 192 } 193 }