diff options
Diffstat (limited to 'spec/lib/container_registry')
-rw-r--r-- | spec/lib/container_registry/gitlab_api_client_spec.rb | 189 | ||||
-rw-r--r-- | spec/lib/container_registry/migration_spec.rb | 36 |
2 files changed, 188 insertions, 37 deletions
diff --git a/spec/lib/container_registry/gitlab_api_client_spec.rb b/spec/lib/container_registry/gitlab_api_client_spec.rb index 4fe229024e5..16d2c42f332 100644 --- a/spec/lib/container_registry/gitlab_api_client_spec.rb +++ b/spec/lib/container_registry/gitlab_api_client_spec.rb @@ -62,6 +62,7 @@ RSpec.describe ContainerRegistry::GitlabApiClient do where(:status_code, :expected_result) do 200 | :already_imported 202 | :ok + 400 | :bad_request 401 | :unauthorized 404 | :not_found 409 | :already_being_imported @@ -86,6 +87,7 @@ RSpec.describe ContainerRegistry::GitlabApiClient do where(:status_code, :expected_result) do 200 | :already_imported 202 | :ok + 400 | :bad_request 401 | :unauthorized 404 | :not_found 409 | :already_being_imported @@ -104,54 +106,106 @@ RSpec.describe ContainerRegistry::GitlabApiClient do end end - describe '#import_status' do - subject { client.import_status(path) } + describe '#cancel_repository_import' do + let(:force) { false } - before do - stub_import_status(path, status) + subject { client.cancel_repository_import(path, force: force) } + + where(:status_code, :expected_result) do + 200 | :already_imported + 202 | :ok + 400 | :bad_request + 401 | :unauthorized + 404 | :not_found + 409 | :already_being_imported + 418 | :error + 424 | :pre_import_failed + 425 | :already_being_imported + 429 | :too_many_imports end - context 'with a status' do + with_them do + before do + stub_import_cancel(path, status_code, force: force) + end + + it { is_expected.to eq({ status: expected_result, migration_state: nil }) } + end + + context 'bad request' do let(:status) { 'this_is_a_test' } - it { is_expected.to eq(status) } + before do + stub_import_cancel(path, 400, status: status, force: force) + end + + it { is_expected.to eq({ status: :bad_request, migration_state: status }) } end - context 'with no status' do - let(:status) { nil } + context 'force cancel' do + let(:force) { true } - it { is_expected.to eq('error') } + before do + stub_import_cancel(path, 202, force: force) + end + + it { is_expected.to eq({ status: :ok, migration_state: nil }) } end end - describe '#repository_details' do - let(:path) { 'namespace/path/to/repository' } - let(:response) { { foo: :bar, this: :is_a_test } } - let(:with_size) { true } - - subject { client.repository_details(path, with_size: with_size) } + describe '#import_status' do + subject { client.import_status(path) } - context 'with size' do + context 'with successful response' do before do - stub_repository_details(path, with_size: with_size, respond_with: response) + stub_import_status(path, status) end - it { is_expected.to eq(response.stringify_keys.deep_transform_values(&:to_s)) } - end + context 'with a status' do + let(:status) { 'this_is_a_test' } + + it { is_expected.to eq(status) } + end + + context 'with no status' do + let(:status) { nil } - context 'without_size' do - let(:with_size) { false } + it { is_expected.to eq('error') } + end + end + context 'with non successful response' do before do - stub_repository_details(path, with_size: with_size, respond_with: response) + stub_import_status(path, nil, status_code: 404) end - it { is_expected.to eq(response.stringify_keys.deep_transform_values(&:to_s)) } + it { is_expected.to eq('pre_import_failed') } + end + end + + describe '#repository_details' do + let(:path) { 'namespace/path/to/repository' } + let(:response) { { foo: :bar, this: :is_a_test } } + + subject { client.repository_details(path, sizing: sizing) } + + [:self, :self_with_descendants, nil].each do |size_type| + context "with sizing #{size_type}" do + let(:sizing) { size_type } + + before do + stub_repository_details(path, sizing: sizing, respond_with: response) + end + + it { is_expected.to eq(response.stringify_keys.deep_transform_values(&:to_s)) } + end end context 'with non successful response' do + let(:sizing) { nil } + before do - stub_repository_details(path, with_size: with_size, status_code: 404) + stub_repository_details(path, sizing: sizing, status_code: 404) end it { is_expected.to eq({}) } @@ -216,6 +270,54 @@ RSpec.describe ContainerRegistry::GitlabApiClient do end end + describe '.deduplicated_size' do + let(:path) { 'foo/bar' } + let(:response) { { 'size_bytes': 555 } } + let(:registry_enabled) { true } + + subject { described_class.deduplicated_size(path) } + + before do + stub_container_registry_config(enabled: registry_enabled, api_url: registry_api_url, key: 'spec/fixtures/x509_certificate_pk.key') + end + + context 'with successful response' do + before do + expect(Auth::ContainerRegistryAuthenticationService).to receive(:pull_nested_repositories_access_token).with(path).and_return(token) + stub_repository_details(path, sizing: :self_with_descendants, status_code: 200, respond_with: response) + end + + it { is_expected.to eq(555) } + end + + context 'with unsuccessful response' do + before do + expect(Auth::ContainerRegistryAuthenticationService).to receive(:pull_nested_repositories_access_token).with(path).and_return(token) + stub_repository_details(path, sizing: :self_with_descendants, status_code: 404, respond_with: response) + end + + it { is_expected.to eq(nil) } + end + + context 'with the registry disabled' do + let(:registry_enabled) { false } + + it { is_expected.to eq(nil) } + end + + context 'with a nil path' do + let(:path) { nil } + let(:token) { nil } + + before do + expect(Auth::ContainerRegistryAuthenticationService).not_to receive(:pull_nested_repositories_access_token) + stub_repository_details(path, sizing: :self_with_descendants, status_code: 401, respond_with: response) + end + + it { is_expected.to eq(nil) } + end + end + def stub_pre_import(path, status_code, pre:) import_type = pre ? 'pre' : 'final' stub_request(:put, "#{registry_api_url}/gitlab/v1/import/#{path}/?import_type=#{import_type}") @@ -230,21 +332,50 @@ RSpec.describe ContainerRegistry::GitlabApiClient do .to_return(status: status_code, body: '') end - def stub_import_status(path, status) + def stub_import_status(path, status, status_code: 200) stub_request(:get, "#{registry_api_url}/gitlab/v1/import/#{path}/") .with(headers: { 'Accept' => described_class::JSON_TYPE, 'Authorization' => "bearer #{import_token}" }) .to_return( - status: 200, + status: status_code, body: { status: status }.to_json, headers: { content_type: 'application/json' } ) end - def stub_repository_details(path, with_size: true, status_code: 200, respond_with: {}) + def stub_import_cancel(path, http_status, status: nil, force: false) + body = {} + + if http_status == 400 + body = { status: status } + end + + headers = { + 'Accept' => described_class::JSON_TYPE, + 'Authorization' => "bearer #{import_token}", + 'User-Agent' => "GitLab/#{Gitlab::VERSION}", + 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3' + } + + params = force ? '?force=true' : '' + + stub_request(:delete, "#{registry_api_url}/gitlab/v1/import/#{path}/#{params}") + .with(headers: headers) + .to_return( + status: http_status, + body: body.to_json, + headers: { content_type: 'application/json' } + ) + end + + def stub_repository_details(path, sizing: nil, status_code: 200, respond_with: {}) url = "#{registry_api_url}/gitlab/v1/repositories/#{path}/" - url += "?size=self" if with_size + url += "?size=#{sizing}" if sizing + + headers = { 'Accept' => described_class::JSON_TYPE } + headers['Authorization'] = "bearer #{token}" if token + stub_request(:get, url) - .with(headers: { 'Accept' => described_class::JSON_TYPE, 'Authorization' => "bearer #{token}" }) + .with(headers: headers) .to_return(status: status_code, body: respond_with.to_json, headers: { 'Content-Type' => described_class::JSON_TYPE }) end end diff --git a/spec/lib/container_registry/migration_spec.rb b/spec/lib/container_registry/migration_spec.rb index ffbbfb249e3..6c0fc94e27f 100644 --- a/spec/lib/container_registry/migration_spec.rb +++ b/spec/lib/container_registry/migration_spec.rb @@ -37,8 +37,8 @@ RSpec.describe ContainerRegistry::Migration do subject { described_class.enqueue_waiting_time } where(:slow_enabled, :fast_enabled, :expected_result) do - false | false | 1.hour - true | false | 6.hours + false | false | 45.minutes + true | false | 165.minutes false | true | 0 true | true | 0 end @@ -154,15 +154,35 @@ RSpec.describe ContainerRegistry::Migration do end end - describe '.target_plan' do - let_it_be(:plan) { create(:plan) } + describe '.target_plans' do + subject { described_class.target_plans } - before do - stub_application_setting(container_registry_import_target_plan: plan.name) + where(:target_plan, :result) do + 'free' | described_class::FREE_TIERS + 'premium' | described_class::PREMIUM_TIERS + 'ultimate' | described_class::ULTIMATE_TIERS end - it 'returns the matching application_setting' do - expect(described_class.target_plan).to eq(plan) + with_them do + before do + stub_application_setting(container_registry_import_target_plan: target_plan) + end + + it { is_expected.to eq(result) } + end + end + + describe '.all_plans?' do + subject { described_class.all_plans? } + + it { is_expected.to eq(true) } + + context 'feature flag disabled' do + before do + stub_feature_flags(container_registry_migration_phase2_all_plans: false) + end + + it { is_expected.to eq(false) } end end end |