|
15 | 15 |
|
16 | 16 | from __future__ import absolute_import, division, print_function
|
17 | 17 |
|
| 18 | +import os |
18 | 19 | import pytest
|
19 | 20 | import tempfile
|
20 | 21 | from tempfile import mkstemp
|
21 | 22 | from ibm_zos_core.tests.helpers.dataset import get_tmp_ds_name
|
| 23 | +import yaml |
| 24 | +from shellescape import quote |
| 25 | +import subprocess |
22 | 26 |
|
23 | 27 | __metaclass__ = type
|
24 | 28 |
|
|
33 | 37 |
|
34 | 38 | USS_FORMATS = ['tar', 'gz', 'bz2', 'zip', 'pax']
|
35 | 39 |
|
| 40 | +PLAYBOOK_ASYNC_TEST = """- hosts: zvm |
| 41 | + collections: |
| 42 | + - ibm.ibm_zos_core |
| 43 | + gather_facts: False |
| 44 | + environment: |
| 45 | + _BPXK_AUTOCVT: "ON" |
| 46 | + ZOAU_HOME: "{0}" |
| 47 | + PYTHONPATH: "{0}/lib/{2}" |
| 48 | + LIBPATH: "{0}/lib:{1}/lib:/lib:/usr/lib:." |
| 49 | + PATH: "{0}/bin:/bin:/usr/lpp/rsusr/ported/bin:/var/bin:/usr/lpp/rsusr/ported/bin:/usr/lpp/java/java180/J8.0_64/bin:{1}/bin:" |
| 50 | + _CEE_RUNOPTS: "FILETAG(AUTOCVT,AUTOTAG) POSIX(ON)" |
| 51 | + _TAG_REDIR_ERR: "txt" |
| 52 | + _TAG_REDIR_IN: "txt" |
| 53 | + _TAG_REDIR_OUT: "txt" |
| 54 | + LANG: "C" |
| 55 | + PYTHONSTDINENCODING: "cp1047" |
| 56 | +
|
| 57 | + tasks: |
| 58 | + - name: Execute script in async mode. |
| 59 | + ibm.ibm_zos_core.zos_unarchive: |
| 60 | + src: {3} |
| 61 | + format: |
| 62 | + name: {4} |
| 63 | + remote_src: True |
| 64 | + async: 45 |
| 65 | + poll: 0 |
| 66 | + register: job_task |
| 67 | +
|
| 68 | + - name: Query async task. |
| 69 | + async_status: |
| 70 | + jid: "{{{{ job_task.ansible_job_id }}}}" |
| 71 | + register: job_result |
| 72 | + until: job_result.finished |
| 73 | + retries: 20 |
| 74 | + delay: 5 |
| 75 | +""" |
| 76 | + |
| 77 | +INVENTORY_ASYNC_TEST = """all: |
| 78 | + hosts: |
| 79 | + zvm: |
| 80 | + ansible_host: {0} |
| 81 | + ansible_ssh_private_key_file: {1} |
| 82 | + ansible_user: {2} |
| 83 | + ansible_python_interpreter: {3}""" |
| 84 | + |
36 | 85 | def set_uss_test_env(ansible_zos_module, test_files):
|
37 | 86 | for key, value in test_files.items():
|
38 | 87 | ansible_zos_module.all.shell(
|
@@ -1185,3 +1234,80 @@ def test_gdg_unarchive(ansible_zos_module, dstype, format):
|
1185 | 1234 | finally:
|
1186 | 1235 | hosts.all.shell(cmd=f'drm "{HLQ}.*"')
|
1187 | 1236 |
|
| 1237 | +@pytest.mark.uss |
| 1238 | +def test_zos_unarchive_async(ansible_zos_module, get_config): |
| 1239 | + try: |
| 1240 | + # Load environment details from the config file |
| 1241 | + with open(get_config, 'r') as file: |
| 1242 | + environment = yaml.safe_load(file) |
| 1243 | + |
| 1244 | + ssh_key = environment["ssh_key"] |
| 1245 | + hosts = environment["host"].upper() |
| 1246 | + user = environment["user"].upper() |
| 1247 | + python_path = environment["python_path"] |
| 1248 | + cut_python_path = python_path[:python_path.find('/bin')].strip() |
| 1249 | + zoau = environment["environment"]["ZOAU_ROOT"] |
| 1250 | + python_version = cut_python_path.split('/')[2] |
| 1251 | + archive_format = USS_FORMATS[0] |
| 1252 | + |
| 1253 | + # Create a temporary archive file for testing |
| 1254 | + hosts_zos = ansible_zos_module |
| 1255 | + hosts_zos.all.file(path=f"{USS_TEMP_DIR}", state="absent") |
| 1256 | + hosts_zos.all.file(path=USS_TEMP_DIR, state="directory") |
| 1257 | + set_uss_test_env(hosts_zos, USS_TEST_FILES) |
| 1258 | + dest = f"{USS_TEMP_DIR}/archive.{archive_format}" |
| 1259 | + archive_result = hosts_zos.all.zos_archive(src=list(USS_TEST_FILES.keys()), |
| 1260 | + dest=dest, |
| 1261 | + format=dict( |
| 1262 | + name=archive_format |
| 1263 | + )) |
| 1264 | + # remove files |
| 1265 | + for file in USS_TEST_FILES.keys(): |
| 1266 | + hosts_zos.all.file(path=file, state="absent") |
| 1267 | + |
| 1268 | + # Create temporary playbook and inventory files |
| 1269 | + playbook = tempfile.NamedTemporaryFile(delete=True) |
| 1270 | + inventory = tempfile.NamedTemporaryFile(delete=True) |
| 1271 | + |
| 1272 | + os.system("echo {0} > {1}".format( |
| 1273 | + quote(PLAYBOOK_ASYNC_TEST.format( |
| 1274 | + zoau, |
| 1275 | + cut_python_path, |
| 1276 | + python_version, |
| 1277 | + dest, |
| 1278 | + archive_format, |
| 1279 | + )), |
| 1280 | + playbook.name |
| 1281 | + )) |
| 1282 | + |
| 1283 | + os.system("echo {0} > {1}".format( |
| 1284 | + quote(INVENTORY_ASYNC_TEST.format( |
| 1285 | + hosts, |
| 1286 | + ssh_key, |
| 1287 | + user, |
| 1288 | + python_path |
| 1289 | + )), |
| 1290 | + inventory.name |
| 1291 | + )) |
| 1292 | + |
| 1293 | + # Run the Ansible playbook |
| 1294 | + command = "ansible-playbook -i {0} {1}".format( |
| 1295 | + inventory.name, |
| 1296 | + playbook.name |
| 1297 | + ) |
| 1298 | + |
| 1299 | + result = subprocess.run( |
| 1300 | + command, |
| 1301 | + capture_output=True, |
| 1302 | + shell=True, |
| 1303 | + timeout=120, |
| 1304 | + encoding='utf-8' |
| 1305 | + ) |
| 1306 | + |
| 1307 | + # Assertions to validate the result |
| 1308 | + assert result.returncode == 0 |
| 1309 | + assert "ok=2" in result.stdout |
| 1310 | + assert "changed=2" in result.stdout |
| 1311 | + assert result.stderr == "" |
| 1312 | + finally: |
| 1313 | + hosts_zos.all.file(path=f"{USS_TEMP_DIR}", state="absent") |
0 commit comments