Skip to content

test: poa cover staking hook error propagation in MsgServer remove#142

Merged
AdriaCarrera merged 1 commit into
mainfrom
test/poa-hook-error-propagation
May 13, 2026
Merged

test: poa cover staking hook error propagation in MsgServer remove#142
AdriaCarrera merged 1 commit into
mainfrom
test/poa-hook-error-propagation

Conversation

@kpitapeersyst
Copy link
Copy Markdown
Contributor

@kpitapeersyst kpitapeersyst commented May 5, 2026

test: poa cover staking hook error propagation in MsgServer remove

Motivation 💡

The shared poaKeeperTestSetup fixture in x/poa/keeper/keeper_test.go configured MockStakingHooks with .AnyTimes() matchers that always returned nil. TestMsgServer_AddValidator and TestMsgServer_RemoveValidator reused this fixture, so their "should pass" cases never exercised the BeforeValidatorModified or BeforeValidatorSlashed error paths at the MsgServer layer. If a future change silently dropped hook errors during validator removal, the existing suite would not catch it.

Changes 🛠

  • Removed the always-pass poaKeeperTestSetup fixture from keeper_test.go.
  • Moved the poaAuthority constant into common_test.go for reuse across tests.
  • Refactored TestMsgServer_AddValidator and TestMsgServer_RemoveValidator to declare per-case stakingMocks / bankMocks closures and build the keeper via setupPoaKeeper, so each case owns its own expectations.
  • Added two MsgServer cases asserting hook errors are swallowed and logged:
    • BeforeValidatorModified returning an error -> log contains "failed to call before validator modified hook", RemoveValidator still succeeds.
    • BeforeValidatorSlashed returning an error -> log contains "failed to call before validator slashed hook", RemoveValidator still succeeds.
  • Captured log output via cosmossdk.io/log JSON logger into a bytes.Buffer and asserted substring presence with require.Contains.

Considerations 🤔

  • These tests lock in the current behavior of keeper.ExecuteRemoveValidator: hook errors are logged via k.Logger(ctx).Error(...) and execution continues. If the policy changes to propagate hook errors, both new cases must be updated.

Summary by CodeRabbit

  • Tests
    • Refactored test infrastructure with improved mock setup patterns for better test isolation.
    • Enhanced test coverage for validator operations with comprehensive mock-driven test cases.
    • Improved test logging and assertion capabilities for better validation.

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented May 5, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 668fa4f0-677c-48d8-aaeb-d468737cc6d6

📥 Commits

Reviewing files that changed from the base of the PR and between 43c1c36 and c3fea9a.

📒 Files selected for processing (4)
  • x/poa/keeper/common_test.go
  • x/poa/keeper/keeper_test.go
  • x/poa/keeper/msg_server_add_validator_test.go
  • x/poa/keeper/msg_server_remove_validator_test.go
💤 Files with no reviewable changes (1)
  • x/poa/keeper/keeper_test.go

📝 Walkthrough

Walkthrough

These changes refactor test infrastructure for the POA keeper by extracting a test-local authority constant, removing a redundant helper function, and refactoring test cases to supply per-test-case mock setup callbacks for staking and bank keepers. Hook error handling and log assertions are added to remove validator tests.

Changes

POA Keeper Test Infrastructure Refactoring

Layer / File(s) Summary
Test Constants
x/poa/keeper/common_test.go
Extracts poaAuthority constant ("ethm1wunfhl05vc8r8xxnnp8gt62wa54r6y52pg03zq") for reuse across tests; updates NewKeeper call to use the constant instead of inline string literal.
Helper Removal
x/poa/keeper/keeper_test.go
Removes poaKeeperTestSetup function that previously wrapped staking/bank mock expectations; tests now call setupPoaKeeper directly.
Test Refactoring: AddValidator
x/poa/keeper/msg_server_add_validator_test.go
Imports cosmossdk.io/math and sdk alias; restructures test table to accept per-case stakingMocks and bankMocks callbacks; detailed mock expectations added for staking parameters, validators, delegations, and bank balance/mint operations; setupPoaKeeper called per subtest with case-specific mocks.
Test Refactoring: RemoveValidator
x/poa/keeper/msg_server_remove_validator_test.go
Imports updated to support hook testing and log assertions (cosmossdk.io/log, gomock, testutil); test table expanded with stakingMocks, bankMocks, and expectedLog fields; two new cases validate that hook errors from BeforeValidatorModified and BeforeValidatorSlashed are swallowed while emitting log messages; conditional logger-buffer wiring added to capture and assert log output.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~22 minutes

Poem

🐰 A constant springs forth, clear and bright,
Old helpers rest, no more in sight,
Mock callbacks dance case by case,
Hooks swallowed gently, logged in place.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately and specifically summarizes the main change: adding test coverage for staking hook error propagation in MsgServer RemoveValidator.
Description check ✅ Passed The description follows the template structure completely with clear Motivation, Changes, and Considerations sections that comprehensively explain the refactoring and new test coverage.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch test/poa-hook-error-propagation

Warning

There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure.

🔧 golangci-lint (2.12.1)

Error: can't load config: unsupported version of the configuration: "" See https://golangci-lint.run/docs/product/migration-guide for migration instructions
The command is terminated due to an error: can't load config: unsupported version of the configuration: "" See https://golangci-lint.run/docs/product/migration-guide for migration instructions


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@AdriaCarrera AdriaCarrera merged commit dc434b9 into main May 13, 2026
6 checks passed
@AdriaCarrera AdriaCarrera deleted the test/poa-hook-error-propagation branch May 13, 2026 15:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants