From 99933195389f6cb013e9f64d69fd303959222a68 Mon Sep 17 00:00:00 2001 From: "michael.richey" Date: Thu, 20 Mar 2025 11:03:21 -0400 Subject: [PATCH] Narrow the scope of DDR checking based on the command --- datadog_sync/utils/configuration.py | 30 +- ...st_import_verify_ddr_status_failure.frozen | 1 - ...test_import_verify_ddr_status_failure.yaml | 150 ------- ...tCli.test_verify_ddr_status_failure.frozen | 1 + ...estCli.test_verify_ddr_status_failure.yaml | 394 ++++++++++++++++++ tests/integration/test_cli.py | 14 +- 6 files changed, 423 insertions(+), 167 deletions(-) delete mode 100644 tests/integration/cassettes/test_cli/TestCli.test_import_verify_ddr_status_failure.frozen delete mode 100644 tests/integration/cassettes/test_cli/TestCli.test_import_verify_ddr_status_failure.yaml create mode 100644 tests/integration/cassettes/test_cli/TestCli.test_verify_ddr_status_failure.frozen create mode 100644 tests/integration/cassettes/test_cli/TestCli.test_verify_ddr_status_failure.yaml diff --git a/datadog_sync/utils/configuration.py b/datadog_sync/utils/configuration.py index 6c2a8b18..e1dbff07 100644 --- a/datadog_sync/utils/configuration.py +++ b/datadog_sync/utils/configuration.py @@ -82,20 +82,22 @@ async def init_async(self, cmd: Command): # Don't sync if DDR is active if self.verify_ddr_status: - try: - await _verify_ddr_status(self.destination_client) - except Exception as err: - self.logger.error( - f"The destination DDR verification failed. {err} Use the --verify-ddr-status flag to override." - ) - sys.exit(1) - try: - await _verify_ddr_status(self.source_client) - except Exception as err: - self.logger.error( - f"The source DDR verification failed. {err} Use the --verify-ddr-status flag to override." - ) - sys.exit(1) + if cmd in [Command.SYNC, Command.DIFFS, Command.MIGRATE, Command.RESET]: + try: + await _verify_ddr_status(self.destination_client) + except Exception as err: + self.logger.error( + f"The destination DDR verification failed. {err} Use the --verify-ddr-status flag to override." + ) + sys.exit(1) + if cmd in [Command.IMPORT, Command.DIFFS, Command.MIGRATE]: + try: + await _verify_ddr_status(self.source_client) + except Exception as err: + self.logger.error( + f"The source DDR verification failed. {err} Use the --verify-ddr-status flag to override." + ) + sys.exit(1) self.logger.info("DDR verified successfully") else: self.logger.warning("DDR verification skipped.") diff --git a/tests/integration/cassettes/test_cli/TestCli.test_import_verify_ddr_status_failure.frozen b/tests/integration/cassettes/test_cli/TestCli.test_import_verify_ddr_status_failure.frozen deleted file mode 100644 index c5d49dc3..00000000 --- a/tests/integration/cassettes/test_cli/TestCli.test_import_verify_ddr_status_failure.frozen +++ /dev/null @@ -1 +0,0 @@ -2025-02-19T16:47:18.749748-05:00 \ No newline at end of file diff --git a/tests/integration/cassettes/test_cli/TestCli.test_import_verify_ddr_status_failure.yaml b/tests/integration/cassettes/test_cli/TestCli.test_import_verify_ddr_status_failure.yaml deleted file mode 100644 index 851ecf63..00000000 --- a/tests/integration/cassettes/test_cli/TestCli.test_import_verify_ddr_status_failure.yaml +++ /dev/null @@ -1,150 +0,0 @@ -interactions: -- request: - body: null - headers: - Content-Type: - - application/json - method: GET - uri: https://us5.datadoghq.com/api/v2/hamr - response: - body: - string: '{"data": {"id": "b9e754c7-857e-11ef-afbd-c64ba5fcf2d1", "type": "hamr_org_connections", - "attributes": {"HamrStatus": 1, "IsPrimary": false, "ModifiedAt": "2024-10-08 - 15:06:20.300120", "ModifiedBy": "charlie.zhang", "TargetOrgDatacenter": "eu1.prod.dog", - "TargetOrgName": "DDR Internal Testing eu1", "TargetOrgUuid": "30187db5-8582-11ef-969b-8248c7cda362"}}}' - headers: - Content-Type: - - application/vnd.api+json - status: - code: 200 - message: OK -- request: - body: null - headers: - Content-Type: - - application/json - method: GET - uri: https://api.datadoghq.eu/api/v2/hamr - response: - body: - string: '{"status": "error", "code": 403, "errors": ["Forbidden"], "statuspage": - "http://status.datadoghq.eu", "twitter": "http://twitter.com/datadogops", - "email": "support@datadoghq.com"}' - headers: - Content-Type: - - application/json - status: - code: 403 - message: Forbidden -- request: - body: null - headers: - Content-Type: - - application/json - method: GET - uri: https://us5.datadoghq.com/api/v2/hamr - response: - body: - string: '{"data": {"id": "b9e754c7-857e-11ef-afbd-c64ba5fcf2d1", "type": "hamr_org_connections", - "attributes": {"HamrStatus": 1, "IsPrimary": false, "ModifiedAt": "2024-10-08 - 15:06:20.300120", "ModifiedBy": "charlie.zhang", "TargetOrgDatacenter": "eu1.prod.dog", - "TargetOrgName": "DDR Internal Testing eu1", "TargetOrgUuid": "30187db5-8582-11ef-969b-8248c7cda362"}}}' - headers: - Content-Type: - - application/vnd.api+json - status: - code: 200 - message: OK -- request: - body: null - headers: - Content-Type: - - application/json - method: GET - uri: https://api.datadoghq.eu/api/v2/hamr - response: - body: - string: '{"status": "error", "code": 403, "errors": ["Forbidden"], "statuspage": - "http://status.datadoghq.eu", "twitter": "http://twitter.com/datadogops", - "email": "support@datadoghq.com"}' - headers: - Content-Type: - - application/json - status: - code: 403 - message: Forbidden -- request: - body: null - headers: - Content-Type: - - application/json - method: GET - uri: https://us5.datadoghq.com/api/v2/hamr - response: - body: - string: '{"data": {"id": "b9e754c7-857e-11ef-afbd-c64ba5fcf2d1", "type": "hamr_org_connections", - "attributes": {"HamrStatus": 1, "IsPrimary": false, "ModifiedAt": "2024-10-08 - 15:06:20.300120", "ModifiedBy": "charlie.zhang", "TargetOrgDatacenter": "eu1.prod.dog", - "TargetOrgName": "DDR Internal Testing eu1", "TargetOrgUuid": "30187db5-8582-11ef-969b-8248c7cda362"}}}' - headers: - Content-Type: - - application/vnd.api+json - status: - code: 200 - message: OK -- request: - body: null - headers: - Content-Type: - - application/json - method: GET - uri: https://api.datadoghq.eu/api/v2/hamr - response: - body: - string: '{"status": "error", "code": 403, "errors": ["Forbidden"], "statuspage": - "http://status.datadoghq.eu", "twitter": "http://twitter.com/datadogops", - "email": "support@datadoghq.com"}' - headers: - Content-Type: - - application/json - status: - code: 403 - message: Forbidden -- request: - body: null - headers: - Content-Type: - - application/json - method: GET - uri: https://us5.datadoghq.com/api/v2/hamr - response: - body: - string: '{"data": {"id": "b9e754c7-857e-11ef-afbd-c64ba5fcf2d1", "type": "hamr_org_connections", - "attributes": {"HamrStatus": 1, "IsPrimary": false, "ModifiedAt": "2024-10-08 - 15:06:20.300120", "ModifiedBy": "charlie.zhang", "TargetOrgDatacenter": "eu1.prod.dog", - "TargetOrgName": "DDR Internal Testing eu1", "TargetOrgUuid": "30187db5-8582-11ef-969b-8248c7cda362"}}}' - headers: - Content-Type: - - application/vnd.api+json - status: - code: 200 - message: OK -- request: - body: null - headers: - Content-Type: - - application/json - method: GET - uri: https://api.datadoghq.eu/api/v2/hamr - response: - body: - string: '{"status": "error", "code": 403, "errors": ["Forbidden"], "statuspage": - "http://status.datadoghq.eu", "twitter": "http://twitter.com/datadogops", - "email": "support@datadoghq.com"}' - headers: - Content-Type: - - application/json - status: - code: 403 - message: Forbidden -version: 1 diff --git a/tests/integration/cassettes/test_cli/TestCli.test_verify_ddr_status_failure.frozen b/tests/integration/cassettes/test_cli/TestCli.test_verify_ddr_status_failure.frozen new file mode 100644 index 00000000..2a490d14 --- /dev/null +++ b/tests/integration/cassettes/test_cli/TestCli.test_verify_ddr_status_failure.frozen @@ -0,0 +1 @@ +2025-03-20T10:50:46.693087-04:00 \ No newline at end of file diff --git a/tests/integration/cassettes/test_cli/TestCli.test_verify_ddr_status_failure.yaml b/tests/integration/cassettes/test_cli/TestCli.test_verify_ddr_status_failure.yaml new file mode 100644 index 00000000..c2b98d8b --- /dev/null +++ b/tests/integration/cassettes/test_cli/TestCli.test_verify_ddr_status_failure.yaml @@ -0,0 +1,394 @@ +interactions: +- request: + body: null + headers: + Content-Type: + - application/json + method: GET + uri: https://api.datadoghq.eu/api/v2/hamr + response: + body: + string: '{"status": "error", "code": 403, "errors": ["Forbidden"], "statuspage": + "http://status.datadoghq.eu", "twitter": "http://twitter.com/datadogops", + "email": "support@datadoghq.com"}' + headers: + Content-Type: + - application/json + status: + code: 403 + message: Forbidden +- request: + body: null + headers: + Content-Type: + - application/json + method: GET + uri: https://us5.datadoghq.com/api/v2/hamr + response: + body: + string: '{"data": {"id": "b9e754c7-857e-11ef-afbd-c64ba5fcf2d1", "type": "hamr_org_connections", + "attributes": {"HamrStatus": 1, "IsPrimary": false, "ModifiedAt": "2024-10-08 + 15:06:20.300120", "ModifiedBy": "charlie.zhang", "TargetOrgDatacenter": "eu1.prod.dog", + "TargetOrgName": "DDR Internal Testing eu1", "TargetOrgUuid": "30187db5-8582-11ef-969b-8248c7cda362"}}}' + headers: + Content-Type: + - application/vnd.api+json + status: + code: 200 + message: OK +- request: + body: null + headers: + Content-Type: + - application/json + method: GET + uri: https://api.datadoghq.eu/api/v2/hamr + response: + body: + string: '{"status": "error", "code": 403, "errors": ["Forbidden"], "statuspage": + "http://status.datadoghq.eu", "twitter": "http://twitter.com/datadogops", + "email": "support@datadoghq.com"}' + headers: + Content-Type: + - application/json + status: + code: 403 + message: Forbidden +- request: + body: null + headers: + Content-Type: + - application/json + method: GET + uri: https://us5.datadoghq.com/api/v2/hamr + response: + body: + string: '{"data": {"id": "b9e754c7-857e-11ef-afbd-c64ba5fcf2d1", "type": "hamr_org_connections", + "attributes": {"HamrStatus": 1, "IsPrimary": false, "ModifiedAt": "2024-10-08 + 15:06:20.300120", "ModifiedBy": "charlie.zhang", "TargetOrgDatacenter": "eu1.prod.dog", + "TargetOrgName": "DDR Internal Testing eu1", "TargetOrgUuid": "30187db5-8582-11ef-969b-8248c7cda362"}}}' + headers: + Content-Type: + - application/vnd.api+json + status: + code: 200 + message: OK +- request: + body: null + headers: + Content-Type: + - application/json + method: GET + uri: https://api.datadoghq.eu/api/v2/hamr + response: + body: + string: '{"status": "error", "code": 403, "errors": ["Forbidden"], "statuspage": + "http://status.datadoghq.eu", "twitter": "http://twitter.com/datadogops", + "email": "support@datadoghq.com"}' + headers: + Content-Type: + - application/json + status: + code: 403 + message: Forbidden +- request: + body: null + headers: + Content-Type: + - application/json + method: GET + uri: https://us5.datadoghq.com/api/v2/hamr + response: + body: + string: "\\n\\n\\n\t\\n\tForbidden\\n\t\\n\\n\\n\t

