Skip to content

Conversation

Fryguy
Copy link
Member

@Fryguy Fryguy commented Aug 27, 2025

@agrare Please review.

With this in place, it's much simpler to test ansible-runner low-level and Ansible::Runner on a production appliance or container, as the setup is the complicated bit that this script now takes care of. This script can also be run locally.

@Fryguy
Copy link
Member Author

Fryguy commented Aug 27, 2025

Example run on a container:

[root@52ff8e273887 vmdb]# bin/test_ansible_runner_execution
== Writing database config ==
== Writing encryption key ==
Installing bats...
CentOS Stream 9 - BaseOS                                                                                                                                                                          1.2 MB/s | 8.8 MB     00:07
CentOS Stream 9 - AppStream                                                                                                                                                                       6.3 MB/s |  25 MB     00:03
CentOS Stream 9 - CRB                                                                                                                                                                             3.0 MB/s | 7.1 MB     00:02
CentOS Stream 9 - Extras packages                                                                                                                                                                  53 kB/s |  19 kB     00:00
Extra Packages for Enterprise Linux 9 - x86_64                                                                                                                                                    6.6 MB/s |  20 MB     00:03
Extra Packages for Enterprise Linux 9 openh264 (From Cisco) - x86_64                                                                                                                              5.2 kB/s | 2.5 kB     00:00
ManageIQ 20 (Tal) Release - x86_64                                                                                                                                                                 19 kB/s | 4.9 kB     00:00
ManageIQ 20 (Tal) Release - noarch                                                                                                                                                                 85 kB/s |  18 kB     00:00
ManageIQ 20 (Tal) Nightly - x86_64                                                                                                                                                                6.3 MB/s |  12 MB     00:01
Red Hat Universal Base Image 9 (RPMs) - BaseOS                                                                                                                                                    1.5 MB/s | 588 kB     00:00
Red Hat Universal Base Image 9 (RPMs) - AppStream                                                                                                                                                 6.4 MB/s | 2.4 MB     00:00
Red Hat Universal Base Image 9 (RPMs) - CodeReady Builder                                                                                                                                         1.8 MB/s | 287 kB     00:00
Dependencies resolved.
==================================================================================================================================================================================================================================
 Package                                                      Architecture                                    Version                                                    Repository                                          Size
==================================================================================================================================================================================================================================
Installing:
 bats                                                         noarch                                          1.8.0-1.el9                                                epel                                                54 k
Installing dependencies:
 parallel                                                     noarch                                          20241222-1.el9                                             epel                                               428 k
 perl-Thread-Queue                                            noarch                                          3.14-460.el9                                               appstream                                           22 k
 perl-threads                                                 x86_64                                          1:2.25-460.el9                                             appstream                                           58 k
 perl-threads-shared                                          x86_64                                          1.61-460.el9                                               appstream                                           45 k

Transaction Summary
==================================================================================================================================================================================================================================
Install  5 Packages

Total download size: 607 k
Installed size: 1.5 M
Downloading Packages:
(1/5): perl-Thread-Queue-3.14-460.el9.noarch.rpm                                                                                                                                                  360 kB/s |  22 kB     00:00
(2/5): perl-threads-2.25-460.el9.x86_64.rpm                                                                                                                                                       804 kB/s |  58 kB     00:00
(3/5): perl-threads-shared-1.61-460.el9.x86_64.rpm                                                                                                                                                548 kB/s |  45 kB     00:00
(4/5): bats-1.8.0-1.el9.noarch.rpm                                                                                                                                                                153 kB/s |  54 kB     00:00
(5/5): parallel-20241222-1.el9.noarch.rpm                                                                                                                                                         887 kB/s | 428 kB     00:00
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Total                                                                                                                                                                                             665 kB/s | 607 kB     00:00
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
  Preparing        :                                                                                                                                                                                                          1/1
  Installing       : perl-threads-1:2.25-460.el9.x86_64                                                                                                                                                                       1/5
  Installing       : perl-threads-shared-1.61-460.el9.x86_64                                                                                                                                                                  2/5
  Installing       : perl-Thread-Queue-3.14-460.el9.noarch                                                                                                                                                                    3/5
  Installing       : parallel-20241222-1.el9.noarch                                                                                                                                                                           4/5
  Installing       : bats-1.8.0-1.el9.noarch                                                                                                                                                                                  5/5
  Running scriptlet: bats-1.8.0-1.el9.noarch                                                                                                                                                                                  5/5
  Verifying        : perl-Thread-Queue-3.14-460.el9.noarch                                                                                                                                                                    1/5
  Verifying        : perl-threads-1:2.25-460.el9.x86_64                                                                                                                                                                       2/5
  Verifying        : perl-threads-shared-1.61-460.el9.x86_64                                                                                                                                                                  3/5
  Verifying        : bats-1.8.0-1.el9.noarch                                                                                                                                                                                  4/5
  Verifying        : parallel-20241222-1.el9.noarch                                                                                                                                                                           5/5
