diff --git a/app/models/conversion_host.rb b/app/models/conversion_host.rb index 9da57ae7fcc..d800b0d6df8 100644 --- a/app/models/conversion_host.rb +++ b/app/models/conversion_host.rb @@ -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 diff --git a/spec/models/conversion_host_spec.rb b/spec/models/conversion_host_spec.rb index ef3669d6cc8..3e22129b092 100644 --- a/spec/models/conversion_host_spec.rb +++ b/spec/models/conversion_host_spec.rb @@ -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 = { @@ -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', @@ -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 @@ -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) }