Skip to content

Commit

Permalink
Add ansible-runner specs for run_async method
Browse files Browse the repository at this point in the history
  • Loading branch information
Fryguy committed Jul 16, 2024
1 parent 39b67ef commit b6d0c4e
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
19 changes: 19 additions & 0 deletions lib/ansible/runner/response_async.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,25 @@ def dump
}
end

# Waits for the async process to complete or hit the given timeout
#
# @param timeout [Integer, ActiveSupport::Duration] Number of seconds to wait for the process to complete
# @return [Ansible::Runner::Response] Response object with all details about the Ansible run
def wait(timeout)
result = nil
# Poll once per second until complete
1.upto(timeout) do |t|
result = response
result ? break : sleep(1)
end
# If the process is still running, then stop it
if result.nil?
stop
result = response
end
result
end

# Creates the Ansible::Runner::ResponseAsync object from hash data
#
# @param hash [Hash] Dumped Ansible::Runner::ResponseAsync object
Expand Down
24 changes: 23 additions & 1 deletion spec/lib/ansible/runner_execution_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,29 @@
it "runs a hello-world-vault-encrypted playbook" do
credential = FactoryBot.create(:embedded_ansible_vault_credential, :password => "vault")
playbook = File.join(__dir__, "runner/data/hello_world_vault_encrypted.yml")
response = Ansible::Runner.run(env_vars, extra_vars, playbook, :credentials => [credential.id])

response = Ansible::Runner.run(env_vars, extra_vars, playbook, :credentials => [credential.id])

expect(response.return_code).to eq(0), "ansible-runner failed with:\n#{response.stderr}"
expect(response.human_stdout).to include('"msg": "Hello World! (NOTE: This message has been encrypted with ansible-vault)"')
end
end

describe ".run_async" do
it "runs a hello-world playbook" do
response = Ansible::Runner.run_async(env_vars, extra_vars, File.join(__dir__, "runner/data/hello_world.yml"))
response = response.wait(5.seconds)

expect(response.return_code).to eq(0), "ansible-runner failed with:\n#{response.stderr}"
expect(response.human_stdout).to include('"msg": "Hello, world!"')
end

it "runs a hello-world-vault-encrypted playbook" do
credential = FactoryBot.create(:embedded_ansible_vault_credential, :password => "vault")
playbook = File.join(__dir__, "runner/data/hello_world_vault_encrypted.yml")

response = Ansible::Runner.run_async(env_vars, extra_vars, playbook, :credentials => [credential.id])
response = response.wait(5.seconds)

expect(response.return_code).to eq(0), "ansible-runner failed with:\n#{response.stderr}"
expect(response.human_stdout).to include('"msg": "Hello World! (NOTE: This message has been encrypted with ansible-vault)"')
Expand Down

0 comments on commit b6d0c4e

Please sign in to comment.