Skip to content

Commit

Permalink
Add MiqLocalMountSession
Browse files Browse the repository at this point in the history
This mount session is intended as being a pass through for when local
files are used.

The same API's for `#add` from MiqGenericMountSession are still usable
for this class, and it will just write to the local file system instead.

This also allows us to test the file splitting code without needing a
mount session in place to do so.
  • Loading branch information
NickLaMuro committed Aug 13, 2018
1 parent c3cb1f9 commit 8476d86
Show file tree
Hide file tree
Showing 2 changed files with 127 additions and 0 deletions.
37 changes: 37 additions & 0 deletions lib/gems/pending/util/mount/miq_local_mount_session.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
require 'util/mount/miq_generic_mount_session'

# MiqLocalMountSession is meant to be a representation of the local file system
# that conforms to the same interface as MiqLocalMountSession (and by proxy,
# MiqFileSystem::Interface).
#
# See MiqGenericMountSession for info on methods available.
class MiqLocalMountSession < MiqGenericMountSession
def self.uri_scheme
"file".freeze
end

# no-op these since they are not relavent to the local file system
#
# rubocop:disable Style/SingleLineMethods, Layout/EmptyLineBetweenDefs
def connect; end # :nodoc:
def disconnect; end # :nodoc:
def mount_share; end # :nodoc:
# rubocop:enable Style/SingleLineMethods, Layout/EmptyLineBetweenDefs

def relative_to_mount(dest_uri) # :nodoc:
dest_uri
end

# :nodoc:
#
# This is an override to allow methods that make use of `@mnt_point` to
# behave as we would expect them to.
#
# Determine the mount point relative to the destination file, and just remove
# the portions of the expanded path that already exist in the
# `remote_file_path` (might be everything if `remote_file_path` is a absolute
# path)
def mnt_point
@mnt_point ||= File.expand_path(remote_file_path).sub(remote_file_path, "")
end
end
90 changes: 90 additions & 0 deletions spec/util/mount/miq_local_mount_session_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
require 'util/mount/miq_local_mount_session'
require 'tempfile'

describe MiqLocalMountSession do
shared_context "generated tmp files" do
let!(:tmpfile_size) { 10.megabytes }
let!(:source_path) { Pathname.new(source_file.path) }
let!(:source_file) do
Tempfile.new("source_file").tap do |file|
file.write("0" * tmpfile_size)
file.close
end
end
let!(:dest_path) do
Pathname.new(Dir::Tmpname.create("") {})
end

after do
source_file.unlink
Dir["#{source_path.expand_path}.*"].each do |file|
File.delete(file)
end
end
end

subject { described_class.new(:uri => "file://") }

describe "#add" do
include_context "generated tmp files"

it "copies single files" do
expect(subject.add(source_path.to_s, dest_path.to_s)).to eq(dest_path.to_s)
expect(File.exist?(dest_path)).to be true
expect(Pathname.new(dest_path).lstat.size).to eq(10.megabytes)
end

it "copies file to splits" do
expected_splitfiles = (1..5).map do |suffix|
source_path.dirname.join("#{dest_path.basename}.0000#{suffix}")
end

File.open(source_path) do |f| # with an IO object this time
subject.add(f, dest_path.to_s, "2M")
end

expected_splitfiles.each do |filename|
expect(File.exist?(filename)).to be true
expect(Pathname.new(filename).lstat.size).to eq(2.megabytes)
end
end

it "can take input from a command" do
expected_splitfiles = (1..5).map do |suffix|
source_path.dirname.join("#{dest_path.basename}.0000#{suffix}")
end

subject.add(dest_path.to_s, "2M") do |input_writer|
`#{Gem.ruby} -e "File.write('#{input_writer}', '0' * #{tmpfile_size})"`
end

expected_splitfiles.each do |filename|
expect(File.exist?(filename)).to be true
expect(Pathname.new(filename).lstat.size).to eq(2.megabytes)
end
end

context "with slightly a slightly smaller input file than 10MB" do
let(:tmpfile_size) { 10.megabytes - 1.kilobyte }

it "properly chunks the file" do
expected_splitfiles = (1..10).map do |suffix|
name = "#{dest_path.basename}.%<suffix>05d" % {:suffix => suffix}
source_path.dirname.join(name)
end

# using pathnames this time
subject.add(source_path, dest_path.to_s, 1.megabyte)

expected_splitfiles[0, 9].each do |filename|
expect(File.exist?(filename)).to be true
expect(Pathname.new(filename).lstat.size).to eq(1.megabyte)
end

last_split = expected_splitfiles.last
expect(File.exist?(last_split)).to be true
expect(Pathname.new(last_split).lstat.size).to eq(1.megabyte - 1.kilobyte)
end
end
end
end

0 comments on commit 8476d86

Please sign in to comment.