Skip to content

Commit

Permalink
Merge pull request #18768 from djberg96/conversion_host_json_parsing
Browse files Browse the repository at this point in the history
[V2V] Add extra error handling for the ConversionHost#run_conversion method

(cherry picked from commit d1ea5d1)

https://bugzilla.redhat.com/show_bug.cgi?id=1712135
  • Loading branch information
agrare authored and simaishi committed May 22, 2019
1 parent 5aab904 commit ec6d2a4
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
8 changes: 6 additions & 2 deletions app/models/conversion_host.rb
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,12 @@ def ipaddress(family = 'ipv4')
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

def kill_process(pid, signal = '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 @@ -459,6 +459,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 "address validation" do
let(:vm) { FactoryBot.create(:vm_openstack) }

Expand Down

0 comments on commit ec6d2a4

Please sign in to comment.