From 10e65aa9748b6f4c7280734c93305782175b59e3 Mon Sep 17 00:00:00 2001 From: Michal Konečný Date: Sep 09 2022 11:11:30 +0000 Subject: Check if the SLA for exists before trying to create it PDC returns 400 if we try to create a SLA for branch that already exists, let's check if the SLA for branch exists first to prevent this error. Signed-off-by: Michal Konečný --- diff --git a/tests/utils/test_pdc.py b/tests/utils/test_pdc.py index 1383460..3800542 100644 --- a/tests/utils/test_pdc.py +++ b/tests/utils/test_pdc.py @@ -120,6 +120,60 @@ class TestPdcGetSla: assert not result +class TestPdcGetSlaForBranch: + """ + Test class for `toddlers.utils.pdc.get_sla_for_branch` function. + """ + + def setup(self): + """ + Setup the PDC module. + """ + pdc._PDC = MagicMock() + + def test_get_sla_for_branch(self): + """ + Assert that correct response is handled. + """ + response = {"count": 1, "results": [{"id": 3}]} + pdc._PDC["component-branch-slas"]._.return_value = response + + sla = "sla" + global_component = "global_component" + branch_name = "branch" + branch_type = "branch_type" + + result = pdc.get_sla_for_branch( + sla_name=sla, + global_component=global_component, + branch=branch_name, + branch_type=branch_type, + ) + + assert result == {"id": 3} + + def test_get_sla_for_branch_not_found(self): + """ + Assert that incorrect response is handled. + """ + response = {"count": 0, "results": []} + pdc._PDC["component-branch-slas"]._.return_value = response + + sla = "sla" + global_component = "global_component" + branch_name = "branch" + branch_type = "branch_type" + + result = pdc.get_sla_for_branch( + sla_name=sla, + global_component=global_component, + branch=branch_name, + branch_type=branch_type, + ) + + assert not result + + class TestPdcNewSLAToBranch: """ Test class for `toddlers.utils.pdc.new_sla_to_branch` function. @@ -131,10 +185,37 @@ class TestPdcNewSLAToBranch: """ pdc._PDC = MagicMock() + def test_new_sla_to_branch_entry_exists(self): + """ + Assert that new SLA is not created if the SLA already exists in PDC. + """ + response = {"count": 1, "results": [{"id": 3}]} + pdc._PDC["component-branch-slas"]._.return_value = response + + sla = "sla" + eol = "2020-01-01" + global_component = "global_component" + branch_name = "branch" + branch_type = "branch_type" + + pdc.new_sla_to_branch( + sla_name=sla, + eol=eol, + global_component=global_component, + branch=branch_name, + branch_type=branch_type, + ) + + # We expect just one call for get function + pdc._PDC["component-branch-slas"]._.assert_called_once() + def test_new_sla_to_branch(self): """ Assert that correct response is handled. """ + response = {"count": 0, "results": []} + pdc._PDC["component-branch-slas"]._.return_value = response + sla = "sla" eol = "2020-01-01" global_component = "global_component" diff --git a/toddlers/utils/pdc.py b/toddlers/utils/pdc.py index 200020d..43afc3f 100644 --- a/toddlers/utils/pdc.py +++ b/toddlers/utils/pdc.py @@ -55,11 +55,45 @@ def get_sla(sla_name: str) -> Optional[dict]: return result +def get_sla_for_branch( + sla_name: str, global_component: str, branch: str, branch_type: str +) -> Optional[dict]: + """ + Get an SLA for branch from PDC. + + Params: + sla_name: Name of the SLA we want to retrieve + global_component: a string of the Global Component the branch + belongs to + branch: a string of the branch name to search for + branch_type: a string of the branch type to search for (e.g. rpm, + module, etc.) + + Returns: + A dictionary of the SLA in PDC or None + """ + query_args = { + "sla": sla_name, + "global_component": global_component, + "branch": branch, + "branch_type": branch_type, + } + pdc = get_pdc() + sla_query = pdc["component-branch-slas"]._(**query_args) + + result = None + + if sla_query["count"] == 1: + result = sla_query["results"][0] + + return result + + def new_sla_to_branch( sla_name: str, eol: str, global_component: str, branch: str, branch_type: str ) -> None: """ - Create a new SLA to branch mapping in PDC. + Create a new SLA to branch mapping in PDC. Does nothing if SLA already exists. Params: sla_name: Name of the SLA to create @@ -68,6 +102,17 @@ def new_sla_to_branch( branch: Name of the branch branch_type: Type of the branch (e.g. rpm, module, etc.) """ + sla = get_sla_for_branch( + sla_name=sla_name, + global_component=global_component, + branch=branch, + branch_type=branch_type, + ) + + # If SLA for branch already exist, don't do anything + if sla: + return + payload = { "sla": sla_name, "eol": eol,