Skip to content

Commit 0508ed5

Browse files
committed
Merge pull request #23301 from Fryguy/add_runner_role_specs
Add Ansible::Runner specs for running roles
2 parents 5f3f33d + 7621856 commit 0508ed5

File tree

1 file changed

+42
-9
lines changed

1 file changed

+42
-9
lines changed

spec/lib/ansible/runner_execution_spec.rb

Lines changed: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,6 @@
1010
let(:env_vars) { {} }
1111
let(:extra_vars) { {} }
1212

13-
def expect_ansible_runner_success(response)
14-
expect(response.return_code).to eq(0), "ansible-runner failed with:\n====== STDERR ======\n#{response.stderr}\n\n====== STDOUT ======\n#{response.human_stdout}"
15-
end
16-
1713
it "runs a playbook" do
1814
playbook = data_directory.join("hello_world.yml")
1915

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

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

62-
after { FileUtils.rm_rf(roles_dir) }
58+
after { FileUtils.rm_rf(roles_path) }
6359

64-
it do
60+
it "runs a playbook using roles from github" do
6561
playbook = data_directory.join("hello_world_with_requirements_github/hello_world_with_requirements_github.yml")
6662

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

70-
expect(roles_dir).to exist
66+
expect(roles_path).to exist
7167
expect_ansible_runner_success(response)
7268
expect(response.human_stdout).to include('"msg": "Hello World! example_var=\'example var value\'"')
7369
end
@@ -82,6 +78,29 @@ def expect_ansible_runner_success(response)
8278
end
8379
end
8480

81+
shared_examples_for :executing_roles do
82+
let(:method_under_test) { async ? :run_role_async : :run_role }
83+
let(:env_vars) { {} }
84+
let(:extra_vars) { {} }
85+
86+
let(:roles_path) { Dir.mktmpdir("ansible-runner-roles-test") }
87+
88+
before do
89+
FileUtils.cp(data_directory.join("hello_world_with_requirements_github/roles/requirements.yml"), roles_path)
90+
AwesomeSpawn.run!("ansible-galaxy", :params => ["install", {:role_file => "requirements.yml", :roles_path => "."}], :chdir => roles_path)
91+
end
92+
93+
after { FileUtils.rm_rf(roles_path) }
94+
95+
it "runs a role" do
96+
response = Ansible::Runner.public_send(method_under_test, env_vars, extra_vars, "manageiq.example", roles_path: roles_path)
97+
response = response.wait(5.seconds) if async
98+
99+
expect_ansible_runner_success(response)
100+
expect(response.human_stdout).to include(%q{"msg": "Hello from manageiq.example role! example_var='example var value'"})
101+
end
102+
end
103+
85104
describe ".run" do
86105
let(:async) { false }
87106
include_examples :executing_playbooks
@@ -91,4 +110,18 @@ def expect_ansible_runner_success(response)
91110
let(:async) { true }
92111
include_examples :executing_playbooks
93112
end
113+
114+
describe ".run_role" do
115+
let(:async) { false }
116+
include_examples :executing_roles
117+
end
118+
119+
describe ".run_role_async" do
120+
let(:async) { true }
121+
include_examples :executing_roles
122+
end
123+
124+
def expect_ansible_runner_success(response)
125+
expect(response.return_code).to eq(0), "ansible-runner failed with:\n====== STDERR ======\n#{response.stderr}\n\n====== STDOUT ======\n#{response.human_stdout}"
126+
end
94127
end

0 commit comments

Comments
 (0)