Skip to content

Commit

Permalink
Add conditions to validate image cron job (#1467)
Browse files Browse the repository at this point in the history
* initial commit

* Update integrity_checkable.rb

* Update integrity_checkable_spec.rb

* Add fixture

* Update comments

---------

Co-authored-by: JP Engstrom <[email protected]>
  • Loading branch information
K8Sewell and JP Engstrom authored Dec 12, 2024
1 parent 177b0b3 commit 32d9635
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 7 deletions.
16 changes: 14 additions & 2 deletions app/models/concerns/integrity_checkable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ module IntegrityCheckable

# rubocop:disable Metrics/AbcSize
# rubocop:disable Metrics/MethodLength
# rubocop:disable Metrics/CyclomaticComplexity
# rubocop:disable Metrics/PerceivedComplexity
# rubocop:disable Layout/LineLength
def integrity_check
Expand All @@ -26,8 +27,18 @@ def integrity_check
add_admin_set_to_bp(sets, co)

if co.access_master_exists?
co.parent_object.processing_event("Integrity check complete for Child Object: #{co.oid}", 'review-complete')
co.processing_event("Child Object: #{co.oid} - file exists.", 'review-complete')
if co&.file_size&.positive?
if co&.access_master_checksum_matches?
co.parent_object.processing_event("Integrity check complete for Child Object: #{co.oid}", 'review-complete')
co.processing_event("Child Object: #{co.oid} - file exists and checksum matches.", 'review-complete')
else
co.parent_object.processing_event("Integrity check complete for Child Object: #{co.oid}", 'failed')
co.processing_event("The Child Object: #{co.oid} - has a checksum mismatch. The checksum of the image file saved to this child oid does not match the checksum of the image file in the database. This may mean that the image has been corrupted. Please verify integrity of image for Child Object: #{co.oid} - by manually comparing the checksum values and update record as necessary.", 'failed')
end
else
co.parent_object.processing_event("Integrity check complete for Child Object: #{co.oid}", 'failed')
co.processing_event("Child Object: #{co.oid} - has a file size of 0. Please verify image for Child Object: #{co.oid}.", 'failed')
end
else
co.parent_object.processing_event("Integrity check complete for Child Object: #{co.oid}", 'failed')
co.processing_event("Child Object: #{co.oid} - file not found at #{co.access_master_path} on #{ENV['ACCESS_MASTER_MOUNT']}.", 'failed')
Expand All @@ -42,6 +53,7 @@ def integrity_check
end
# rubocop:enable Metrics/AbcSize
# rubocop:enable Metrics/MethodLength
# rubocop:enable Metrics/CyclomaticComplexity
# rubocop:enable Metrics/PerceivedComplexity
# rubocop:enable Layout/LineLength
end
Binary file not shown.
15 changes: 10 additions & 5 deletions spec/models/concerns/integrity_checkable_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@
let(:parent_object_one) { FactoryBot.create(:parent_object, oid: '111', child_object_count: 1, authoritative_metadata_source: metadata_source, admin_set: admin_set) }
let(:parent_object_two) { FactoryBot.create(:parent_object, oid: '222', child_object_count: 1, authoritative_metadata_source: metadata_source, admin_set: admin_set) }
let(:parent_object_three) { FactoryBot.create(:parent_object, oid: '333', child_object_count: 1, authoritative_metadata_source: metadata_source, admin_set: admin_set) }
let(:parent_object_four) { FactoryBot.create(:parent_object, oid: '444', child_object_count: 1, authoritative_metadata_source: metadata_source, admin_set: admin_set) }
let(:child_object_one) { FactoryBot.create(:child_object, oid: '1', parent_object: parent_object_one) }
let(:child_object_two) { FactoryBot.create(:child_object, oid: '356789', parent_object: parent_object_two, checksum: '78909999999999999') }
let(:child_object_three) { FactoryBot.create(:child_object, oid: '456789', parent_object: parent_object_three, checksum: 'f3755c5d9e086b4522a0d3916e9a0bfcbd47564e') }
let(:child_object_three) { FactoryBot.create(:child_object, oid: '456789', parent_object: parent_object_three, file_size: 1234, checksum: 'f3755c5d9e086b4522a0d3916e9a0bfcbd47564e') }
let(:child_object_four) { FactoryBot.create(:child_object, oid: '567890', parent_object: parent_object_four, file_size: 1234, checksum: 'f3755c5d9e086b4522a0d3916e9a0bfcbd47564et') }

around do |example|
original_access_master_mount = ENV["ACCESS_MASTER_MOUNT"]
Expand All @@ -28,18 +30,21 @@
before do
# file not present
stub_request(:get, File.join(child_object_one.access_master_path)).to_return(status: 200, body: '')
# file present but checksum does not match
# file present but file size is less than 0
stub_request(:get, File.join(child_object_two.access_master_path)).to_return(status: 200, body: File.open(File.join(child_object_two.access_master_path)).read)
# file present and checksum matches
stub_request(:get, File.join(child_object_three.access_master_path)).to_return(status: 200, body: File.open(File.join(child_object_three.access_master_path)).read)
# file present and file size greater than 0 but checksum does not match
stub_request(:get, File.join(child_object_four.access_master_path)).to_return(status: 200, body: File.open(File.join(child_object_three.access_master_path)).read)
end

it 'reflects messages as expected' do
expect { ChildObjectIntegrityCheckJob.new.perform }.to change { IngestEvent.count }.by(7)
expect { ChildObjectIntegrityCheckJob.new.perform }.to change { IngestEvent.count }.by(9)
# rubocop:disable Layout/LineLength
expect(child_object_one.events_for_batch_process(BatchProcess.first)[0].reason).to eq "Child Object: #{child_object_one.oid} - file not found at #{child_object_one.access_master_path} on #{ENV['ACCESS_MASTER_MOUNT']}."
expect(child_object_two.events_for_batch_process(BatchProcess.first)[0].reason).to eq "Child Object: #{child_object_two.oid} - file exists."
expect(child_object_three.events_for_batch_process(BatchProcess.first)[0].reason).to eq "Child Object: #{child_object_three.oid} - file exists."
expect(child_object_two.events_for_batch_process(BatchProcess.first)[0].reason).to eq "Child Object: #{child_object_two.oid} - has a file size of 0. Please verify image for Child Object: #{child_object_two.oid}."
expect(child_object_three.events_for_batch_process(BatchProcess.first)[0].reason).to eq "Child Object: #{child_object_three.oid} - file exists and checksum matches."
expect(child_object_four.events_for_batch_process(BatchProcess.first)[0].reason).to eq "The Child Object: #{child_object_four.oid} - has a checksum mismatch. The checksum of the image file saved to this child oid does not match the checksum of the image file in the database. This may mean that the image has been corrupted. Please verify integrity of image for Child Object: #{child_object_four.oid} - by manually comparing the checksum values and update record as necessary."
# rubocop:enable Layout/LineLength
end
end
Expand Down

0 comments on commit 32d9635

Please sign in to comment.