Skip to content

Commit

Permalink
Merge pull request #18258 from djberg96/get_conversion_state
Browse files Browse the repository at this point in the history
Add more robust error handling to ConversionHost#get_conversion_state

(cherry picked from commit f73e7e6)

Fixes https://bugzilla.redhat.com/show_bug.cgi?id=1702055
  • Loading branch information
agrare authored and simaishi committed Apr 22, 2019
1 parent d003c2d commit 7709b44
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
11 changes: 9 additions & 2 deletions app/models/conversion_host.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,18 @@ def kill_process(pid, signal = 'TERM')
false
end

# Retrieve the conversion state information from a remote file as a stream.
# Then parse and return the stream data as a hash using JSON.parse.
#
def get_conversion_state(path)
json_state = connect_ssh { |ssu| ssu.get_file(path, nil) }
JSON.parse(json_state)
rescue => e
raise "Could not get state file '#{path}' from '#{resource.name}' with [#{e.class}: #{e}"
rescue MiqException::MiqInvalidCredentialsError, MiqException::MiqSshUtilHostKeyMismatch => err
raise "Failed to connect and retrieve conversion state data from file '#{path}' with [#{err.class}: #{err}"
rescue JSON::ParserError
raise "Could not parse conversion state data from file '#{path}': #{json_state}"
rescue StandardError => err
raise "Error retrieving and parsing conversion state file '#{path}' from '#{resource.name}' with [#{err.class}: #{err}"
end

def get_conversion_log(path)
Expand Down
29 changes: 29 additions & 0 deletions spec/models/conversion_host_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -221,4 +221,33 @@
expect(conversion_host.name).to eql(redhat_host.name)
end
end

context "#get_conversion_state" do
let(:vm) { FactoryBot.create(:vm_openstack) }
let(:conversion_host) { FactoryBot.create(:conversion_host, :resource => vm) }
let(:path) { 'some_path' }

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.get_conversion_state(path)).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 conversion state data from file '#{path}': bogus"
expect { conversion_host.get_conversion_state(path) }.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 retrieve conversion state data from file '#{path}'"
expect { conversion_host.get_conversion_state(path) }.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 = "Error retrieving and parsing conversion state file '#{path}' from '#{vm.name}'"
expect { conversion_host.get_conversion_state(path) }.to raise_error(/#{expected_message}/)
end
end
end

0 comments on commit 7709b44

Please sign in to comment.