Installed products updated.

Installed:
  bats-1.8.0-1.el9.noarch            parallel-20241222-1.el9.noarch            perl-Thread-Queue-3.14-460.el9.noarch            perl-threads-1:2.25-460.el9.x86_64            perl-threads-shared-1.61-460.el9.x86_64

Complete!
Installing bats-support...
Cloning into '/root/.bats/libs/bats-support'...
remote: Enumerating objects: 218, done.
remote: Counting objects: 100% (64/64), done.
remote: Compressing objects: 100% (19/19), done.
remote: Total 218 (delta 51), reused 45 (delta 45), pack-reused 154 (from 1)
Receiving objects: 100% (218/218), 55.05 KiB | 3.06 MiB/s, done.
Resolving deltas: 100% (93/93), done.
Installing bats-assert...
Cloning into '/root/.bats/libs/bats-assert'...
remote: Enumerating objects: 739, done.
remote: Counting objects: 100% (485/485), done.
remote: Compressing objects: 100% (114/114), done.
remote: Total 739 (delta 403), reused 402 (delta 368), pack-reused 254 (from 1)
Receiving objects: 100% (739/739), 197.79 KiB | 8.99 MiB/s, done.
Resolving deltas: 100% (449/449), done.
Cloning ansible specs...
Cloning into '/tmp/manageiq'...
remote: Enumerating objects: 1, done.
remote: Counting objects: 100% (1/1), done.
remote: Total 1 (delta 0), reused 0 (delta 0), pack-reused 0 (from 0)
Receiving objects: 100% (1/1), done.
/tmp/manageiq /var/www/miq/vmdb
remote: Enumerating objects: 497, done.
remote: Counting objects: 100% (497/497), done.
remote: Compressing objects: 100% (330/330), done.
remote: Total 497 (delta 1), reused 379 (delta 0), pack-reused 0 (from 0)
Receiving objects: 100% (497/497), 113.44 KiB | 9.45 MiB/s, done.
Resolving deltas: 100% (1/1), done.
remote: Enumerating objects: 28, done.
remote: Counting objects: 100% (28/28), done.
remote: Compressing objects: 100% (21/21), done.
remote: Total 28 (delta 8), reused 19 (delta 6), pack-reused 0 (from 0)
Receiving objects: 100% (28/28), 16.60 KiB | 2.08 MiB/s, done.
Resolving deltas: 100% (8/8), done.
Your branch is up to date with 'origin/master'.
/var/www/miq/vmdb
Running tests...
runner_execution_spec.bats
 ✓ [ansible-runner] runs a playbook
 ✓ [ansible-runner] runs a playbook with variables in a vars file
 ✓ [ansible-runner] runs a playbook with vault encrypted variables
 ✓ [ansible-runner] runs a playbook with variables in a vault encrypted vars file
 ✓ [ansible-runner] runs a playbook using roles from github
 ✓ [ansible-runner] runs a role
 ✓ [ansible-runner] vmware collection
 ✓ [ansible-runner] aws collection
 ✓ [Ansible::Runner] runs a playbook
 ✓ [Ansible::Runner] runs a playbook with variables in a vars file
 - [Ansible::Runner] runs a playbook with vault encrypted variables (skipped: requires database access)
 - [Ansible::Runner] runs a playbook with variables in a vault encrypted vars file (skipped: requires database access)
 ✓ [Ansible::Runner] runs a playbook using roles from github
 ✓ [Ansible::Runner] runs a role
 ✓ [Ansible::Runner] vmware collection
 ✓ [Ansible::Runner] aws collection

