Skip to content

Commit 10a5b0c

Browse files
authored
[zos_operator] Update zos_operator test cases with new way to get zoau version (#1956)
* code changes to add version module to tests * get version string for operator_func * changelog file addition * changelog modifications
1 parent 0f87f3b commit 10a5b0c

File tree

4 files changed

+170
-22
lines changed

4 files changed

+170
-22
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
trivial:
2+
3+
- tests/functional/modules/test_zos_operator_func.py- test function test_zos_operator_positive_verbose_blocking
4+
is enabled and condition to version check is operational now.
5+
(https://github.com/ansible-collections/ibm_zos_core/pull/1956).
6+
7+
- tests/functional/modules/test_zos_apf_func.py- version check added for test_add_already_present,
8+
and test_delete_not_present.
9+
(https://github.com/ansible-collections/ibm_zos_core/pull/1956).
10+
11+
- tests/helpers/version.py- New helper file that supports the version fetch,
12+
and operations like validating string and comparing version.
13+
(https://github.com/ansible-collections/ibm_zos_core/pull/1956).

tests/functional/modules/test_zos_apf_func.py

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,9 @@
1212
# limitations under the License.
1313

1414
from __future__ import absolute_import, division, print_function
15-
import re
16-
import pytest
1715
from ibm_zos_core.tests.helpers.dataset import get_tmp_ds_name
1816
from ibm_zos_core.tests.helpers.volumes import Volume_Handler
17+
from ibm_zos_core.tests.helpers.version import get_zoau_version
1918

2019
__metaclass__ = type
2120

@@ -405,17 +404,6 @@ def test_operation_list_with_filter(ansible_zos_module, volumes_with_vvds):
405404
#
406405
# Negative tests
407406
#
408-
def get_zoa_version(ansible_zos_module):
409-
cmd_str = "zoaversion"
410-
version_results = ansible_zos_module.all.shell(cmd=cmd_str)
411-
zoa_version = None
412-
for result in version_results.contacted.values():
413-
output = result.get("stdout")
414-
if output:
415-
match = re.search(r'v(\d+\.\d+\.\d+\.\d+)', output)
416-
if match:
417-
zoa_version = match.group(1)
418-
return zoa_version
419407

420408
def test_add_already_present(ansible_zos_module, volumes_with_vvds):
421409
try:
@@ -455,7 +443,7 @@ def test_add_already_present(ansible_zos_module, volumes_with_vvds):
455443
# RC 0 should be allowed for ZOAU >= 1.3.4,
456444
# in zoau < 1.3.4 -i is not recognized in apfadm
457445
# Return code 16 if ZOAU < 1.2.0 and RC is 8 if ZOAU >= 1.2.0
458-
zoa_version = get_zoa_version(hosts) or "0.0.0.0"
446+
zoa_version = get_zoau_version(hosts) or "0.0.0.0"
459447
rc = result.get("rc")
460448
if zoa_version >= "1.3.4.0":
461449
assert rc == 0
@@ -503,7 +491,7 @@ def test_del_not_present(ansible_zos_module, volumes_with_vvds):
503491
# RC 0 should be allowed for ZOAU >= 1.3.4,
504492
# in zoau < 1.3.4 -i is not recognized in apfadm
505493
# Return code 16 if ZOAU < 1.2.0 and RC is 8 if ZOAU >= 1.2.0
506-
zoa_version = get_zoa_version(hosts) or "0.0.0.0"
494+
zoa_version = get_zoau_version(hosts) or "0.0.0.0"
507495
rc = result.get("rc")
508496
if zoa_version >= "1.3.4.0":
509497
assert rc == 0

tests/functional/modules/test_zos_operator_func.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,7 @@
1818
import os
1919
import yaml
2020
from shellescape import quote
21-
22-
from ibm_zos_core.plugins.module_utils import (
23-
zoau_version_checker,
24-
)
25-
21+
from ibm_zos_core.tests.helpers.version import is_zoau_version_higher_than
2622

2723
__metaclass__ = type
2824

@@ -156,8 +152,8 @@ def test_zos_operator_positive_verbose_with_quick_delay(ansible_zos_module):
156152

157153

158154
def test_zos_operator_positive_verbose_blocking(ansible_zos_module):
159-
if zoau_version_checker.is_zoau_version_higher_than("1.2.4.5"):
160-
hosts = ansible_zos_module
155+
hosts = ansible_zos_module
156+
if is_zoau_version_higher_than(hosts,"1.2.4.5"):
161157
wait_time_s=5
162158
results = hosts.all.zos_operator(
163159
cmd="d u,all", verbose=True, wait_time_s=wait_time_s

tests/helpers/version.py

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
# -*- coding: utf-8 -*-
2+
# Copyright (c) IBM Corporation 2022, 2025
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
# http://www.apache.org/licenses/LICENSE-2.0
7+
# Unless required by applicable law or agreed to in writing, software
8+
# distributed under the License is distributed on an "AS IS" BASIS,
9+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
# See the License for the specific language governing permissions and
11+
# limitations under the License.
12+
13+
14+
from __future__ import absolute_import, division, print_function
15+
16+
import re
17+
18+
__metaclass__ = type
19+
20+
21+
def get_zoau_version(ansible_zos_module):
22+
"""
23+
Fetches the current ZOAU version from the z/OS system using the `zoaversion` command.
24+
25+
This function runs the `zoaversion` command on the z/OS system and parses the output
26+
to extract the version number in the format `vX.Y.Z.W`, where X, Y, Z, and W are numerical digits.
27+
28+
Parameters
29+
----------
30+
ansible_zos_module : ansible.module_utils.basic.AnsibleModule
31+
The Ansible z/OS module that provides the `all.shell()` method to run shell commands.
32+
33+
Returns
34+
-------
35+
str
36+
The ZOAU version number (e.g., "1.2.3.4") extracted from the command output.
37+
Returns `"1.2.0"` if no valid version is found.
38+
"""
39+
cmd_str = "zoaversion"
40+
version_results = ansible_zos_module.all.shell(cmd=cmd_str)
41+
zoa_version = "0.0.0" # Default version if not found
42+
43+
# Iterate through all contacted hosts and check the result output
44+
for result in version_results.contacted.values():
45+
output = result.get("stdout")
46+
if output:
47+
# Search for the version in the format "vX.Y.Z.W"
48+
match = re.search(r'v?(\d+\.\d+\.\d+(?:\.\d+)?)', output)
49+
if match:
50+
zoa_version = match.group(1)
51+
52+
return zoa_version
53+
54+
def is_zoau_version_higher_than(ansible_zos_module,min_version_str):
55+
"""Reports back if ZOAU version is high enough.
56+
57+
Parameters
58+
----------
59+
min_version_str : str
60+
The minimal desired ZOAU version '#.#.#'.
61+
62+
Returns
63+
-------
64+
bool
65+
Whether ZOAU version found was high enough.
66+
"""
67+
if is_valid_version_string(min_version_str):
68+
# check zoau version on system (already a list)
69+
system_version_list = get_zoau_version_str(ansible_zos_module)
70+
71+
# convert input to list format
72+
min_version_list = min_version_str.split('.')
73+
74+
# convert list of strs to list of ints
75+
system_version_list = [int(i) for i in system_version_list]
76+
min_version_list = [int(i) for i in min_version_list]
77+
78+
# compare major
79+
if system_version_list[0] > min_version_list[0]:
80+
return True
81+
if system_version_list[0] < min_version_list[0]:
82+
return False
83+
84+
# majors are equal, compare minor
85+
if system_version_list[1] > min_version_list[1]:
86+
return True
87+
if system_version_list[1] < min_version_list[1]:
88+
return False
89+
90+
# majors and minors are equal, compare patch
91+
if system_version_list[2] > min_version_list[2]:
92+
return True
93+
if system_version_list[2] < min_version_list[2]:
94+
return False
95+
96+
# check for a 4th level if system and min_version provided it
97+
if len(system_version_list) < 4:
98+
system_version_list.append(0)
99+
if len(min_version_list) < 4:
100+
min_version_list.append(0)
101+
102+
# return result of comparison of 4th levels.
103+
return system_version_list[3] >= min_version_list[3]
104+
105+
# invalid version string
106+
return False
107+
108+
109+
def is_valid_version_string(version_str):
110+
"""Reports back if string is in proper version format. Expected format is a
111+
series of numbers (major) followed by a dot(.) followed by another
112+
series of numbers (minor) followed by another dot(.) followed by a
113+
series of numbers (patch) i.e. "#.#.#" where '#' can be any integer.
114+
There is a provision for a 4th level to this eg "v1.2.0.1".
115+
116+
Parameters
117+
----------
118+
min_version_str : str
119+
String to be verified is in correct format.
120+
121+
Returns
122+
-------
123+
bool
124+
Whether provided str is in correct format.
125+
"""
126+
127+
# split string into [major, minor, patch]
128+
version_list = version_str.split('.')
129+
130+
# check each element in list isnumeric()
131+
for ver in version_list:
132+
if not ver.isnumeric():
133+
return False
134+
135+
return True
136+
137+
def get_zoau_version_str(ansible_zos_module):
138+
"""Attempts to call zoaversion on target and parses out version string.
139+
140+
Returns
141+
-------
142+
Union[int, int, int]
143+
ZOAU version found in format [#,#,#]. There is a
144+
provision for a 4th level eg "v1.2.0.1".
145+
"""
146+
ZOAU_API_VERSION = get_zoau_version(ansible_zos_module)
147+
version_list = (
148+
ZOAU_API_VERSION.split('.')
149+
)
150+
151+
return version_list

0 commit comments

Comments
 (0)