From aa3ce1913516cfcd95a073ba170aa12ef39e76f4 Mon Sep 17 00:00:00 2001 From: Alexei Znamensky Date: Sun, 22 Mar 2026 15:47:10 +1300 Subject: [PATCH] test(python_runner): add integration tests --- .../integration/targets/python_runner/aliases | 25 +++++++ .../targets/python_runner/files/settings.py | 9 +++ .../targets/python_runner/meta/main.yml | 6 ++ .../targets/python_runner/tasks/main.yml | 73 +++++++++++++++++++ 4 files changed, 113 insertions(+) create mode 100644 tests/integration/targets/python_runner/aliases create mode 100644 tests/integration/targets/python_runner/files/settings.py create mode 100644 tests/integration/targets/python_runner/meta/main.yml create mode 100644 tests/integration/targets/python_runner/tasks/main.yml diff --git a/tests/integration/targets/python_runner/aliases b/tests/integration/targets/python_runner/aliases new file mode 100644 index 0000000000..061ccbaa91 --- /dev/null +++ b/tests/integration/targets/python_runner/aliases @@ -0,0 +1,25 @@ +# Copyright (c) 2026, Ansible Project +# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) +# SPDX-License-Identifier: GPL-3.0-or-later + +azp/posix/2 +skip/freebsd +skip/macos +skip/osx +skip/rhel8.2 +skip/rhel8.3 +skip/rhel8.4 +skip/rhel8.5 +skip/rhel8.6 +skip/rhel8.7 +skip/rhel8.8 +skip/rhel9.0 +skip/rhel9.1 +skip/rhel9.2 +skip/rhel9.3 +skip/rhel9.4 +skip/rhel9.5 +skip/rhel9.6 +skip/rhel9.7 +skip/rhel10.0 +skip/rhel10.1 diff --git a/tests/integration/targets/python_runner/files/settings.py b/tests/integration/targets/python_runner/files/settings.py new file mode 100644 index 0000000000..85bbb00973 --- /dev/null +++ b/tests/integration/targets/python_runner/files/settings.py @@ -0,0 +1,9 @@ +# Copyright (c) 2026, Alexei Znamensky +# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) +# SPDX-License-Identifier: GPL-3.0-or-later + +SECRET_KEY = "test-secret-key-for-python-runner-integration-test-do-not-use-in-production" + +INSTALLED_APPS = [] + +DATABASES = {} diff --git a/tests/integration/targets/python_runner/meta/main.yml b/tests/integration/targets/python_runner/meta/main.yml new file mode 100644 index 0000000000..324b0193eb --- /dev/null +++ b/tests/integration/targets/python_runner/meta/main.yml @@ -0,0 +1,6 @@ +--- +# Copyright (c) 2026, Ansible Project +# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) +# SPDX-License-Identifier: GPL-3.0-or-later + +dependencies: [] diff --git a/tests/integration/targets/python_runner/tasks/main.yml b/tests/integration/targets/python_runner/tasks/main.yml new file mode 100644 index 0000000000..27d917836a --- /dev/null +++ b/tests/integration/targets/python_runner/tasks/main.yml @@ -0,0 +1,73 @@ +# Copyright (c) 2026, Alexei Znamensky +# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) +# SPDX-License-Identifier: GPL-3.0-or-later + +- name: Create temporary directory + ansible.builtin.tempfile: + state: directory + suffix: .python_runner + register: tmp_python_runner + +- name: Create virtualenv + ansible.builtin.command: + cmd: "python3 -m venv {{ tmp_python_runner.path }}/venv" + +- name: Update pip in virtualenv + ansible.builtin.pip: + name: pip + state: latest + virtualenv: "{{ tmp_python_runner.path }}/venv" + +- name: Install django in virtualenv + ansible.builtin.pip: + name: django + state: present + virtualenv: "{{ tmp_python_runner.path }}/venv" + +- name: Copy settings file to temporary directory + ansible.builtin.copy: + src: settings.py + dest: "{{ tmp_python_runner.path }}/settings.py" + mode: "0644" + +# Regression test for https://github.com/ansible-collections/community.general/issues/8884 +# Bug: PythonRunner was setting path_prefix to a string instead of a list, corrupting PATH +# when venv was specified, so the venv's python could not be found. +- name: Run django_command check using virtualenv (regression test for issue 8884) + community.general.django_command: + command: check + settings: settings + pythonpath: "{{ tmp_python_runner.path }}" + venv: "{{ tmp_python_runner.path }}/venv" + register: result_check + +- name: Assert django_command succeeded and returned a version + ansible.builtin.assert: + that: + - result_check is success + - result_check.version is defined + - result_check.version != "" + +# Second test: mirrors the scenario from issue 8884 — a real Django project created +# with django-admin startproject, then checked via django_command with a venv. +# The original reporter was running `migrate`; we use `check` to avoid needing a database. +- name: Create Django project using venv django-admin + ansible.builtin.command: + cmd: >- + {{ tmp_python_runner.path }}/venv/bin/django-admin startproject + testproject {{ tmp_python_runner.path }}/testproject + +- name: Run django_command check on the created project (issue 8884 scenario) + community.general.django_command: + command: check + settings: testproject.settings + pythonpath: "{{ tmp_python_runner.path }}/testproject" + venv: "{{ tmp_python_runner.path }}/venv" + register: result_project_check + +- name: Assert project check succeeded and returned a version + ansible.builtin.assert: + that: + - result_project_check is success + - result_project_check.version is defined + - result_project_check.version != ""