Skip to content

Commit

Permalink
100% unit test coverage for update.py
Browse files Browse the repository at this point in the history
  • Loading branch information
allenrobel committed Feb 16, 2024
1 parent f6b2ef9 commit 8cf74c4
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 5 deletions.
9 changes: 4 additions & 5 deletions plugins/module_utils/image_policy/update.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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):
"""
Expand All @@ -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()
Original file line number Diff line number Diff line change
Expand Up @@ -600,6 +600,7 @@
"rpmimages": null
}
},
"test_image_policy_update_00034a": {},
"test_image_policy_update_00035a": {
"KR5M": {
"agnostic": false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 8cf74c4

Please sign in to comment.