-
Notifications
You must be signed in to change notification settings - Fork 26
fix: reconcile state.destination on skip-without-write outcomes #705
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
michael-richey
merged 4 commits into
main
from
michael.richey/skip-reconcile-destination-state
Sep 23, 2026
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
e4751ff
fix: reconcile state.destination on skip-without-write outcomes
michael-richey 95b2af1
fix: use 'key is not None' gate for reconcile consistency
michael-richey 89d45bb
fix: reconcile pre-hook skips; pre-format reconcile debug log
michael-richey 4bfa586
test: remove unused r_class mock setup from handler accounting test
michael-richey File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| # Unless explicitly stated otherwise all files in this repository are licensed | ||
| # under the 3-clause BSD style license (see LICENSE). | ||
| # This product includes software developed at Datadog (https://www.datadoghq.com/). | ||
| # Copyright 2019 Datadog, Inc. | ||
|
|
||
| """logs_indexes: regression guard + framework safety-net proof. | ||
|
|
||
| logs_indexes' exists-path is already correct (write state.destination, delegate to | ||
| update_resource PUT). The framework is a pure no-op for it; the safety-net test | ||
| proves the framework WOULD reconcile logs_indexes if its exists-path regressed. | ||
| logs_indexes keys _existing_resources_map by name, and import_resource returns | ||
| resource["name"] as the source id, so source _id == name == map key. | ||
|
|
||
| All identifiers are obviously synthetic (``index-test``). | ||
| """ | ||
|
|
||
| import asyncio | ||
| from collections import defaultdict | ||
| from unittest.mock import AsyncMock, MagicMock | ||
|
|
||
| import pytest | ||
|
|
||
| from datadog_sync.model.logs_indexes import LogsIndexes | ||
| from datadog_sync.utils.resource_utils import SkipResource | ||
|
|
||
|
|
||
| def _index(name): | ||
| return { | ||
| "name": name, | ||
| "daily_limit": None, | ||
| } | ||
|
|
||
|
|
||
| def _make_logs_indexes(existing_map=None, destination_client=None): | ||
| config = MagicMock() | ||
| config.destination_client = destination_client or MagicMock() | ||
| config.state = MagicMock() | ||
| config.state.source = defaultdict(dict) | ||
| config.state.destination = defaultdict(dict) | ||
| config.logger = MagicMock() | ||
| li = LogsIndexes(config=config) | ||
| li._existing_resources_map = existing_map or {} | ||
| return li, config | ||
|
|
||
|
|
||
| class TestLogsIndexesSkipReconcile: | ||
| def test_existing_resource_create_writes_state_destination(self): | ||
| # source _id == name == map key. | ||
| _id = "index-test" | ||
| dest = _index("index-test") | ||
| put_resp = {"name": "index-test", "daily_limit": 42} | ||
| destination_client = MagicMock() | ||
| destination_client.put = AsyncMock(return_value=put_resp) | ||
| li, config = _make_logs_indexes( | ||
| existing_map={"index-test": dest}, | ||
| destination_client=destination_client, | ||
| ) | ||
| resource = _index("index-test") | ||
|
|
||
| asyncio.run(li._create_resource(_id, resource)) | ||
|
|
||
| destination_client.put.assert_called_once() | ||
| # update_resource does state.destination[_id].update(resp); wrapper rewrites. | ||
| assert config.state.destination["logs_indexes"][_id]["daily_limit"] == 42 | ||
|
|
||
| def test_framework_reconciles_if_create_raised_skip(self): | ||
| _id = "index-test" | ||
| dest = _index("index-test") | ||
| li, config = _make_logs_indexes(existing_map={"index-test": dest}) | ||
| li.create_resource = AsyncMock(side_effect=SkipResource(_id, "logs_indexes", "exists")) | ||
| resource = _index("index-test") | ||
|
|
||
| with pytest.raises(SkipResource): | ||
| asyncio.run(li._create_resource(_id, resource)) | ||
|
|
||
| assert config.state.destination["logs_indexes"][_id] == dest | ||
|
|
||
| def test_create_non_existing_still_posts(self): | ||
| _id = "index-test" | ||
| posted = _index("index-test") | ||
| destination_client = MagicMock() | ||
| destination_client.post = AsyncMock(return_value=posted) | ||
| li, config = _make_logs_indexes( | ||
| existing_map={}, | ||
| destination_client=destination_client, | ||
| ) | ||
| resource = _index("index-test") | ||
|
|
||
| out_id, out_r = asyncio.run(li.create_resource(_id, resource)) | ||
|
|
||
| destination_client.post.assert_called_once() | ||
| assert out_id == _id | ||
| assert out_r == posted |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| # Unless explicitly stated otherwise all files in this repository are licensed | ||
| # under the 3-clause BSD style license (see LICENSE). | ||
| # This product includes software developed at Datadog (https://www.datadoghq.com/). | ||
| # Copyright 2019 Datadog, Inc. | ||
|
|
||
| """logs_metrics: regression guard + framework safety-net proof. | ||
|
|
||
| logs_metrics' exists-path is already correct (write state.destination, delegate to | ||
| update_resource PATCH). The framework is a pure no-op for it; the safety-net test | ||
| proves the framework WOULD reconcile logs_metrics if its exists-path regressed. | ||
|
|
||
| All identifiers are obviously synthetic (``metric-src``, ``metric-dst``). | ||
| """ | ||
|
|
||
| import asyncio | ||
| from collections import defaultdict | ||
| from unittest.mock import AsyncMock, MagicMock | ||
|
|
||
| import pytest | ||
|
|
||
| from datadog_sync.model.logs_metrics import LogsMetrics | ||
| from datadog_sync.utils.resource_utils import SkipResource | ||
|
|
||
|
|
||
| def _metric(metric_id, name=None): | ||
| return { | ||
| "id": metric_id, | ||
| "type": "logs_metrics", | ||
| "attributes": {"name": name or metric_id}, | ||
| } | ||
|
|
||
|
|
||
| def _make_logs_metrics(existing_map=None, destination_client=None): | ||
| config = MagicMock() | ||
| config.destination_client = destination_client or MagicMock() | ||
| config.state = MagicMock() | ||
| config.state.source = defaultdict(dict) | ||
| config.state.destination = defaultdict(dict) | ||
| config.logger = MagicMock() | ||
| lm = LogsMetrics(config=config) | ||
| lm._existing_resources_map = existing_map or {} | ||
| return lm, config | ||
|
|
||
|
|
||
| class TestLogsMetricsSkipReconcile: | ||
| def test_existing_resource_create_writes_state_destination(self): | ||
| # logs_metrics ids are stable across orgs, so source _id == map key. | ||
| _id = "metric-test" | ||
| dest = _metric("metric-test") | ||
| patched = _metric("metric-test") | ||
| patched["attributes"]["marker"] = "patched" | ||
| destination_client = MagicMock() | ||
| destination_client.patch = AsyncMock(return_value={"data": patched}) | ||
| lm, config = _make_logs_metrics( | ||
| existing_map={"metric-test": dest}, | ||
| destination_client=destination_client, | ||
| ) | ||
| resource = _metric("metric-test") | ||
|
|
||
| asyncio.run(lm._create_resource(_id, resource)) | ||
|
|
||
| destination_client.patch.assert_called_once() | ||
| assert config.state.destination["logs_metrics"][_id] == patched | ||
|
|
||
| def test_framework_reconciles_if_create_raised_skip(self): | ||
| _id = "metric-test" | ||
| dest = _metric("metric-test") | ||
| lm, config = _make_logs_metrics(existing_map={"metric-test": dest}) | ||
| lm.create_resource = AsyncMock(side_effect=SkipResource(_id, "logs_metrics", "exists")) | ||
| resource = _metric("metric-test") | ||
|
|
||
| with pytest.raises(SkipResource): | ||
| asyncio.run(lm._create_resource(_id, resource)) | ||
|
|
||
| assert config.state.destination["logs_metrics"][_id] == dest | ||
|
|
||
| def test_create_non_existing_still_posts(self): | ||
| _id = "metric-test" | ||
| posted = _metric("metric-test") | ||
| destination_client = MagicMock() | ||
| destination_client.post = AsyncMock(return_value={"data": posted}) | ||
| lm, config = _make_logs_metrics( | ||
| existing_map={}, | ||
| destination_client=destination_client, | ||
| ) | ||
| resource = _metric("metric-test") | ||
|
|
||
| out_id, out_r = asyncio.run(lm.create_resource(_id, resource)) | ||
|
|
||
| destination_client.post.assert_called_once() | ||
| assert out_id == _id | ||
| assert out_r == posted |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.