|
| 1 | +import os |
| 2 | +from unittest.mock import MagicMock |
| 3 | +from unittest.mock import patch |
| 4 | + |
| 5 | +import pytest |
| 6 | + |
| 7 | +from core.utils.controller import build_collection_uri |
| 8 | +from core.utils.controller import download_collection |
| 9 | + |
| 10 | + |
| 11 | +@pytest.fixture |
| 12 | +def mock_tar_data(): |
| 13 | + """A fixture to provide mock tarball data.""" |
| 14 | + return b"This is some mock tarball content" |
| 15 | + |
| 16 | + |
| 17 | +@pytest.fixture |
| 18 | +def mock_download_success(mock_tar_data): |
| 19 | + """ |
| 20 | + Corrected fixture to mock a successful download scenario, |
| 21 | + including mocking os.makedirs and ensuring cleanup. |
| 22 | + """ |
| 23 | + with ( |
| 24 | + patch("core.utils.controller.helpers.get") as mock_get, |
| 25 | + patch("core.utils.controller.helpers.tarfile.open") as mock_tar_open, |
| 26 | + patch("core.utils.controller.helpers.os.makedirs") as mock_makedirs, |
| 27 | + patch("core.utils.controller.helpers.shutil.rmtree") as mock_rmtree, |
| 28 | + patch( |
| 29 | + "core.utils.controller.helpers.tempfile.mkdtemp", |
| 30 | + return_value="/mock/temp/dir", |
| 31 | + ) as mock_mkdtemp, |
| 32 | + ): |
| 33 | + |
| 34 | + mock_response = MagicMock() |
| 35 | + mock_response.raw = mock_tar_data |
| 36 | + mock_get.return_value = mock_response |
| 37 | + |
| 38 | + mock_tar_open.return_value.__enter__.return_value = MagicMock() |
| 39 | + |
| 40 | + yield mock_get, mock_tar_open, mock_makedirs, mock_mkdtemp, mock_rmtree |
| 41 | + |
| 42 | + |
| 43 | +@pytest.fixture |
| 44 | +def mock_download_failure(): |
| 45 | + """ |
| 46 | + Fixture to mock a download failure scenario by raising an exception |
| 47 | + and mocking the cleanup functions. |
| 48 | + """ |
| 49 | + with ( |
| 50 | + patch( |
| 51 | + "core.utils.controller.helpers.get", side_effect=Exception("Network error") |
| 52 | + ) as mock_get, |
| 53 | + patch("core.utils.controller.helpers.shutil.rmtree") as mock_rmtree, |
| 54 | + patch("core.utils.controller.helpers.os.makedirs"), |
| 55 | + patch( |
| 56 | + "core.utils.controller.helpers.tempfile.mkdtemp", |
| 57 | + return_value="/mock/temp/dir", |
| 58 | + ) as mock_mkdtemp, |
| 59 | + ): |
| 60 | + |
| 61 | + yield mock_get, mock_rmtree, mock_mkdtemp |
| 62 | + |
| 63 | + |
| 64 | +@pytest.mark.parametrize( |
| 65 | + "collection_name, version, expected_uri", |
| 66 | + [ |
| 67 | + ( |
| 68 | + "another.collection", |
| 69 | + "2.0.0", |
| 70 | + ( |
| 71 | + "http://localhost:44926/api/galaxy/v3/plugin/ansible/content/published/" |
| 72 | + "collections/artifacts/another-collection-2.0.0.tar.gz" |
| 73 | + ), |
| 74 | + ), |
| 75 | + ( |
| 76 | + "edge.case", |
| 77 | + "0.1.0-beta", |
| 78 | + ( |
| 79 | + "http://localhost:44926/api/galaxy/v3/plugin/ansible/content/published/" |
| 80 | + "collections/artifacts/edge-case-0.1.0-beta.tar.gz" |
| 81 | + ), |
| 82 | + ), |
| 83 | + ], |
| 84 | +) |
| 85 | +def test_build_collection_uri(collection_name, version, expected_uri): |
| 86 | + """ |
| 87 | + Tests that various collection names and versions build the correct URI. |
| 88 | + """ |
| 89 | + assert build_collection_uri(collection_name, version) == expected_uri |
| 90 | + |
| 91 | + |
| 92 | +def test_download_collection_success(mock_download_success): |
| 93 | + """ |
| 94 | + Tests the successful download and extraction of a collection. |
| 95 | + """ |
| 96 | + mock_get, mock_tar_open, mock_makedirs, mock_mkdtemp, mock_rmtree = ( |
| 97 | + mock_download_success |
| 98 | + ) |
| 99 | + |
| 100 | + collection_name = "my_namespace.my_collection" |
| 101 | + version = "1.0.0" |
| 102 | + expected_path = os.path.join("/mock/temp/dir", "my_namespace.my_collection-1.0.0") |
| 103 | + |
| 104 | + with download_collection(collection_name, version) as path: |
| 105 | + # Assert that the correct path was yielded |
| 106 | + assert path == expected_path |
| 107 | + |
| 108 | + mock_mkdtemp.assert_called_once() |
| 109 | + mock_makedirs.assert_called_once_with(expected_path, exist_ok=True) |
| 110 | + mock_get.assert_called_once() |
| 111 | + mock_tar_open.assert_called_once() |
| 112 | + |
| 113 | + mock_rmtree.assert_called_once_with("/mock/temp/dir") |
| 114 | + |
| 115 | + |
| 116 | +def test_download_collection_failure(mock_download_failure): |
| 117 | + """ |
| 118 | + Tests that an exception during download. |
| 119 | + """ |
| 120 | + mock_get, mock_rmtree, mock_mkdtemp = mock_download_failure |
| 121 | + |
| 122 | + collection_name = "my_namespace.my_collection" |
| 123 | + version = "1.0.0" |
| 124 | + |
| 125 | + with pytest.raises(Exception, match="Network error"): |
| 126 | + with download_collection(collection_name, version): |
| 127 | + pass |
| 128 | + |
| 129 | + mock_mkdtemp.assert_called_once() |
| 130 | + mock_rmtree.assert_called_once_with("/mock/temp/dir") |
0 commit comments