Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(tests): Port auth integration tests to server v2 #22554

Merged
merged 33 commits into from
Nov 27, 2024

Conversation

sontrinh16
Copy link
Member

@sontrinh16 sontrinh16 commented Nov 19, 2024

Description

ref: #20799


Author Checklist

All items are required. Please add a note to the item if the item is not applicable and
please add links to any relevant follow up issues.

I have...

  • included the correct type prefix in the PR title, you can find examples of the prefixes below:
  • confirmed ! in the type prefix if API or client breaking change
  • targeted the correct branch (see PR Targeting)
  • provided a link to the relevant issue or specification
  • reviewed "Files changed" and left comments if necessary
  • included the necessary unit and integration tests
  • added a changelog entry to CHANGELOG.md
  • updated the relevant documentation or specification, including comments for documenting Go code
  • confirmed all CI checks have passed

Reviewers Checklist

All items are required. Please add a note if the item is not applicable and please add
your handle next to the items reviewed if you only reviewed selected items.

Please see Pull Request Reviewer section in the contributing guide for more information on how to review a pull request.

I have...

  • confirmed the correct type prefix in the PR title
  • confirmed all author checklist items have been addressed
  • reviewed state machine logic, API design and naming, documentation is accurate, tests and test coverage

Summary by CodeRabbit

  • New Features

    • Enhanced service registration and environment provisioning for improved flexibility.
    • Introduced integration tests for retrocompatibility between authentication and accounts modules.
    • Added a test suite for the NonAtomicExec message handling in the auth module.
  • Bug Fixes

    • Improved error handling for invalid message types and context validation.
  • Tests

    • Expanded integration testing framework with new test cases and functionalities for account migration and message handling.

Copy link
Contributor

coderabbitai bot commented Nov 19, 2024

📝 Walkthrough
📝 Walkthrough
📝 Walkthrough

Walkthrough

The pull request introduces significant enhancements to the runtime/v2/module.go file, focusing on service registration and environment provisioning. It adds new types and methods for dynamic service construction using a builder pattern, replacing static instantiations. The integration testing framework is also improved, with new test files and methods to validate the functionalities of authentication and account migration. Overall, the changes promote modularity and extensibility in service management within the Cosmos SDK.

Changes

File Path Change Summary
runtime/v2/module.go Added RouterServiceBuilder interface and routerBuilder type; updated ProvideEnvironment and DefaultServiceBindings functions with new parameters.
tests/integration/v2/app.go Introduced RunMsgConfig type and WithAutomaticCommit function; updated NewApp function to accept new parameters for service configuration.
tests/integration/v2/auth/accounts_retro_compatibility_test.go Added integration tests for retrocompatibility with mock account implementations and various test scenarios.
tests/integration/v2/auth/app_test.go Created a new test suite for the authentication module, including setup for application components and message routing.
tests/integration/v2/auth/migrate_x_accounts_test.go Introduced tests for account migration functionality, validating various migration scenarios.
tests/integration/v2/auth/msg_server_test.go Added tests for NonAtomicExec message handling with various scenarios for valid and invalid inputs.
tests/integration/v2/services.go Introduced BranchService and RouterService types with methods for message handling and error management; added new error variable for invalid types.

Possibly related PRs

Suggested labels

C:server/v2, backport/v0.52.x

Suggested reviewers

  • julienrbrt
  • kocubinski
  • tac0turtle

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Outside diff range and nitpick comments (1)
runtime/v2/module.go (1)

290-293: Consider adding error handling for router service initialization

The router service factory initialization could benefit from error handling to gracefully handle initialization failures.

Consider wrapping the service initialization with error handling:

 routerBuilder RouterServiceBuilder = routerBuilder{
-			msgRouterServiceFactory: stf.NewMsgRouterService,
+			msgRouterServiceFactory: func(actor []byte) router.Service {
+				svc := stf.NewMsgRouterService(actor)
+				if svc == nil {
+					panic("failed to initialize message router service")
+				}
+				return svc
+			},
 			queryRouter:             stf.NewQueryRouterService(),
 		}
