2019-07-25 01:21:37 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-05-17 01:51:56 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-24 05:08:32 -04:00
|
|
|
RSpec.describe ContainerRegistry::Client do
|
2020-11-09 16:08:48 -05:00
|
|
|
using RSpec::Parameterized::TableSyntax
|
|
|
|
|
2017-05-17 01:51:56 -04:00
|
|
|
let(:token) { '12345' }
|
|
|
|
let(:options) { { token: token } }
|
2020-11-09 16:08:48 -05:00
|
|
|
let(:registry_api_url) { 'http://container-registry' }
|
|
|
|
let(:client) { described_class.new(registry_api_url, options) }
|
2020-04-15 05:09:46 -04:00
|
|
|
let(:push_blob_headers) do
|
|
|
|
{
|
|
|
|
'Accept' => 'application/vnd.docker.distribution.manifest.v2+json, application/vnd.oci.image.manifest.v1+json',
|
|
|
|
'Authorization' => "bearer #{token}",
|
|
|
|
'Content-Type' => 'application/octet-stream',
|
|
|
|
'User-Agent' => "GitLab/#{Gitlab::VERSION}"
|
|
|
|
}
|
|
|
|
end
|
2020-08-10 23:11:00 -04:00
|
|
|
|
2020-04-15 05:09:46 -04:00
|
|
|
let(:headers_with_accept_types) do
|
|
|
|
{
|
|
|
|
'Accept' => 'application/vnd.docker.distribution.manifest.v2+json, application/vnd.oci.image.manifest.v1+json',
|
|
|
|
'Authorization' => "bearer #{token}",
|
|
|
|
'User-Agent' => "GitLab/#{Gitlab::VERSION}"
|
|
|
|
}
|
|
|
|
end
|
2017-05-17 01:51:56 -04:00
|
|
|
|
2021-01-07 22:10:42 -05:00
|
|
|
let(:expected_faraday_headers) { { user_agent: "GitLab/#{Gitlab::VERSION}" } }
|
|
|
|
let(:expected_faraday_request_options) { Gitlab::HTTP::DEFAULT_TIMEOUT_OPTIONS }
|
|
|
|
|
|
|
|
shared_examples 'handling timeouts' do
|
|
|
|
let(:retry_options) do
|
|
|
|
ContainerRegistry::Client::RETRY_OPTIONS.merge(
|
|
|
|
interval: 0.1,
|
|
|
|
interval_randomness: 0,
|
|
|
|
backoff_factor: 0
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
before do
|
|
|
|
stub_request(method, url).to_timeout
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'handles network timeouts' do
|
|
|
|
actual_retries = 0
|
|
|
|
retry_options_with_block = retry_options.merge(
|
|
|
|
retry_block: -> (_, _, _, _) { actual_retries += 1 }
|
|
|
|
)
|
|
|
|
|
|
|
|
stub_const('ContainerRegistry::Client::RETRY_OPTIONS', retry_options_with_block)
|
|
|
|
|
|
|
|
expect { subject }.to raise_error(Faraday::ConnectionFailed)
|
|
|
|
expect(actual_retries).to eq(retry_options_with_block[:max])
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'logs the error' do
|
|
|
|
stub_const('ContainerRegistry::Client::RETRY_OPTIONS', retry_options)
|
|
|
|
|
|
|
|
expect(Gitlab::ErrorTracking)
|
|
|
|
.to receive(:log_exception)
|
|
|
|
.exactly(retry_options[:max] + 1)
|
|
|
|
.times
|
|
|
|
.with(
|
|
|
|
an_instance_of(Faraday::ConnectionFailed),
|
|
|
|
class: described_class.name,
|
|
|
|
url: URI(url)
|
|
|
|
)
|
|
|
|
|
|
|
|
expect { subject }.to raise_error(Faraday::ConnectionFailed)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
shared_examples 'handling repository manifest' do |manifest_type|
|
|
|
|
let(:method) { :get }
|
|
|
|
let(:url) { 'http://container-registry/v2/group/test/manifests/mytag' }
|
2019-07-25 00:44:07 -04:00
|
|
|
let(:manifest) do
|
|
|
|
{
|
|
|
|
"schemaVersion" => 2,
|
|
|
|
"config" => {
|
|
|
|
"mediaType" => manifest_type,
|
|
|
|
"digest" =>
|
|
|
|
"sha256:4a3ef0786dd241be6000311e1503869b320be433b9cba84cfafeb512d1720c95",
|
|
|
|
"size" => 6608
|
|
|
|
},
|
|
|
|
"layers" => [
|
|
|
|
{
|
|
|
|
"mediaType" => manifest_type,
|
|
|
|
"digest" =>
|
|
|
|
"sha256:83ef92b73cf4595aa7fe214ec6747228283d585f373d8f6bc08d66bebab531b7",
|
|
|
|
"size" => 2828661
|
|
|
|
}
|
|
|
|
]
|
2020-04-15 05:09:46 -04:00
|
|
|
}
|
2019-07-25 00:44:07 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'GET /v2/:name/manifests/mytag' do
|
2021-01-07 22:10:42 -05:00
|
|
|
stub_request(method, url)
|
2019-07-25 00:44:07 -04:00
|
|
|
.with(headers: {
|
2020-04-15 05:09:46 -04:00
|
|
|
'Accept' => 'application/vnd.docker.distribution.manifest.v2+json, application/vnd.oci.image.manifest.v1+json',
|
|
|
|
'Authorization' => "bearer #{token}",
|
|
|
|
'User-Agent' => "GitLab/#{Gitlab::VERSION}"
|
2019-07-25 00:44:07 -04:00
|
|
|
})
|
|
|
|
.to_return(status: 200, body: manifest.to_json, headers: { content_type: manifest_type })
|
|
|
|
|
2021-01-07 22:10:42 -05:00
|
|
|
expect_new_faraday
|
|
|
|
|
|
|
|
expect(subject).to eq(manifest)
|
2019-07-25 00:44:07 -04:00
|
|
|
end
|
2021-01-07 22:10:42 -05:00
|
|
|
|
|
|
|
it_behaves_like 'handling timeouts'
|
2019-07-25 00:44:07 -04:00
|
|
|
end
|
|
|
|
|
2021-01-07 22:10:42 -05:00
|
|
|
describe '#repository_manifest' do
|
|
|
|
subject { client.repository_manifest('group/test', 'mytag') }
|
|
|
|
|
|
|
|
it_behaves_like 'handling repository manifest', described_class::DOCKER_DISTRIBUTION_MANIFEST_V2_TYPE
|
|
|
|
it_behaves_like 'handling repository manifest', described_class::OCI_MANIFEST_V1_TYPE
|
|
|
|
end
|
2019-07-25 00:44:07 -04:00
|
|
|
|
2017-05-17 01:51:56 -04:00
|
|
|
describe '#blob' do
|
2021-01-07 22:10:42 -05:00
|
|
|
let(:method) { :get }
|
|
|
|
let(:url) { 'http://container-registry/v2/group/test/blobs/sha256:0123456789012345' }
|
2020-04-15 05:09:46 -04:00
|
|
|
let(:blob_headers) do
|
|
|
|
{
|
|
|
|
'Accept' => 'application/octet-stream',
|
|
|
|
'Authorization' => "bearer #{token}",
|
|
|
|
'User-Agent' => "GitLab/#{Gitlab::VERSION}"
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:redirect_header) do
|
|
|
|
{
|
|
|
|
'User-Agent' => "GitLab/#{Gitlab::VERSION}"
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2021-01-07 22:10:42 -05:00
|
|
|
subject { client.blob('group/test', 'sha256:0123456789012345') }
|
|
|
|
|
2017-05-17 01:51:56 -04:00
|
|
|
it 'GET /v2/:name/blobs/:digest' do
|
2021-01-07 22:10:42 -05:00
|
|
|
stub_request(method, url)
|
2020-04-15 05:09:46 -04:00
|
|
|
.with(headers: blob_headers)
|
2017-06-21 09:48:12 -04:00
|
|
|
.to_return(status: 200, body: "Blob")
|
2017-05-17 01:51:56 -04:00
|
|
|
|
2021-01-07 22:10:42 -05:00
|
|
|
expect_new_faraday
|
|
|
|
|
|
|
|
expect(subject).to eq('Blob')
|
2017-05-17 01:51:56 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'follows 307 redirect for GET /v2/:name/blobs/:digest' do
|
2021-01-07 22:10:42 -05:00
|
|
|
stub_request(method, url)
|
2020-04-15 05:09:46 -04:00
|
|
|
.with(headers: blob_headers)
|
2020-05-15 08:08:28 -04:00
|
|
|
.to_return(status: 307, body: '', headers: { Location: 'http://redirected' })
|
2017-05-17 01:51:56 -04:00
|
|
|
# We should probably use hash_excluding here, but that requires an update to WebMock:
|
|
|
|
# https://github.com/bblimke/webmock/blob/master/lib/webmock/matchers/hash_excluding_matcher.rb
|
2017-06-21 09:48:12 -04:00
|
|
|
stub_request(:get, "http://redirected/")
|
2020-04-15 05:09:46 -04:00
|
|
|
.with(headers: redirect_header) do |request|
|
|
|
|
!request.headers.include?('Authorization')
|
|
|
|
end
|
2017-06-21 09:48:12 -04:00
|
|
|
.to_return(status: 200, body: "Successfully redirected")
|
2017-05-17 01:51:56 -04:00
|
|
|
|
2021-01-07 22:10:42 -05:00
|
|
|
expect_new_faraday(times: 2)
|
2017-05-17 01:51:56 -04:00
|
|
|
|
2021-01-07 22:10:42 -05:00
|
|
|
expect(subject).to eq('Successfully redirected')
|
2017-05-17 01:51:56 -04:00
|
|
|
end
|
2021-01-07 22:10:42 -05:00
|
|
|
|
|
|
|
it_behaves_like 'handling timeouts'
|
2017-05-17 01:51:56 -04:00
|
|
|
end
|
2019-09-19 07:50:12 -04:00
|
|
|
|
|
|
|
describe '#upload_blob' do
|
|
|
|
subject { client.upload_blob('path', 'content', 'sha256:123') }
|
|
|
|
|
|
|
|
context 'with successful uploads' do
|
|
|
|
it 'starts the upload and posts the blob' do
|
|
|
|
stub_upload('path', 'content', 'sha256:123')
|
|
|
|
|
2021-01-07 22:10:42 -05:00
|
|
|
expect_new_faraday(timeout: false)
|
|
|
|
|
2019-09-19 07:50:12 -04:00
|
|
|
expect(subject).to be_success
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with a failed upload' do
|
|
|
|
before do
|
|
|
|
stub_upload('path', 'content', 'sha256:123', 400)
|
|
|
|
end
|
|
|
|
|
2019-11-11 04:06:38 -05:00
|
|
|
it 'returns a failure' do
|
|
|
|
expect(subject).not_to be_success
|
2019-09-19 07:50:12 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#generate_empty_manifest' do
|
|
|
|
subject { client.generate_empty_manifest('path') }
|
|
|
|
|
|
|
|
let(:result_manifest) do
|
|
|
|
{
|
|
|
|
schemaVersion: 2,
|
|
|
|
mediaType: 'application/vnd.docker.distribution.manifest.v2+json',
|
|
|
|
config: {
|
|
|
|
mediaType: 'application/vnd.docker.container.image.v1+json',
|
|
|
|
size: 21,
|
|
|
|
digest: 'sha256:4435000728ee66e6a80e55637fc22725c256b61de344a2ecdeaac6bdb36e8bc3'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'uploads a random image and returns the manifest' do
|
|
|
|
stub_upload('path', "{\n \"config\": {\n }\n}", 'sha256:4435000728ee66e6a80e55637fc22725c256b61de344a2ecdeaac6bdb36e8bc3')
|
|
|
|
|
|
|
|
expect(subject).to eq(result_manifest)
|
|
|
|
end
|
2019-11-11 04:06:38 -05:00
|
|
|
|
|
|
|
context 'when upload fails' do
|
|
|
|
before do
|
|
|
|
stub_upload('path', "{\n \"config\": {\n }\n}", 'sha256:4435000728ee66e6a80e55637fc22725c256b61de344a2ecdeaac6bdb36e8bc3', 500)
|
|
|
|
end
|
|
|
|
|
|
|
|
it { is_expected.to be nil }
|
|
|
|
end
|
2019-09-19 07:50:12 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
describe '#put_tag' do
|
2020-04-15 05:09:46 -04:00
|
|
|
let(:manifest_headers) do
|
|
|
|
{
|
|
|
|
'Accept' => 'application/vnd.docker.distribution.manifest.v2+json, application/vnd.oci.image.manifest.v1+json',
|
|
|
|
'Authorization' => "bearer #{token}",
|
|
|
|
'Content-Type' => 'application/vnd.docker.distribution.manifest.v2+json',
|
|
|
|
'User-Agent' => "GitLab/#{Gitlab::VERSION}"
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2019-09-19 07:50:12 -04:00
|
|
|
subject { client.put_tag('path', 'tagA', { foo: :bar }) }
|
|
|
|
|
|
|
|
it 'uploads the manifest and returns the digest' do
|
|
|
|
stub_request(:put, "http://container-registry/v2/path/manifests/tagA")
|
2020-04-15 05:09:46 -04:00
|
|
|
.with(body: "{\n \"foo\": \"bar\"\n}", headers: manifest_headers)
|
2019-09-19 07:50:12 -04:00
|
|
|
.to_return(status: 200, body: "", headers: { 'docker-content-digest' => 'sha256:123' })
|
|
|
|
|
2021-01-07 22:10:42 -05:00
|
|
|
expect_new_faraday(timeout: false)
|
|
|
|
|
2019-09-19 07:50:12 -04:00
|
|
|
expect(subject).to eq 'sha256:123'
|
|
|
|
end
|
|
|
|
end
|
2020-02-12 10:09:37 -05:00
|
|
|
|
|
|
|
describe '#delete_repository_tag_by_name' do
|
|
|
|
subject { client.delete_repository_tag_by_name('group/test', 'a') }
|
|
|
|
|
|
|
|
context 'when the tag exists' do
|
|
|
|
before do
|
|
|
|
stub_request(:delete, "http://container-registry/v2/group/test/tags/reference/a")
|
2020-04-15 05:09:46 -04:00
|
|
|
.with(headers: headers_with_accept_types)
|
2020-02-12 10:09:37 -05:00
|
|
|
.to_return(status: 200, body: "")
|
|
|
|
end
|
|
|
|
|
|
|
|
it { is_expected.to be_truthy }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the tag does not exist' do
|
|
|
|
before do
|
|
|
|
stub_request(:delete, "http://container-registry/v2/group/test/tags/reference/a")
|
2020-04-15 05:09:46 -04:00
|
|
|
.with(headers: headers_with_accept_types)
|
2020-02-12 10:09:37 -05:00
|
|
|
.to_return(status: 404, body: "")
|
|
|
|
end
|
|
|
|
|
|
|
|
it { is_expected.to be_truthy }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when an error occurs' do
|
|
|
|
before do
|
|
|
|
stub_request(:delete, "http://container-registry/v2/group/test/tags/reference/a")
|
2020-04-15 05:09:46 -04:00
|
|
|
.with(headers: headers_with_accept_types)
|
2020-02-12 10:09:37 -05:00
|
|
|
.to_return(status: 500, body: "")
|
|
|
|
end
|
|
|
|
|
|
|
|
it { is_expected.to be_falsey }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#supports_tag_delete?' do
|
|
|
|
subject { client.supports_tag_delete? }
|
|
|
|
|
2020-11-09 16:08:48 -05:00
|
|
|
where(:registry_tags_support_enabled, :is_on_dot_com, :container_registry_features, :expect_registry_to_be_pinged, :expected_result) do
|
|
|
|
true | true | [ContainerRegistry::Client::REGISTRY_TAG_DELETE_FEATURE] | false | true
|
|
|
|
true | false | [ContainerRegistry::Client::REGISTRY_TAG_DELETE_FEATURE] | true | true
|
|
|
|
true | true | [] | true | true
|
|
|
|
true | false | [] | true | true
|
|
|
|
false | true | [ContainerRegistry::Client::REGISTRY_TAG_DELETE_FEATURE] | false | true
|
|
|
|
false | false | [ContainerRegistry::Client::REGISTRY_TAG_DELETE_FEATURE] | true | false
|
|
|
|
false | true | [] | true | false
|
|
|
|
false | false | [] | true | false
|
2020-02-12 10:09:37 -05:00
|
|
|
end
|
|
|
|
|
2020-11-09 16:08:48 -05:00
|
|
|
with_them do
|
2020-02-12 10:09:37 -05:00
|
|
|
before do
|
2020-11-09 16:08:48 -05:00
|
|
|
allow(::Gitlab).to receive(:com?).and_return(is_on_dot_com)
|
|
|
|
stub_registry_tags_support(registry_tags_support_enabled)
|
|
|
|
stub_application_setting(container_registry_features: container_registry_features)
|
2020-02-12 10:09:37 -05:00
|
|
|
end
|
|
|
|
|
2020-11-09 16:08:48 -05:00
|
|
|
it 'returns the expected result' do
|
|
|
|
if expect_registry_to_be_pinged
|
|
|
|
expect_next_instance_of(Faraday::Connection) do |connection|
|
|
|
|
expect(connection).to receive(:run_request).and_call_original
|
|
|
|
end
|
|
|
|
else
|
|
|
|
expect(Faraday::Connection).not_to receive(:new)
|
|
|
|
end
|
2020-05-15 08:08:28 -04:00
|
|
|
|
2020-11-09 16:08:48 -05:00
|
|
|
expect(subject).to be expected_result
|
|
|
|
end
|
|
|
|
end
|
2020-05-15 08:08:28 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
describe '#registry_info' do
|
|
|
|
subject { client.registry_info }
|
|
|
|
|
|
|
|
context 'when the check is successful' do
|
|
|
|
context 'when using the GitLab container registry' do
|
|
|
|
before do
|
|
|
|
stub_registry_info(headers: {
|
|
|
|
'GitLab-Container-Registry-Version' => '2.9.1-gitlab',
|
|
|
|
'GitLab-Container-Registry-Features' => 'a,b,c'
|
|
|
|
})
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'identifies the vendor as "gitlab"' do
|
|
|
|
expect(subject).to include(vendor: 'gitlab')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'identifies version and features' do
|
|
|
|
expect(subject).to include(version: '2.9.1-gitlab', features: %w[a b c])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when using a third-party container registry' do
|
|
|
|
before do
|
|
|
|
stub_registry_info
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'identifies the vendor as "other"' do
|
|
|
|
expect(subject).to include(vendor: 'other')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not identify version or features' do
|
|
|
|
expect(subject).to include(version: nil, features: [])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the check is not successful' do
|
|
|
|
it 'does not identify vendor, version or features' do
|
|
|
|
stub_registry_info(status: 500)
|
|
|
|
|
|
|
|
expect(subject).to eq({})
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2020-09-02 05:10:23 -04:00
|
|
|
|
|
|
|
describe '.supports_tag_delete?' do
|
|
|
|
subject { described_class.supports_tag_delete? }
|
|
|
|
|
2020-11-09 16:08:48 -05:00
|
|
|
where(:registry_api_url, :registry_enabled, :registry_tags_support_enabled, :is_on_dot_com, :container_registry_features, :expect_registry_to_be_pinged, :expected_result) do
|
|
|
|
'http://sandbox.local' | true | true | true | [ContainerRegistry::Client::REGISTRY_TAG_DELETE_FEATURE] | false | true
|
|
|
|
'http://sandbox.local' | true | true | false | [ContainerRegistry::Client::REGISTRY_TAG_DELETE_FEATURE] | true | true
|
|
|
|
'http://sandbox.local' | true | false | true | [ContainerRegistry::Client::REGISTRY_TAG_DELETE_FEATURE] | false | true
|
|
|
|
'http://sandbox.local' | true | false | false | [ContainerRegistry::Client::REGISTRY_TAG_DELETE_FEATURE] | true | false
|
|
|
|
'http://sandbox.local' | false | true | true | [ContainerRegistry::Client::REGISTRY_TAG_DELETE_FEATURE] | false | false
|
|
|
|
'http://sandbox.local' | false | true | false | [ContainerRegistry::Client::REGISTRY_TAG_DELETE_FEATURE] | false | false
|
|
|
|
'http://sandbox.local' | false | false | true | [ContainerRegistry::Client::REGISTRY_TAG_DELETE_FEATURE] | false | false
|
|
|
|
'http://sandbox.local' | false | false | false | [ContainerRegistry::Client::REGISTRY_TAG_DELETE_FEATURE] | false | false
|
|
|
|
'http://sandbox.local' | true | true | true | [] | true | true
|
|
|
|
'http://sandbox.local' | true | true | false | [] | true | true
|
|
|
|
'http://sandbox.local' | true | false | true | [] | true | false
|
|
|
|
'http://sandbox.local' | true | false | false | [] | true | false
|
|
|
|
'http://sandbox.local' | false | true | true | [] | false | false
|
|
|
|
'http://sandbox.local' | false | true | false | [] | false | false
|
|
|
|
'http://sandbox.local' | false | false | true | [] | false | false
|
|
|
|
'http://sandbox.local' | false | false | false | [] | false | false
|
|
|
|
'' | true | true | true | [ContainerRegistry::Client::REGISTRY_TAG_DELETE_FEATURE] | false | false
|
|
|
|
'' | true | true | false | [ContainerRegistry::Client::REGISTRY_TAG_DELETE_FEATURE] | false | false
|
|
|
|
'' | true | false | true | [ContainerRegistry::Client::REGISTRY_TAG_DELETE_FEATURE] | false | false
|
|
|
|
'' | true | false | false | [ContainerRegistry::Client::REGISTRY_TAG_DELETE_FEATURE] | false | false
|
|
|
|
'' | false | true | true | [ContainerRegistry::Client::REGISTRY_TAG_DELETE_FEATURE] | false | false
|
|
|
|
'' | false | true | false | [ContainerRegistry::Client::REGISTRY_TAG_DELETE_FEATURE] | false | false
|
|
|
|
'' | false | false | true | [ContainerRegistry::Client::REGISTRY_TAG_DELETE_FEATURE] | false | false
|
|
|
|
'' | false | false | false | [ContainerRegistry::Client::REGISTRY_TAG_DELETE_FEATURE] | false | false
|
|
|
|
'' | true | true | true | [] | false | false
|
|
|
|
'' | true | true | false | [] | false | false
|
|
|
|
'' | true | false | true | [] | false | false
|
|
|
|
'' | true | false | false | [] | false | false
|
|
|
|
'' | false | true | true | [] | false | false
|
|
|
|
'' | false | true | false | [] | false | false
|
|
|
|
'' | false | false | true | [] | false | false
|
|
|
|
'' | false | false | false | [] | false | false
|
2020-09-02 05:10:23 -04:00
|
|
|
end
|
|
|
|
|
2020-11-09 16:08:48 -05:00
|
|
|
with_them do
|
|
|
|
before do
|
|
|
|
allow(::Gitlab).to receive(:com?).and_return(is_on_dot_com)
|
|
|
|
stub_container_registry_config(enabled: registry_enabled, api_url: registry_api_url, key: 'spec/fixtures/x509_certificate_pk.key')
|
|
|
|
stub_registry_tags_support(registry_tags_support_enabled)
|
|
|
|
stub_application_setting(container_registry_features: container_registry_features)
|
2020-09-02 05:10:23 -04:00
|
|
|
end
|
|
|
|
|
2020-11-09 16:08:48 -05:00
|
|
|
it 'returns the expected result' do
|
|
|
|
if expect_registry_to_be_pinged
|
|
|
|
expect_next_instance_of(Faraday::Connection) do |connection|
|
|
|
|
expect(connection).to receive(:run_request).and_call_original
|
|
|
|
end
|
|
|
|
else
|
|
|
|
expect(Faraday::Connection).not_to receive(:new)
|
|
|
|
end
|
2020-09-02 05:10:23 -04:00
|
|
|
|
2020-11-09 16:08:48 -05:00
|
|
|
expect(subject).to be expected_result
|
2020-09-02 05:10:23 -04:00
|
|
|
end
|
|
|
|
end
|
2020-11-09 16:08:48 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def stub_upload(path, content, digest, status = 200)
|
|
|
|
stub_request(:post, "#{registry_api_url}/v2/#{path}/blobs/uploads/")
|
|
|
|
.with(headers: headers_with_accept_types)
|
|
|
|
.to_return(status: status, body: "", headers: { 'location' => "#{registry_api_url}/next_upload?id=someid" })
|
2020-09-02 05:10:23 -04:00
|
|
|
|
2020-11-09 16:08:48 -05:00
|
|
|
stub_request(:put, "#{registry_api_url}/next_upload?digest=#{digest}&id=someid")
|
|
|
|
.with(body: content, headers: push_blob_headers)
|
|
|
|
.to_return(status: status, body: "", headers: {})
|
|
|
|
end
|
2020-09-02 05:10:23 -04:00
|
|
|
|
2020-11-09 16:08:48 -05:00
|
|
|
def stub_registry_info(headers: {}, status: 200)
|
|
|
|
stub_request(:get, "#{registry_api_url}/v2/")
|
|
|
|
.to_return(status: status, body: "", headers: headers)
|
|
|
|
end
|
2020-09-02 05:10:23 -04:00
|
|
|
|
2020-11-09 16:08:48 -05:00
|
|
|
def stub_registry_tags_support(supported = true)
|
|
|
|
status_code = supported ? 200 : 404
|
|
|
|
stub_request(:options, "#{registry_api_url}/v2/name/tags/reference/tag")
|
|
|
|
.to_return(
|
|
|
|
status: status_code,
|
|
|
|
body: '',
|
|
|
|
headers: { 'Allow' => 'DELETE' }
|
|
|
|
)
|
2020-09-02 05:10:23 -04:00
|
|
|
end
|
2021-01-07 22:10:42 -05:00
|
|
|
|
|
|
|
def expect_new_faraday(times: 1, timeout: true)
|
|
|
|
request_options = timeout ? expected_faraday_request_options : nil
|
|
|
|
expect(Faraday)
|
|
|
|
.to receive(:new)
|
|
|
|
.with(
|
|
|
|
'http://container-registry',
|
|
|
|
headers: expected_faraday_headers,
|
|
|
|
request: request_options
|
|
|
|
).and_call_original
|
|
|
|
.exactly(times)
|
|
|
|
.times
|
|
|
|
end
|
2017-05-17 01:51:56 -04:00
|
|
|
end
|