Skip to content

Commit

Permalink
Add tests for download_repodata
Browse files Browse the repository at this point in the history
serve_repo_data fixture copied from rattler
  • Loading branch information
beenje committed Dec 3, 2023
1 parent 20eb037 commit 2210304
Showing 1 changed file with 108 additions and 0 deletions.
108 changes: 108 additions & 0 deletions quetz/tests/test_mirror.py
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -21,6 +24,7 @@
RemoteServerError,
create_packages_from_channeldata,
create_versions_from_repodata,
download_repodata,
handle_repodata_package,
initial_sync_mirror,
)
Expand Down Expand Up @@ -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

0 comments on commit 2210304

Please sign in to comment.