diff --git a/app/models/conversion_host.rb b/app/models/conversion_host.rb index 9da57ae7fcc..c8a7e8f7c07 100644 --- a/app/models/conversion_host.rb +++ b/app/models/conversion_host.rb @@ -144,7 +144,7 @@ def ipaddress(family = 'ipv4') def apply_task_limits(path, limits = {}) connect_ssh { |ssu| ssu.put_file(path, limits.to_json) } rescue MiqException::MiqInvalidCredentialsError, MiqException::MiqSshUtilHostKeyMismatch => err - raise "Failed to connect and apply limits in file '#{path}' with [#{err.class}: #{err}" + raise "Failed to connect and apply limits in file '#{path}' with [#{err.class}: #{err}]" rescue JSON::GeneratorError => err raise "Could not generate JSON from limits '#{limits}' with [#{err.class}: #{err}]" rescue StandardError => err @@ -154,8 +154,12 @@ def apply_task_limits(path, limits = {}) def run_conversion(conversion_options) result = connect_ssh { |ssu| ssu.shell_exec('/usr/bin/virt-v2v-wrapper.py', nil, nil, conversion_options.to_json) } JSON.parse(result) - rescue => e - raise "Starting conversion failed on '#{resource.name}' with [#{e.class}: #{e}]" + rescue MiqException::MiqInvalidCredentialsError, MiqException::MiqSshUtilHostKeyMismatch => err + raise "Failed to connect and run conversion using options #{conversion_options} with [#{err.class}: #{err}]" + rescue JSON::ParserError + raise "Could not parse result data after running virt-v2v-wrapper.py using options: #{conversion_options}. Result was: #{result}." + rescue StandardError => err + raise "Starting conversion failed on '#{resource.name}' with [#{err.class}: #{err}]" end # Kill a specific remote process over ssh, sending the specified +signal+, or 'TERM' diff --git a/spec/models/conversion_host_spec.rb b/spec/models/conversion_host_spec.rb index ef3669d6cc8..256a882ad6f 100644 --- a/spec/models/conversion_host_spec.rb +++ b/spec/models/conversion_host_spec.rb @@ -444,6 +444,36 @@ end end + context "#run_conversion" do + let(:vm) { FactoryBot.create(:vm_openstack) } + let(:conversion_host) { FactoryBot.create(:conversion_host, :resource => vm) } + let(:conversion_options) { {:foo => 1, :bar => 'hello' } } + + it "works as expected if the connection is successful and the JSON is valid" do + allow(conversion_host).to receive(:connect_ssh).and_return({:alpha => {:beta => 'hello'}}.to_json) + expect(conversion_host.run_conversion(conversion_options)).to eql('alpha' => {'beta' => 'hello'}) + end + + it "works as expected if the connection is successful but the JSON is invalid" do + allow(conversion_host).to receive(:connect_ssh).and_return('bogus') + expected_message = "Could not parse result data after running virt-v2v-wrapper.py using "\ + "options: #{conversion_options}. Result was: bogus." + expect { conversion_host.run_conversion(conversion_options) }.to raise_error(expected_message) + end + + it "works as expected if the connection is unsuccessful" do + allow(conversion_host).to receive(:connect_ssh).and_raise(MiqException::MiqInvalidCredentialsError) + expected_message = "Failed to connect and run conversion using options #{conversion_options}" + expect { conversion_host.run_conversion(conversion_options) }.to raise_error(/#{expected_message}/) + end + + it "works as expected if an unknown error occurs" do + allow(conversion_host).to receive(:connect_ssh).and_raise(StandardError) + expected_message = "Starting conversion failed on '#{vm.name}'" + expect { conversion_host.run_conversion(conversion_options) }.to raise_error(/#{expected_message}/) + end + end + context "#get_conversion_state" do let(:vm) { FactoryBot.create(:vm_openstack) } let(:conversion_host) { FactoryBot.create(:conversion_host, :resource => vm) }