16 tests, 0 failures, 2 skipped

Copy link
Member

@agrare agrare left a comment

Choose a reason for hiding this comment

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

I haven't seen or used the bats tests before so trying to catch up...

This feels a little like one of those "magic" curl https://example.com/install.sh | bash scripts that does everything in one go. A lot of this is stuff a developer would only need to do once (e.g. in developer_setup.md + bin/setup) and after that it is just rake test:ansible_runner_execution

Comment on lines +11 to +20
# Ensure bats and its support repos are installed
if [ ! -x "$(command -v bats)" ]; then
if [ "$APPLIANCE" == "true" ]; then
echo -e "\033[1;34mInstalling bats...\033[0m"
dnf install -y bats
else
echo -e "\033[1;31mERROR: Unable to find the bats test framework.\033[0m"
exit 1
fi
fi
Copy link
Member

Choose a reason for hiding this comment

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

Should this be in developer_setup.md? I know that wouldn't work help with a production appliance but we don't have anything else I don't think which auto-configures prod appliances as pseudo dev systems so some manual work isn't uncalled for there

Copy link
Member Author

Choose a reason for hiding this comment

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

This is basically written to be "ensure bats is installed, and if not and we're on an appliance/container, then install it". I'm good with adding the bats installation instructions to developer-setup.md for local testing, but I'd still have to keep this for appliances anyway.

Note that the check keeps it idempotent, so it only installs once.

Comment on lines 21 to 29
if [ ! -d "$HOME/.bats/libs/bats-support" ]; then
echo -e "\033[1;34mInstalling bats-support...\033[0m"
git clone https://github.com/bats-core/bats-support $HOME/.bats/libs/bats-support
fi
if [ ! -d "$HOME/.bats/libs/bats-assert" ]; then
echo -e "\033[1;34mInstalling bats-assert...\033[0m"
git clone https://github.com/bats-core/bats-assert $HOME/.bats/libs/bats-assert
fi
Copy link
Member

Choose a reason for hiding this comment

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

Is this something we should do in bin/setup? And does this have to be in $HOME? Don't love it when tools clutter my top-level $HOME dotfiles when they could be in the current working directory

Copy link
Member Author

@Fryguy Fryguy Aug 27, 2025

Choose a reason for hiding this comment

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

There is a BATS_LIB_PATH, but it's typically not exported, even with a normal bats installation. I could check for BATS_LIB_PATH presence first then fallback to $HOME/.bats/libs. See https://bats-core.readthedocs.io/en/stable/writing-tests.html#bats-load-library-load-system-wide-libraries. I'd be open to changing where this lives, I would just have to also change the tests.

Copy link
Member Author

Choose a reason for hiding this comment

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

ok switched this to use BATS_LIB_PATH, however, I think I agree that maybe we only clone on appliance/container, and then assume the person set it up correctly locally (such as via developer-setup.md). Would that work? That's kind of what I did with bats installation itself.

Comment on lines +31 to +44
if [ ! -d "./spec/lib/ansible" ]; then
echo -e "\033[1;34mCloning ansible specs...\033[0m"
rm -rf /tmp/manageiq
git clone --no-checkout --depth=1 --filter=tree:0 https://github.com/ManageIQ/manageiq /tmp/manageiq
pushd /tmp/manageiq
git sparse-checkout set --no-cone /spec/lib/ansible
git checkout
popd

mkdir -p ./spec/lib
cp -r /tmp/manageiq/spec/lib/ansible ./spec/lib
rm -rf /tmp/manageiq
fi
Copy link
Member

Choose a reason for hiding this comment

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

Seems weird that we're adding a command into the core repo but have code to handle when self/spec doesn't exist

Copy link
Member Author

Choose a reason for hiding this comment

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

See below. On production builds, spec dir doesn't exist. However, locally it will. This also keeps this piece idempotent, so that it only clones once (and/or can reclone if I rm -rf the directory and re-run the script)

