Changeset View
Changeset View
Standalone View
Standalone View
swh/web/tests/misc/test_coverage.py
# Copyright (C) 2021-2022 The Software Heritage developers | # Copyright (C) 2021-2022 The Software Heritage developers | ||||
# See the AUTHORS file at the top-level directory of this distribution | # See the AUTHORS file at the top-level directory of this distribution | ||||
# License: GNU Affero General Public License version 3, or any later version | # License: GNU Affero General Public License version 3, or any later version | ||||
# See top-level LICENSE file for more information | # See top-level LICENSE file for more information | ||||
from datetime import datetime, timezone | from datetime import datetime, timezone | ||||
from itertools import chain | from itertools import chain | ||||
import os | import os | ||||
from random import randint | from random import choices, randint | ||||
import uuid | import uuid | ||||
import pytest | |||||
from django.conf import settings | from django.conf import settings | ||||
from django.utils.html import escape | from django.utils.html import escape | ||||
from swh.scheduler.model import LastVisitStatus, ListedOrigin, OriginVisitStats | from swh.scheduler.model import LastVisitStatus, ListedOrigin, OriginVisitStats | ||||
from swh.web.common.utils import reverse | from swh.web.common.utils import reverse | ||||
from swh.web.config import SWH_WEB_SERVER_NAME | from swh.web.config import SWH_WEB_SERVER_NAME | ||||
from swh.web.misc.coverage import deposited_origins, legacy_origins, listed_origins | from swh.web.misc.coverage import deposited_origins, legacy_origins, listed_origins | ||||
from swh.web.tests.django_asserts import assert_contains | from swh.web.tests.django_asserts import assert_contains | ||||
from swh.web.tests.utils import check_html_get_response, check_http_get_response | from swh.web.tests.utils import check_html_get_response, check_http_get_response | ||||
def test_coverage_view_no_metrics(client, swh_scheduler): | def test_coverage_view_no_metrics(client, swh_scheduler): | ||||
""" | """ | ||||
Check coverage view can be rendered when scheduler metrics and deposits | Check coverage view can be rendered when scheduler metrics and deposits | ||||
data are not available. | data are not available. | ||||
""" | """ | ||||
url = reverse("swh-coverage") | url = reverse("swh-coverage") | ||||
check_html_get_response( | check_html_get_response( | ||||
client, url, status_code=200, template_used="misc/coverage.html" | client, url, status_code=200, template_used="misc/coverage.html" | ||||
) | ) | ||||
def test_coverage_view_with_metrics(client, swh_scheduler, mocker): | visit_types = ["git", "hg", "svn", "bzr", "svn"] | ||||
""" | |||||
Generate some sample scheduler metrics and some sample deposits | |||||
that will be consumed by the archive coverage view, then check | |||||
the HTML page gets rendered without errors. | |||||
""" | |||||
@pytest.fixture(autouse=True) | |||||
def archive_coverage_data(mocker, swh_scheduler): | |||||
"""Generate some sample scheduler metrics and some sample deposits | |||||
that will be consumed by the archive coverage view. | |||||
""" | |||||
# mock calls to get nixguix origin counts | # mock calls to get nixguix origin counts | ||||
mock_archive = mocker.patch("swh.web.misc.coverage.archive") | mock_archive = mocker.patch("swh.web.misc.coverage.archive") | ||||
mock_archive.lookup_latest_origin_snapshot.return_value = {"id": "some-snapshot"} | mock_archive.lookup_latest_origin_snapshot.return_value = {"id": "some-snapshot"} | ||||
mock_archive.lookup_snapshot_sizes.return_value = {"release": 30095} | mock_archive.lookup_snapshot_sizes.return_value = {"release": 30095} | ||||
listers = [] | listers = [] | ||||
visit_types = ["git", "hg", "svn", "bzr", "svn"] | |||||
for origins in listed_origins["origins"]: | for origins in listed_origins["origins"]: | ||||
# create some instances for each lister | # create some instances for each lister | ||||
for instance in range(randint(1, 5)): | for instance in range(randint(1, 5)): | ||||
lister = swh_scheduler.get_or_create_lister( | lister = swh_scheduler.get_or_create_lister( | ||||
origins["type"], f"instance-{instance}" | origins["type"], f"instance-{instance}" | ||||
) | ) | ||||
listers.append(lister) | listers.append(lister) | ||||
# record some sample listed origins | # record some sample listed origins | ||||
Show All 37 Lines | for origins in deposited_origins["origins"]: | ||||
{ | { | ||||
"origin_url": f"https://{origins['search_pattern']}/{uuid.uuid4()}", | "origin_url": f"https://{origins['search_pattern']}/{uuid.uuid4()}", | ||||
"status": "done", | "status": "done", | ||||
} | } | ||||
) | ) | ||||
get_deposits_list = mocker.patch("swh.web.misc.coverage.get_deposits_list") | get_deposits_list = mocker.patch("swh.web.misc.coverage.get_deposits_list") | ||||
get_deposits_list.return_value = deposits | get_deposits_list.return_value = deposits | ||||
def test_coverage_view_with_metrics(client): | |||||
# check view gets rendered without errors | # check view gets rendered without errors | ||||
url = reverse("swh-coverage") | url = reverse("swh-coverage") | ||||
resp = check_html_get_response( | resp = check_html_get_response( | ||||
client, url, status_code=200, template_used="misc/coverage.html" | client, url, status_code=200, template_used="misc/coverage.html" | ||||
) | ) | ||||
# check logos and origins search links are present in the rendered page | # check logos and origins search links are present in the rendered page | ||||
for origins in chain( | for origins in chain( | ||||
Show All 15 Lines | def test_coverage_view_with_metrics(client): | ||||
for visit_type in visit_types: | for visit_type in visit_types: | ||||
assert_contains(resp, f"<td>{visit_type}</td>") | assert_contains(resp, f"<td>{visit_type}</td>") | ||||
# check request as in production with cache enabled | # check request as in production with cache enabled | ||||
check_http_get_response( | check_http_get_response( | ||||
client, url, status_code=200, server_name=SWH_WEB_SERVER_NAME | client, url, status_code=200, server_name=SWH_WEB_SERVER_NAME | ||||
) | ) | ||||
def test_coverage_view_with_focus(client): | |||||
origins = ( | |||||
listed_origins["origins"] | |||||
+ legacy_origins["origins"] | |||||
+ deposited_origins["origins"] | |||||
) | |||||
focus = choices([o["type"] for o in origins], k=randint(1, 3)) | |||||
# check view gets rendered without errors | |||||
url = reverse("swh-coverage", query_params={"focus": ",".join(focus)}) | |||||
resp = check_html_get_response( | |||||
client, url, status_code=200, template_used="misc/coverage.html" | |||||
) | |||||
# check focused elements | |||||
assert_contains( | |||||
resp, | |||||
"swh-coverage-focus", | |||||
count=len([o for o in origins if o["type"] in focus]), | |||||
) | |||||
# check bootstrap cards are expanded | |||||
assert_contains( | |||||
resp, | |||||
'class="collapse show"', | |||||
count=len(origins), | |||||
) |