|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from unittest.mock import MagicMock |
| 4 | + |
| 5 | +import pytest |
| 6 | + |
| 7 | +import tidy3d |
| 8 | +from tidy3d.web.core import s3utils |
| 9 | + |
| 10 | + |
| 11 | +@pytest.fixture |
| 12 | +def mock_S3STSToken(monkeypatch): |
| 13 | + mock_token = MagicMock() |
| 14 | + mock_token.cloud_path = "" |
| 15 | + mock_token.user_credential = "" |
| 16 | + mock_token.get_bucket = lambda: "" |
| 17 | + mock_token.get_s3_key = lambda: "" |
| 18 | + mock_token.is_expired = lambda: False |
| 19 | + mock_token.get_client = lambda: tidy3d.web.core.s3utils.boto3.client() |
| 20 | + monkeypatch.setattr( |
| 21 | + target=tidy3d.web.core.s3utils, name="_S3STSToken", value=MagicMock(return_value=mock_token) |
| 22 | + ) |
| 23 | + return mock_token |
| 24 | + |
| 25 | + |
| 26 | +@pytest.fixture |
| 27 | +def mock_get_s3_sts_token(monkeypatch): |
| 28 | + def _mock_get_s3_sts_token(resource_id, remote_filename): |
| 29 | + return s3utils._S3STSToken(resource_id, remote_filename) |
| 30 | + |
| 31 | + monkeypatch.setattr( |
| 32 | + target=tidy3d.web.core.s3utils, name="get_s3_sts_token", value=_mock_get_s3_sts_token |
| 33 | + ) |
| 34 | + return _mock_get_s3_sts_token |
| 35 | + |
| 36 | + |
| 37 | +@pytest.fixture |
| 38 | +def mock_s3_client(monkeypatch): |
| 39 | + """ |
| 40 | + Fixture that provides a generic mock S3 client. |
| 41 | + Method-specific side_effects are omitted here and are specified later in the unit tests. |
| 42 | + """ |
| 43 | + mock_client = MagicMock() |
| 44 | + # Patch the `client` as it is imported within `tidy3d.web.core.s3utils.boto3` so that |
| 45 | + # whenever it's invoked (for example with "s3"), it returns our `mock_client`. |
| 46 | + monkeypatch.setattr( |
| 47 | + target=tidy3d.web.core.s3utils.boto3, |
| 48 | + name="client", |
| 49 | + value=MagicMock(return_value=mock_client), |
| 50 | + ) |
| 51 | + return mock_client |
| 52 | + |
| 53 | + |
| 54 | +def test_download_s3_file_success(mock_s3_client, mock_get_s3_sts_token, mock_S3STSToken, tmp_path): |
| 55 | + """Tests a successful download.""" |
| 56 | + destination_path = tmp_path / "downloaded_file.txt" |
| 57 | + expected_content = "abcdefg" |
| 58 | + |
| 59 | + def simulate_download_success(Bucket, Key, Filename, Callback, Config, **kwargs): |
| 60 | + with open(Filename, "w") as f: |
| 61 | + f.write(expected_content) |
| 62 | + return None |
| 63 | + |
| 64 | + mock_s3_client.download_file.side_effect = simulate_download_success |
| 65 | + mock_S3STSToken.get_bucket = lambda: "test-bucket" |
| 66 | + mock_S3STSToken.get_s3_key = lambda: "test-key" |
| 67 | + |
| 68 | + s3utils.download_file( |
| 69 | + resource_id="1234567890", |
| 70 | + remote_filename=destination_path.name, |
| 71 | + to_file=str(destination_path), |
| 72 | + verbose=False, |
| 73 | + progress_callback=None, |
| 74 | + ) |
| 75 | + |
| 76 | + # Check that mock_s3_client.download_file() was invoked with the correct arguments. |
| 77 | + mock_s3_client.download_file.assert_called_once() |
| 78 | + call_args, call_kwargs = mock_s3_client.download_file.call_args |
| 79 | + assert call_kwargs["Bucket"] == "test-bucket" |
| 80 | + assert call_kwargs["Key"] == "test-key" |
| 81 | + assert call_kwargs["Filename"].endswith(s3utils.IN_TRANSIT_SUFFIX) |
| 82 | + assert destination_path.exists() |
| 83 | + with open(destination_path) as f: |
| 84 | + assert f.read() == expected_content |
| 85 | + for p in destination_path.parent.iterdir(): |
| 86 | + assert not p.name.endswith(s3utils.IN_TRANSIT_SUFFIX) # no temporary files are present |
| 87 | + |
| 88 | + |
| 89 | +def test_download_s3_file_raises_oserror( |
| 90 | + mock_s3_client, mock_get_s3_sts_token, mock_S3STSToken, tmp_path |
| 91 | +): |
| 92 | + """Tests download failing with an ``OSError`` (No space left on device).""" |
| 93 | + destination_path = tmp_path / "downloaded_file.txt" |
| 94 | + |
| 95 | + def simulate_download_failure(Bucket, Key, Filename, Callback, Config, **kwargs): |
| 96 | + with open(Filename, "w") as f: |
| 97 | + f.write("abc") |
| 98 | + raise OSError("No space left on device") |
| 99 | + |
| 100 | + mock_s3_client.download_file.side_effect = simulate_download_failure |
| 101 | + mock_S3STSToken.get_bucket = lambda: "test-bucket" |
| 102 | + mock_S3STSToken.get_s3_key = lambda: "test-key" |
| 103 | + |
| 104 | + with pytest.raises(OSError, match="No space left on device"): |
| 105 | + s3utils.download_file( |
| 106 | + resource_id="1234567890", |
| 107 | + remote_filename=destination_path.name, |
| 108 | + to_file=str(destination_path), |
| 109 | + verbose=False, |
| 110 | + progress_callback=None, |
| 111 | + ) |
| 112 | + |
| 113 | + # Check that mock_s3_client.download_file() was invoked with the correct arguments. |
| 114 | + mock_s3_client.download_file.assert_called_once() |
| 115 | + call_args, call_kwargs = mock_s3_client.download_file.call_args |
| 116 | + assert call_kwargs["Bucket"] == "test-bucket" |
| 117 | + assert call_kwargs["Key"] == "test-key" |
| 118 | + assert call_kwargs["Filename"].endswith(s3utils.IN_TRANSIT_SUFFIX) |
| 119 | + # Since downloading failed, no new files should exist locally. |
| 120 | + assert not destination_path.exists() |
| 121 | + for p in destination_path.parent.iterdir(): |
| 122 | + assert not p.name.endswith(s3utils.IN_TRANSIT_SUFFIX) # no temporary files are present |
0 commit comments