Skip to content

Introduce mutation testing #2763

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

Merged
merged 1 commit into from
Jul 1, 2024

Conversation

dunxen
Copy link
Contributor

@dunxen dunxen commented Nov 30, 2023

We introduce a CI job for mutation testing of PR diffs using cargo-mutants.
Missed cases do not trigger a fail of this job yet as we just introduce it now for visibility. We may start enforcing stricter rules at a later stage.

See #188. Only partially addresses everything we want I believe.

@dunxen dunxen marked this pull request as ready for review November 30, 2023 11:50
@tnull
Copy link
Contributor

tnull commented Nov 30, 2023

Mh, given that we have a number of known flaky tests, wouldn't this trip up cargo muntants? At least their docs state:

cargo-mutants can't do anything until you have a tree where cargo test passes reliably when copied to a temporary directory.

However, I wonder if spurious test failures wouldn't just produce a bunch of false-negatives, i.e., everything it would yield as missed would still be usable information?

@TheBlueMatt
Copy link
Collaborator

Github doesn't like your CI:

Error: .github#L1
every step must define a `uses` or `run` key

Mh, given that we have a number of known flaky tests, wouldn't this trip up cargo muntants?

Probably, but we should also just fix those. I'm not sure having flaky tests should be a blocker to landing better testing.

@TheBlueMatt
Copy link
Collaborator

How do we display the results here? Is there a way to get codecov integration or just leave a comment on the pr? Or is it too slow to run in CI and just do it on a daily cron job (with issues opened on regression? Not sure how to surface information).

@codecov-commenter
Copy link

codecov-commenter commented Dec 5, 2023

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 89.79%. Comparing base (3ccf064) to head (c42699d).

❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2763      +/-   ##
==========================================
- Coverage   89.80%   89.79%   -0.01%     
==========================================
  Files         121      121              
  Lines       99314    99314              
  Branches    99314    99314              
==========================================
- Hits        89187    89179       -8     
- Misses       7522     7524       +2     
- Partials     2605     2611       +6     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@dunxen
Copy link
Contributor Author

dunxen commented Dec 5, 2023

How do we display the results here? Is there a way to get codecov integration or just leave a comment on the pr? Or is it too slow to run in CI and just do it on a daily cron job (with issues opened on regression? Not sure how to surface information).

I'll look into this and propose something here after I get some idea of run time.

@dunxen dunxen force-pushed the 2023-11-mutation-testing branch from c7ee4fd to 84887de Compare February 8, 2024 14:22
Copy link

coderabbitai bot commented Feb 8, 2024

Walkthrough

The recent update introduces a mutation testing job in the CI pipeline, enhancing the code quality by identifying potential weak spots. Additionally, it adjusts the behavior in a core component of the project by disabling a panic trigger under specific conditions, potentially modifying how the system handles certain channel operations.

Changes

File Path Change Summary
.github/workflows/build.yml Added a mutation job for PRs to check for mutants in the diff.
lightning/.../channel.rs Disabled a panic for outbound funding_created messages on inbound channels.

🐇✨
In the land of code and byte,
A rabbit hops through tests, so bright.
Mutants lurking, now in sight,
Fixes pushed with all its might.
🌟🚀


Tip

Early access features: enabled

We are currently testing the following features in early access:

  • OpenAI gpt-4o model for code reviews and chat: OpenAI claims that this model is better at understanding and generating code than the previous models. We seek your feedback over the next few weeks before making it generally available.

Note:

  • You can enable or disable early access features from the CodeRabbit UI or by updating the CodeRabbit configuration file.
  • Please join our Discord Community to provide feedback and report issues.
  • OSS projects are always opted into early access features.

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>.
    • 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 generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @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 as 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.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

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

@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.

Review Status

Actionable comments generated: 2

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between e0323ec and 84887de.
Files selected for processing (2)
  • .github/workflows/build.yml (1 hunks)
  • lightning/src/ln/channel.rs (1 hunks)