📜 Review details

Configuration used: .coderabbit.yml
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between eb6f21b and 2bfe906.

📒 Files selected for processing (1)
  • runtime/v2/module.go (3 hunks)
🧰 Additional context used
📓 Path-based instructions (1)
runtime/v2/module.go (1)

Pattern **/*.go: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.

🔇 Additional comments (2)
runtime/v2/module.go (2)

218-232: LGTM: Environment provisioning changes are well-integrated

The new services (branchService and routerBuilder) are properly integrated into the environment while maintaining backward compatibility.


246-268: LGTM: Clean RouterServiceBuilder implementation

The implementation is concise, follows Go idioms, and correctly implements the interface contract.

x/auth/keeper/genesis.go Outdated Show resolved Hide resolved
tests/integration/v2/app.go Outdated Show resolved Hide resolved
tests/integration/v2/app.go Show resolved Hide resolved
@github-actions github-actions bot removed the C:x/auth label Nov 26, 2024
julienrbrt
julienrbrt previously approved these changes Nov 26, 2024
Copy link
Member

@julienrbrt julienrbrt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ACK, nice work! One nit.

queryRouter router.Service
}

func NewRouterBuilder(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's kill this as you aren't supposed to use construct that manually

Copy link
Member

@julienrbrt julienrbrt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One Q, are we sure those integration tests run on CI? I cannot find the job in v2-test.yml so I do not think they run.

Could you please add it? (copying test-integration from test.yml to v2-test.yml and renaming it test-integration-v2 and changing what's necessary)
I'll mark it as required afterwards

@julienrbrt julienrbrt dismissed their stale review November 26, 2024 08:35

dismissed because of above comment

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🧹 Outside diff range and nitpick comments (5)
tests/integration/v2/app.go (4)

57-58: Add documentation for the handler type.

Consider adding a doc comment explaining the purpose and usage of the handler type for better code maintainability.

+// handler is a function type that processes a context and returns a transaction message and an error.
 type handler = func(ctx context.Context) (transaction.Msg, error)

94-98: Enhance documentation for new configuration fields.

The documentation for BranchService and RouterServiceBuilder could be more descriptive about their purposes and default behaviors.

-	// BranchService defines the custom branch service to be used in the app.
+	// BranchService defines the custom branch service to be used in the app.
+	// If not provided, a default stf.BranchService will be used.
 	BranchService corebranch.Service
-	// RouterServiceBuilder defines the custom builder
-	// for msg router and query router service to be used in the app.
+	// RouterServiceBuilder defines the custom builder for message and query routing.
+	// It constructs services that handle message routing and query handling.
+	// If not provided, a default builder with stf router services will be used.
 	RouterServiceBuilder runtime.RouterServiceBuilder

124-128: Consider making service initialization more configurable.

The current implementation directly initializes services with specific implementations. Consider allowing for more flexibility by accepting service factories or builders as parameters.

 return StartupConfig{
     ValidatorSet:    CreateRandomValidatorSet,
     GenesisBehavior: Genesis_COMMIT,
     GenesisAccounts: []GenesisAccount{ga},
     HomeDir:         homedir,
-    BranchService:   stf.BranchService{},
-    RouterServiceBuilder: runtime.NewRouterBuilder(
-        stf.NewMsgRouterService, stf.NewQueryRouterService(),
-    ),
+    BranchService:   getDefaultBranchService(),
+    RouterServiceBuilder: getDefaultRouterBuilder(),
 }

131-143: Consider adding more configuration options.

The RunMsgConfig currently only supports automatic commit. Consider adding more options that might be useful for testing scenarios, such as:

  • Gas limit configuration
  • Error handling behavior
  • State verification callbacks
 type RunMsgConfig struct {
     Commit bool
+    GasLimit uint64
+    SkipErrorHandling bool
+    StateVerifier func(context.Context) error
 }

 // WithAutomaticCommit enables automatic commit.
 func WithAutomaticCommit() Option {
     return func(cfg *RunMsgConfig) {
         cfg.Commit = true
     }
 }

+// WithGasLimit sets a custom gas limit for the message execution.
+func WithGasLimit(limit uint64) Option {
+    return func(cfg *RunMsgConfig) {
+        cfg.GasLimit = limit
+    }
+}
tests/integration/v2/auth/app_test.go (1)

35-35: Remove unused field cdc in suite struct

The field cdc is declared but never used. Removing it will clean up the code.

Apply this diff to remove the unused field:

 type suite struct {
 	app *integration.App

-	cdc codec.Codec
 	ctx context.Context

 	authKeeper     authkeeper.AccountKeeper
 	accountsKeeper accounts.Keeper
 	bankKeeper     bankkeeper.Keeper
 }
📜 Review details

Configuration used: .coderabbit.yml
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between c60f7db and 53c884b.

📒 Files selected for processing (2)
  • tests/integration/v2/app.go (6 hunks)
  • tests/integration/v2/auth/app_test.go (1 hunks)
🧰 Additional context used
📓 Path-based instructions (2)
tests/integration/v2/app.go (2)

Pattern **/*.go: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.


