2019-10-12 21:52:04 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-10-15 14:42:47 +05:30
|
|
|
module StubObjectStorage
|
2020-07-28 23:09:34 +05:30
|
|
|
def stub_dependency_proxy_object_storage(**params)
|
2023-06-20 00:43:36 +05:30
|
|
|
stub_object_storage_uploader(
|
|
|
|
config: ::Gitlab.config.dependency_proxy.object_store,
|
|
|
|
uploader: ::DependencyProxy::FileUploader,
|
|
|
|
**params
|
|
|
|
)
|
2020-07-28 23:09:34 +05:30
|
|
|
end
|
|
|
|
|
2018-05-09 12:01:36 +05:30
|
|
|
def stub_object_storage_uploader(
|
2020-11-24 15:15:51 +05:30
|
|
|
config:,
|
2018-05-09 12:01:36 +05:30
|
|
|
uploader:,
|
|
|
|
enabled: true,
|
|
|
|
proxy_download: false,
|
2022-11-25 23:54:43 +05:30
|
|
|
direct_upload: false,
|
|
|
|
cdn: {}
|
2018-05-09 12:01:36 +05:30
|
|
|
)
|
2023-07-09 08:55:56 +05:30
|
|
|
old_config = ::GitlabSettings::Options.build(config.to_h.deep_stringify_keys)
|
2021-02-22 17:27:13 +05:30
|
|
|
new_config = config.to_h.deep_symbolize_keys.merge({
|
|
|
|
enabled: enabled,
|
|
|
|
proxy_download: proxy_download,
|
2022-11-25 23:54:43 +05:30
|
|
|
direct_upload: direct_upload,
|
|
|
|
cdn: cdn
|
2021-02-22 17:27:13 +05:30
|
|
|
})
|
|
|
|
|
|
|
|
# Needed for ObjectStorage::Config compatibility
|
|
|
|
allow(config).to receive(:to_hash).and_return(new_config)
|
|
|
|
allow(config).to receive(:to_h).and_return(new_config)
|
2018-05-09 12:01:36 +05:30
|
|
|
allow(config).to receive(:enabled) { enabled }
|
|
|
|
allow(config).to receive(:proxy_download) { proxy_download }
|
|
|
|
allow(config).to receive(:direct_upload) { direct_upload }
|
|
|
|
|
2023-07-09 08:55:56 +05:30
|
|
|
uploader_config = ::GitlabSettings::Options.build(new_config.to_h.deep_stringify_keys)
|
2022-11-25 23:54:43 +05:30
|
|
|
allow(uploader).to receive(:object_store_options).and_return(uploader_config)
|
|
|
|
allow(uploader.options).to receive(:object_store).and_return(uploader_config)
|
|
|
|
|
2018-05-09 12:01:36 +05:30
|
|
|
return unless enabled
|
|
|
|
|
2023-06-20 00:43:36 +05:30
|
|
|
stub_object_storage(
|
|
|
|
connection_params: uploader.object_store_credentials,
|
|
|
|
remote_directory: old_config.remote_directory
|
|
|
|
)
|
2018-11-08 19:23:39 +05:30
|
|
|
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|
|
2020-10-24 23:57:45 +05:30
|
|
|
connection.directories.create(key: remote_directory) # rubocop:disable Rails/SaveBang
|
2019-07-07 11:18:12 +05:30
|
|
|
|
|
|
|
# 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
|
|
|
|
|
2020-10-24 23:57:45 +05:30
|
|
|
def stub_artifacts_object_storage(uploader = JobArtifactUploader, **params)
|
2023-06-20 00:43:36 +05:30
|
|
|
stub_object_storage_uploader(
|
|
|
|
config: Gitlab.config.artifacts.object_store,
|
|
|
|
uploader: uploader,
|
|
|
|
**params
|
|
|
|
)
|
2018-05-09 12:01:36 +05:30
|
|
|
end
|
|
|
|
|
2019-03-02 22:35:43 +05:30
|
|
|
def stub_external_diffs_object_storage(uploader = described_class, **params)
|
2023-06-20 00:43:36 +05:30
|
|
|
stub_object_storage_uploader(
|
|
|
|
config: Gitlab.config.external_diffs.object_store,
|
|
|
|
uploader: uploader,
|
|
|
|
**params
|
|
|
|
)
|
2019-03-02 22:35:43 +05:30
|
|
|
end
|
|
|
|
|
2018-05-09 12:01:36 +05:30
|
|
|
def stub_lfs_object_storage(**params)
|
2023-06-20 00:43:36 +05:30
|
|
|
stub_object_storage_uploader(
|
|
|
|
config: Gitlab.config.lfs.object_store,
|
|
|
|
uploader: LfsObjectUploader,
|
|
|
|
**params
|
|
|
|
)
|
2018-05-09 12:01:36 +05:30
|
|
|
end
|
2020-01-01 13:55:28 +05:30
|
|
|
|
|
|
|
def stub_package_file_object_storage(**params)
|
2023-06-20 00:43:36 +05:30
|
|
|
stub_object_storage_uploader(
|
|
|
|
config: Gitlab.config.packages.object_store,
|
|
|
|
uploader: ::Packages::PackageFileUploader,
|
|
|
|
**params
|
|
|
|
)
|
2020-01-01 13:55:28 +05:30
|
|
|
end
|
2018-05-09 12:01:36 +05:30
|
|
|
|
2022-11-25 23:54:43 +05:30
|
|
|
def stub_rpm_repository_file_object_storage(**params)
|
2023-06-20 00:43:36 +05:30
|
|
|
stub_object_storage_uploader(
|
|
|
|
config: Gitlab.config.packages.object_store,
|
|
|
|
uploader: ::Packages::Rpm::RepositoryFileUploader,
|
|
|
|
**params
|
|
|
|
)
|
2022-11-25 23:54:43 +05:30
|
|
|
end
|
|
|
|
|
2021-03-11 19:13:27 +05:30
|
|
|
def stub_composer_cache_object_storage(**params)
|
2023-06-20 00:43:36 +05:30
|
|
|
stub_object_storage_uploader(
|
|
|
|
config: Gitlab.config.packages.object_store,
|
|
|
|
uploader: ::Packages::Composer::CacheUploader,
|
|
|
|
**params
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def debian_component_file_object_storage(**params)
|
|
|
|
stub_object_storage_uploader(
|
|
|
|
config: Gitlab.config.packages.object_store,
|
|
|
|
uploader: ::Packages::Debian::ComponentFileUploader,
|
|
|
|
**params
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def debian_distribution_release_file_object_storage(**params)
|
|
|
|
stub_object_storage_uploader(
|
|
|
|
config: Gitlab.config.packages.object_store,
|
|
|
|
uploader: ::Packages::Debian::DistributionReleaseFileUploader,
|
|
|
|
**params
|
|
|
|
)
|
2021-03-11 19:13:27 +05:30
|
|
|
end
|
|
|
|
|
2018-05-09 12:01:36 +05:30
|
|
|
def stub_uploads_object_storage(uploader = described_class, **params)
|
2023-06-20 00:43:36 +05:30
|
|
|
stub_object_storage_uploader(
|
|
|
|
config: Gitlab.config.uploads.object_store,
|
|
|
|
uploader: uploader,
|
|
|
|
**params
|
|
|
|
)
|
2018-05-09 12:01:36 +05:30
|
|
|
end
|
2018-11-08 19:23:39 +05:30
|
|
|
|
2022-03-02 08:16:31 +05:30
|
|
|
def stub_ci_secure_file_object_storage(**params)
|
2023-06-20 00:43:36 +05:30
|
|
|
stub_object_storage_uploader(
|
|
|
|
config: Gitlab.config.ci_secure_files.object_store,
|
|
|
|
uploader: Ci::SecureFileUploader,
|
|
|
|
**params
|
|
|
|
)
|
2022-03-02 08:16:31 +05:30
|
|
|
end
|
|
|
|
|
2021-01-03 14:25:43 +05:30
|
|
|
def stub_terraform_state_object_storage(**params)
|
2023-06-20 00:43:36 +05:30
|
|
|
stub_object_storage_uploader(
|
|
|
|
config: Gitlab.config.terraform_state.object_store,
|
|
|
|
uploader: Terraform::StateUploader,
|
|
|
|
**params
|
|
|
|
)
|
2020-04-22 19:07:51 +05:30
|
|
|
end
|
|
|
|
|
2021-01-03 14:25:43 +05:30
|
|
|
def stub_pages_object_storage(uploader = described_class, **params)
|
2023-06-20 00:43:36 +05:30
|
|
|
stub_object_storage_uploader(
|
|
|
|
config: Gitlab.config.pages.object_store,
|
|
|
|
uploader: uploader,
|
|
|
|
**params
|
|
|
|
)
|
2021-01-03 14:25:43 +05:30
|
|
|
end
|
|
|
|
|
2018-11-08 19:23:39 +05:30
|
|
|
def stub_object_storage_multipart_init(endpoint, upload_id = "upload_id")
|
2021-04-17 20:07:23 +05:30
|
|
|
stub_request(:post, %r{\A#{endpoint}tmp/uploads/[%A-Za-z0-9-]*\?uploads\z})
|
2018-11-08 19:23:39 +05:30
|
|
|
.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
|
2023-07-09 08:55:56 +05:30
|
|
|
|
|
|
|
def stub_object_storage_multipart_init_with_final_store_path(full_path, upload_id = "upload_id")
|
|
|
|
stub_request(:post, %r{\A#{full_path}\?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
|