Skip to content

Commit

Permalink
ImageUpgradeCommon: add unit tests
Browse files Browse the repository at this point in the history
Brings ImageUpgradeCommon unit test coverage to 99%

- Updated setters which expect an int() to test first for bool().

- Added unit tests for:
  - changed
  - diff
  - failed
  - response_current
  - response
  - result_current
  - result
  - send_interval
  - timeout
  - unit_test
  • Loading branch information
allenrobel committed Feb 25, 2024
1 parent c1ff802 commit 9bb1b50
Show file tree
Hide file tree
Showing 2 changed files with 379 additions and 24 deletions.
30 changes: 19 additions & 11 deletions plugins/module_utils/image_upgrade/image_upgrade_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ def changed(self, value):
method_name = inspect.stack()[0][3]
if not isinstance(value, bool):
msg = f"{self.class_name}.{method_name}: "
msg += f"changed must be a bool. Got {value}"
msg += f"{method_name} must be a bool. Got {value}"
self.module.fail_json(msg)
self.properties["changed"] = value

Expand All @@ -279,7 +279,7 @@ def diff(self, value):
method_name = inspect.stack()[0][3]
if not isinstance(value, dict):
msg = f"{self.class_name}.{method_name}: "
msg += f"diff must be a dict. Got {value}"
msg += f"{method_name} must be a dict. Got {value}"
self.module.fail_json(msg)
self.properties["diff"].append(value)

Expand All @@ -297,7 +297,7 @@ def failed(self, value):
method_name = inspect.stack()[0][3]
if not isinstance(value, bool):
msg = f"{self.class_name}.{method_name}: "
msg += f"failed must be a bool. Got {value}"
msg += f"{method_name} must be a bool. Got {value}"
self.module.fail_json(msg)
self.properties["failed"] = value

Expand All @@ -316,7 +316,7 @@ def response_current(self, value):
method_name = inspect.stack()[0][3]
if not isinstance(value, dict):
msg = f"{self.class_name}.{method_name}: "
msg += "instance.response_current must be a dict. "
msg += f"{method_name} must be a dict. "
msg += f"Got {value}."
self.module.fail_json(msg, **self.failed_result)
self.properties["response_current"] = value
Expand All @@ -336,7 +336,7 @@ def response(self, value):
method_name = inspect.stack()[0][3]
if not isinstance(value, dict):
msg = f"{self.class_name}.{method_name}: "
msg += "instance.response must be a dict. "
msg += f"{method_name} must be a dict. "
msg += f"Got {value}."
self.module.fail_json(msg, **self.failed_result)
self.properties["response"].append(value)
Expand Down Expand Up @@ -367,7 +367,7 @@ def result(self, value):
method_name = inspect.stack()[0][3]
if not isinstance(value, dict):
msg = f"{self.class_name}.{method_name}: "
msg += "instance.result must be a dict. "
msg += f"{method_name} must be a dict. "
msg += f"Got {value}."
self.module.fail_json(msg, **self.failed_result)
self.properties["result"].append(value)
Expand All @@ -387,7 +387,7 @@ def result_current(self, value):
method_name = inspect.stack()[0][3]
if not isinstance(value, dict):
msg = f"{self.class_name}.{method_name}: "
msg += "instance.result_current must be a dict. "
msg += f"{method_name} must be a dict. "
msg += f"Got {value}."
self.module.fail_json(msg, **self.failed_result)
self.properties["result_current"] = value
Expand All @@ -404,9 +404,13 @@ def send_interval(self):
@send_interval.setter
def send_interval(self, value):
method_name = inspect.stack()[0][3]
msg = f"{self.class_name}.{method_name}: "
msg += f"{method_name} must be an integer. Got {value}."
# isinstance(False, int) returns True, so we need first
# to test for this and fail_json specifically for bool values.
if isinstance(value, bool):
self.module.fail_json(msg, **self.failed_result)
if not isinstance(value, int):
msg = f"{self.class_name}.{method_name}: "
msg += f"{method_name} must be an int(). Got {value}."
self.module.fail_json(msg, **self.failed_result)
self.properties["send_interval"] = value

Expand All @@ -422,9 +426,13 @@ def timeout(self):
@timeout.setter
def timeout(self, value):
method_name = inspect.stack()[0][3]
msg = f"{self.class_name}.{method_name}: "
msg += f"{method_name} must be an integer. Got {value}."
# isinstance(False, int) returns True, so we need first
# to test for this and fail_json specifically for bool values.
if isinstance(value, bool):
self.module.fail_json(msg, **self.failed_result)
if not isinstance(value, int):
msg = f"{self.class_name}.{method_name}: "
msg += f"{method_name} must be an int(). Got {value}."
self.module.fail_json(msg, **self.failed_result)
self.properties["timeout"] = value

Expand Down
Loading

0 comments on commit 9bb1b50

Please sign in to comment.