Skip to content

Conversation

@mayankmani-sde
Copy link
Collaborator

@mayankmani-sde mayankmani-sde commented Oct 17, 2025

SUMMARY
  • Added and updated unit tests for validate_dependencies to cover both success and failure scenarios.
  • Introduced version.py to define the collection version (__version__) for use in dependency checks.
  • Unit tests use FakeModule and monkey-patched version fetchers for deterministic, environment-independent testing.
  • Version fetchers (get_python_version, get_zos_version, get_zoau_version) are monkey patched to simulate different Python, z/OS, and ZOAU versions.
  • Ensures environment-independent, deterministic testing of dependency checks.
    Rationale:
  • Improves test coverage of validate_dependencies across supported and unsupported configurations.
  • Provides environment-independent, reproducible tests without relying on actual system versions.
  • Enhances readability and maintainability for future developers by clearly simulating runtime conditions.

#2133

ISSUE TYPE
  • Feature Pull Request
COMPONENT NAME
ADDITIONAL INFORMATION
unit/test_dependency_checker.py::test_validate_dependencies_success ✓                                                                                                            20% ██        
 unit/test_dependency_checker.py::test_validate_dependencies_python_upper ✓                                                                                                       40% ████      
 unit/test_dependency_checker.py::test_validate_dependencies_python_out_of_range ✓                                                                                                60% ██████    
 unit/test_dependency_checker.py::test_validate_dependencies_zos_out_of_range ✓                                                                                                   80% ████████  
 unit/test_dependency_checker.py::test_validate_dependencies_zoau_mismatch ✓                                                                                                     100% ██████████

Results (0.02s):
       5 passed