Pattern tests/**/*: "Assess the integration and e2e test code assessing sufficient code coverage for the changes associated in the pull request"

tests/integration/v2/auth/app_test.go (3)

Pattern **/*.go: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.


Pattern tests/**/*: "Assess the integration and e2e test code assessing sufficient code coverage for the changes associated in the pull request"


Pattern **/*_test.go: "Assess the unit test code assessing sufficient code coverage for the changes associated in the pull request"

🪛 golangci-lint (1.62.2)
tests/integration/v2/app.go

433-433: test helper function should start from t.Helper()

(thelper)

Comment on lines +430 to +453
// RunMsg runs the handler for a transaction message.
// It required the context to have the integration context.
// a new state is committed if the option WithAutomaticCommit is set in options.
func (app *App) RunMsg(t *testing.T, ctx context.Context, handler handler, option ...Option) (resp transaction.Msg, err error) {
// set options
cfg := &RunMsgConfig{}
for _, opt := range option {
opt(cfg)
}

// need to have integration context
integrationCtx, ok := ctx.Value(contextKey).(*integrationContext)
require.True(t, ok)

resp, err = handler(ctx)

if cfg.Commit {
app.lastHeight++
_, err := app.Commit(integrationCtx.state)
require.NoError(t, err)
}

return resp, err
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Improve error handling and test helper implementation.

  1. Add t.Helper() as this is a test helper function
  2. Replace require assertions with proper error handling
  3. Add input validation
 func (app *App) RunMsg(t *testing.T, ctx context.Context, handler handler, option ...Option) (resp transaction.Msg, err error) {
+    t.Helper()
+
+    if handler == nil {
+        return nil, fmt.Errorf("handler cannot be nil")
+    }
+
     // set options
     cfg := &RunMsgConfig{}
     for _, opt := range option {
         opt(cfg)
     }

     // need to have integration context
     integrationCtx, ok := ctx.Value(contextKey).(*integrationContext)
-    require.True(t, ok)
+    if !ok {
+        return nil, fmt.Errorf("context must contain integration context")
+    }

     resp, err = handler(ctx)
+    if err != nil {
+        return nil, fmt.Errorf("handler execution failed: %w", err)
+    }

     if cfg.Commit {
         app.lastHeight++
         _, err := app.Commit(integrationCtx.state)
-        require.NoError(t, err)
+        if err != nil {
+            return nil, fmt.Errorf("failed to commit state: %w", err)
+        }
     }

-    return resp, err
+    return resp, nil
 }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
// RunMsg runs the handler for a transaction message.
// It required the context to have the integration context.
// a new state is committed if the option WithAutomaticCommit is set in options.
func (app *App) RunMsg(t *testing.T, ctx context.Context, handler handler, option ...Option) (resp transaction.Msg, err error) {
// set options
cfg := &RunMsgConfig{}
for _, opt := range option {
opt(cfg)
}
// need to have integration context
integrationCtx, ok := ctx.Value(contextKey).(*integrationContext)
require.True(t, ok)
resp, err = handler(ctx)
if cfg.Commit {
app.lastHeight++
_, err := app.Commit(integrationCtx.state)
require.NoError(t, err)
}
return resp, err
}
// RunMsg runs the handler for a transaction message.
// It required the context to have the integration context.
// a new state is committed if the option WithAutomaticCommit is set in options.
func (app *App) RunMsg(t *testing.T, ctx context.Context, handler handler, option ...Option) (resp transaction.Msg, err error) {
t.Helper()
if handler == nil {
return nil, fmt.Errorf("handler cannot be nil")
}
// set options
cfg := &RunMsgConfig{}
for _, opt := range option {
opt(cfg)
}
// need to have integration context
integrationCtx, ok := ctx.Value(contextKey).(*integrationContext)
if !ok {
return nil, fmt.Errorf("context must contain integration context")
}
resp, err = handler(ctx)
if err != nil {
return nil, fmt.Errorf("handler execution failed: %w", err)
}
if cfg.Commit {
app.lastHeight++
_, err := app.Commit(integrationCtx.state)
if err != nil {
return nil, fmt.Errorf("failed to commit state: %w", err)
}
}
return resp, nil
}
🧰 Tools
🪛 golangci-lint (1.62.2)

433-433: test helper function should start from t.Helper()

(thelper)

Comment on lines +43 to +46
func (s suite) mustAddr(address []byte) string {
str, _ := s.authKeeper.AddressCodec().BytesToString(address)
return str
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Use pointer receiver for mustAddr method for consistency

The mustAddr method uses a value receiver, whereas other methods in the suite struct use pointer receivers. For consistency and to avoid unintended copies, consider changing it to a pointer receiver.

Apply this diff to change the receiver:

-func (s suite) mustAddr(address []byte) string {
+func (s *suite) mustAddr(address []byte) string {
 	str, _ := s.authKeeper.AddressCodec().BytesToString(address)
 	return str
 }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
func (s suite) mustAddr(address []byte) string {
str, _ := s.authKeeper.AddressCodec().BytesToString(address)
return str
}
func (s *suite) mustAddr(address []byte) string {
str, _ := s.authKeeper.AddressCodec().BytesToString(address)
return str
}

@sontrinh16
Copy link
Member Author

One Q, are we sure those integration tests run on CI? I cannot find the job in v2-test.yml so I do not think they run.

Could you please add it? (copying test-integration from test.yml to v2-test.yml and renaming it test-integration-v2 and changing what's necessary) I'll mark it as required afterwards

i think it run with the current integration test job
image

@julienrbrt
Copy link
Member

One Q, are we sure those integration tests run on CI? I cannot find the job in v2-test.yml so I do not think they run.
Could you please add it? (copying test-integration from test.yml to v2-test.yml and renaming it test-integration-v2 and changing what's necessary) I'll mark it as required afterwards

i think it run with the current integration test job image

ah okay, missed that 👍🏾

Copy link
Contributor

@akhilkumarpilli akhilkumarpilli left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ACK

@julienrbrt julienrbrt added this pull request to the merge queue Nov 27, 2024
Merged via the queue into main with commit 5048f26 Nov 27, 2024
71 of 72 checks passed
@julienrbrt julienrbrt deleted the son/port_auth_integration_tests_to_server_v2 branch November 27, 2024 11:30
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.

8 participants