\"Datadog\"Datadog

\\n\t
\\n\t\t\"Sad\\n\t\t

403 - Forbidden!

\\n\t\t\\n\t\t
\\n\t\t\t

Scheduled Maintenance

\\n\t\t\t
\\n\t\t\t\\n\t\t
\\n\t
\\n\\n\t\\n\t\\n\\n\\n" + headers: + Content-Type: + - text/html + status: + code: 403 + message: Forbidden +- request: + body: null + headers: + Content-Type: + - application/json + method: GET + uri: https://us5.datadoghq.com/api/v2/hamr + response: + body: + string: "\\n\\n\\n\t\\n\tForbidden\\n\t\\n\\n\\n\t

\"Datadog\"Datadog

\\n\t
\\n\t\t\"Sad\\n\t\t

403 - Forbidden!

\\n\t\t\\n\t\t
\\n\t\t\t

Scheduled Maintenance

\\n\t\t\t
\\n\t\t\t\\n\t\t
\\n\t
\\n\\n\t\\n\t\\n\\n\\n" + headers: + Content-Type: + - text/html + status: + code: 403 + message: Forbidden +- request: + body: null + headers: + Content-Type: + - application/json + method: GET + uri: https://us5.datadoghq.com/api/v2/hamr + response: + body: + string: "\\n\\n\\n\t\\n\tForbidden\\n\t\\n\\n\\n\t

