|
| 1 | +import pathlib |
| 2 | + |
| 3 | +import pytest |
| 4 | + |
| 5 | +from flyte.connectors import _connector |
| 6 | +from flyte.models import CodeBundle |
| 7 | + |
| 8 | + |
| 9 | +@pytest.mark.asyncio |
| 10 | +async def test_upload_code_bundle_cleans_up_local_copy(tmp_path: pathlib.Path, monkeypatch: pytest.MonkeyPatch) -> None: |
| 11 | + bundle_dirs: list[pathlib.Path] = [] |
| 12 | + |
| 13 | + async def build_code_bundle( |
| 14 | + *, from_dir: pathlib.Path, dryrun: bool, copy_bundle_to: pathlib.Path | None = None |
| 15 | + ) -> CodeBundle: |
| 16 | + assert from_dir == tmp_path |
| 17 | + assert dryrun is True |
| 18 | + assert copy_bundle_to is not None |
| 19 | + bundle_dirs.append(copy_bundle_to) |
| 20 | + local_bundle = copy_bundle_to / "bundle.tar.gz" |
| 21 | + local_bundle.write_bytes(b"bundle") |
| 22 | + return CodeBundle(tgz=str(local_bundle), computed_version="v1") |
| 23 | + |
| 24 | + async def put(from_path: str, to_path: str) -> str: |
| 25 | + local_path = pathlib.Path(from_path) |
| 26 | + assert bundle_dirs |
| 27 | + assert local_path.parent == bundle_dirs[0] |
| 28 | + assert local_path.exists() |
| 29 | + assert to_path == "s3://bucket/prefix/code_bundle/bundle.tar.gz" |
| 30 | + return "s3://bucket/uploaded/bundle.tar.gz" |
| 31 | + |
| 32 | + monkeypatch.setattr(_connector, "build_code_bundle", build_code_bundle) |
| 33 | + monkeypatch.setattr(_connector.storage, "put", put) |
| 34 | + |
| 35 | + result = await _connector._build_and_upload_code_bundle(tmp_path, "s3://bucket/prefix") |
| 36 | + |
| 37 | + assert result == CodeBundle( |
| 38 | + tgz="s3://bucket/uploaded/bundle.tar.gz", |
| 39 | + computed_version="v1", |
| 40 | + destination="/opt/flyte/", |
| 41 | + ) |
| 42 | + assert len(bundle_dirs) == 1 |
| 43 | + assert not bundle_dirs[0].exists() |
| 44 | + |
| 45 | + |
| 46 | +@pytest.mark.asyncio |
| 47 | +async def test_upload_code_bundle_cleans_up_local_copy_when_upload_fails( |
| 48 | + tmp_path: pathlib.Path, monkeypatch: pytest.MonkeyPatch |
| 49 | +) -> None: |
| 50 | + bundle_dirs: list[pathlib.Path] = [] |
| 51 | + |
| 52 | + async def build_code_bundle( |
| 53 | + *, from_dir: pathlib.Path, dryrun: bool, copy_bundle_to: pathlib.Path | None = None |
| 54 | + ) -> CodeBundle: |
| 55 | + assert from_dir == tmp_path |
| 56 | + assert dryrun is True |
| 57 | + assert copy_bundle_to is not None |
| 58 | + bundle_dirs.append(copy_bundle_to) |
| 59 | + local_bundle = copy_bundle_to / "bundle.tar.gz" |
| 60 | + local_bundle.write_bytes(b"bundle") |
| 61 | + return CodeBundle(tgz=str(local_bundle), computed_version="v1") |
| 62 | + |
| 63 | + async def put(from_path: str, to_path: str) -> str: |
| 64 | + local_path = pathlib.Path(from_path) |
| 65 | + assert bundle_dirs |
| 66 | + assert local_path.parent == bundle_dirs[0] |
| 67 | + assert local_path.exists() |
| 68 | + assert to_path == "s3://bucket/prefix/code_bundle/bundle.tar.gz" |
| 69 | + raise OSError("upload failed") |
| 70 | + |
| 71 | + monkeypatch.setattr(_connector, "build_code_bundle", build_code_bundle) |
| 72 | + monkeypatch.setattr(_connector.storage, "put", put) |
| 73 | + |
| 74 | + with pytest.raises(OSError, match="upload failed"): |
| 75 | + await _connector._build_and_upload_code_bundle(tmp_path, "s3://bucket/prefix") |
| 76 | + |
| 77 | + assert len(bundle_dirs) == 1 |
| 78 | + assert not bundle_dirs[0].exists() |
0 commit comments