Skip to content

Commit

Permalink
[Enhancement][1745][zos_script]Gracefully_fail_when_remote_src_does_n…
Browse files Browse the repository at this point in the history
…ot_exist (#1894)

* First iteration

* Add validation for remote_src

* Add fragment

* Fix changes copy

* Add copy

* Fix copy

* Update changelogs/fragments/1894-Gracefully_fail_when_remote_src_does_not_exist.yml

Co-authored-by: Fernando Flores <[email protected]>

* Update changelogs/fragments/1894-Gracefully_fail_when_remote_src_does_not_exist.yml

Co-authored-by: Fernando Flores <[email protected]>

* Add test cases and fix args

* Fix typos

---------

Co-authored-by: Fernando Flores <[email protected]>
  • Loading branch information
AndreMarcel99 and fernandofloresg authored Jan 30, 2025
1 parent 7d2be87 commit 6e84b32
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
minor_changes:
- zos_script - Adds error message for when remote source does not exist.
(https://github.com/ansible-collections/ibm_zos_core/pull/1894).
11 changes: 10 additions & 1 deletion plugins/modules/zos_script.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-

# Copyright (c) IBM Corporation 2023, 2024
# Copyright (c) IBM Corporation 2023, 2025
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
Expand Down Expand Up @@ -338,6 +338,7 @@ def run_module():
executable = module.params.get('executable')
creates = module.params.get('creates')
removes = module.params.get('removes')
remote_src = module.params.get('remote_src')
script_permissions = None

if creates and os.path.exists(creates):
Expand All @@ -361,6 +362,14 @@ def run_module():
msg='The given chdir {0} does not exist on the system.'.format(chdir)
)

if remote_src and not os.path.exists(script_path):
result = dict(
changed=False,
skipped=True,
msg='File {0} does not exist on the system, skipping script'.format(script_path)
)
module.fail_json(**result)

# Checking if current user has permission to execute the script.
# If not, we'll try to set execution permissions if possible.
if not os.access(script_path, os.X_OK):
Expand Down
84 changes: 82 additions & 2 deletions tests/functional/modules/test_zos_script_func.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@
__metaclass__ = type

from ibm_zos_core.tests.helpers.users import ManagedUserType, ManagedUser


# Using || to concatenate strings without extra spaces.
REXX_SCRIPT_ARGS = """/* REXX */
parse arg 'FIRST=' A ' SECOND=' B
Expand Down Expand Up @@ -480,3 +478,85 @@ def test_user_run_script_from_another_user(ansible_zos_module, z_python_interpre
finally:
hosts.all.file(path=script_path, state="absent")
managed_user.delete_managed_user()


def test_remote_script_does_not_exist(ansible_zos_module):
hosts = ansible_zos_module

script_path = '/tmp/zos_script_test'

msg = 'File {0} does not exist on the system, skipping script'.format(script_path)

zos_script_result = hosts.all.zos_script(
cmd=script_path,
remote_src=True
)

for result in zos_script_result.contacted.values():
assert result.get('changed') is False
assert result.get('failed') is True
assert msg in result.get('msg')


def test_remote_script_with_args_does_not_exist(ansible_zos_module):
hosts = ansible_zos_module

script_path = '/tmp/zos_script_test'
first_arg = 'one'
second_arg = 'two'
args = f'FIRST={first_arg} SECOND={second_arg}'
cmd = f"{script_path} '{args}'"

msg = 'File {0} does not exist on the system, skipping script'.format(script_path)

zos_script_result = hosts.all.zos_script(
cmd=cmd,
remote_src=True
)

for result in zos_script_result.contacted.values():
assert result.get('changed') is False
assert result.get('failed') is True
assert msg in result.get('msg')


def test_rexx_script_with_args_remote_src(ansible_zos_module):
hosts = ansible_zos_module

try:
rexx_script = REXX_SCRIPT_ARGS
local_script = create_local_file(rexx_script, 'rexx')

script_path = '/tmp/zos_script_test_script'
copy_result = hosts.all.zos_copy(
src=local_script,
dest=script_path,
force=True
)
for result in copy_result.contacted.values():
assert result.get('changed') is True

first_arg = 'one'
second_arg = 'two'
args = f'FIRST={first_arg} SECOND={second_arg}'
cmd = f"{script_path} '{args}'"

zos_script_result = hosts.all.zos_script(
cmd=cmd,
remote_src=True
)

for result in zos_script_result.contacted.values():
assert result.get('changed') is True
assert result.get('failed', False) is False
assert result.get('rc') == 0
assert first_arg in result.get('stdout', '')
assert second_arg in result.get('stdout', '')
assert args in result.get('invocation').get('module_args').get('cmd')
assert args in result.get('remote_cmd')
assert result.get('stderr', '') == ''
finally:
if os.path.exists(script_path):
os.remove(script_path)
if os.path.exists(local_script):
os.remove(local_script)

0 comments on commit 6e84b32

Please sign in to comment.