Copy link
Member

Choose a reason for hiding this comment

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

Yeah longer term maybe the runner_execution_spec.bats tests don't belong in spec/lib/ansible/runner since they're not necessarily testing Ansible::Runner so that way you wouldn't need to clone spec on a production appliance for integration testing.

Copy link
Member Author

Choose a reason for hiding this comment

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

The main problems are that a) I want to share the actual playbooks in spec/lib/ansible/data and b) the tests in the .bats file should be almost entirely duplicated of runner_execution_spec.rb.

Note that we do test Ansible::Runner via bats. The tests first test ansible-runner execution directly to ensure the CLI, venv, and collections are setup correctly (without Ruby), then they test the same set of tests via Ansible::Runner to ensure that the Ruby code can see the CLI and run it correctly. Then the tests verify that specific collections from the venv are valid (this helps if we pull in newer collections to ensure that things like vmware and aws can still work correctly).

@Fryguy
Copy link
Member Author

Fryguy commented Aug 27, 2025

@agrare Apologies I thought you were familiar with the background on this.

So the background is that on a production appliance or container, rspec doesn't exist nor does the spec directory. In order to verify ansible-runner on one of our builds (or more accurately, verify that the manageiq-ansible-venv rpm is working correctly), I put together this bats based set of tests in #23482. The reasoning for using BATS vs rspec was a) it was really difficult to install rspec in a production appliance/container due to the way our gemset rpm is designed and b) I wanted to run a bunch of lower level tests against ansible-runner cli that didn't need Rails. However, I did want to reuse the existing set of playbooks from the spec directory as much as possible.

So, the bats tests use the bats framework, however, in a production appliance I first need to install bats, the test dependencies bats-support and bats-assert, and get a copy of the specs themselves. When I was building #23482 initially, I didn't script it, and basically just mapped a volume to the container and/or scp'd the specs into the appliance, since I was actively building them. I would also run the bats tests locally to make sure they worked before transferring to those other locations to test. However, now, a few months later, I forgot how onerous the setup actually is, hence this script.

My intention is that this script is part of core, because that's where the specs live, but shipped with the appliance, so a developer can just ssh into an appliance or spin up the main container, and then just run the script. This is why it's not in developer-setup.md, because it's not only meant to be run locally. However, I did design the script to also run locally as well so developers can verify their local setup, which also helps with writing new ansible-runner tests as I mentioned above.

I agree this is very different than other things, but it's also the only "test-in-production" thing we have. I could move it to another directory if you think bin is not appropriate, but it can't be the spec dir, because we don't ship the spec dir.

@Fryguy Fryguy force-pushed the test_ansible_runner_execution branch from 88621ff to 8dbe2c5 Compare August 27, 2025 19:29
Copy link
Member

@agrare agrare left a comment

Choose a reason for hiding this comment

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

Good with this for now, maybe in the future this can live with other automated integration tests location TBD

@agrare agrare merged commit bf61e24 into ManageIQ:master Aug 27, 2025
8 checks passed
@Fryguy
Copy link
Member Author

Fryguy commented Aug 27, 2025

Yeah I like the idea of resurrecting integration_tests in a new form and/or test things on remote appliances/containers. This would definitely belong there.

@Fryguy Fryguy deleted the test_ansible_runner_execution branch August 27, 2025 19:46
@Fryguy
Copy link
Member Author

Fryguy commented Sep 3, 2025

Backported to spassky in commit 7c4cb8d.

commit 7c4cb8d4ddf0b95e30811a1880c425bd5182133d
Author: Adam Grare <[email protected]>
Date:   Wed Aug 27 15:45:32 2025 -0400

    Merge pull request #23574 from Fryguy/test_ansible_runner_execution
    
    Add script to help setup and execute ansible_runner execution tests
    
    (cherry picked from commit bf61e243ab0029e29bad9238c9e3368ef74062fb)

Fryguy pushed a commit that referenced this pull request Sep 3, 2025
Add script to help setup and execute ansible_runner execution tests

(cherry picked from commit bf61e24)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants