gitlab-org--gitlab-foss/spec/support/helpers/stub_object_storage.rb

79 lines
2.9 KiB
Ruby
Raw Normal View History

module StubObjectStorage
2018-03-22 16:13:06 -04:00
def stub_object_storage_uploader(
2018-03-23 06:07:22 -04:00
config:,
uploader:,
remote_directory:,
2018-03-22 16:13:06 -04:00
enabled: true,
proxy_download: false,
2018-03-23 06:07:22 -04:00
background_upload: false,
direct_upload: false
)
allow(config).to receive(:enabled) { enabled }
2018-03-22 14:37:47 -04:00
allow(config).to receive(:proxy_download) { proxy_download }
allow(config).to receive(:background_upload) { background_upload }
2018-03-23 06:07:22 -04:00
allow(config).to receive(:direct_upload) { direct_upload }
return unless enabled
2018-06-22 09:32:57 -04:00
stub_object_storage(connection_params: uploader.object_store_credentials,
remote_directory: remote_directory)
end
def stub_object_storage(connection_params:, remote_directory:)
2018-03-23 06:07:22 -04:00
Fog.mock!
2018-06-22 09:32:57 -04:00
::Fog::Storage.new(connection_params).tap do |connection|
2019-03-13 09:42:43 -04:00
connection.directories.create(key: remote_directory)
# Cleanup remaining files
connection.directories.each do |directory|
directory.files.map(&:destroy)
end
2019-03-13 09:42:43 -04:00
rescue Excon::Error::Conflict
end
end
def stub_artifacts_object_storage(**params)
stub_object_storage_uploader(config: Gitlab.config.artifacts.object_store,
uploader: JobArtifactUploader,
remote_directory: 'artifacts',
**params)
end
def stub_external_diffs_object_storage(uploader = described_class, **params)
stub_object_storage_uploader(config: Gitlab.config.external_diffs.object_store,
uploader: uploader,
remote_directory: 'external_diffs',
**params)
end
def stub_lfs_object_storage(**params)
stub_object_storage_uploader(config: Gitlab.config.lfs.object_store,
uploader: LfsObjectUploader,
remote_directory: 'lfs-objects',
**params)
end
def stub_uploads_object_storage(uploader = described_class, **params)
stub_object_storage_uploader(config: Gitlab.config.uploads.object_store,
uploader: uploader,
remote_directory: 'uploads',
**params)
end
2018-05-09 11:27:38 -04:00
def stub_object_storage_multipart_init(endpoint, upload_id = "upload_id")
2018-06-04 08:06:07 -04:00
stub_request(:post, %r{\A#{endpoint}tmp/uploads/[a-z0-9-]*\?uploads\z})
.to_return status: 200, body: <<-EOS.strip_heredoc
2018-05-09 11:27:38 -04:00
<?xml version="1.0" encoding="UTF-8"?>
<InitiateMultipartUploadResult xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
<Bucket>example-bucket</Bucket>
<Key>example-object</Key>
<UploadId>#{upload_id}</UploadId>
</InitiateMultipartUploadResult>
EOS
end
end
2019-04-10 11:46:16 -04:00
require_relative '../../../ee/spec/support/helpers/ee/stub_object_storage' if
Dir.exist?("#{__dir__}/../../../ee")