Skip to content

test: add failure path coverage for IMDS helpers#1154

Merged
muddlebee merged 1 commit intoTracer-Cloud:mainfrom
TejasS1233:test/imds-failure-paths
May 1, 2026
Merged

test: add failure path coverage for IMDS helpers#1154
muddlebee merged 1 commit intoTracer-Cloud:mainfrom
TejasS1233:test/imds-failure-paths

Conversation

@TejasS1233
Copy link
Copy Markdown
Contributor

Summary

  • Add tests for _imds_token() returning None on URLError
  • Add tests for _imds_get() returning None on TimeoutError and OSError
  • All tests fully mocked, no network calls

Closes #1126

Copilot AI review requested due to automatic review settings April 30, 2026 17:31
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Adds isolated unit tests to ensure the EC2 Instance Metadata Service (IMDS) helper functions fail quietly (return None) when urlopen() raises common network-related exceptions, aligning with the “no network calls in tests” requirement.

Changes:

  • Import _imds_token and _imds_get into tests/app/remote/test_server.py.
  • Add mocked tests asserting _imds_token() returns None when urlopen() raises URLError, TimeoutError, or OSError.
  • Add mocked tests asserting _imds_get() returns None when urlopen() raises URLError, TimeoutError, or OSError.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@greptile-apps
Copy link
Copy Markdown
Contributor

greptile-apps Bot commented Apr 30, 2026

Greptile Summary

This PR adds six unit tests covering the failure paths of _imds_token() and _imds_get() — the AWS EC2 Instance Metadata Service helpers — verifying that URLError, TimeoutError, and OSError all result in a None return. All tests are fully mocked via monkeypatch, the patching target (urllib.request.urlopen) is correct for how server.py accesses it, and the assertions match the implementation's exception handling.

Confidence Score: 5/5

Safe to merge — test-only change with no impact on production code.

All findings are P2 style issues (import placement and missing type hints on inner helpers). The tests themselves are logically correct and the monkeypatching strategy is sound.

No files require special attention.

Important Files Changed

Filename Overview
tests/app/remote/test_server.py Adds 6 failure-path tests for _imds_token() and _imds_get(); monkeypatching strategy is correct, assertions are sound; two minor style issues (local imports and missing type hints on helpers).

Flowchart

%%{init: {'theme': 'neutral'}}%%
flowchart TD
    A[_imds_token / _imds_get called] --> B[urllib.request.urlopen]
    B -->|Success| C[return decoded string or None]
    B -->|urllib.error.URLError| D[return None]
    B -->|TimeoutError| E[return None]
    B -->|OSError| F[return None]

    subgraph Tests Added
        T1[test_imds_token_returns_none_on_url_error]
        T2[test_imds_token_returns_none_on_timeout]
        T3[test_imds_token_returns_none_on_os_error]
        T4[test_imds_get_returns_none_on_url_error]
        T5[test_imds_get_returns_none_on_timeout]
        T6[test_imds_get_returns_none_on_os_error]
    end

    T1 -.covers.-> D
    T2 -.covers.-> E
    T3 -.covers.-> F
    T4 -.covers.-> D
    T5 -.covers.-> E
    T6 -.covers.-> F
Loading

Reviews (1): Last reviewed commit: "test: add failure path coverage for IMDS..." | Re-trigger Greptile

Comment on lines +219 to +227
def test_imds_token_returns_none_on_url_error(monkeypatch: pytest.MonkeyPatch) -> None:
import urllib.error

def _raise_url_error(*args, **kwargs):
raise urllib.error.URLError("connection refused")

monkeypatch.setattr("urllib.request.urlopen", _raise_url_error)

assert _imds_token() is None
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

P2 Local imports inside test functions

urllib.error is imported inside the test body on lines 221 and 249 rather than at the module level. The codebase style guide calls for imports to live at the top of the file. Moving them up keeps the test body clean and avoids the repeated local import.

Add import urllib.error to the existing import block at the top of the file, then remove the two inline import urllib.error statements from the test bodies.

Context Used: Code style and conventions (source)

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

Comment thread tests/app/remote/test_server.py Outdated
Comment on lines +222 to +223
def _raise_url_error(*args, **kwargs):
raise urllib.error.URLError("connection refused")
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

P2 Missing type annotations on inner helpers

The stub helpers (_raise_url_error, _raise_timeout, _raise_os_error) have no type annotations on their parameters or return type. The project style guide requires type hints on all function parameters and return values (-> None here). The same applies to the equivalent helpers in all six new tests.

Suggested change
def _raise_url_error(*args, **kwargs):
raise urllib.error.URLError("connection refused")
def _raise_url_error(*args: object, **kwargs: object) -> None:
raise urllib.error.URLError("connection refused")

Context Used: Code style and conventions (source)

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

@Tracer-Cloud Tracer-Cloud deleted a comment from Ghraven May 1, 2026
@muddlebee
Copy link
Copy Markdown
Collaborator

@TejasS1233 pls address review comments and merge conflicts

@TejasS1233 TejasS1233 force-pushed the test/imds-failure-paths branch from 066ef85 to 0d07186 Compare May 1, 2026 05:41
@TejasS1233
Copy link
Copy Markdown
Contributor Author

Addressed review comments: moved urllib.error import to module level and added type annotations to inner helpers. Rebased onto latest main to resolve conflicts.
do check

@TejasS1233 TejasS1233 force-pushed the test/imds-failure-paths branch from 0d07186 to 1a0dad0 Compare May 1, 2026 05:50
@TejasS1233
Copy link
Copy Markdown
Contributor Author

Force-pushed to clean up the commit history -- the branch was behind upstream and pulled in unrelated changes. The PR now contains only the IMDS failure path tests.

@muddlebee
Copy link
Copy Markdown
Collaborator

LGTM. will wait for CI checks..

@muddlebee muddlebee merged commit 49f82b3 into Tracer-Cloud:main May 1, 2026
10 checks passed
@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented May 1, 2026

🎻 "The diff was clean, the tests did pass, the reviewer wept." That poem was about @TejasS1233's PR. 🥹


👋 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 IMDS helper failure-path tests

3 participants