Comment on lines 220 to 257
mutation:
runs-on: ubuntu-latest
if: github.event_name == 'pull_request'
steps:
- name: Checkout source code
uses: actions/checkout@v3
with:
fetch-depth: 0
- name: Relative diff
run: |
git branch -av
git diff origin/${{ github.base_ref }}.. | tee git.diff
- run: cargo install cargo-mutants
- name: Mutants
run: |
cargo mutants --no-shuffle -j 2 -vV --in-diff git.diff
- name: Archive mutants.out
uses: actions/upload-artifact@v3
if: always()
with:
name: mutants-incremental.out
path: mutants.out
Copy link

Choose a reason for hiding this comment

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

The mutation testing job configuration looks correct but consider the following improvements:

  • Line 232: Ensure cargo-mutants is installed from a specific version to avoid potential issues with future breaking changes. Specify the version using cargo install cargo-mutants --version x.y.z.
  • Line 235: The use of --no-shuffle might not be ideal for all scenarios. If the intention is to reduce test flakiness, document this decision. Otherwise, consider removing it to allow cargo-mutants to optimize the mutation testing process.
  • Line 238: The conditional if: always() ensures that artifacts are uploaded regardless of the job's success or failure. Confirm this aligns with the intended behavior, as it might clutter the artifacts with failed mutation test outputs.

