Skip to content

Commit

Permalink
[MiqFtpStorage] Add #magic_number_for support
Browse files Browse the repository at this point in the history
Since we need a connection for any download/upload to work (sets @ftp on
the instance), we need a slight override of `#magic_number_for` to wrap
it with a connection.
  • Loading branch information
NickLaMuro committed Nov 7, 2018
1 parent 26ca237 commit 19f754c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 12 deletions.
5 changes: 5 additions & 0 deletions lib/gems/pending/util/object_storage/miq_ftp_storage.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ def download(*download_args)
with_connection { super }
end

# Override for connection handling
def magic_number_for(*magic_number_for_args)
with_connection { super }
end

# Specific version of Net::FTP#storbinary that doesn't use an existing local
# file, and only uploads a specific size (byte_count) from the input_file
def upload_single(dest_uri)
Expand Down
29 changes: 17 additions & 12 deletions spec/util/object_storage/miq_ftp_storage_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -115,21 +115,26 @@
# Nothing written, just printed the streamed file in the above command
expect(source_data.size).to eq(10.megabytes)
end
end

# Note, we are using `#download` in the spec, but really, this is a feature
# for `#download_single`. `#download` really doesn't make use of this
# directly.
it "respects the `byte_count` attribute" do
downloaded_data = StringIO.new
subject.instance_variable_set(:@byte_count, 42)
subject.download(downloaded_data, source_path)
describe "#magic_number_for" do
let(:source_file) { existing_ftp_file(10.megabytes) }
let(:source_path) { File.basename(source_file.path) }

# Sanity check that what we are downloading is the size we expect
expect(source_path).to exist_on_ftp_server
expect(source_path).to have_size_on_ftp_server_of(10.megabytes)
it "returns 256 bytes by default" do
result = subject.magic_number_for(source_path)

expect(result.size).to eq(256)
expect(result).to eq("0" * 256)
end

expect(downloaded_data.string.size).to eq(42)
expect(downloaded_data.string).to eq("0" * 42)
describe "with a hash of accepted magics" do
it "returns key for the passed in magic number value" do
magics = { :zero => "000", :one => "1", :foo => "bar" }
result = subject.magic_number_for(source_path, :accepted => magics)

expect(result).to eq(:zero)
end
end
end
end

0 comments on commit 19f754c

Please sign in to comment.