|
| 1 | +#/usr/bin/env bats |
| 2 | + |
| 3 | +# This test file is for testing ansible-runner on a production appliance to verify |
| 4 | +# that the real installation is working as expected. It is a duplicate of the tests |
| 5 | +# in runner_execution_spec.rb but without the rspec and rspec-rails overhead. |
| 6 | +# |
| 7 | +# This test requires the Bats test framework to be installed |
| 8 | +# macOS: brew install bats-core |
| 9 | +# appliance: dnf install bats |
| 10 | +# as well as the bats-support and bats-assert plugins installed |
| 11 | +# git clone https://github.com/bats-core/bats-support ~/.bats/libs/bats-support |
| 12 | +# git clone https://github.com/bats-core/bats-assert ~/.bats/libs/bats-assert |
| 13 | + |
| 14 | +setup_file() { |
| 15 | + export BATS_LIB_PATH="$HOME/.bats/libs:$BATS_LIB_PATH" |
| 16 | + |
| 17 | + export PYTHON_VERSION="3.12" |
| 18 | + |
| 19 | + export SCRIPT_DIR="$(cd "$(dirname "$BATS_TEST_FILENAME")" >/dev/null 2>&1 && pwd)" |
| 20 | + export DATA_DIR="$SCRIPT_DIR/runner/data" |
| 21 | + export TEST_DIR="/tmp/ansible-runner-test" |
| 22 | + export ROLES_DIR="/tmp/ansible-runner-test-roles" |
| 23 | + export VAULT_FILE="$TEST_DIR/vault_password" |
| 24 | +} |
| 25 | + |
| 26 | +setup() { |
| 27 | + bats_load_library 'bats-support' |
| 28 | + bats_load_library 'bats-assert' |
| 29 | + |
| 30 | + rm -rf $TEST_DIR |
| 31 | + rm -rf $ROLES_DIR |
| 32 | + |
| 33 | + mkdir -p $TEST_DIR |
| 34 | +} |
| 35 | + |
| 36 | +teardown() { |
| 37 | + rm -rf $DATA_DIR/hello_world_with_requirements_github/roles/manageiq.example |
| 38 | +} |
| 39 | + |
| 40 | +setup_roles_dir() { |
| 41 | + # In prod builds, ansible-galaxy lives in the venv, so set up the PATH temporarily to install the roles |
| 42 | + PATH="/var/lib/manageiq/venv/bin:$PATH" |
| 43 | + |
| 44 | + roles_path="$1" |
| 45 | + source_role_file="${2:-$1/requirements.yml}" |
| 46 | + role_file="$roles_path/requirements.yml" |
| 47 | + if [ "$source_role_file" != "$role_file" ]; then |
| 48 | + mkdir -p $roles_path |
| 49 | + cp $source_role_file $role_file |
| 50 | + fi |
| 51 | + ansible-galaxy install --roles-path=$roles_path --role-file=$role_file |
| 52 | + |
| 53 | + PATH="${PATH#*:}" |
| 54 | +} |
| 55 | + |
| 56 | +################################################################################ |
| 57 | + |
| 58 | +exec_ansible_runner_cli() { |
| 59 | + PATH="/var/lib/manageiq/venv/bin:$PATH" \ |
| 60 | + PYTHONPATH="/var/lib/manageiq/venv/lib/python${PYTHON_VERSION}/site-packages:/usr/local/lib64/python${PYTHON_VERSION}/site-packages:/usr/local/lib/python${PYTHON_VERSION}/site-packages:/usr/lib64/python${PYTHON_VERSION}/site-packages:/usr/lib/python${PYTHON_VERSION}/site-packages" \ |
| 61 | + ansible-runner run $TEST_DIR --ident result --playbook $1 --project-dir $DATA_DIR |
| 62 | +} |
| 63 | + |
| 64 | +exec_ansible_runner_cli_role() { |
| 65 | + PATH="/var/lib/manageiq/venv/bin:$PATH" \ |
| 66 | + PYTHONPATH="/var/lib/manageiq/venv/lib/python${PYTHON_VERSION}/site-packages:/usr/local/lib64/python${PYTHON_VERSION}/site-packages:/usr/local/lib/python${PYTHON_VERSION}/site-packages:/usr/lib64/python${PYTHON_VERSION}/site-packages:/usr/lib/python${PYTHON_VERSION}/site-packages" \ |
| 67 | + ansible-runner run $TEST_DIR --ident result --role $1 --roles-path $ROLES_DIR --role-skip-facts --hosts localhost |
| 68 | +} |
| 69 | + |
| 70 | +@test "[ansible-runner] runs a playbook" { |
| 71 | + run exec_ansible_runner_cli hello_world.yml |
| 72 | + assert_success |
| 73 | + assert_output --partial '"msg": "Hello World!"' |
| 74 | +} |
| 75 | + |
| 76 | +@test "[ansible-runner] runs a playbook with variables in a vars file" { |
| 77 | + run exec_ansible_runner_cli hello_world_vars_file.yml |
| 78 | + assert_success |
| 79 | + assert_output --partial '"msg": "Hello World! vars_file_1=vars_file_1_value, vars_file_2=vars_file_2_value"' |
| 80 | +} |
| 81 | + |
| 82 | +@test "[ansible-runner] runs a playbook with vault encrypted variables" { |
| 83 | + echo -n "vault" >> $VAULT_FILE |
| 84 | + ANSIBLE_VAULT_PASSWORD_FILE=$VAULT_FILE run exec_ansible_runner_cli hello_world_vault_encrypted_vars.yml |
| 85 | + assert_success |
| 86 | + assert_output --partial '"msg": "Hello World! (NOTE: This message has been encrypted with ansible-vault)"' |
| 87 | +} |
| 88 | + |
| 89 | +@test "[ansible-runner] runs a playbook with variables in a vault encrypted vars file" { |
| 90 | + echo -n "vault" >> $VAULT_FILE |
| 91 | + ANSIBLE_VAULT_PASSWORD_FILE=$VAULT_FILE run exec_ansible_runner_cli hello_world_vault_encrypted_vars_file.yml |
| 92 | + assert_success |
| 93 | + assert_output --partial '"msg": "Hello World! vars_file_1=vars_file_1_value, vars_file_2=vars_file_2_value"' |
| 94 | +} |
| 95 | + |
| 96 | +@test "[ansible-runner] runs a playbook using roles from github" { |
| 97 | + setup_roles_dir $DATA_DIR/hello_world_with_requirements_github/roles |
| 98 | + |
| 99 | + run exec_ansible_runner_cli hello_world_with_requirements_github/hello_world_with_requirements_github.yml |
| 100 | + assert_success |
| 101 | + assert_output --partial '"msg": "Hello World! example_var='\''example var value'\''"' |
| 102 | +} |
| 103 | + |
| 104 | +@test "[ansible-runner] runs a role" { |
| 105 | + setup_roles_dir $ROLES_DIR $DATA_DIR/hello_world_with_requirements_github/roles/requirements.yml |
| 106 | + |
| 107 | + run exec_ansible_runner_cli_role manageiq.example |
| 108 | + assert_success |
| 109 | + assert_output --partial '"msg": "Hello from manageiq.example role! example_var='\''example var value'\''"' |
| 110 | +} |
| 111 | + |
| 112 | +@test "[ansible-runner] vmware collection" { |
| 113 | + if [ ! -d /var/lib/manageiq/venv ]; then |
| 114 | + skip "manageiq venv collections are not present" |
| 115 | + fi |
| 116 | + |
| 117 | + run exec_ansible_runner_cli vmware.yml |
| 118 | + assert_failure # We expect to this to fail due to connecting to an unknown vcenter |
| 119 | + assert_output --partial '"msg": "Unknown error while connecting to vCenter or ESXi API at vcenter_hostname:443 : [Errno -2] Name or service not known"' |
| 120 | +} |
| 121 | + |
| 122 | +@test "[ansible-runner] aws collection" { |
| 123 | + if [ ! -d /var/lib/manageiq/venv ]; then |
| 124 | + skip "manageiq venv collections are not present" |
| 125 | + fi |
| 126 | + |
| 127 | + run exec_ansible_runner_cli aws.yml |
| 128 | + assert_failure # We expect to this to fail due to connecting with bad creds |
| 129 | + assert_output --partial '"msg": "Failed to describe instances: An error occurred (AuthFailure) when calling the DescribeInstances operation: AWS was not able to validate the provided access credentials"' |
| 130 | +} |
| 131 | + |
| 132 | +################################################################################ |
| 133 | + |
| 134 | +exec_ansible_runner() { |
| 135 | + rails runner "resp = Ansible::Runner.run({}, {}, '$DATA_DIR/$1'); puts resp.human_stdout; exit resp.return_code" |
| 136 | +} |
| 137 | + |
| 138 | +exec_ansible_runner_role() { |
| 139 | + rails runner "resp = Ansible::Runner.run_role({}, {}, '$1', roles_path: '$ROLES_DIR'); puts resp.human_stdout; exit resp.return_code" |
| 140 | +} |
| 141 | + |
| 142 | +@test "[Ansible::Runner] runs a playbook" { |
| 143 | + run exec_ansible_runner hello_world.yml |
| 144 | + assert_success |
| 145 | + assert_output --partial '"msg": "Hello World!"' |
| 146 | +} |
| 147 | + |
| 148 | +@test "[Ansible::Runner] runs a playbook with variables in a vars file" { |
| 149 | + run exec_ansible_runner hello_world_vars_file.yml |
| 150 | + assert_success |
| 151 | + assert_output --partial '"msg": "Hello World! vars_file_1=vars_file_1_value, vars_file_2=vars_file_2_value"' |
| 152 | +} |
| 153 | + |
| 154 | +@test "[Ansible::Runner] runs a playbook with vault encrypted variables" { |
| 155 | + skip "requires database access" |
| 156 | + |
| 157 | + run exec_ansible_runner hello_world_vault_encrypted_vars.yml |
| 158 | + assert_success |
| 159 | + assert_output --partial '"msg": "Hello World! (NOTE: This message has been encrypted with ansible-vault)"' |
| 160 | +} |
| 161 | + |
| 162 | +@test "[Ansible::Runner] runs a playbook with variables in a vault encrypted vars file" { |
| 163 | + skip "requires database access" |
| 164 | + |
| 165 | + run exec_ansible_runner hello_world_vault_encrypted_vars_file.yml |
| 166 | + assert_success |
| 167 | + assert_output --partial '"msg": "Hello World! vars_file_1=vars_file_1_value, vars_file_2=vars_file_2_value"' |
| 168 | +} |
| 169 | + |
| 170 | +@test "[Ansible::Runner] runs a playbook using roles from github" { |
| 171 | + run exec_ansible_runner hello_world_with_requirements_github/hello_world_with_requirements_github.yml |
| 172 | + assert_success |
| 173 | + assert_output --partial '"msg": "Hello World! example_var='\''example var value'\''"' |
| 174 | +} |
| 175 | + |
| 176 | +@test "[Ansible::Runner] runs a role" { |
| 177 | + setup_roles_dir $ROLES_DIR $DATA_DIR/hello_world_with_requirements_github/roles/requirements.yml |
| 178 | + |
| 179 | + run exec_ansible_runner_role manageiq.example |
| 180 | + assert_success |
| 181 | + assert_output --partial '"msg": "Hello from manageiq.example role! example_var='\''example var value'\''"' |
| 182 | +} |
| 183 | + |
| 184 | +@test "[Ansible::Runner] vmware collection" { |
| 185 | + if [ ! -d /var/lib/manageiq/venv ]; then |
| 186 | + skip "manageiq venv collections are not present" |
| 187 | + fi |
| 188 | + |
| 189 | + run exec_ansible_runner vmware.yml |
| 190 | + assert_failure # We expect to this to fail due to connecting to an unknown vcenter |
| 191 | + assert_output --partial '"msg": "Unknown error while connecting to vCenter or ESXi API at vcenter_hostname:443 : [Errno -2] Name or service not known"' |
| 192 | +} |
| 193 | + |
| 194 | +@test "[Ansible::Runner] aws collection" { |
| 195 | + if [ ! -d /var/lib/manageiq/venv ]; then |
| 196 | + skip "manageiq venv collections are not present" |
| 197 | + fi |
| 198 | + |
| 199 | + run exec_ansible_runner aws.yml |
| 200 | + assert_failure # We expect to this to fail due to connecting with bad creds |
| 201 | + assert_output --partial '"msg": "Failed to describe instances: An error occurred (AuthFailure) when calling the DescribeInstances operation: AWS was not able to validate the provided access credentials"' |
| 202 | +} |
0 commit comments