|
| 1 | +import json |
1 | 2 | import os |
2 | 3 | import time |
3 | 4 |
|
@@ -342,7 +343,10 @@ def test_get_client_do_request_evp_proxy_headers(): |
342 | 343 | ) |
343 | 344 |
|
344 | 345 | _request.assert_called_once_with( |
345 | | - "POST", "http://base_url/repository/endpoint", "payload", {"X-Datadog-EVP-Subdomain": "api"} |
| 346 | + "POST", |
| 347 | + "http://base_url/repository/endpoint", |
| 348 | + "payload", |
| 349 | + {"X-Datadog-EVP-Subdomain": "api", "X-Datadog-NeedsAppKey": "true"}, |
346 | 350 | ) |
347 | 351 |
|
348 | 352 |
|
@@ -417,6 +421,74 @@ def test_git_client_upload_packfiles(git_repo): |
417 | 421 | assert call_kwargs["headers"] == {"Content-Type": "multipart/form-data; boundary=----------boundary------"} |
418 | 422 |
|
419 | 423 |
|
| 424 | +def test_git_do_request_agentless(git_repo): |
| 425 | + mock_serializer = CIVisibilityGitClientSerializerV1("fakeapikey", "fakeappkey") |
| 426 | + response = mock.MagicMock() |
| 427 | + setattr(response, "status", 200) |
| 428 | + |
| 429 | + with mock.patch("ddtrace.internal.ci_visibility.git_client.get_connection") as mock_get_connection: |
| 430 | + with mock.patch("ddtrace.internal.compat.get_connection_response", return_value=response): |
| 431 | + mock_http_connection = mock.Mock() |
| 432 | + mock_http_connection.request = mock.Mock() |
| 433 | + mock_http_connection.close = mock.Mock() |
| 434 | + mock_get_connection.return_value = mock_http_connection |
| 435 | + |
| 436 | + CIVisibilityGitClient._do_request( |
| 437 | + REQUESTS_MODE.AGENTLESS_EVENTS, |
| 438 | + "base_url", |
| 439 | + "endpoint", |
| 440 | + '{"payload": "payload"}', |
| 441 | + mock_serializer, |
| 442 | + {"mock_header_name": "mock_header_value"}, |
| 443 | + ) |
| 444 | + |
| 445 | + mock_get_connection.assert_called_once_with("base_url/repositoryendpoint") |
| 446 | + mock_http_connection.request.assert_called_once_with( |
| 447 | + "POST", |
| 448 | + "base_url/repositoryendpoint", |
| 449 | + '{"payload": "payload"}', |
| 450 | + { |
| 451 | + "dd-api-key": "fakeapikey", |
| 452 | + "dd-application-key": "fakeappkey", |
| 453 | + "mock_header_name": "mock_header_value", |
| 454 | + }, |
| 455 | + ) |
| 456 | + |
| 457 | + |
| 458 | +def test_git_do_request_evp(git_repo): |
| 459 | + mock_serializer = CIVisibilityGitClientSerializerV1("foo", "bar") |
| 460 | + response = mock.MagicMock() |
| 461 | + setattr(response, "status", 200) |
| 462 | + |
| 463 | + with mock.patch("ddtrace.internal.ci_visibility.git_client.get_connection") as mock_get_connection: |
| 464 | + with mock.patch("ddtrace.internal.compat.get_connection_response", return_value=response): |
| 465 | + mock_http_connection = mock.Mock() |
| 466 | + mock_http_connection.request = mock.Mock() |
| 467 | + mock_http_connection.close = mock.Mock() |
| 468 | + mock_get_connection.return_value = mock_http_connection |
| 469 | + |
| 470 | + CIVisibilityGitClient._do_request( |
| 471 | + REQUESTS_MODE.EVP_PROXY_EVENTS, |
| 472 | + "base_url", |
| 473 | + "endpoint", |
| 474 | + '{"payload": "payload"}', |
| 475 | + mock_serializer, |
| 476 | + {"mock_header_name": "mock_header_value"}, |
| 477 | + ) |
| 478 | + |
| 479 | + mock_get_connection.assert_called_once_with("base_url/repositoryendpoint") |
| 480 | + mock_http_connection.request.assert_called_once_with( |
| 481 | + "POST", |
| 482 | + "base_url/repositoryendpoint", |
| 483 | + '{"payload": "payload"}', |
| 484 | + { |
| 485 | + "X-Datadog-NeedsAppKey": "true", |
| 486 | + "X-Datadog-EVP-Subdomain": "api", |
| 487 | + "mock_header_name": "mock_header_value", |
| 488 | + }, |
| 489 | + ) |
| 490 | + |
| 491 | + |
420 | 492 | def test_civisibilitywriter_agentless_url(): |
421 | 493 | with override_env(dict(DD_API_KEY="foobar.baz")): |
422 | 494 | with override_global_config({"_ci_visibility_agentless_url": "https://foo.bar"}): |
@@ -521,6 +593,106 @@ def test_civisibilitywriter_only_traces(_check_enabled_features): |
521 | 593 | CIVisibility.disable() |
522 | 594 |
|
523 | 595 |
|
| 596 | +def test_civisibility_check_enabled_features_agentless_do_request_called_correctly(): |
| 597 | + with mock.patch("ddtrace.internal.ci_visibility.recorder.uuid4", return_value="checkoutmyuuid4"): |
| 598 | + with mock.patch("ddtrace.internal.ci_visibility.recorder._do_request") as mock_do_request: |
| 599 | + mock_do_request.return_value = Response( |
| 600 | + status=200, |
| 601 | + body='{"data":{"id":"1234","type":"ci_app_tracers_test_service_settings","attributes":' |
| 602 | + '{"code_coverage":true,"tests_skipping":true}}}', |
| 603 | + ) |
| 604 | + with mock.patch.object(CIVisibility, "__init__", return_value=None): |
| 605 | + mock_civisibilty = CIVisibility() |
| 606 | + |
| 607 | + ddtrace.internal.ci_visibility.recorder.ddconfig = ddtrace.settings.Config() |
| 608 | + mock_civisibilty._requests_mode = REQUESTS_MODE.AGENTLESS_EVENTS |
| 609 | + mock_civisibilty._service = "service" |
| 610 | + mock_civisibilty._api_key = "myfakeapikey" |
| 611 | + mock_civisibilty._app_key = "myfakeappkey" |
| 612 | + mock_civisibilty._dd_site = "datad0g.com" |
| 613 | + mock_civisibilty._tags = { |
| 614 | + ci.git.REPOSITORY_URL: "my_repo_url", |
| 615 | + ci.git.COMMIT_SHA: "mycommitshaaaaaaalalala", |
| 616 | + ci.git.BRANCH: "notmain", |
| 617 | + } |
| 618 | + |
| 619 | + enabled_features = mock_civisibilty._check_enabled_features() |
| 620 | + |
| 621 | + mock_do_request.assert_called_once() |
| 622 | + do_request_call_args = mock_do_request.call_args[0] |
| 623 | + do_request_payload = json.loads(do_request_call_args[2]) |
| 624 | + |
| 625 | + assert do_request_call_args[0] == "POST" |
| 626 | + assert do_request_call_args[1] == "https://api.datad0g.com/api/v2/libraries/tests/services/setting" |
| 627 | + assert do_request_call_args[3] == { |
| 628 | + "dd-api-key": "myfakeapikey", |
| 629 | + "dd-application-key": "myfakeappkey", |
| 630 | + "Content-Type": "application/json", |
| 631 | + } |
| 632 | + assert do_request_payload == { |
| 633 | + "data": { |
| 634 | + "id": "checkoutmyuuid4", |
| 635 | + "type": "ci_app_test_service_libraries_settings", |
| 636 | + "attributes": { |
| 637 | + "service": "service", |
| 638 | + "env": None, |
| 639 | + "repository_url": "my_repo_url", |
| 640 | + "sha": "mycommitshaaaaaaalalala", |
| 641 | + "branch": "notmain", |
| 642 | + }, |
| 643 | + } |
| 644 | + } |
| 645 | + assert enabled_features == (True, True) |
| 646 | + |
| 647 | + |
| 648 | +def test_civisibility_check_enabled_features_evp_do_request_called_correctly(): |
| 649 | + with mock.patch("ddtrace.internal.ci_visibility.recorder.uuid4", return_value="checkoutmyuuid4"): |
| 650 | + with mock.patch("ddtrace.internal.ci_visibility.recorder._do_request") as mock_do_request: |
| 651 | + mock_do_request.return_value = Response( |
| 652 | + status=200, |
| 653 | + body='{"data":{"id":"1234","type":"ci_app_tracers_test_service_settings","attributes":' |
| 654 | + '{"code_coverage":true,"tests_skipping":true}}}', |
| 655 | + ) |
| 656 | + with mock.patch.object(CIVisibility, "__init__", return_value=None): |
| 657 | + mock_civisibilty = CIVisibility() |
| 658 | + |
| 659 | + ddtrace.internal.ci_visibility.recorder.ddconfig = ddtrace.settings.Config() |
| 660 | + mock_civisibilty._requests_mode = REQUESTS_MODE.EVP_PROXY_EVENTS |
| 661 | + mock_civisibilty._service = "service" |
| 662 | + mock_civisibilty._tags = { |
| 663 | + ci.git.REPOSITORY_URL: "my_repo_url", |
| 664 | + ci.git.COMMIT_SHA: "mycommitshaaaaaaalalala", |
| 665 | + ci.git.BRANCH: "notmain", |
| 666 | + } |
| 667 | + |
| 668 | + enabled_features = mock_civisibilty._check_enabled_features() |
| 669 | + |
| 670 | + mock_do_request.assert_called_once() |
| 671 | + do_request_call_args = mock_do_request.call_args[0] |
| 672 | + do_request_payload = json.loads(do_request_call_args[2]) |
| 673 | + |
| 674 | + assert do_request_call_args[0] == "POST" |
| 675 | + assert ( |
| 676 | + do_request_call_args[1] |
| 677 | + == "http://localhost:8126/evp_proxy/v2/api/v2/libraries/tests/services/setting" |
| 678 | + ) |
| 679 | + assert do_request_call_args[3] == {"X-Datadog-EVP-Subdomain": "api", "X-Datadog-NeedsAppKey": "true"} |
| 680 | + assert do_request_payload == { |
| 681 | + "data": { |
| 682 | + "id": "checkoutmyuuid4", |
| 683 | + "type": "ci_app_test_service_libraries_settings", |
| 684 | + "attributes": { |
| 685 | + "service": "service", |
| 686 | + "env": None, |
| 687 | + "repository_url": "my_repo_url", |
| 688 | + "sha": "mycommitshaaaaaaalalala", |
| 689 | + "branch": "notmain", |
| 690 | + }, |
| 691 | + } |
| 692 | + } |
| 693 | + assert enabled_features == (True, True) |
| 694 | + |
| 695 | + |
524 | 696 | @mock.patch("ddtrace.internal.ci_visibility.recorder._do_request") |
525 | 697 | def test_civisibility_check_enabled_features_no_app_key_request_not_called(_do_request): |
526 | 698 | with override_env( |
|
0 commit comments