Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Enhancement][1745][zos_script]Gracefully_fail_when_remote_src_does_not_exist #1894

Merged
merged 11 commits into from
Jan 30, 2025

Conversation

AndreMarcel99
Copy link
Collaborator

@AndreMarcel99 AndreMarcel99 commented Jan 24, 2025

SUMMARY

Add verification to remote_src and file does not exist with error message

Fixes #1745

ISSUE TYPE
  • Enhancement Pull Request
ADDITIONAL INFORMATION

Here is the new error message

{'failed': True, 'changed': False, 'msg': 'File /tmp/zos_script_test_script does not exists on the system, skipping script', 'invocation': {'module_args': {'chdir': None, 'cmd': '/tmp/zos_script_test_script', 'creates': None, 'executable': None, 'remote_src': True, 'removes': None, 'use_template': False, 'template_parameters': None}}, '_ansible_no_log': None}

Captura de pantalla 2025-01-24 a la(s) 12 15 14 p m
Captura de pantalla 2025-01-24 a la(s) 12 14 59 p m

@AndreMarcel99 AndreMarcel99 changed the title Enhacement/1745/gracefully fail source [Enhancement][1745][zos_script]Gracefully_fail_when_remote_src_does_not_exist Jan 24, 2025
@AndreMarcel99 AndreMarcel99 marked this pull request as ready for review January 24, 2025 18:17
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.

Looks like if statement logic needs a revamp.

plugins/modules/zos_script.py Show resolved Hide resolved
@@ -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(cmd_str):
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is not taking into account when the cmd_str contains args. Modified the following test case

def test_rexx_script_with_args(ansible_zos_module):
    hosts = ansible_zos_module

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

        first_arg = 'one'
        second_arg = 'two'
        args = f'FIRST={first_arg} SECOND={second_arg}'
        script_path = '/test.rexx'
        cmd = f"{script_path} '{args}'"

        hosts.all.shell(cmd="echo '{0}' '{1}' ".format(REXX_SCRIPT_ARGS, script_path))
        zos_script_result = hosts.all.zos_script(
            cmd=cmd,
            remote_src=True,
        )

        for result in zos_script_result.contacted.values():
            print(result)
            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', '')
            # Making sure the action plugin passed every argument to the module.
            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)

and fails with error message:

 {'failed': True, 'changed': False, 'msg': "File /test.rexx 'FIRST=one SECOND=two' does not exists on the system, skipping script", 'invocation': {'module_args': {'chdir': None, 'cmd': "/test.rexx 'FIRST=one SECOND=two'", 'creates': None, 'executable': None, 'remote_src': True, 'removes': None, 'use_template': False, 'template_parameters': None}}, '_ansible_no_log': False}

This was not discovered in functional tests because there is no functional tests that test scripts with args when remote_src is true.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Good catch

@@ -480,3 +480,20 @@ 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_doest_not_exist(ansible_zos_module):
Copy link
Collaborator

Choose a reason for hiding this comment

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

Just above comment please test with args too.

@AndreMarcel99
Copy link
Collaborator Author

Captura de pantalla 2025-01-24 a la(s) 7 26 05 p m Captura de pantalla 2025-01-24 a la(s) 7 26 17 p m

result = dict(
changed=False,
skipped=True,
msg='File {0} does not exists on the system, skipping script'.format(script_path)
Copy link
Collaborator

Choose a reason for hiding this comment

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

A tiny typo: exist instead of exists

@AndreMarcel99 AndreMarcel99 requested a review from rexemin January 29, 2025 17:51
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.

Great! You even added a test case for testing a script with args and remote.

@fernandofloresg fernandofloresg merged commit 6e84b32 into dev Jan 30, 2025
5 checks passed
@fernandofloresg fernandofloresg deleted the enhacement/1745/Gracefully_fail_source branch January 30, 2025 02:29
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] [zos_script] Gracefully fail when source is not found
4 participants