Skip to content

Commit

Permalink
Add tests for SourceObject#storage_providers_for_source_path
Browse files Browse the repository at this point in the history
  • Loading branch information
elohanlon committed Jun 13, 2024
1 parent b7f8a9f commit ac4cd7e
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 2 deletions.
10 changes: 10 additions & 0 deletions app/models/source_object.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# frozen_string_literal: true

# rubocop:disable Metrics/AbcSize
# rubocop:disable Metrics/CyclomaticComplexity
# rubocop:disable Metrics/PerceivedComplexity
# rubocop:disable Metrics/MethodLength

class SourceObject < ApplicationRecord
Expand Down Expand Up @@ -43,6 +45,14 @@ def storage_providers_for_source_path
storage_providers << storage_provider unless storage_provider.nil?
end
end
# If this method is being called, we expect there to be a storage provider for this source_object's path.
# So if no storage_providers were found, raise an exception.
if storage_providers.empty?
raise Atc::Exceptions::StorageProviderMappingNotFound,
'Could not find an atc.yml storage provider that maps to the path '\
"for SourceObject #{self.id} (path = #{self.path})"
end

storage_providers
end
end
Expand Down
65 changes: 63 additions & 2 deletions spec/models/source_object_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@

describe SourceObject do
let(:sha256_checksum_algorithm) { FactoryBot.create(:checksum_algorithm, :sha256) }
let(:source_object) { FactoryBot.create(:source_object) }

describe 'validations' do
subject(:source_object) { FactoryBot.create(:source_object) }

it 'has a path_hash of 32 bytes' do
expect(source_object.path_hash.bytesize).to be 32
end
Expand Down Expand Up @@ -92,4 +91,66 @@
expect(source_object.fixity_checksum_value.length).to eq(64)
end
end

describe '#storage_providers_for_source_path' do
let(:path_prefix) { source_object.path.match(%r{/[^/]+/})[0] }
let!(:aws_storage_provider) { FactoryBot.create(:storage_provider, :aws) }
let!(:gcp_storage_provider) { FactoryBot.create(:storage_provider, :gcp) }

context 'with matching source providers defined in atc.yml' do
before do
stub_const('ATC', ATC.merge({
source_paths_to_storage_providers: {
# Add a mapping for our source_object's path
path_prefix.to_sym => {
path_mapping: '',
storage_providers: [
{
storage_type: aws_storage_provider.storage_type,
container_name: aws_storage_provider.container_name
},
{
storage_type: gcp_storage_provider.storage_type,
container_name: gcp_storage_provider.container_name
}
]
}
}
}))
end

it 'returns the expected source providers' do
expect(source_object.storage_providers_for_source_path).to eq([aws_storage_provider, gcp_storage_provider])
end
end

context 'with NON-matching source providers defined in atc.yml' do
before do
stub_const('ATC', ATC.merge({
source_paths_to_storage_providers: {
# Add a mapping for our source_object's path
'/path/does/not/match/': {
path_mapping: '',
storage_providers: [
{
storage_type: aws_storage_provider.storage_type,
container_name: aws_storage_provider.container_name
},
{
storage_type: gcp_storage_provider.storage_type,
container_name: gcp_storage_provider.container_name
}
]
}
}
}))
end

it "raises an exception because this source_object's path cannot be resolved to a StorageProvider" do
expect {
source_object.storage_providers_for_source_path
}.to raise_error(Atc::Exceptions::StorageProviderMappingNotFound)
end
end
end
end

0 comments on commit ac4cd7e

Please sign in to comment.