@@ -6824,6 +6824,7 @@ impl<SP: Deref> OutboundV1Channel<SP> where SP::Target: SignerProvider {
pub fn get_funding_created<L: Deref>(&mut self, funding_transaction: Transaction, funding_txo: OutPoint, is_batch_funding: bool, logger: &L)
-> Result<Option<msgs::FundingCreated>, (Self, ChannelError)> where L::Target: Logger {
if !self.context.is_outbound() {
// REMOVE ME - TRIGGER MUTANTS
Copy link

@coderabbitai coderabbitai bot Feb 8, 2024

Choose a reason for hiding this comment

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

Disabling a panic statement for mutation testing could mask potential logic errors in runtime behavior. Consider alternatives that allow mutation testing without compromising safety checks.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You might be confused, Mr. Bunny.

Copy link

Choose a reason for hiding this comment

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

@dunxen, I appreciate your perspective! My initial concern was aimed at ensuring that mutation testing, while valuable for uncovering untested paths, doesn't inadvertently bypass critical runtime safety checks. It's crucial to strike a balance where mutation testing enhances code quality without compromising the integrity of safety mechanisms. If there's a specific approach or context within cargo-mutants that addresses this balance, I'd be keen to understand more. Your insights are invaluable in this discussion.

@dunxen
Copy link
Contributor Author

dunxen commented Feb 8, 2024

Looks like 2 viable things caught in channel.rs:

 Found 4 mutants to test
 INFO Auto-set test timeout to 3m 30s
ok       Unmutated baseline in 170.9s build + 42.7s test
caught   lightning/src/ln/channel.rs:6826:3: replace OutboundV1Channel<SP>::get_funding_created -> Result<Option<msgs::FundingCreated>, (Self, ChannelError)> with Ok(None) in 28.9s build + 14.8s test
unviable lightning/src/ln/channel.rs:6826:3: replace OutboundV1Channel<SP>::get_funding_created -> Result<Option<msgs::FundingCreated>, (Self, ChannelError)> with Ok(Some(Default::default())) in 54.6s build
caught   lightning/src/ln/channel.rs:6826:3: replace OutboundV1Channel<SP>::get_funding_created -> Result<Option<msgs::FundingCreated>, (Self, ChannelError)> with Ok(None) in 11.5s build + 10.5s test
unviable lightning/src/ln/channel.rs:6826:3: replace OutboundV1Channel<SP>::get_funding_created -> Result<Option<msgs::FundingCreated>, (Self, ChannelError)> with Ok(Some(Default::default())) in 16.9s build
4 mutants tested in 4m 45s: 2 caught, 2 unviable

Took around 6 minutes from a cold start for this PR checking channel.rs.

@TheBlueMatt
Copy link
Collaborator

Oh, --in-diff is cool! How do we handle truly unreachable branches in PRs (like the one you added). If we do like unreachable!() does mutants ignore it?


mutation:
runs-on: ubuntu-latest
if: github.event_name == 'pull_request'

Choose a reason for hiding this comment

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

I think it does not need to run it for every PR. PRs that do not change any relevant code could be skipped (e.g. upgrading dependencies, test changes, etc?). Not sure how to deal with it in this project.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Since it's based on diff, it shouldn't spend much time doing anything in those cases, but I'll have a look if that's not what happens.

Choose a reason for hiding this comment

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

I do believe it would spend a lot of time in PRs like #3063, for example. It's what is expected?

- run: cargo install cargo-mutants
- name: Mutants
run: |
cargo mutants --no-shuffle -j 2 -vV --in-diff git.diff

Choose a reason for hiding this comment

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

Not sure how cargo-mutants works in depth but will the CI task fail if a % of mutants has not been killed?

@dunxen dunxen force-pushed the 2023-11-mutation-testing branch 2 times, most recently from 0d4c36b to 4f58150 Compare May 31, 2024 14:54
Copy link
Collaborator

@TheBlueMatt TheBlueMatt left a comment

Choose a reason for hiding this comment

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

I guess absent any clever ideas we should just ship this as-is and let it fail on any things that don't have full mutants coverage (we can skip requiring it anyway).

Comment on lines 245 to 248
- uses: taiki-e/install-action@v2
name: Install cargo-mutants using install-action
with:
tool: cargo-mutants
Copy link
Collaborator

Choose a reason for hiding this comment

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

Isn't this the same as just running a shell of cargo install cargo-mutants?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Same effect. This was pulled from the cargo-mutants example, I'll change to cargo install cargo-mutants to be consistent.

- name: Mutants
run: |
cargo mutants --no-shuffle -j 2 -vV --in-diff git.diff
- name: Archive mutants.out
Copy link
Collaborator

Choose a reason for hiding this comment

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

Does the CLI output for cargo mutants not suffice? What additional data does this give?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't believe we'll need this either. It'd only be useful if we had some perhaps custom app to feed it to so it could leave a PR comment like codecov. Will remove for now.

@dunxen
Copy link
Contributor Author

dunxen commented Jun 26, 2024

I guess absent any clever ideas we should just ship this as-is and let it fail on any things that don't have full mutants coverage (we can skip requiring it anyway).

Skip requiring it to pass for merge is done from the GitHub project settings, right? Sounds good. Let me clean it up.

@dunxen dunxen force-pushed the 2023-11-mutation-testing branch from a7f6e21 to 381c512 Compare June 26, 2024 10:07
@TheBlueMatt
Copy link
Collaborator

TheBlueMatt commented Jun 29, 2024

This LGTM, feel free to drop the "trigger" part of the diff, I think we should just land this and deal with if we want to make it quieter somehow later.

@dunxen
Copy link
Contributor Author

dunxen commented Jun 30, 2024

feel free to drop the "trigger" part of the diff

oh gosh thought I dropped it but probably squashed instead. anyway, it helped check if it still works after the last changes to the action!

will drop the trigger now.

We introduce a CI job for mutation testing of PR diffs using cargo-mutants.
Missed cases do not trigger a fail of this job yet as we just introduce it
now for visibility. We may start enforcing stricter rules at a later stage.
@dunxen dunxen force-pushed the 2023-11-mutation-testing branch from 381c512 to c42699d Compare June 30, 2024 15:26
Copy link
Collaborator

@TheBlueMatt TheBlueMatt left a comment

Choose a reason for hiding this comment

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

LGTM. Worst case we have a new CI job that is kinda useless/that we ignore. Best case reviewers and devs use it to decide where to focus new testing. Gonna go ahead and merge this and we can decide whether its useful after we see it a few times.

@TheBlueMatt TheBlueMatt merged commit c64b8e4 into lightningdevkit:main Jul 1, 2024
14 of 17 checks passed
@tnull tnull mentioned this pull request Jul 8, 2024
@dunxen dunxen deleted the 2023-11-mutation-testing branch January 20, 2025 08:31
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.

5 participants