2019-10-12 21:52:04 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-10-15 14:42:47 +05:30
|
|
|
module StubObjectStorage
|
2018-05-09 12:01:36 +05:30
|
|
|
def stub_object_storage_uploader(
|
|
|
|
config:,
|
|
|
|
uploader:,
|
|
|
|
remote_directory:,
|
|
|
|
enabled: true,
|
|
|
|
proxy_download: false,
|
|
|
|
background_upload: false,
|
|
|
|
direct_upload: false
|
|
|
|
)
|
|
|
|
allow(config).to receive(:enabled) { enabled }
|
|
|
|
allow(config).to receive(:proxy_download) { proxy_download }
|
|
|
|
allow(config).to receive(:background_upload) { background_upload }
|
|
|
|
allow(config).to receive(:direct_upload) { direct_upload }
|
|
|
|
|
|
|
|
return unless enabled
|
|
|
|
|
2018-11-08 19:23:39 +05:30
|
|
|
stub_object_storage(connection_params: uploader.object_store_credentials,
|
|
|
|
remote_directory: remote_directory)
|
|
|
|
end
|
|
|
|
|
|
|
|
def stub_object_storage(connection_params:, remote_directory:)
|
2018-05-09 12:01:36 +05:30
|
|
|
Fog.mock!
|
|
|
|
|
2018-11-08 19:23:39 +05:30
|
|
|
::Fog::Storage.new(connection_params).tap do |connection|
|
2019-07-07 11:18:12 +05:30
|
|
|
connection.directories.create(key: remote_directory)
|
|
|
|
|
|
|
|
# Cleanup remaining files
|
|
|
|
connection.directories.each do |directory|
|
|
|
|
directory.files.map(&:destroy)
|
2018-05-09 12:01:36 +05:30
|
|
|
end
|
2019-07-07 11:18:12 +05:30
|
|
|
rescue Excon::Error::Conflict
|
2018-05-09 12:01:36 +05:30
|
|
|
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
|
|
|
|
|
2019-03-02 22:35:43 +05:30
|
|
|
def stub_external_diffs_object_storage(uploader = described_class, **params)
|
|
|
|
stub_object_storage_uploader(config: Gitlab.config.external_diffs.object_store,
|
|
|
|
uploader: uploader,
|
2020-05-24 23:13:21 +05:30
|
|
|
remote_directory: 'external-diffs',
|
2019-03-02 22:35:43 +05:30
|
|
|
**params)
|
|
|
|
end
|
|
|
|
|
2018-05-09 12:01:36 +05:30
|
|
|
def stub_lfs_object_storage(**params)
|
|
|
|
stub_object_storage_uploader(config: Gitlab.config.lfs.object_store,
|
|
|
|
uploader: LfsObjectUploader,
|
|
|
|
remote_directory: 'lfs-objects',
|
|
|
|
**params)
|
|
|
|
end
|
2020-01-01 13:55:28 +05:30
|
|
|
|
|
|
|
def stub_package_file_object_storage(**params)
|
|
|
|
stub_object_storage_uploader(config: Gitlab.config.packages.object_store,
|
|
|
|
uploader: ::Packages::PackageFileUploader,
|
|
|
|
remote_directory: 'packages',
|
|
|
|
**params)
|
|
|
|
end
|
2018-05-09 12:01:36 +05:30
|
|
|
|
|
|
|
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-11-08 19:23:39 +05:30
|
|
|
|
2020-04-22 19:07:51 +05:30
|
|
|
def stub_terraform_state_object_storage(uploader = described_class, **params)
|
|
|
|
stub_object_storage_uploader(config: Gitlab.config.terraform_state.object_store,
|
|
|
|
uploader: uploader,
|
|
|
|
remote_directory: 'terraform_state',
|
|
|
|
**params)
|
|
|
|
end
|
|
|
|
|
2018-11-08 19:23:39 +05:30
|
|
|
def stub_object_storage_multipart_init(endpoint, upload_id = "upload_id")
|
|
|
|
stub_request(:post, %r{\A#{endpoint}tmp/uploads/[a-z0-9-]*\?uploads\z})
|
|
|
|
.to_return status: 200, body: <<-EOS.strip_heredoc
|
|
|
|
<?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
|
2018-05-09 12:01:36 +05:30
|
|
|
end
|
2019-07-31 22:56:46 +05:30
|
|
|
|
|
|
|
require_relative '../../../ee/spec/support/helpers/ee/stub_object_storage' if
|
|
|
|
Dir.exist?("#{__dir__}/../../../ee")
|
2019-12-04 20:38:33 +05:30
|
|
|
|
|
|
|
StubObjectStorage.prepend_if_ee('EE::StubObjectStorage')
|