Skip to content

Commit

Permalink
Merge pull request ManageIQ#18747 from djberg96/conversion_host_logging2
Browse files Browse the repository at this point in the history
[V2V] Add better logging to ConversionHost methods
  • Loading branch information
agrare authored May 15, 2019
2 parents 8679a53 + 52072d7 commit 23d2532
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
7 changes: 6 additions & 1 deletion app/models/conversion_host.rb
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,12 @@ def ansible_playbook(playbook, extra_vars = {}, miq_task_id = nil, auth_type = '
extra_vars.each { |k, v| command << " --extra-vars '#{k}=#{v}'" }

result = AwesomeSpawn.run(command)
raise unless result.exit_status.zero?

if result.failure?
error_message = result.error.presence || result.output
_log.error("#{result.command_line} ==> #{error_message}")
raise
end
ensure
task&.update_context(task.context_data.merge!(File.basename(playbook, '.yml') => result.output)) unless result.nil?
ssh_private_key_file&.unlink
Expand Down
20 changes: 17 additions & 3 deletions spec/models/conversion_host_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,10 @@
end

context "#ansible_playbook" do
let(:auth_v2v) { FactoryBot.create(:authentication_v2v, :resource => conversion_host) }
let(:package_url) { 'http://file.example.com/vddk-stable.tar.gz' }
let(:enable_playbook) { '/usr/share/v2v-conversion-host-ansible/playbooks/conversion_host_enable.yml' }

it "check_conversion_host_role calls ansible_playbook with extra_vars" do
check_playbook = '/usr/share/v2v-conversion-host-ansible/playbooks/conversion_host_check.yml'
check_extra_vars = {
Expand Down Expand Up @@ -193,12 +197,11 @@
end

it "enable_conversion_host_role calls ansible_playbook with extra_vars" do
enable_playbook = '/usr/share/v2v-conversion-host-ansible/playbooks/conversion_host_enable.yml'
check_playbook = '/usr/share/v2v-conversion-host-ansible/playbooks/conversion_host_check.yml'
enable_extra_vars = {
:v2v_host_type => 'rhevm',
:v2v_transport_method => 'vddk',
:v2v_vddk_package_url => 'http://file.example.com/vddk-stable.tar.gz'
:v2v_vddk_package_url => package_url
}
check_extra_vars = {
:v2v_host_type => 'rhevm',
Expand All @@ -208,6 +211,18 @@
expect(conversion_host).to receive(:ansible_playbook).once.ordered.with(check_playbook, check_extra_vars, nil)
conversion_host.enable_conversion_host_role('http://file.example.com/vddk-stable.tar.gz', nil)
end

it "logs an error message if the ansible_playbook command fails" do
command = "ansible_playbook #{enable_playbook}"
result = instance_double(AwesomeSpawn::CommandResult, :command_line => command, :failure? => true, :error => "oops")

allow(conversion_host).to receive(:check_conversion_host_role)
allow(conversion_host).to receive(:find_credentials).and_return(auth_v2v)
allow(AwesomeSpawn).to receive(:run).and_return(result)

expect($log).to receive(:error).with("MIQ(ConversionHost#ansible_playbook) #{command} ==> oops")
expect { conversion_host.enable_conversion_host_role(package_url, nil) }.to raise_error(RuntimeError)
end
end
end

Expand Down Expand Up @@ -380,7 +395,6 @@
let(:ems_redhat) { FactoryBot.create(:ems_redhat, :zone => FactoryBot.create(:zone), :api_version => '4.2.4') }
let(:ems_openstack) { FactoryBot.create(:ems_openstack, :zone => FactoryBot.create(:zone)) }
let(:auth_default) { FactoryBot.create(:authentication) }
let(:auth_v2v) { FactoryBot.create(:authentication_v2v) }

let(:host) { FactoryBot.create(:host_redhat, :ext_management_system => ems_redhat) }
let(:vm) { FactoryBot.create(:vm_openstack, :ext_management_system => ems_openstack) }
Expand Down

0 comments on commit 23d2532

Please sign in to comment.