Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[V2V] Add extra error handling for the ConversionHost#run_conversion method #18768

Merged
merged 1 commit into from
May 21, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions app/models/conversion_host.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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'
Expand Down
30 changes: 30 additions & 0 deletions spec/models/conversion_host_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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) }
Expand Down