Skip to content

test: add unit tests for AlertmanagerClient (#876)#1194

Merged
muddlebee merged 1 commit intoTracer-Cloud:mainfrom
turancannb02:issue/876-alertmanager-client-tests
May 2, 2026
Merged

test: add unit tests for AlertmanagerClient (#876)#1194
muddlebee merged 1 commit intoTracer-Cloud:mainfrom
turancannb02:issue/876-alertmanager-client-tests

Conversation

@turancannb02
Copy link
Copy Markdown
Contributor

@turancannb02 turancannb02 commented May 2, 2026

Fixes #876

Describe the changes you have made in this PR -

Added 37 direct unit tests for the AlertmanagerClient class following the OpsGenie/Vercel test patterns. The tests use a _FakeResponse helper class and monkeypatch to mock httpx.Client, ensuring no real HTTP calls are made.

Coverage: 92% of app/services/alertmanager/client.py

Demo/Screenshot for feature changes and bug fixes -

$ python -m pytest tests/services/test_alertmanager_client.py -v                                                                             ─╯
============================================================= test session starts ==============================================================
platform darwin -- Python 3.14.3, pytest-9.0.3, pluggy-1.6.0 -- /Users/turanbuyukkamaci/Developer/ai-ml-research/opensre/.venv/bin/python
cachedir: .pytest_cache
rootdir: /Users/turanbuyukkamaci/Developer/ai-ml-research/opensre
configfile: pytest.ini
plugins: cov-7.1.0, xdist-3.8.0, asyncio-1.3.0, langsmith-0.7.36, anyio-4.13.0
asyncio: mode=Mode.STRICT, debug=False, asyncio_default_fixture_loop_scope=None, asyncio_default_test_loop_scope=function
collected 37 items                                                                                                                             

tests/services/test_alertmanager_client.py::test_bearer_token_auth_includes_header PASSED                                                [  2%]
tests/services/test_alertmanager_client.py::test_basic_auth_config_set PASSED                                                            [  5%]
tests/services/test_alertmanager_client.py::test_basic_auth_headers_do_not_include_bearer PASSED                                         [  8%]
tests/services/test_alertmanager_client.py::test_is_configured_with_bearer_token PASSED                                                  [ 10%]
tests/services/test_alertmanager_client.py::test_is_configured_with_basic_auth PASSED                                                    [ 13%]
tests/services/test_alertmanager_client.py::test_is_configured_without_credentials PASSED                                                [ 16%]
tests/services/test_alertmanager_client.py::test_is_configured_false_without_base_url PASSED                                             [ 18%]
tests/services/test_alertmanager_client.py::test_dual_auth_rejected PASSED                                                               [ 21%]
tests/services/test_alertmanager_client.py::test_get_client_forwards_auth_config_to_httpx PASSED                                         [ 24%]
tests/services/test_alertmanager_client.py::test_get_status_success PASSED                                                               [ 27%]
tests/services/test_alertmanager_client.py::test_get_status_http_error PASSED                                                            [ 29%]
tests/services/test_alertmanager_client.py::test_get_status_network_error PASSED                                                         [ 32%]
tests/services/test_alertmanager_client.py::test_get_status_calls_correct_endpoint PASSED                                                [ 35%]
tests/services/test_alertmanager_client.py::test_list_alerts_success PASSED                                                              [ 37%]
tests/services/test_alertmanager_client.py::test_list_alerts_with_filter_params PASSED                                                   [ 40%]
tests/services/test_alertmanager_client.py::test_list_alerts_http_error PASSED                                                           [ 43%]
tests/services/test_alertmanager_client.py::test_list_alerts_empty_response PASSED                                                       [ 45%]
tests/services/test_alertmanager_client.py::test_list_alerts_unexpected_format PASSED                                                    [ 48%]
tests/services/test_alertmanager_client.py::test_list_alerts_limit_respected PASSED                                                      [ 51%]
tests/services/test_alertmanager_client.py::test_list_silences_success PASSED                                                            [ 54%]
tests/services/test_alertmanager_client.py::test_list_silences_http_error PASSED                                                         [ 56%]
tests/services/test_alertmanager_client.py::test_list_silences_empty_response PASSED                                                     [ 59%]
tests/services/test_alertmanager_client.py::test_list_silences_unexpected_format PASSED                                                  [ 62%]
tests/services/test_alertmanager_client.py::test_list_silences_limit_respected PASSED                                                    [ 64%]
tests/services/test_alertmanager_client.py::test_close_releases_http_client PASSED                                                       [ 67%]
tests/services/test_alertmanager_client.py::test_close_is_idempotent PASSED                                                              [ 70%]
tests/services/test_alertmanager_client.py::test_context_manager_closes_on_exit PASSED                                                   [ 72%]
tests/services/test_alertmanager_client.py::test_context_manager_closes_on_exception PASSED                                              [ 75%]
tests/services/test_alertmanager_client.py::test_context_manager_returns_self PASSED                                                     [ 78%]
tests/services/test_alertmanager_client.py::test_make_client_with_bearer_token PASSED                                                    [ 81%]
tests/services/test_alertmanager_client.py::test_make_client_with_basic_auth PASSED                                                      [ 83%]
tests/services/test_alertmanager_client.py::test_make_client_returns_none_when_no_url PASSED                                             [ 86%]
tests/services/test_alertmanager_client.py::test_make_client_normalizes_url PASSED                                                       [ 89%]
tests/services/test_alertmanager_client.py::test_make_client_strips_whitespace PASSED                                                    [ 91%]
tests/services/test_alertmanager_client.py::test_probe_access_success PASSED                                                             [ 94%]
tests/services/test_alertmanager_client.py::test_probe_access_failure PASSED                                                             [ 97%]
tests/services/test_alertmanager_client.py::test_probe_access_not_configured PASSED                                                      [100%]

============================================================= slowest 20 durations =============================================================
0.04s call     tests/services/test_alertmanager_client.py::test_get_status_success
0.01s call     tests/services/test_alertmanager_client.py::test_list_alerts_success
0.01s call     tests/services/test_alertmanager_client.py::test_list_alerts_http_error
0.01s call     tests/services/test_alertmanager_client.py::test_probe_access_failure
0.01s call     tests/services/test_alertmanager_client.py::test_list_alerts_unexpected_format
0.01s call     tests/services/test_alertmanager_client.py::test_get_status_network_error
0.01s call     tests/services/test_alertmanager_client.py::test_list_silences_http_error
0.01s call     tests/services/test_alertmanager_client.py::test_list_silences_unexpected_format
0.01s call     tests/services/test_alertmanager_client.py::test_probe_access_success
0.01s call     tests/services/test_alertmanager_client.py::test_list_silences_success
0.01s call     tests/services/test_alertmanager_client.py::test_get_status_http_error
0.01s call     tests/services/test_alertmanager_client.py::test_get_status_calls_correct_endpoint
0.01s call     tests/services/test_alertmanager_client.py::test_list_silences_empty_response
0.01s call     tests/services/test_alertmanager_client.py::test_list_alerts_empty_response
0.01s call     tests/services/test_alertmanager_client.py::test_list_alerts_limit_respected
0.01s call     tests/services/test_alertmanager_client.py::test_list_silences_limit_respected
0.01s call     tests/services/test_alertmanager_client.py::test_list_alerts_with_filter_params
0.00s call     tests/services/test_alertmanager_client.py::test_context_manager_closes_on_exception
0.00s setup    tests/services/test_alertmanager_client.py::test_bearer_token_auth_includes_header
0.00s teardown tests/services/test_alertmanager_client.py::test_get_status_network_error
============================================================== 37 passed in 0.87s ==============================================================

$ make lint && make format-check && make typecheck                                                                                          
.venv/bin/python -m ruff check app/ tests/
All checks passed!
.venv/bin/python -m ruff format --check app/ tests/
1047 files already formatted
.venv/bin/python -m mypy app/
Success: no issues found in 452 source files

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:

Added unit tests for AlertmanagerClient using the same mocking pattern as OpsGenie/Vercel client tests (_FakeResponse class with monkeypatch for httpx.Client). This ensures fast, isolated tests without real HTTP calls.

Key components:

  • _FakeResponse: Mock HTTP response class that simulates success/error responses
  • _client(): Helper factory for creating test clients
  • Config tests: Verify bearer-token and basic-auth configuration
  • get_status tests: Cover success, HTTP errors, network errors
  • list_alerts tests: Cover filters, pagination, and error cases
  • list_silences tests: Cover success, HTTP errors, empty response, unexpected response format, and limit handling
  • Context manager tests: Verify close() and with statement behavior
  • Factory function tests: Verify make_alertmanager_client() edge cases
  • Probe access tests: Verify connectivity checking

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: Please check Allow edits from maintainers if you would like us to assist in the PR.

Comment thread tests/services/test_alertmanager_client.py Fixed
@greptile-apps
Copy link
Copy Markdown
Contributor

greptile-apps Bot commented May 2, 2026

Greptile Summary

This PR adds 32 unit tests for AlertmanagerClient, covering auth configuration, get_status, list_alerts, context-manager lifecycle, the make_alertmanager_client factory, and probe_access. The tests follow the established _FakeResponse + monkeypatch pattern used by the OpsGenie and Vercel client test suites, achieve 87% coverage (above the 80% target), and all pass cleanly.

Confidence Score: 4/5

Safe to merge — only P2 findings (missing list_silences tests and a PR description count discrepancy); no logic or correctness issues found.

All 32 tests are logically correct, mock the right targets, and match the production implementation's control flow. The only gaps are a missing test suite for list_silences and a one-off error in the PR description. No P0 or P1 findings.

No files require special attention; tests/services/test_alertmanager_client.py could be extended with list_silences coverage.

Important Files Changed

Filename Overview
tests/services/test_alertmanager_client.py Adds 32 unit tests for AlertmanagerClient covering auth config, get_status, list_alerts, context-manager lifecycle, make_alertmanager_client factory, and probe_access. Test logic is correct throughout; _FakeResponse and monkeypatch mocking pattern is consistent with OpsGenie/Vercel tests. The only gap is no coverage for list_silences, and the PR description overstates the count as 33.

Flowchart

%%{init: {'theme': 'neutral'}}%%
flowchart TD
    A[test suite entry] --> B{test category}
    B --> C[Config tests\nbearer / basic-auth / is_configured / dual-auth]
    B --> D[get_status tests\nsuccess / HTTP error / network error / endpoint]
    B --> E[list_alerts tests\nsuccess / filters / HTTP error / empty / bad format / limit]
    B --> F[Context-manager tests\nclose / idempotent / with-block / exception / returns-self]
    B --> G[Factory tests\nmake_alertmanager_client\nbearer / basic-auth / no-url / normalize / strip]
    B --> H[Probe access tests\nsuccess / failure / not-configured]
    B --> I[NOT TESTED\nlist_silences]
    C --> J[AlertmanagerConfig Pydantic model]
    D --> K[AlertmanagerClient.get_status]
    E --> L[AlertmanagerClient.list_alerts]
    F --> M[AlertmanagerClient.close / __enter__ / __exit__]
    G --> N[make_alertmanager_client factory]
    H --> O[AlertmanagerClient.probe_access]
Loading

Reviews (1): Last reviewed commit: "test: add unit tests for AlertmanagerCli..." | Re-trigger Greptile

Comment thread tests/services/test_alertmanager_client.py
Comment thread tests/services/test_alertmanager_client.py
@turancannb02 turancannb02 force-pushed the issue/876-alertmanager-client-tests branch from 24e436b to d780828 Compare May 2, 2026 05:32
@muddlebee muddlebee merged commit c6d6e9c into Tracer-Cloud:main May 2, 2026
10 checks passed
@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented May 2, 2026

🧑‍💻 @turancannb02 has entered the contributor hall of fame. Merged. Done. Shipped. Go touch grass (then come back with another PR). 🌱


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

@turancannb02 turancannb02 deleted the issue/876-alertmanager-client-tests branch May 2, 2026 06:37
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 direct unit tests for app/services/alertmanager/client.py

3 participants