Skip to content

Migrate spark tests to mock_http fixture#23585

Open
mwdd146980 wants to merge 4 commits intomwdd146980/httpx-migration-basefrom
mwdd146980/step5-spark
Open

Migrate spark tests to mock_http fixture#23585
mwdd146980 wants to merge 4 commits intomwdd146980/httpx-migration-basefrom
mwdd146980/step5-spark

Conversation

@mwdd146980
Copy link
Copy Markdown
Contributor

@mwdd146980 mwdd146980 commented May 4, 2026

Motivation

Step 5.4 of the httpx migration. Removes every mock.patch('requests.Session.*') and mocker.patch('requests.Session.*') from spark/tests/test_spark.py so the test layer does not anchor to requests.

Approach

Each mock.patch('requests.Session.get', helper) becomes mock_http.get.side_effect = helper. One site uses return_value instead. The seven mock helpers drop their leading session arg since the fixture no longer passes one through, which also lets the proxy helper drop its *args[1:] shim. Connection-failure tests raise HTTPConnectionError from datadog_checks.base.utils.http_exceptions instead of requests.ConnectionError. Spark production already catches both at spark.py:687-706, so behavior is preserved.

test_auth_yarn previously ran the full check under a custom helper that asserted auth in kwargs and verified service checks on the auth path. The auth-kwarg observation lived in the wrapper layer that mock_http bypasses. Coverage is split across two tests now. test_auth_yarn_config constructs the check without the fixture and asserts c.http.options['auth'] propagates from instance config. test_auth_yarn keeps the smoke run with mock_http plus service-check assertions under YARN_AUTH_CONFIG.

The split is required because mock_http's PropertyMock on AgentCheck.http blocks reading the real wrapper options during the same run. Wire-level "auth applied per request" coverage is not restorable under mock_http and belongs in the wrapper's own tests in datadog_checks_base. The yarn_requests_auth_mock helper is removed.

The last from requests import drove test_do_not_crash_on_version_collection_failure's side_effect=[RequestException, []]. Now uses HTTPRequestError("test failure"). Instantiation is required because unittest.mock raises cls() and HTTPError.__init__ requires message. Production catches bare Exception there, so the exception type is illustrative.

Verification

  • ddev test -fs spark clean.
  • ddev --no-interactive test spark: 57 passed, 1 skipped. The three test_integration_* errors require dd_environment and are unchanged from baseline.
  • rg "requests" spark/tests/test_spark.py returns zero matches.

Plan

PR 5.4 Spark Test Migration Plan

🤖 Generated with Claude Code

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@mwdd146980 mwdd146980 self-assigned this May 4, 2026
@codecov
Copy link
Copy Markdown

codecov Bot commented May 4, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 93.31%. Comparing base (faddc6c) to head (b877cb9).
⚠️ 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-official
Copy link
Copy Markdown
Contributor

datadog-official Bot commented May 4, 2026

Tests

🎉 All green!

❄️ No new flaky tests detected
🧪 All tests passed

🎯 Code Coverage (details)
Patch Coverage: 100.00%
Overall Coverage: 93.32% (+6.04%)

This comment will be updated automatically if new data arrives.
🔗 Commit SHA: b877cb9 | Docs | Datadog PR Page | Give us feedback!

`test_do_not_crash_on_version_collection_failure` simulates a network
failure inside `_collect_version`. Production catches bare Exception,
so the choice of exception type is cosmetic. Swap RequestException for
HTTPRequestError to drop the last `requests` import from the test file.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@mwdd146980
Copy link
Copy Markdown
Contributor Author

@codex review

Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 2e690a234e

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread spark/tests/test_spark.py Outdated
`unittest.mock` treats an exception class in a side_effect iterable as
`raise cls`, which expands to `cls()`. `HTTPError.__init__` requires a
`message` arg, so the bare class form raised TypeError instead of
HTTPRequestError. Test still passed via the bare `except Exception` in
`_collect_version`, but exercised the wrong path.

Caught by codex review on PR 23585.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@mwdd146980 mwdd146980 marked this pull request as ready for review May 5, 2026 20:33
@mwdd146980 mwdd146980 requested a review from a team as a code owner May 5, 2026 20:33
Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 5a327b159b

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread spark/tests/test_spark.py Outdated
Comment on lines +735 to +737
def test_auth_yarn():
c = SparkCheck('spark', {}, [YARN_AUTH_CONFIG])
assert c.http.options['auth'] == (TEST_USERNAME, TEST_PASSWORD)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Exercise auth-enabled check run in test_auth_yarn

This change turns test_auth_yarn into a constructor-only assertion (c.http.options['auth']) and no longer runs dd_run_check, so it no longer verifies the auth code path used during actual HTTP collection. A regression where Spark check requests stop applying auth at request time would now pass this test, whereas the previous version asserted auth on the request path and validated successful service checks under auth config.

Useful? React with 👍 / 👎.

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.

Restored the dd_run_check smoke run in b877cb9. Split into two tests because mock_http's PropertyMock on AgentCheck.http blocks reading wrapper options during the same run: test_auth_yarn_config asserts config propagates to c.http.options['auth'], test_auth_yarn asserts the check runs cleanly under auth config and produces OK service checks.

Wire-level "auth applied at request time" coverage is not restorable here. mock_http replaces the layer that applies auth on each request, so the wrapper-forwarding behavior cannot be observed under this fixture. That coverage belongs in datadog_checks_base where the wrapper itself is tested.

Adds back the dd_run_check + service-check assertions under YARN_AUTH_CONFIG
that the migration dropped. Splits into two tests because mock_http's
PropertyMock on AgentCheck.http blocks reading the real wrapper options
during the same run. test_auth_yarn_config asserts config propagation;
test_auth_yarn asserts the check runs cleanly under auth config.

Wire-level auth verification is impossible with mock_http (the layer that
applies auth on each request is exactly what mock_http replaces); that
coverage belongs in the wrapper's own test suite in datadog_checks_base.

Caught by codex review on PR 23585.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@dd-octo-sts
Copy link
Copy Markdown
Contributor

dd-octo-sts Bot commented May 5, 2026

Validation Report

All 20 validations passed.

Show details
Validation Description Status
agent-reqs Verify check versions match the Agent requirements file
ci Validate CI configuration and Codecov settings
codeowners Validate every integration has a CODEOWNERS entry
config Validate default configuration files against spec.yaml
dep Verify dependency pins are consistent and Agent-compatible
http Validate integrations use the HTTP wrapper correctly
imports Validate check imports do not use deprecated modules
integration-style Validate check code style conventions
jmx-metrics Validate JMX metrics definition files and config
labeler Validate PR labeler config matches integration directories
legacy-signature Validate no integration uses the legacy Agent check signature
license-headers Validate Python files have proper license headers
licenses Validate third-party license attribution list
metadata Validate metadata.csv metric definitions
models Validate configuration data models match spec.yaml
openmetrics Validate OpenMetrics integrations disable the metric limit
package Validate Python package metadata and naming
readmes Validate README files have required sections
saved-views Validate saved view JSON file structure and fields
version Validate version consistency between package and changelog

View full run

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant