diff --git a/plugins/module_utils/image_policy/update.py b/plugins/module_utils/image_policy/update.py index adecc51c2..c2894b664 100644 --- a/plugins/module_utils/image_policy/update.py +++ b/plugins/module_utils/image_policy/update.py @@ -133,7 +133,7 @@ def _build_payloads_to_commit(self): updated_payload.pop("imageName", None) updated_payload.pop("platformPolicies", None) self._payloads_to_commit.append(copy.deepcopy(updated_payload)) - msg = f"self._payloads to commit: {json.dumps(self._payloads_to_commit, indent=4, sort_keys=True)}" + msg = f"self._payloads_to_commit: {json.dumps(self._payloads_to_commit, indent=4, sort_keys=True)}" self.log.debug(msg) def _send_payloads(self): @@ -381,8 +381,9 @@ def payload(self): @payload.setter def payload(self, value): self._verify_payload(value) - self.properties["payloads"] = [value] self.properties["payload"] = value + # ImagePolicyUpdateCommon expects a list of payloads + self.properties["payloads"] = [value] def commit(self): """ @@ -394,11 +395,9 @@ def commit(self): msg += "payload must be set prior to calling commit." self.ansible_module.fail_json(msg, **self.failed_result) - # ImagePolicyUpdateCommon expects a list of payloads - self.payloads = [self.payload] self._build_payloads_to_commit() - if not self._payloads_to_commit: + if len(self._payloads_to_commit) == 0: return self._send_payloads() self._process_responses() diff --git a/tests/unit/modules/dcnm/dcnm_image_policy/fixtures/all_policies_ImagePolicies.json b/tests/unit/modules/dcnm/dcnm_image_policy/fixtures/all_policies_ImagePolicies.json index b11f6fd76..d577b57f9 100644 --- a/tests/unit/modules/dcnm/dcnm_image_policy/fixtures/all_policies_ImagePolicies.json +++ b/tests/unit/modules/dcnm/dcnm_image_policy/fixtures/all_policies_ImagePolicies.json @@ -600,6 +600,7 @@ "rpmimages": null } }, + "test_image_policy_update_00034a": {}, "test_image_policy_update_00035a": { "KR5M": { "agnostic": false, diff --git a/tests/unit/modules/dcnm/dcnm_image_policy/fixtures/payloads_ImagePolicyUpdate.json b/tests/unit/modules/dcnm/dcnm_image_policy/fixtures/payloads_ImagePolicyUpdate.json index dd54ba7ea..a63853490 100644 --- a/tests/unit/modules/dcnm/dcnm_image_policy/fixtures/payloads_ImagePolicyUpdate.json +++ b/tests/unit/modules/dcnm/dcnm_image_policy/fixtures/payloads_ImagePolicyUpdate.json @@ -79,6 +79,17 @@ "policyType": "PLATFORM", "rpmimages": "mtx-grpctunnel-2.1.0.0-10.4.1.lib32_64_n9000" }, + "test_image_policy_update_00034a": { + "agnostic": false, + "epldImgName": "n9000-epld.10.3.2.F.img", + "nxosVersion": "10.3.1_nxos64-cs_64bit", + "packageName": "mtx-openconfig-all-2.0.0.0-10.4.1.src.rpm", + "platform": "N9K", + "policyDescr": "image policy of 10.3(3)F", + "policyName": "FOO", + "policyType": "PLATFORM", + "rpmimages": "mtx-grpctunnel-2.1.0.0-10.4.1.lib32_64_n9000" + }, "test_image_policy_update_00035a": { "agnostic": false, "epldImgName": "n9000-epld.10.2.5.M.img", diff --git a/tests/unit/modules/dcnm/dcnm_image_policy/test_image_policy_update.py b/tests/unit/modules/dcnm/dcnm_image_policy/test_image_policy_update.py index ca722af40..b214bad03 100644 --- a/tests/unit/modules/dcnm/dcnm_image_policy/test_image_policy_update.py +++ b/tests/unit/modules/dcnm/dcnm_image_policy/test_image_policy_update.py @@ -256,6 +256,46 @@ def test_image_policy_update_00033(image_policy_update) -> None: instance.commit() +def test_image_policy_update_00034(monkeypatch, image_policy_update) -> None: + """ + Classes and Methods + - ImagePolicyUpdateCommon + - _build_payloads_to_commit() + - ImagePolicyUpdate + - payload setter + - commit() + + Summary + Verify that commit() returns without doing anything when payloads + is set to a policy that does not exist on the controller. + + Setup + - ImagePolicies().all_policies, is mocked to indicate that no policies + exist on the controller. + - ImagePolicyUpdate().payload is set to a policy (FOO) that does not + exist on the controller + + Test + - ImagePolicyUpdate().commit returns without doing anything + - ImagePolicyUpdate()._payloads_to_commit is an empty list + - ImagePolicyUpdate().changed is False + - ImagePolicyUpdate().failed is False + """ + key = "test_image_policy_update_00034a" + + with does_not_raise(): + instance = image_policy_update + instance.payload = payloads_image_policy_update(key) + + monkeypatch.setattr(instance, "_image_policies", MockImagePolicies(key)) + + with does_not_raise(): + instance.commit() + assert instance._payloads_to_commit == [] + assert instance.changed is False + assert instance.failed is False + + def test_image_policy_update_00035(monkeypatch, image_policy_update) -> None: """ Classes and Methods