From 1b9336a9d2524fad9c02432496cceaffaa735b04 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Wed, 26 Jun 2024 08:24:20 +0200 Subject: Adding upstream version 2.17.1. Signed-off-by: Daniel Baumann --- .../integration/targets/any_errors_fatal/80981.yml | 2 +- test/integration/targets/any_errors_fatal/runme.sh | 8 ++-- .../unqualified_and_collections_kw.yml | 6 ++- .../targets/json_cleanup/library/bad_json | 2 +- .../json_cleanup/module_output_cleaning.yml | 7 +++ .../targets/module_defaults/tasks/main.yml | 11 ++++- .../library/ansible_basic_tests.ps1 | 31 ++++++++++++ test/integration/targets/shell/meta/main.yml | 3 ++ .../targets/shell/tasks/command-building.yml | 53 +++++++++++++++++++++ test/integration/targets/shell/tasks/main.yml | 2 + .../shell/test-command-building-playbook.yml | 4 ++ .../targets/win_exec_wrapper/tasks/main.yml | 55 ++++++++++++++++++---- test/lib/ansible_test/_internal/pypi_proxy.py | 2 +- test/units/playbook/test_block.py | 4 ++ test/units/playbook/test_helpers.py | 5 +- test/units/utils/display/test_broken_cowsay.py | 3 +- 16 files changed, 174 insertions(+), 24 deletions(-) create mode 100644 test/integration/targets/shell/meta/main.yml create mode 100644 test/integration/targets/shell/tasks/command-building.yml create mode 100644 test/integration/targets/shell/test-command-building-playbook.yml (limited to 'test') diff --git a/test/integration/targets/any_errors_fatal/80981.yml b/test/integration/targets/any_errors_fatal/80981.yml index 51cf8df..5654aa1 100644 --- a/test/integration/targets/any_errors_fatal/80981.yml +++ b/test/integration/targets/any_errors_fatal/80981.yml @@ -11,7 +11,7 @@ rescue: - name: Rescues both hosts debug: - msg: rescue + msg: rescuedd - name: You can recover from fatal errors by adding a rescue section to the block. debug: msg: recovered diff --git a/test/integration/targets/any_errors_fatal/runme.sh b/test/integration/targets/any_errors_fatal/runme.sh index 58f0ddf..55381a7 100755 --- a/test/integration/targets/any_errors_fatal/runme.sh +++ b/test/integration/targets/any_errors_fatal/runme.sh @@ -15,8 +15,6 @@ if [ "${res}" -eq 0 ] ; then exit 1 fi -set -ux - ansible-playbook -i inventory "$@" always_block.yml | tee out.txt | grep 'any_errors_fatal_always_block_start' res=$? cat out.txt @@ -25,8 +23,6 @@ if [ "${res}" -ne 0 ] ; then exit 1 fi -set -ux - for test_name in test_include_role test_include_tasks; do ansible-playbook -i inventory "$@" -e test_name=$test_name 50897.yml | tee out.txt | grep 'any_errors_fatal_this_should_never_be_reached' res=$? @@ -36,6 +32,8 @@ for test_name in test_include_role test_include_tasks; do fi done +set -e + ansible-playbook -i inventory "$@" 31543.yml | tee out.txt [ "$(grep -c 'SHOULD NOT HAPPEN' out.txt)" -eq 0 ] @@ -47,5 +45,5 @@ ansible-playbook -i inventory "$@" 73246.yml | tee out.txt ansible-playbook -i inventory "$@" 80981.yml | tee out.txt [ "$(grep -c 'SHOULD NOT HAPPEN' out.txt)" -eq 0 ] -[ "$(grep -c 'rescue' out.txt)" -eq 2 ] +[ "$(grep -c 'rescuedd' out.txt)" -eq 2 ] [ "$(grep -c 'recovered' out.txt)" -eq 2 ] diff --git a/test/integration/targets/collections/test_task_resolved_plugin/unqualified_and_collections_kw.yml b/test/integration/targets/collections/test_task_resolved_plugin/unqualified_and_collections_kw.yml index 5af4eda..ac70778 100644 --- a/test/integration/targets/collections/test_task_resolved_plugin/unqualified_and_collections_kw.yml +++ b/test/integration/targets/collections/test_task_resolved_plugin/unqualified_and_collections_kw.yml @@ -10,5 +10,7 @@ - ping: - collection_action: - collection_module: - - formerly_action: - - formerly_module: + - local_action: + module: formerly_action + - action: + module: formerly_module diff --git a/test/integration/targets/json_cleanup/library/bad_json b/test/integration/targets/json_cleanup/library/bad_json index 1df8c72..e84de04 100644 --- a/test/integration/targets/json_cleanup/library/bad_json +++ b/test/integration/targets/json_cleanup/library/bad_json @@ -1,4 +1,4 @@ -#!/usr/bin/env bash +#!/bin/bash set -eu diff --git a/test/integration/targets/json_cleanup/module_output_cleaning.yml b/test/integration/targets/json_cleanup/module_output_cleaning.yml index 165352a..94ae9c1 100644 --- a/test/integration/targets/json_cleanup/module_output_cleaning.yml +++ b/test/integration/targets/json_cleanup/module_output_cleaning.yml @@ -2,10 +2,17 @@ hosts: localhost gather_facts: false tasks: + - name: where is bash + shell: command -v bash + register: bash + changed_when: false + - name: call module that spews extra stuff bad_json: register: clean_json ignore_errors: true + vars: + ansible_bash_interpreter: '{{ bash.stdout }}' - name: all expected is there assert: diff --git a/test/integration/targets/module_defaults/tasks/main.yml b/test/integration/targets/module_defaults/tasks/main.yml index 747c2f9..0483278 100644 --- a/test/integration/targets/module_defaults/tasks/main.yml +++ b/test/integration/targets/module_defaults/tasks/main.yml @@ -10,16 +10,23 @@ - debug: register: foo + - local_action: + module: debug + register: local_action_foo + - name: test that 'debug' task used default 'msg' param assert: - that: foo.msg == "test default" + that: + - foo.msg == "test default" + - local_action_foo.msg == "test default" - name: remove test file file: state: absent - name: touch test file - file: + local_action: + module: file state: touch - name: stat test file diff --git a/test/integration/targets/module_utils_Ansible.Basic/library/ansible_basic_tests.ps1 b/test/integration/targets/module_utils_Ansible.Basic/library/ansible_basic_tests.ps1 index 625cb21..27b0d10 100644 --- a/test/integration/targets/module_utils_Ansible.Basic/library/ansible_basic_tests.ps1 +++ b/test/integration/targets/module_utils_Ansible.Basic/library/ansible_basic_tests.ps1 @@ -1323,6 +1323,37 @@ test_no_log - Invoked with: $actual | Assert-DictionaryEqual -Expected $expected } + "Run with exec wrapper warnings" = { + Set-Variable -Name complex_args -Scope Global -Value @{ + _ansible_exec_wrapper_warnings = [System.Collections.Generic.List[string]]@( + 'Warning 1', + 'Warning 2' + ) + } + $m = [Ansible.Basic.AnsibleModule]::Create(@(), @{}) + $m.Warn("Warning 3") + + $failed = $false + try { + $m.ExitJson() + } + catch [System.Management.Automation.RuntimeException] { + $failed = $true + $_.Exception.Message | Assert-Equal -Expected "exit: 0" + $actual = [Ansible.Basic.AnsibleModule]::FromJson($_.Exception.InnerException.Output) + } + $failed | Assert-Equal -Expected $true + + $expected = @{ + changed = $false + invocation = @{ + module_args = @{} + } + warnings = @("Warning 1", "Warning 2", "Warning 3") + } + $actual | Assert-DictionaryEqual -Expected $expected + } + "FailJson with message" = { $m = [Ansible.Basic.AnsibleModule]::Create(@(), @{}) diff --git a/test/integration/targets/shell/meta/main.yml b/test/integration/targets/shell/meta/main.yml new file mode 100644 index 0000000..cb6005d --- /dev/null +++ b/test/integration/targets/shell/meta/main.yml @@ -0,0 +1,3 @@ +dependencies: + - prepare_tests + - setup_remote_tmp_dir diff --git a/test/integration/targets/shell/tasks/command-building.yml b/test/integration/targets/shell/tasks/command-building.yml new file mode 100644 index 0000000..bd45261 --- /dev/null +++ b/test/integration/targets/shell/tasks/command-building.yml @@ -0,0 +1,53 @@ +- vars: + atd: '{{ remote_tmp_dir }}/shell/t m p' + api: '{{ remote_tmp_dir }}/shell/p y t h o n' + block: + - name: create test dir + file: + path: '{{ atd|dirname }}' + state: directory + + - name: create tempdir with spaces + file: + path: '{{ atd }}' + state: directory + + - name: create symlink for ansible_python_interpreter to file with spaces + file: + dest: '{{ api }}' + src: '{{ ansible_facts.python.executable }}' + state: link + + - name: run simple test playbook + command: >- + ansible-playbook -vvv -i inventory + -e 'ansible_python_interpreter="{{ api }}"' + -e 'ansible_pipelining=0' + "{{ role_path }}/test-command-building-playbook.yml" + environment: + ANSIBLE_REMOTE_TMP: '{{ atd }}' + ANSIBLE_NOCOLOR: "1" + ANSIBLE_FORCE_COLOR: "0" + register: command_building + delegate_to: localhost + + - debug: + var: command_building.stdout_lines + + - block: + - debug: + var: py_cmd + + - debug: + var: tmp_dir + + - assert: + that: + - py_cmd in exec_line + - tmp_dir in exec_line + vars: + exec_line: '{{ command_building.stdout_lines | select("search", "EXEC.*p y t h o n") | first }}' + py_cmd: >- + '"'"'{{ api }}'"'"' + tmp_dir: >- + '"'"'{{ atd }}/ansible-tmp- diff --git a/test/integration/targets/shell/tasks/main.yml b/test/integration/targets/shell/tasks/main.yml index d6f2a2b..7a442f4 100644 --- a/test/integration/targets/shell/tasks/main.yml +++ b/test/integration/targets/shell/tasks/main.yml @@ -34,3 +34,5 @@ with_items: - powershell - sh + +- import_tasks: command-building.yml diff --git a/test/integration/targets/shell/test-command-building-playbook.yml b/test/integration/targets/shell/test-command-building-playbook.yml new file mode 100644 index 0000000..f77cf4c --- /dev/null +++ b/test/integration/targets/shell/test-command-building-playbook.yml @@ -0,0 +1,4 @@ +- hosts: testhost + gather_facts: false + tasks: + - ping: diff --git a/test/integration/targets/win_exec_wrapper/tasks/main.yml b/test/integration/targets/win_exec_wrapper/tasks/main.yml index f1342c4..75da3d6 100644 --- a/test/integration/targets/win_exec_wrapper/tasks/main.yml +++ b/test/integration/targets/win_exec_wrapper/tasks/main.yml @@ -194,16 +194,33 @@ become_test_username: ansible_become_test gen_pw: "{{ 'password123!' + lookup('password', '/dev/null chars=ascii_letters,digits length=8') }}" -- name: create unprivileged user - win_user: - name: "{{ become_test_username }}" - password: "{{ gen_pw }}" - update_password: always - groups: Users - register: become_test_user_result - - name: execute tests and ensure that test user is deleted regardless of success/failure block: + - name: create unprivileged user + win_user: + name: "{{ become_test_username }}" + password: "{{ gen_pw }}" + update_password: always + groups: Users + register: become_test_user_result + + - name: create tempdir for test user + win_file: + path: C:\Windows\TEMP\test-dir + state: directory + + - name: deny delete permissions on new temp dir for test user + win_acl: + path: C:\Windows\TEMP\test-dir + user: '{{ become_test_user_result.sid }}' + type: '{{ item.type }}' + rights: '{{ item.rights }}' + loop: + - type: allow + rights: ListDirectory, CreateFiles, CreateDirectories, ReadAttributes, ReadExtendedAttributes, WriteData, WriteAttributes, WriteExtendedAttributes, Synchronize + - type: deny + rights: DeleteSubdirectoriesAndFiles, Delete + - name: ensure current user is not the become user win_shell: whoami register: whoami_out @@ -238,6 +255,21 @@ - become_system is successful - become_system.output == become_test_user_result.sid + - name: run module with tempdir with no delete access + win_ping: + register: temp_deletion_warning + vars: + <<: *become_vars + ansible_remote_tmp: C:\Windows\TEMP\test-dir + + - name: assert warning about tmpdir deletion is present + assert: + that: + - temp_deletion_warning.warnings | count == 1 + - >- + temp_deletion_warning.warnings[0] is + regex("(?i).*Failed to cleanup temporary directory 'C:\\\\Windows\\\\TEMP\\\\test-dir\\\\.*' used for compiling C# code\\. Files may still be present after the task is complete\\..*") + always: - name: ensure test user is deleted win_user: @@ -249,7 +281,12 @@ win_shell: rmdir /S /Q {{ profile_dir_out.stdout_lines[0] }} args: executable: cmd.exe - when: become_test_username in profile_dir_out.stdout_lines[0] + when: become_test_username in profile_dir_out.stdout_lines[0] | default("") + + - name: remove test tempdir + win_file: + path: C:\Windows\TEMP\test-dir + state: absent - name: test common functions in exec test_common_functions: diff --git a/test/lib/ansible_test/_internal/pypi_proxy.py b/test/lib/ansible_test/_internal/pypi_proxy.py index d119efa..1c9a9aa 100644 --- a/test/lib/ansible_test/_internal/pypi_proxy.py +++ b/test/lib/ansible_test/_internal/pypi_proxy.py @@ -69,7 +69,7 @@ def run_pypi_proxy(args: EnvironmentConfig, targets_use_pypi: bool) -> None: display.warning('Unable to use the PyPI proxy because Docker is not available. Installation of packages using `pip` may fail.') return - image = 'quay.io/ansible/pypi-test-container:2.0.0' + image = 'quay.io/ansible/pypi-test-container:3.1.0' port = 3141 run_support_container( diff --git a/test/units/playbook/test_block.py b/test/units/playbook/test_block.py index aac5f71..3c4dfb1 100644 --- a/test/units/playbook/test_block.py +++ b/test/units/playbook/test_block.py @@ -20,6 +20,10 @@ from __future__ import annotations import unittest from ansible.playbook.block import Block from ansible.playbook.task import Task +from ansible.plugins.loader import init_plugin_loader + + +init_plugin_loader() class TestBlock(unittest.TestCase): diff --git a/test/units/playbook/test_helpers.py b/test/units/playbook/test_helpers.py index 2977b0d..fb6170c 100644 --- a/test/units/playbook/test_helpers.py +++ b/test/units/playbook/test_helpers.py @@ -24,13 +24,16 @@ from unittest.mock import MagicMock from units.mock.loader import DictDataLoader from ansible import errors +from ansible.playbook import helpers from ansible.playbook.block import Block from ansible.playbook.handler import Handler from ansible.playbook.task import Task from ansible.playbook.task_include import TaskInclude from ansible.playbook.role.include import RoleInclude +from ansible.plugins.loader import init_plugin_loader -from ansible.playbook import helpers + +init_plugin_loader() class MixinForMocks(object): diff --git a/test/units/utils/display/test_broken_cowsay.py b/test/units/utils/display/test_broken_cowsay.py index 854b78b..50691c2 100644 --- a/test/units/utils/display/test_broken_cowsay.py +++ b/test/units/utils/display/test_broken_cowsay.py @@ -10,13 +10,12 @@ from unittest.mock import MagicMock def test_display_with_fake_cowsay_binary(capsys, mocker): - display = Display() mocker.patch("ansible.constants.ANSIBLE_COW_PATH", "./cowsay.sh") - mock_popen = MagicMock() mock_popen.return_value.returncode = 1 mocker.patch("subprocess.Popen", mock_popen) + display = Display() assert not hasattr(display, "cows_available") assert display.b_cowsay is None -- cgit v1.2.3