\"Datadog\"Datadog

\\n\t
\\n\t\t\"Sad\\n\t\t

403 - Forbidden!

\\n\t\t\\n\t\t
\\n\t\t\t

Scheduled Maintenance

\\n\t\t\t
\\n\t\t\t\\n\t\t
\\n\t
\\n\\n\t\\n\t\\n\\n\\n" + headers: + Content-Type: + - text/html + status: + code: 403 + message: Forbidden +- request: + body: null + headers: + Content-Type: + - application/json + method: GET + uri: https://us5.datadoghq.com/api/v2/hamr + response: + body: + string: "\\n\\n\\n\t\\n\tForbidden\\n\t\\n\\n\\n\t

\"Datadog\"Datadog

\\n\t
\\n\t\t\"Sad\\n\t\t

403 - Forbidden!

\\n\t\t\\n\t\t
\\n\t\t\t

Scheduled Maintenance

\\n\t\t\t
\\n\t\t\t\\n\t\t
\\n\t
\\n\\n\t\\n\t\\n\\n\\n" + headers: + Content-Type: + - text/html + status: + code: 403 + message: Forbidden +version: 1 diff --git a/tests/integration/test_cli.py b/tests/integration/test_cli.py index a31953cd..913bc005 100644 --- a/tests/integration/test_cli.py +++ b/tests/integration/test_cli.py @@ -94,11 +94,21 @@ def test_sync(self, runner, caplog): # cleanup after ourselves self.test_cleanup(runner, caplog) - def test_import_verify_ddr_status_failure(self, runner, caplog): + def test_verify_ddr_status_failure(self, runner, caplog): caplog.set_level(logging.DEBUG) + # source ddr fails with mock.patch.dict(os.environ, {"DD_SOURCE_API_KEY": "fake"}): - for command in ["import", "sync", "migrate", "diffs"]: + for command in ["import", "migrate", "diffs"]: + ret = runner.invoke(cli, [command, "--validate=false", f"--resources={self.resources}"]) + # The above should fail + assert "No match for the request" not in caplog.text + assert 1 == ret.exit_code + assert "verification failed" in caplog.text + + # destination ddr fails + with mock.patch.dict(os.environ, {"DD_DESTINATION_API_KEY": "fake"}): + for command in ["sync", "migrate", "diffs", "reset"]: ret = runner.invoke(cli, [command, "--validate=false", f"--resources={self.resources}"]) # The above should fail assert "No match for the request" not in caplog.text