Skip to content

Fix/check disk health tests#1155

Merged
VaibhavUpreti merged 5 commits intoTracer-Cloud:mainfrom
Bhavarth7:fix/check-disk-health-tests
Apr 30, 2026
Merged

Fix/check disk health tests#1155
VaibhavUpreti merged 5 commits intoTracer-Cloud:mainfrom
Bhavarth7:fix/check-disk-health-tests

Conversation

@Bhavarth7
Copy link
Copy Markdown
Contributor

Fixes #1124


Describe the changes you have made in this PR -

Added deterministic tests for the _check_disk_health() threshold logic in tests/app/remote/test_server.py.

Previously, both the passed and warn branches had no test coverage. Since the function relies on shutil.disk_usage("/"), tests would depend on the actual machine disk state, making them non-deterministic.

This PR introduces controlled tests using monkeypatching to ensure consistent behavior:

  • Passed branch: Simulated ~50% disk usage → asserts status == "passed"
  • Warn branch: Simulated ~90% disk usage → asserts status == "warn"

To avoid dependency on real system state, shutil.disk_usage is monkeypatched to return a synthetic usage object constructed using the same named tuple type as the real return value.


Demo/Screenshot for feature changes and bug fixes -

python -m pytest tests/app/remote/test_server.py -q

All tests pass consistently regardless of actual disk state.


Code Understanding and AI Usage

Did you use AI assistance (ChatGPT, Claude, Copilot, etc.) to write any part of this code?

  • No, I wrote all the code myself
  • Yes, I used AI assistance (continue below)

If you used AI assistance:

  • I have reviewed every single line of the AI-generated code
  • I can explain the purpose and logic of each function/component I added
  • I have tested edge cases and understand how the code handles them
  • I have modified the AI output to follow this project's coding standards and conventions

Explain your implementation approach:

The goal was to make the tests deterministic and independent of the host machine.

  • The main challenge was that _check_disk_health() reads real disk usage using shutil.disk_usage("/"), which makes direct testing unreliable.

  • I used monkeypatch to override shutil.disk_usage and return controlled values.

  • Instead of incorrectly trying to instantiate shutil.disk_usage.__class__, I derived the correct return type using type(shutil.disk_usage("/")) and constructed synthetic usage values.

  • Two focused tests were added to cover:

    • Below threshold (passed)
    • At/above threshold (warn)

This approach ensures:

  • No dependency on real disk state
  • Clear coverage of threshold logic
  • Minimal and isolated test changes

Checklist before requesting a review

  • I have added proper PR title and linked to the issue
  • I have performed a self-review of my code
  • I can explain the purpose of every function, class, and logic block I added
  • I understand why my changes work and have tested them thoroughly
  • I have considered potential edge cases and how my code handles them
  • If it is a core feature, I have added thorough tests
  • My code follows the project's style guidelines and conventions

Note: Maintainers are welcome to suggest improvements or edits if needed.

@greptile-apps
Copy link
Copy Markdown
Contributor

greptile-apps Bot commented Apr 30, 2026

Greptile Summary

Adds two deterministic unit tests for _check_disk_health() using monkeypatch to cover the "passed" (50% usage) and "warn" (90% usage) branches, replacing previously non-existent and non-deterministic coverage. The monkeypatching strategy is correct — patching shutil.disk_usage on the module object is properly visible to server.py's shutil.disk_usage(...) call site.

Confidence Score: 5/5

Safe to merge — all findings are P2 style suggestions that do not affect correctness or runtime behavior.

The new tests are logically correct and cover the two primary threshold branches of _check_disk_health(). Remaining feedback (duplicate comment, pre-patch filesystem call, missing "missing" branch test) are all non-blocking P2 suggestions.

No files require special attention.

Important Files Changed

Filename Overview
tests/app/remote/test_server.py Two deterministic tests added for _check_disk_health(); correct monkeypatching approach, but a duplicate section-header comment and residual real-filesystem calls (shutil.disk_usage("/") for type recovery) are present, and the "missing" status branch (total==0) is untested.

Flowchart

%%{init: {'theme': 'neutral'}}%%
flowchart TD
    A[_check_disk_health called] --> B[shutil.disk_usage '/']
    B --> C{usage.total == 0?}
    C -- yes --> D[status = 'missing']
    C -- no --> E[used_pct = used/total * 100]
    E --> F{used_pct < 85?}
    F -- yes --> G[status = 'passed']
    F -- no --> H[status = 'warn']
    G --> I[return DeepHealthCheck]
    H --> I
    D --> I
    style G fill:#2ecc71,color:#fff
    style H fill:#f39c12,color:#fff
    style D fill:#95a5a6,color:#fff
    subgraph Tests Added
        T1[test_passed_when_below_warn_threshold - 50 GiB / 100 GiB = 50% covered]
        T2[test_warn_when_at_or_above_threshold - 90 GiB / 100 GiB = 90% covered]
        T3[No test for total==0 branch]
    end
    G -.covered by.- T1
    H -.covered by.- T2
    D -.not covered.- T3
Loading

Reviews (1): Last reviewed commit: "chore: remove personal notes from featur..." | Re-trigger Greptile

Comment thread tests/app/remote/test_server.py Outdated
Comment thread tests/app/remote/test_server.py Outdated
Comment thread tests/app/remote/test_server.py
Bhavarth7 and others added 2 commits May 1, 2026 00:25
Co-authored-by: greptile-apps[bot] <165735046+greptile-apps[bot]@users.noreply.github.com>
Copy link
Copy Markdown
Member

@VaibhavUpreti VaibhavUpreti left a comment

Choose a reason for hiding this comment

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

awesome @Bhavarth7 , thanks a lot for your contribution!

@VaibhavUpreti VaibhavUpreti merged commit ee0d4fe into Tracer-Cloud:main Apr 30, 2026
6 checks passed
@github-actions
Copy link
Copy Markdown
Contributor

🎊 Achievement unlocked: PR Merged. @Bhavarth7 passed code review, survived CI, and shipped. Respect. 🤝


👋 Join us on Discord - OpenSRE : hang out, contribute, or hunt for features and issues. Everyone's welcome.

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.

Add _check_disk_health() threshold tests

2 participants