From 8d7dccc54a4b92b3a1c53aeb95e9d45f654731bb Mon Sep 17 00:00:00 2001 From: Antoine Lambert Date: Thu, 29 Jun 2023 17:45:03 +0200 Subject: [PATCH] opam: Fix 'opam init' error when relisting an opam instance When relisting an opam instance and the opam root directory is already populated, the '--set-default' parameter must be provided otherwise the following error is reported: No switch is currently set. Please use 'opam switch' to set or install a switch Related to swh/infra/sysadm-environment#4971. --- swh/lister/opam/lister.py | 1 + swh/lister/opam/tests/test_lister.py | 46 +++++++++++++++++----------- 2 files changed, 29 insertions(+), 18 deletions(-) diff --git a/swh/lister/opam/lister.py b/swh/lister/opam/lister.py index dc3efec..2701c96 100644 --- a/swh/lister/opam/lister.py +++ b/swh/lister/opam/lister.py @@ -156,6 +156,7 @@ def opam_init(opam_root: str, instance: str, url: str, env: Dict[str, Any]) -> N opam(), "repository", "add", + "--set-default", "--root", opam_root, instance, diff --git a/swh/lister/opam/tests/test_lister.py b/swh/lister/opam/tests/test_lister.py index 04efd23..40c37e8 100644 --- a/swh/lister/opam/tests/test_lister.py +++ b/swh/lister/opam/tests/test_lister.py @@ -163,30 +163,40 @@ def test_opam_multi_instance(datadir, swh_scheduler, tmp_path, mocker): # Patch opam_init to use the local directory mocker.patch("swh.lister.opam.lister.opam_init", side_effect=mock_opam_init) - lister = OpamLister( - swh_scheduler, - url=instance_url, - instance="fake", - opam_root=mkdtemp(dir=tmp_path, prefix="swh_opam_lister"), - ) + opam_root = mkdtemp(dir=tmp_path, prefix="swh_opam_lister") - stats = lister.run() + def check_listing(): + lister = OpamLister( + swh_scheduler, + url=instance_url, + instance="fake", + opam_root=opam_root, + ) - assert stats.pages == 4 - assert stats.origins == 4 + stats = lister.run() - scheduler_origins = swh_scheduler.get_listed_origins(lister.lister_obj.id).results + assert stats.pages == 4 + assert stats.origins == 4 - expected_urls = [ - f"opam+{instance_url}/packages/agrid/", - f"opam+{instance_url}/packages/calculon/", - f"opam+{instance_url}/packages/directories/", - f"opam+{instance_url}/packages/ocb/", - ] + scheduler_origins = swh_scheduler.get_listed_origins( + lister.lister_obj.id + ).results - result_urls = [origin.url for origin in scheduler_origins] + expected_urls = [ + f"opam+{instance_url}/packages/agrid/", + f"opam+{instance_url}/packages/calculon/", + f"opam+{instance_url}/packages/directories/", + f"opam+{instance_url}/packages/ocb/", + ] - assert expected_urls == result_urls + result_urls = [origin.url for origin in scheduler_origins] + + assert expected_urls == result_urls + + # first listing + check_listing() + # check second listing works as expected + check_listing() def test_opam_init_failure(swh_scheduler, mocker, tmp_path):