Skip to content

Commit

Permalink
Add Ansible::Runner specs for running roles
Browse files Browse the repository at this point in the history
  • Loading branch information
Fryguy committed Jan 6, 2025
1 parent d4f76b2 commit 7621856
Showing 1 changed file with 42 additions and 9 deletions.
51 changes: 42 additions & 9 deletions spec/lib/ansible/runner_execution_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,6 @@
let(:env_vars) { {} }
let(:extra_vars) { {} }

def expect_ansible_runner_success(response)
expect(response.return_code).to eq(0), "ansible-runner failed with:\n====== STDERR ======\n#{response.stderr}\n\n====== STDOUT ======\n#{response.human_stdout}"
end

it "runs a playbook" do
playbook = data_directory.join("hello_world.yml")

Expand Down Expand Up @@ -56,18 +52,18 @@ def expect_ansible_runner_success(response)
expect(response.human_stdout).to include('"msg": "Hello World! vars_file_1=vars_file_1_value, vars_file_2=vars_file_2_value"')
end

context "runs a playbook using roles from github" do
let(:roles_dir) { data_directory.join("hello_world_with_requirements_github/roles/manageiq.example") }
context "with a roles/requirements.yml" do
let(:roles_path) { data_directory.join("hello_world_with_requirements_github/roles/manageiq.example") }

after { FileUtils.rm_rf(roles_dir) }
after { FileUtils.rm_rf(roles_path) }

it do
it "runs a playbook using roles from github" do
playbook = data_directory.join("hello_world_with_requirements_github/hello_world_with_requirements_github.yml")

response = Ansible::Runner.public_send(method_under_test, env_vars, extra_vars, playbook)
response = response.wait(5.seconds) if async

expect(roles_dir).to exist
expect(roles_path).to exist
expect_ansible_runner_success(response)
expect(response.human_stdout).to include('"msg": "Hello World! example_var=\'example var value\'"')
end
Expand All @@ -82,6 +78,29 @@ def expect_ansible_runner_success(response)
end
end

shared_examples_for :executing_roles do
let(:method_under_test) { async ? :run_role_async : :run_role }
let(:env_vars) { {} }
let(:extra_vars) { {} }

let(:roles_path) { Dir.mktmpdir("ansible-runner-roles-test") }

before do
FileUtils.cp(data_directory.join("hello_world_with_requirements_github/roles/requirements.yml"), roles_path)
AwesomeSpawn.run!("ansible-galaxy", :params => ["install", {:role_file => "requirements.yml", :roles_path => "."}], :chdir => roles_path)
end

after { FileUtils.rm_rf(roles_path) }

it "runs a role" do
response = Ansible::Runner.public_send(method_under_test, env_vars, extra_vars, "manageiq.example", roles_path: roles_path)
response = response.wait(5.seconds) if async

expect_ansible_runner_success(response)
expect(response.human_stdout).to include(%q{"msg": "Hello from manageiq.example role! example_var='example var value'"})
end
end

describe ".run" do
let(:async) { false }
include_examples :executing_playbooks
Expand All @@ -91,4 +110,18 @@ def expect_ansible_runner_success(response)
let(:async) { true }
include_examples :executing_playbooks
end

describe ".run_role" do
let(:async) { false }
include_examples :executing_roles
end

describe ".run_role_async" do
let(:async) { true }
include_examples :executing_roles
end

def expect_ansible_runner_success(response)
expect(response.return_code).to eq(0), "ansible-runner failed with:\n====== STDERR ======\n#{response.stderr}\n\n====== STDOUT ======\n#{response.human_stdout}"
end
end

0 comments on commit 7621856

Please sign in to comment.