From 2f3f3d9d71b2f8fb45e66529d048dfefd96e1f8c Mon Sep 17 00:00:00 2001 From: Mikhail Sandakov Date: Thu, 28 Dec 2023 14:27:39 +0200 Subject: [PATCH] Make sure packageinfo object in leapp json configuration has out_packageset/in_packageset before do anything --- pleskdistup/common/src/leapp_configs.py | 12 +++++++ .../common/tests/leapp_configs_tests.py | 32 +++++++++++++++++-- 2 files changed, 42 insertions(+), 2 deletions(-) diff --git a/pleskdistup/common/src/leapp_configs.py b/pleskdistup/common/src/leapp_configs.py index 195fff8..4ba99e7 100644 --- a/pleskdistup/common/src/leapp_configs.py +++ b/pleskdistup/common/src/leapp_configs.py @@ -238,13 +238,19 @@ def add_repositories_mapping(repofiles: typing.List[str], ignore: typing.List = def set_package_repository(package: str, repository: str, leapp_pkgs_conf_path: str = LEAPP_PKGS_CONF_PATH) -> None: pkg_mapping = None + log.debug("Reconfigure mapping for package '{}' to repository '{}'".format(package, repository)) with open(leapp_pkgs_conf_path, "r") as pkg_mapping_file: pkg_mapping = json.load(pkg_mapping_file) for info in pkg_mapping["packageinfo"]: + if not info["out_packageset"] or not info["out_packageset"]["package"]: + continue + for outpkg in info["out_packageset"]["package"]: if outpkg["name"] == package: + log.debug("Change '{}' package repository in info '{}' -> out packageset '{}'".format(package, info["id"], info["out_packageset"]["set_id"])) outpkg["repository"] = repository + log.debug("Write json into '{}'".format(leapp_pkgs_conf_path)) files.rewrite_json_file(leapp_pkgs_conf_path, pkg_mapping) @@ -263,11 +269,17 @@ class LeappActionType(IntEnum): def set_package_action(package: str, type: LeappActionType, leapp_pkgs_conf_path: str = LEAPP_PKGS_CONF_PATH): pkg_mapping = None + log.debug("Reconfigure action for package '{}' to type '{}'".format(package, type)) with open(leapp_pkgs_conf_path, "r") as pkg_mapping_file: pkg_mapping = json.load(pkg_mapping_file) for info in pkg_mapping["packageinfo"]: + if not info["in_packageset"] or not info["in_packageset"]["package"]: + continue + for inpackage in info["in_packageset"]["package"]: if inpackage["name"] == package: + log.debug("Change '{}' package action in info '{}' -> out packageset '{}'".format(package, info["id"], info["in_packageset"]["set_id"])) info["action"] = type + log.debug("Write json into '{}'".format(leapp_pkgs_conf_path)) files.rewrite_json_file(leapp_pkgs_conf_path, pkg_mapping) diff --git a/pleskdistup/common/tests/leapp_configs_tests.py b/pleskdistup/common/tests/leapp_configs_tests.py index 9948ef9..b4321f3 100644 --- a/pleskdistup/common/tests/leapp_configs_tests.py +++ b/pleskdistup/common/tests/leapp_configs_tests.py @@ -406,6 +406,7 @@ class SetPackageRepositoryTests(unittest.TestCase): INITIAL_JSON = { "packageinfo": [ { + "id": "1", "in_packageset": { "package": [ { @@ -413,6 +414,7 @@ class SetPackageRepositoryTests(unittest.TestCase): "repository": "some-repo", }, ], + "set_id": "1", }, "out_packageset": { "package": [ @@ -421,9 +423,11 @@ class SetPackageRepositoryTests(unittest.TestCase): "repository": "other-repo", }, ], + "set_id": "2", }, }, { + "id": "2", "in_packageset": { "package": [ { @@ -431,6 +435,7 @@ class SetPackageRepositoryTests(unittest.TestCase): "repository": "some-repo", }, ], + "set_id": "3", }, "out_packageset": { "package": [ @@ -439,8 +444,22 @@ class SetPackageRepositoryTests(unittest.TestCase): "repository": "other-repo", }, ], + "set_id": "4", }, - } + }, + { + "id": "3", + "in_packageset": { + "package": [ + { + "name": "empty", + "repository": "no-outpout-repo", + }, + ], + "set_id": "5", + }, + "out_packageset": None, + }, ] } @@ -479,6 +498,7 @@ class SetPackageActionTests(unittest.TestCase): INITIAL_JSON = { "packageinfo": [ { + "id": "1", "action": 1, "in_packageset": { "package": [ @@ -487,9 +507,11 @@ class SetPackageActionTests(unittest.TestCase): "repository": "some-repo", }, ], + "set_id": "1", }, }, { + "id": "2", "action": 4, "in_packageset": { "package": [ @@ -498,8 +520,14 @@ class SetPackageActionTests(unittest.TestCase): "repository": "some-repo", }, ], + "set_id": "2", }, - } + }, + { + "id": "3", + "action": 4, + "in_packageset": None, + }, ] }