From 22103046ec776c6a7619d7cc6b828155492fd026 Mon Sep 17 00:00:00 2001 From: Benjamin Bertrand Date: Sat, 2 Dec 2023 18:20:12 +0100 Subject: [PATCH] Add tests for download_repodata serve_repo_data fixture copied from rattler --- quetz/tests/test_mirror.py | 108 +++++++++++++++++++++++++++++++++++++ 1 file changed, 108 insertions(+) diff --git a/quetz/tests/test_mirror.py b/quetz/tests/test_mirror.py index c2023f7f..b8cbc06a 100644 --- a/quetz/tests/test_mirror.py +++ b/quetz/tests/test_mirror.py @@ -1,9 +1,12 @@ import concurrent.futures import json import os +import subprocess +import time import uuid from io import BytesIO from pathlib import Path +from unittest import mock from unittest.mock import MagicMock from urllib.parse import urlparse @@ -21,6 +24,7 @@ RemoteServerError, create_packages_from_channeldata, create_versions_from_repodata, + download_repodata, handle_repodata_package, initial_sync_mirror, ) @@ -1398,3 +1402,107 @@ def test_handle_repodata_package_with_plugin( ) assert plugin.about['conda_version'] == '4.8.4' + + +@pytest.fixture(scope="session") +def serve_repo_data() -> None: + port, repo_name = 8912, "test-repo" + + test_data_dir = Path(__file__).parent / "data" / "test-server" + + with subprocess.Popen( + [ + "python", + str(test_data_dir / "reposerver.py"), + "-d", + str(test_data_dir / "repo"), + "-n", + repo_name, + "-p", + str(port), + ] + ) as proc: + time.sleep(0.5) + yield port, repo_name + proc.terminate() + + +@pytest.fixture(scope="session") +def test_repodata_json() -> bytes: + repodata_json_file = ( + Path(__file__).parent + / "data" + / "test-server" + / "repo" + / "noarch" + / "repodata.json" + ) + return repodata_json_file.read_bytes() + + +@pytest.fixture +def server_proxy_channel(db, serve_repo_data): + port, repo = serve_repo_data + channel = Channel( + name="server-proxy-channel", + mirror_channel_url=f"http://localhost:{port}/{repo}", + mirror_mode="proxy", + ) + db.add(channel) + db.commit() + + yield channel + + db.delete(channel) + db.commit() + + +@pytest.mark.asyncio +async def test_download_repodata( + config, + test_repodata_json, + serve_repo_data, +): + port, repo = serve_repo_data + repository = RemoteRepository(f"http://localhost:{port}/{repo}", session=None) + channel = "test-proxy-channel" + platform = "noarch" + rattler_cache_path = Path(config.general_rattler_cache_dir) / channel / platform + assert not rattler_cache_path.exists() + with mock.patch('quetz.tasks.mirror.Config', return_value=config): + result = await download_repodata(repository, channel, platform) + assert rattler_cache_path.is_dir() + assert result == test_repodata_json + + +@mock.patch("quetz.tasks.mirror.download_repodata") +def test_download_remote_file_repodata( + mock_download_repodata, client, server_proxy_channel, test_repodata_json +): + """Test downloading repodata.json using download_repodata.""" + # download from remote server using download_repodata + mock_download_repodata.return_value = test_repodata_json + assert not mock_download_repodata.called + response = client.get(f"/get/{server_proxy_channel.name}/noarch/repodata.json") + assert response.status_code == 200 + assert response.content == test_repodata_json + assert mock_download_repodata.call_count == 1 + # download from cache (download_repodata not called again) + response = client.get(f"/get/{server_proxy_channel.name}/noarch/repodata.json") + assert response.status_code == 200 + assert response.content == test_repodata_json + assert mock_download_repodata.call_count == 1 + + +@mock.patch("quetz.tasks.mirror.download_repodata") +def test_download_remote_file_current_repodata( + mock_download_repodata, client, server_proxy_channel, test_repodata_json +): + """Test downloading current_repodata.json.""" + response = client.get( + f"/get/{server_proxy_channel.name}/noarch/current_repodata.json" + ) + assert response.status_code == 200 + assert response.content == test_repodata_json + # download_repodata isn't used (download done with repository.open) + assert not mock_download_repodata.called