From 416b28a9c6f11d4903b4b4b3f775399a7c1dd74c Mon Sep 17 00:00:00 2001 From: Raphael Lima Date: Mon, 18 Nov 2024 13:17:43 -0300 Subject: [PATCH] Make v prefix optional when specifying to-version This commit updates the kubernetes validator for dcmanager's orchestrator, allowing it to be optionally sent without the v prefix. Test plan: 1. PASS: Create a kube-upgrade strategy without specifying the to-version parameter and verify that the extra args for the version is set as null. 2. PASS: Create a kube-upgrade strategy specifying the kubernetes version as v1.32.2 and verify it is correctly set in the extra args. 3. PASS: Create a kube-upgrade strategy specifying the kubernetes version as 1.32.2 and verify it is set with the v prefix in the extra args, e.g. v1.32.2. 4. PASS: In all test cases above, verify that the strategy is applied successfully. Closes-bug: 2104149 Change-Id: Ic717a5bf76ee2bcd5ab5b66514f0d45ba3ce452e Signed-off-by: Raphael --- .../validators/kubernetes_validator.py | 9 ++++-- .../validators/test_kubernetes_validator.py | 30 ++++++++++++++++--- 2 files changed, 32 insertions(+), 7 deletions(-) diff --git a/distributedcloud/dcmanager/orchestrator/validators/kubernetes_validator.py b/distributedcloud/dcmanager/orchestrator/validators/kubernetes_validator.py index d37623ed5..f4bcf14fb 100644 --- a/distributedcloud/dcmanager/orchestrator/validators/kubernetes_validator.py +++ b/distributedcloud/dcmanager/orchestrator/validators/kubernetes_validator.py @@ -35,6 +35,9 @@ class KubernetesStrategyValidator(StrategyValidationBase): :param payload: strategy request payload """ - return { - consts.EXTRA_ARGS_TO_VERSION: payload.get(consts.EXTRA_ARGS_TO_VERSION), - } + to_version = payload.get(consts.EXTRA_ARGS_TO_VERSION) + + if to_version and not to_version.startswith("v"): + to_version = f"v{to_version}" + + return {consts.EXTRA_ARGS_TO_VERSION: to_version} diff --git a/distributedcloud/dcmanager/tests/unit/orchestrator/validators/test_kubernetes_validator.py b/distributedcloud/dcmanager/tests/unit/orchestrator/validators/test_kubernetes_validator.py index 8be418eeb..36b6c5fa8 100644 --- a/distributedcloud/dcmanager/tests/unit/orchestrator/validators/test_kubernetes_validator.py +++ b/distributedcloud/dcmanager/tests/unit/orchestrator/validators/test_kubernetes_validator.py @@ -42,7 +42,29 @@ class TestKubernetesValidator( def _get_validator(self): return self.validator - def _get_build_extra_args_payload(self): - return { - consts.EXTRA_ARGS_TO_VERSION: "22.09", - } + def _get_build_extra_args_payload(self, version="v1.32.2"): + return {consts.EXTRA_ARGS_TO_VERSION: version} + + def _get_expected_extra_args(self): + return {consts.EXTRA_ARGS_TO_VERSION: "v1.32.2"} + + def test_build_extra_args_succeeds_without_v_prefix(self): + """Test build_extra_args succeeds without v prefix + + When the v prefix is not specified for a kube version, it should be + automatically included. + """ + + payload = self._get_build_extra_args_payload("1.32.2") + expected_extra_args = self._get_expected_extra_args() + + extra_args = self._get_validator().build_extra_args(payload) + + if payload and not expected_extra_args: + expected_extra_args = payload + + if expected_extra_args: + for key, value in expected_extra_args.items(): + self.assertEqual(extra_args[key], value) + else: + self.assertIsNone(extra_args)