|
12 | 12 | import botocore |
13 | 13 | import freezegun |
14 | 14 | import pytest |
| 15 | +from botocore.config import Config |
15 | 16 | from opentaskpy.taskhandlers import transfer |
16 | 17 | from pytest_shell import fs |
17 | 18 |
|
@@ -477,6 +478,139 @@ def test_remote_handler(): |
477 | 478 | assert transfer_obj.dest_remote_handlers is None |
478 | 479 |
|
479 | 480 |
|
| 481 | +def test_s3_transfer_passes_botocore_config(monkeypatch): |
| 482 | + captured = {} |
| 483 | + |
| 484 | + class DummyClient: |
| 485 | + def close(self): |
| 486 | + return None |
| 487 | + |
| 488 | + def fake_get_aws_client( |
| 489 | + client_type, |
| 490 | + credentials, |
| 491 | + token_expiry_seconds=900, |
| 492 | + assume_role_arn=None, |
| 493 | + assume_role_external_id=None, |
| 494 | + config=None, |
| 495 | + ): |
| 496 | + captured["client_type"] = client_type |
| 497 | + captured["credentials"] = credentials |
| 498 | + captured["token_expiry_seconds"] = token_expiry_seconds |
| 499 | + captured["assume_role_arn"] = assume_role_arn |
| 500 | + captured["assume_role_external_id"] = assume_role_external_id |
| 501 | + captured["config"] = config |
| 502 | + return {"client": DummyClient(), "temporary_creds": None} |
| 503 | + |
| 504 | + monkeypatch.setattr( |
| 505 | + "opentaskpy.addons.aws.remotehandlers.s3.get_aws_client", fake_get_aws_client |
| 506 | + ) |
| 507 | + |
| 508 | + handler = S3Transfer( |
| 509 | + { |
| 510 | + "task_id": "s3-config-test", |
| 511 | + "bucket": BUCKET_NAME, |
| 512 | + "directory": "src", |
| 513 | + "fileRegex": ".*\\.txt", |
| 514 | + "protocol": { |
| 515 | + "name": "opentaskpy.addons.aws.remotehandlers.s3.S3Transfer", |
| 516 | + "botocoreReadTimeout": 120, |
| 517 | + "botocoreConnectTimeout": 30, |
| 518 | + "max_attempts": 2, |
| 519 | + }, |
| 520 | + } |
| 521 | + ) |
| 522 | + |
| 523 | + assert isinstance(captured["config"], Config) |
| 524 | + assert captured["client_type"] == "s3" |
| 525 | + assert captured["config"].read_timeout == 120 |
| 526 | + assert captured["config"].connect_timeout == 30 |
| 527 | + assert captured["config"].retries["max_attempts"] == 2 |
| 528 | + handler.tidy() |
| 529 | + |
| 530 | + |
| 531 | +def test_s3_transfer_uses_default_botocore_config(monkeypatch): |
| 532 | + captured = {} |
| 533 | + |
| 534 | + class DummyClient: |
| 535 | + def close(self): |
| 536 | + return None |
| 537 | + |
| 538 | + def fake_get_aws_client( |
| 539 | + client_type, |
| 540 | + credentials, |
| 541 | + token_expiry_seconds=900, |
| 542 | + assume_role_arn=None, |
| 543 | + assume_role_external_id=None, |
| 544 | + config=None, |
| 545 | + ): |
| 546 | + captured["config"] = config |
| 547 | + return {"client": DummyClient(), "temporary_creds": None} |
| 548 | + |
| 549 | + monkeypatch.setattr( |
| 550 | + "opentaskpy.addons.aws.remotehandlers.s3.get_aws_client", fake_get_aws_client |
| 551 | + ) |
| 552 | + |
| 553 | + handler = S3Transfer( |
| 554 | + { |
| 555 | + "task_id": "s3-default-config-test", |
| 556 | + "bucket": BUCKET_NAME, |
| 557 | + "directory": "src", |
| 558 | + "fileRegex": ".*\\.txt", |
| 559 | + "protocol": { |
| 560 | + "name": "opentaskpy.addons.aws.remotehandlers.s3.S3Transfer", |
| 561 | + }, |
| 562 | + } |
| 563 | + ) |
| 564 | + |
| 565 | + assert isinstance(captured["config"], Config) |
| 566 | + assert captured["config"].read_timeout == 60 |
| 567 | + assert captured["config"].connect_timeout == 10 |
| 568 | + assert captured["config"].retries["max_attempts"] == 0 |
| 569 | + handler.tidy() |
| 570 | + |
| 571 | + |
| 572 | +def test_s3_transfer_tidy_is_idempotent(monkeypatch): |
| 573 | + class DummyClient: |
| 574 | + def __init__(self): |
| 575 | + self.close_calls = 0 |
| 576 | + |
| 577 | + def close(self): |
| 578 | + self.close_calls += 1 |
| 579 | + |
| 580 | + def fake_get_aws_client( |
| 581 | + client_type, |
| 582 | + credentials, |
| 583 | + token_expiry_seconds=900, |
| 584 | + assume_role_arn=None, |
| 585 | + assume_role_external_id=None, |
| 586 | + config=None, |
| 587 | + ): |
| 588 | + return {"client": DummyClient(), "temporary_creds": None} |
| 589 | + |
| 590 | + monkeypatch.setattr( |
| 591 | + "opentaskpy.addons.aws.remotehandlers.s3.get_aws_client", fake_get_aws_client |
| 592 | + ) |
| 593 | + |
| 594 | + handler = S3Transfer( |
| 595 | + { |
| 596 | + "task_id": "s3-tidy-test", |
| 597 | + "bucket": BUCKET_NAME, |
| 598 | + "directory": "src", |
| 599 | + "fileRegex": ".*\\.txt", |
| 600 | + "protocol": { |
| 601 | + "name": "opentaskpy.addons.aws.remotehandlers.s3.S3Transfer", |
| 602 | + }, |
| 603 | + } |
| 604 | + ) |
| 605 | + |
| 606 | + client = handler.s3_client |
| 607 | + handler.tidy() |
| 608 | + handler.tidy() |
| 609 | + |
| 610 | + assert client.close_calls == 1 |
| 611 | + assert handler.s3_client is None |
| 612 | + |
| 613 | + |
480 | 614 | def test_s3_file_watch(s3_client, setup_bucket, tmp_path): |
481 | 615 | transfer_obj = transfer.Transfer( |
482 | 616 | None, "s3-file-watch", s3_file_watch_task_definition |
|
0 commit comments