# Compatibility Matrix
# ------------------------------
COMPATIBILITY_MATRIX = [
{"zoau_version": "1.3.5", "python_version": "3.12", "galaxy_core_version": "1.12.0", "zos_range": "2.5-3.1"},
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it will be easier to use min_zos_version and max_zos_version instead of zos_range and then having to do a string manipulation, same for things that allow ranges like python.

Instead of galaxy_core_version I would use collection_version since we know this is our collection.

Suggested change
{"zoau_version": "1.3.5", "python_version": "3.12", "galaxy_core_version": "1.12.0", "zos_range": "2.5-3.1"},
{"zoau_version": "1.3.5", "python_version": "3.12", "collection_version": "1.12.0", "min_zos_version": "2.5", "max_zos_version": "3.1"},

# Version Fetchers
# ------------------------------
def get_zoau_version(module):
output = run_command(module, "zoaversion")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to run zoaversion command, as we spoke before better to use the zoau_dependency_checker functionality.

try:
    from zoautil_py import ZOAU_API_VERSION
except Exception:
    ZOAU_API_VERSION = "1.4.0"

return f"{int(match_ver.group(1))}.{int(match_rel.group(1))}"
return None

def get_galaxy_core_version():
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we are in the managed node we cannot access the collection version using ansible-galaxy command, this is what we were talking about the other day, is it possible to get through an import by fetching the valye from meta/ibm_zos_core?

def run_command(module, cmd):
rc, out, err = module.run_command(cmd)
if rc != 0:
module.fail_json(msg=f"Command failed: {cmd}", stdout=out, stderr=err)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This might be problematic, rather than catching the error here I think we should do a try in the main dependency validation, and throw an Exception from there saying something like "The dependencies could not be fetched correctly. " and maybe add some details about the dependencias that were fetched successfully

return f"{sys.version_info.major}.{sys.version_info.minor}.0"

def get_zos_version(module):
output = run_command(module, "zinfo -t sys -j")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same, better to use ZOAU's zsystem python API https://www.ibm.com/docs/en/zoau/1.3.x?topic=apis-zsystem we try to avoid using commands when an API is available unless there is a bug in the python API

except (ValueError, TypeError):
module.fail_json(msg=f"Unable to parse z/OS version: {zos_version}")

for row in COMPATIBILITY_MATRIX:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of iterating over the compatibility matrix we can use the core collection version as the key to find the correct row and only check the compatibility for the current version

@@ -0,0 +1,43 @@
import pytest
from ansible_collections.ibm.ibm_zos_core.plugins.module_utils import dependency_checker as dc
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For readability, would recommend this to be kept as dependency_checker


def test_validate_dependencies_success(monkeypatch):
# Monkeypatch fetchers to match the compatibility matrix exactly
monkeypatch.setattr(dc, "get_zoau_version", lambda mod: "1.3.5")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't know about this fixture, please add a comment explaining what this does so that next person who sees this doesn't have to spend much time researching why it was implemented this way and not with simple dictionaries as inputs

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Resolved

@mayankmani-sde mayankmani-sde marked this pull request as ready for review October 21, 2025 19:13
@mayankmani-sde mayankmani-sde requested review from fernandofloresg and removed request for fernandofloresg October 21, 2025 19:43
Copy link
Collaborator

@fernandofloresg fernandofloresg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Requested few changes

COMPATIBILITY_MATRIX = {
"2.0.0": {
"zoau_version": "1.4.0",
"min_python_version": "3.12",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can pick the python versions from this page:

https://access.redhat.com/support/policy/updates/ansible-automation-platform#phases

Image

Right now we are officially supporting 2.15 trhough 2.18

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Image

return ZOAU_API_VERSION
except ImportError:
if module:
module.fail_json(msg=" ZOAU Python API not found.")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would change this error message.

Suggested change
module.fail_json(msg=" ZOAU Python API not found.")
module.fail_json(msg="Unable to import ZOAU. Please check PYTHONPATH, LIBPATH, ZOAU_HOME and PATH environment variables.")

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Resolved

COMPATIBILITY_MATRIX = {
"2.0.0": [
{"zoau_version": "1.4.0", "min_python_version": "3.12", "max_python_version": "3.13", "min_zos_version": 2.5, "max_zos_version": 3.1},
{"zoau_version": "1.4.1", "min_python_version": "3.12", "max_python_version": "3.13", "min_zos_version": 2.5, "max_zos_version": 3.1},
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think there need to add different zoau_version objects here, we can have only min_zoau_version and validate from there

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

like min_zoau_version: 1.4.0, if current zoau version is 1.4.1 then is ok

{"zoau_version": "1.4.2", "min_python_version": "3.12", "max_python_version": "3.13", "min_zos_version": 2.5, "max_zos_version": 3.1},
],
"2.1.0": [
{"zoau_version": "1.4.1", "min_python_version": "3.12", "max_python_version": "3.13", "min_zos_version": 2.5, "max_zos_version": 3.1},
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the version compatibilities of python you can refer to this

Image Image

We are currently in AAP 2.3, 2.4 so ansible 2.15-2.18, python version 3.11-3.13 is the current supported version, but this check should be a warning only.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry supported Python version in the managed node is indeed 3.12

# Ensure all versions are available
if not all([zoau_version, zos_version_str, collection_version]):
module.fail_json(
msg=" Missing one or more required versions (ZOAU, Python, z/OS, Collection Version)."
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This mentions python but is not checked in line 100. Also, lets change the message:

Suggested change
msg=" Missing one or more required versions (ZOAU, Python, z/OS, Collection Version)."
msg="Unable to fetch one or more required dependencies. Depedencies checked are ZOAU, Python, z/OS."

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Resolved

"""Fetch z/OS version using ZOAU Python API and return a string like '2.5'."""
if zsystem is None:
if module:
module.fail_json(msg="ZOAU Python API not found.")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
module.fail_json(msg="ZOAU Python API not found.")
module.fail_json(msg="Unable to import ZOAU. Please check PYTHONPATH, LIBPATH, ZOAU_HOME and PATH environment variables.")

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Resolved

{"zoau_version": "1.4.2", "min_python_version": "3.12", "max_python_version": "3.13", "min_zos_version": 2.5, "max_zos_version": 3.1},
],
"2.1.0": [
{"zoau_version": "1.4.1", "min_python_version": "3.12", "max_python_version": "3.13", "min_zos_version": 2.5, "max_zos_version": 3.1},
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry supported Python version in the managed node is indeed 3.12

Copy link
Collaborator

@fernandofloresg fernandofloresg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Partial Review, need to go back an check the messages

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Enhancement][2.0] Implement dependency checker for all modules in the collection

3 participants