Skip to content

Commit

Permalink
Add code, fixture, spec for zipmaker worker
Browse files Browse the repository at this point in the history
  Fixes #707
  • Loading branch information
tallenaz committed May 25, 2018
1 parent b415300 commit 3d19844
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 3 deletions.
21 changes: 21 additions & 0 deletions app/jobs/zipmaker_job.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require 'open3'
# Responsibilities:
# locate files
# zip files
Expand All @@ -9,6 +10,26 @@ class ZipmakerJob < ApplicationJob
# @param [String] druid
# @param [Integer] version
def perform(druid, version)
binary_path = Moab::StorageServices.object_version_path(druid, version)
zip_path = DruidVersionZip.new(druid, version).file_path
unless File.exist?(zip_path)
ZipmakerJob.zip_binary(zip_path, binary_path) if binary_path
end
PlexerJob.perform_later(druid, version)
end

# @param [String] path to druid version zip
# @param [String] path to druid version
# @todo calculate md5 of zip for plexer
def self.zip_binary(zip_path, binary_path)
_output, error, status = Open3.capture3(zip_command(zip_path, binary_path))
raise "zipmaker failure #{error}" unless status.success?
end

# @param [String] path to druid version zip
# @param [String] path to druid version
# @return [String]
def self.zip_command(zip_path, binary_path)
"zip -vr0X -s 10g #{zip_path} #{binary_path}"
end
end
Empty file.
70 changes: 67 additions & 3 deletions spec/jobs/zipmaker_job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,77 @@
describe ZipmakerJob, type: :job do
let(:druid) { 'bj102hs9687' }
let(:version) { 1 }
let(:binary_path) { Moab::StorageServices.object_version_path(druid, version) }
let(:zip_path) { "spec/fixtures/transfers/bj/102/hs/9687/#{druid}#{format('.v%04d.zip', version)}" }

before do
allow(PlexerJob).to receive(:perform_later).with(any_args)
allow(Settings).to receive(:zip_storage).and_return('spec/fixtures/transfers')
end

it 'descends from ApplicationJob' do
expect(described_class.new).to be_an(ApplicationJob)
end

it 'invokes PlexerJob' do
expect(PlexerJob).to receive(:perform_later).with(druid, version)
described_class.perform_now(druid, version)
describe 'zip already exists in zip storage' do

it 'does nothing' do
expect(File).to exist(zip_path)
expect(described_class).not_to receive(:zip_binary)
described_class.perform_now(druid, version)
end
end

describe 'zip is not yet in zip storage' do
let(:version) { 2 }

after { File.delete(zip_path) }

it 'zips up the druid version into zip storage' do
described_class.perform_now(druid, version)
expect(File).to exist(zip_path)
end

it 'invokes PlexerJob' do
expect(PlexerJob).to receive(:perform_later).with(druid, version)
described_class.perform_now(druid, version)
end
end

describe '.zip_binary' do
let(:version) { 2 }

describe 'succeeds in zipping the binary' do
it 'does not raise an error' do
expect { described_class.zip_binary(zip_path, binary_path) }.not_to raise_error
File.delete(zip_path)
end
end

describe 'fails to zip the binary' do
describe 'when inpath is incorrect' do
it 'raises error' do
expect { described_class.zip_binary(zip_path, 'bar') }.to raise_error(RuntimeError, /zipmaker failure/)
end
end
describe 'when options are unsupported' do
it 'raises error' do
allow(described_class).to receive(:zip_command).and_return("zip -a #{zip_path} #{binary_path}")
expect { described_class.zip_binary(zip_path, binary_path) }.to raise_error(RuntimeError, /zipmaker failure/)
end
end
describe 'if the utility "moved"' do
it 'raises error' do
allow(described_class).to receive(:zip_command).and_return("zap -vr0X -s 10g #{zip_path} #{binary_path}")
expect { described_class.zip_binary(zip_path, binary_path) }.to raise_error(Errno::ENOENT, /No such file/)
end
end
end

describe '.zip_command' do
it 'returns a string representing the command to zip' do
expect(described_class.zip_command(zip_path, binary_path)).to eq "zip -vr0X -s 10g #{zip_path} #{binary_path}"
end
end
end
end

0 comments on commit 3d19844

Please sign in to comment.