Skip to content

Add mock_http fixture; migrate 9 intg to mock_http#22710

Merged
mwdd146980 merged 20 commits intomwdd146980/httpx-migration-basefrom
mwdd146980/phase1b-test-decoupling
Feb 28, 2026
Merged

Add mock_http fixture; migrate 9 intg to mock_http#22710
mwdd146980 merged 20 commits intomwdd146980/httpx-migration-basefrom
mwdd146980/phase1b-test-decoupling

Conversation

@mwdd146980
Copy link
Copy Markdown
Contributor

@mwdd146980 mwdd146980 commented Feb 23, 2026

Test Decoupling Step 1 (plan). Stacked on #22676.

Adds the mock_http pytest fixture and migrates 9 integration test suites to library-agnostic HTTP mocking. The OM V2 scraper is also refactored to reuse check.http instead of constructing its own RequestsWrapper, which uncovered two bugs fixed here:

  • openmetrics: Accept header was being mutated into the shared check.http.options dict; fixed by injecting it via extra_headers per-request
  • vault: auth headers (X-Vault-Token, X-Vault-Request) needed to be reapplied to check.http in configure_scrapers after the scraper started sharing it
File Change
checks/base.py AgentCheck.http return type: RequestsWrapperHTTPClientProtocol
checks/openmetrics/v2/scraper/base_scraper.py Scraper reuses check.http; Accept header injected via extra_headers
utils/http_testing.py MockHTTPResponse.json_data: type widened to Any
plugin/pytest.py mock_http fixture added

Integration tests migrated: falco, strimzi, couchbase, ray, tekton, kubevirt_api, kubevirt_controller, kubevirt_handler, bentoml

Review checklist (to be filled by reviewers)

  • Feature or bugfix MUST have appropriate tests (unit, integration, e2e)
  • Add the qa/skip-qa label if the PR doesn't need to be tested during QA.
  • If you need to backport this PR to another branch, you can add the backport/<branch-name> label to the PR and it will automatically open a backport PR once this one is merged

Copy link
Copy Markdown
Contributor Author

mwdd146980 commented Feb 23, 2026

Warning

This pull request is not mergeable via GitHub because a downstack PR is open. Once all requirements are satisfied, merge this PR as a stack on Graphite.
Learn more

This stack of pull requests is managed by Graphite. Learn more about stacking.

@codecov
Copy link
Copy Markdown

codecov Bot commented Feb 23, 2026

Codecov Report

❌ Patch coverage is 98.97959% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 90.53%. Comparing base (6f90874) to head (ea5790d).
⚠️ Report is 1 commits behind head on mwdd146980/httpx-migration-base.

Additional details and impacted files
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@datadog-datadog-prod-us1

This comment has been minimized.

mwdd146980 and others added 18 commits February 27, 2026 00:46
Patches get/post/put/delete/head/patch on RequestsWrapper so all three
HTTP paths are intercepted: AgentCheck.http, OpenMetrics V2 scraper,
and kube* health check handlers. Real RequestsWrapper instances are
still created, so check.http.options remains accessible.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Replace direct mock.patch('requests.Session.get') with the library-agnostic
mock_http fixture and MockHTTPResponse from http_testing.py.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
…xture

Replace direct mocker.patch('requests.Session.get') with the library-agnostic
mock_http fixture and MockHTTPResponse from http_testing.py.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
…fixture

Replace direct mocker.patch('requests.Session.get') with the library-agnostic
mock_http fixture and MockHTTPResponse from http_testing.py.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
The fixture mocks the HTTP client (RequestsWrapper), not responses.
mock_http_client is more precise and avoids confusion with the existing
mock_http_response fixture from datadog_checks.dev.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
… fixture

Replace direct mocker.patch('requests.Session.get') with the library-agnostic
mock_http_client fixture and MockHTTPResponse from http_testing.py.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
…ent fixture

Replace direct mocker.patch('requests.Session.get') with the library-agnostic
mock_http_client fixture and MockHTTPResponse from http_testing.py.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Use direct import with noqa: F401 to match the existing codebase pattern.
No other integration uses pytest_plugins for fixture registration.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
The fixture name mock_http_client was added in the previous commit, but the
team prefers the shorter mock_http name. Revert all usages across integrations
and the fixture definition in http_testing.py.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
…yMock on AgentCheck.http

- mock_http now patches AgentCheck.http via PropertyMock with a create_autospec(HTTPClientProtocol)
  client, constraining the mock to the protocol interface and enforcing call signatures
- AgentCheck.http return type updated from RequestsWrapper to HTTPClientProtocol
- OM V2 scraper reuses check.http directly instead of constructing its own RequestsWrapper;
  options access guarded with hasattr for mock compatibility

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
…mock_http fixture

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
base_scraper.py shares check.http via `self.http = check.http`. The
previous code mutated `self.http.options['headers']['Accept']` which
bled through to check.http since they are the same object, breaking
tests that assert check.http.options['headers']['Accept'] == '*/*'.

Fix: store the accept header as self._accept_header and inject it
per-request via extra_headers in send_request. Using extra_headers
(not headers=) is required because RequestsWrapper._request uses
ChainMap — passing headers= directly would shadow the entire session
headers dict rather than merging a single key.

Update test assertions in three files to check scraper._accept_header
instead of scraper.http.options['headers']['Accept'].

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
After self.http = check.http in the OM V2 scraper, patching
BentomlCheck.http at the class level intercepted scraper HTTP calls too,
causing AttributeError on the minimal mock response. Replace both
patch('BentomlCheck.http') blocks with URL-based dispatch on the
already-patched requests.Session.get mock.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Comment thread bentoml/tests/test_unit.py Outdated
aggregator.assert_metric_has_tag('bentoml.service.request.count', 'bentoml_endpoint:/summarize')
aggregator.assert_service_check('bentoml.openmetrics.health', ServiceCheck.OK)
aggregator.assert_all_metrics_covered()
assert get_mock.call_count == 3 # 1 metrics scrape + 2 health endpoints (/livez, /readyz)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
assert get_mock.call_count == 3 # 1 metrics scrape + 2 health endpoints (/livez, /readyz)

Asserting this would tie us to the implementation.

IMO: What our users see are metrics and health checks, those are the things we should ensure our code is doing, testing the code itself will only require us to update the tests for every change in the code, even if it doesn't change any output or observed behavior. This is similar to the black-box testing concept.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Applied suggestion.

mwdd146980 and others added 2 commits February 27, 2026 15:17
Asserting on the number of HTTP calls ties the test to implementation
details rather than observable behavior (metrics, service checks).

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment