2018-03-29 05:47:54 -04:00
|
|
|
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
|
|
|
|
)
|
2017-09-07 17:27:04 -04:00
|
|
|
allow(config).to receive(:enabled) { enabled }
|
2018-03-22 14:37:47 -04:00
|
|
|
allow(config).to receive(:proxy_download) { proxy_download }
|
2017-12-08 04:09:06 -05:00
|
|
|
allow(config).to receive(:background_upload) { background_upload }
|
2018-03-23 06:07:22 -04:00
|
|
|
allow(config).to receive(:direct_upload) { direct_upload }
|
2017-09-07 17:27:04 -04:00
|
|
|
|
|
|
|
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)
|
2017-09-07 17:27:04 -04:00
|
|
|
end
|
2019-03-13 09:42:43 -04:00
|
|
|
rescue Excon::Error::Conflict
|
2017-09-07 17:27:04 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def stub_artifacts_object_storage(**params)
|
|
|
|
stub_object_storage_uploader(config: Gitlab.config.artifacts.object_store,
|
2017-12-05 09:31:33 -05:00
|
|
|
uploader: JobArtifactUploader,
|
2017-09-07 17:27:04 -04:00
|
|
|
remote_directory: 'artifacts',
|
|
|
|
**params)
|
|
|
|
end
|
|
|
|
|
2019-01-09 12:01:28 -05:00
|
|
|
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
|
|
|
|
|
2017-09-07 17:27:04 -04:00
|
|
|
def stub_lfs_object_storage(**params)
|
|
|
|
stub_object_storage_uploader(config: Gitlab.config.lfs.object_store,
|
|
|
|
uploader: LfsObjectUploader,
|
|
|
|
remote_directory: 'lfs-objects',
|
|
|
|
**params)
|
|
|
|
end
|
2018-02-02 08:59:43 -05:00
|
|
|
|
|
|
|
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
|
2017-09-07 17:27:04 -04:00
|
|
|
end
|
2019-04-10 11:46:16 -04:00
|
|
|
|
|
|
|
require_relative '../../../ee/spec/support/helpers/ee/stub_object_storage' if
|
|
|
|
Dir.exist?("#{__dir__}/../../../ee")
|