2019-12-26 22:10:19 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-03-02 22:35:43 +05:30
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
RSpec.describe API::ProjectContainerRepositories do
|
2019-09-30 21:07:59 +05:30
|
|
|
include ExclusiveLeaseHelpers
|
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
let_it_be(:project) { create(:project, :private) }
|
|
|
|
let_it_be(:maintainer) { create(:user) }
|
|
|
|
let_it_be(:developer) { create(:user) }
|
|
|
|
let_it_be(:reporter) { create(:user) }
|
|
|
|
let_it_be(:guest) { create(:user) }
|
2019-03-02 22:35:43 +05:30
|
|
|
let(:root_repository) { create(:container_repository, :root, project: project) }
|
|
|
|
let(:test_repository) { create(:container_repository, project: project) }
|
|
|
|
|
2019-10-12 21:52:04 +05:30
|
|
|
let(:users) do
|
|
|
|
{
|
|
|
|
anonymous: nil,
|
|
|
|
developer: developer,
|
|
|
|
guest: guest,
|
|
|
|
maintainer: maintainer,
|
|
|
|
reporter: reporter
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2019-03-02 22:35:43 +05:30
|
|
|
let(:api_user) { maintainer }
|
|
|
|
|
|
|
|
before do
|
|
|
|
project.add_maintainer(maintainer)
|
|
|
|
project.add_developer(developer)
|
|
|
|
project.add_reporter(reporter)
|
|
|
|
project.add_guest(guest)
|
|
|
|
|
|
|
|
stub_container_registry_config(enabled: true)
|
|
|
|
|
|
|
|
root_repository
|
|
|
|
test_repository
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'GET /projects/:id/registry/repositories' do
|
2019-10-12 21:52:04 +05:30
|
|
|
let(:url) { "/projects/#{project.id}/registry/repositories" }
|
2019-03-02 22:35:43 +05:30
|
|
|
|
2019-10-12 21:52:04 +05:30
|
|
|
subject { get api(url, api_user) }
|
2019-03-02 22:35:43 +05:30
|
|
|
|
2019-10-12 21:52:04 +05:30
|
|
|
it_behaves_like 'rejected container repository access', :guest, :forbidden
|
|
|
|
it_behaves_like 'rejected container repository access', :anonymous, :not_found
|
2021-01-03 14:25:43 +05:30
|
|
|
it_behaves_like 'a package tracking event', described_class.name, 'list_repositories'
|
2019-03-02 22:35:43 +05:30
|
|
|
|
2019-10-12 21:52:04 +05:30
|
|
|
it_behaves_like 'returns repositories for allowed users', :reporter, 'project' do
|
|
|
|
let(:object) { project }
|
2019-03-02 22:35:43 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'DELETE /projects/:id/registry/repositories/:repository_id' do
|
|
|
|
subject { delete api("/projects/#{project.id}/registry/repositories/#{root_repository.id}", api_user) }
|
|
|
|
|
2019-10-12 21:52:04 +05:30
|
|
|
it_behaves_like 'rejected container repository access', :developer, :forbidden
|
|
|
|
it_behaves_like 'rejected container repository access', :anonymous, :not_found
|
2021-01-03 14:25:43 +05:30
|
|
|
it_behaves_like 'a package tracking event', described_class.name, 'delete_repository'
|
2019-03-02 22:35:43 +05:30
|
|
|
|
|
|
|
context 'for maintainer' do
|
|
|
|
let(:api_user) { maintainer }
|
|
|
|
|
|
|
|
it 'schedules removal of repository' do
|
|
|
|
expect(DeleteContainerRepositoryWorker).to receive(:perform_async)
|
|
|
|
.with(maintainer.id, root_repository.id)
|
|
|
|
|
|
|
|
subject
|
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(:accepted)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'GET /projects/:id/registry/repositories/:repository_id/tags' do
|
|
|
|
subject { get api("/projects/#{project.id}/registry/repositories/#{root_repository.id}/tags", api_user) }
|
|
|
|
|
2019-10-12 21:52:04 +05:30
|
|
|
it_behaves_like 'rejected container repository access', :guest, :forbidden
|
|
|
|
it_behaves_like 'rejected container repository access', :anonymous, :not_found
|
2019-03-02 22:35:43 +05:30
|
|
|
|
|
|
|
context 'for reporter' do
|
|
|
|
let(:api_user) { reporter }
|
|
|
|
|
|
|
|
before do
|
|
|
|
stub_container_registry_tags(repository: root_repository.path, tags: %w(rootA latest))
|
|
|
|
end
|
|
|
|
|
2021-01-03 14:25:43 +05:30
|
|
|
it_behaves_like 'a package tracking event', described_class.name, 'list_tags'
|
2019-12-26 22:10:19 +05:30
|
|
|
|
2019-03-02 22:35:43 +05:30
|
|
|
it 'returns a list of tags' do
|
|
|
|
subject
|
|
|
|
|
|
|
|
expect(json_response.length).to eq(2)
|
|
|
|
expect(json_response.map { |repository| repository['name'] }).to eq %w(latest rootA)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns a matching schema' do
|
|
|
|
subject
|
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
|
|
|
expect(response).to match_response_schema('registry/tags')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'DELETE /projects/:id/registry/repositories/:repository_id/tags' do
|
|
|
|
subject { delete api("/projects/#{project.id}/registry/repositories/#{root_repository.id}/tags", api_user), params: params }
|
|
|
|
|
2019-10-12 21:52:04 +05:30
|
|
|
context 'disallowed' do
|
2019-03-02 22:35:43 +05:30
|
|
|
let(:params) do
|
2020-04-08 14:13:33 +05:30
|
|
|
{ name_regex_delete: 'v10.*' }
|
2019-03-02 22:35:43 +05:30
|
|
|
end
|
2019-10-12 21:52:04 +05:30
|
|
|
|
|
|
|
it_behaves_like 'rejected container repository access', :developer, :forbidden
|
|
|
|
it_behaves_like 'rejected container repository access', :anonymous, :not_found
|
2021-01-03 14:25:43 +05:30
|
|
|
it_behaves_like 'a package tracking event', described_class.name, 'delete_tag_bulk'
|
2019-03-02 22:35:43 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
context 'for maintainer' do
|
|
|
|
let(:api_user) { maintainer }
|
|
|
|
|
|
|
|
context 'without required parameters' do
|
|
|
|
let(:params) { }
|
|
|
|
|
|
|
|
it 'returns bad request' do
|
|
|
|
subject
|
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(:bad_request)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-04-08 14:13:33 +05:30
|
|
|
context 'without name_regex' do
|
|
|
|
let(:params) do
|
|
|
|
{ keep_n: 100,
|
|
|
|
older_than: '1 day',
|
|
|
|
other: 'some value' }
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns bad request' do
|
|
|
|
subject
|
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(:bad_request)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-03-02 22:35:43 +05:30
|
|
|
context 'passes all declared parameters' do
|
|
|
|
let(:params) do
|
2020-04-08 14:13:33 +05:30
|
|
|
{ name_regex_delete: 'v10.*',
|
|
|
|
name_regex_keep: 'v10.1.*',
|
2019-03-02 22:35:43 +05:30
|
|
|
keep_n: 100,
|
|
|
|
older_than: '1 day',
|
|
|
|
other: 'some value' }
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:worker_params) do
|
2020-04-08 14:13:33 +05:30
|
|
|
{ name_regex: nil,
|
|
|
|
name_regex_delete: 'v10.*',
|
|
|
|
name_regex_keep: 'v10.1.*',
|
2019-03-02 22:35:43 +05:30
|
|
|
keep_n: 100,
|
2020-03-13 15:44:24 +05:30
|
|
|
older_than: '1 day',
|
|
|
|
container_expiration_policy: false }
|
2019-03-02 22:35:43 +05:30
|
|
|
end
|
|
|
|
|
2019-09-30 21:07:59 +05:30
|
|
|
let(:lease_key) { "container_repository:cleanup_tags:#{root_repository.id}" }
|
|
|
|
|
2019-03-02 22:35:43 +05:30
|
|
|
it 'schedules cleanup of tags repository' do
|
2020-03-13 15:44:24 +05:30
|
|
|
stub_last_activity_update
|
2019-09-30 21:07:59 +05:30
|
|
|
stub_exclusive_lease(lease_key, timeout: 1.hour)
|
2019-03-02 22:35:43 +05:30
|
|
|
expect(CleanupContainerRepositoryWorker).to receive(:perform_async)
|
|
|
|
.with(maintainer.id, root_repository.id, worker_params)
|
|
|
|
|
|
|
|
subject
|
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(:accepted)
|
|
|
|
end
|
2019-09-30 21:07:59 +05:30
|
|
|
|
2019-12-21 20:55:43 +05:30
|
|
|
context 'called multiple times in one hour', :clean_gitlab_redis_shared_state do
|
2019-09-30 21:07:59 +05:30
|
|
|
it 'returns 400 with an error message' do
|
|
|
|
stub_exclusive_lease_taken(lease_key, timeout: 1.hour)
|
|
|
|
subject
|
|
|
|
|
2020-04-08 14:13:33 +05:30
|
|
|
expect(response).to have_gitlab_http_status(:bad_request)
|
2019-09-30 21:07:59 +05:30
|
|
|
expect(response.body).to include('This request has already been made.')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'executes service only for the first time' do
|
|
|
|
expect(CleanupContainerRepositoryWorker).to receive(:perform_async).once
|
|
|
|
|
|
|
|
2.times { subject }
|
|
|
|
end
|
|
|
|
end
|
2019-03-02 22:35:43 +05:30
|
|
|
end
|
2020-04-08 14:13:33 +05:30
|
|
|
|
|
|
|
context 'with deprecated name_regex param' do
|
|
|
|
let(:params) do
|
|
|
|
{ name_regex: 'v10.*',
|
|
|
|
name_regex_keep: 'v10.1.*',
|
|
|
|
keep_n: 100,
|
|
|
|
older_than: '1 day',
|
|
|
|
other: 'some value' }
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:worker_params) do
|
|
|
|
{ name_regex: 'v10.*',
|
|
|
|
name_regex_delete: nil,
|
|
|
|
name_regex_keep: 'v10.1.*',
|
|
|
|
keep_n: 100,
|
|
|
|
older_than: '1 day',
|
|
|
|
container_expiration_policy: false }
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:lease_key) { "container_repository:cleanup_tags:#{root_repository.id}" }
|
|
|
|
|
|
|
|
it 'schedules cleanup of tags repository' do
|
|
|
|
stub_last_activity_update
|
|
|
|
stub_exclusive_lease(lease_key, timeout: 1.hour)
|
|
|
|
expect(CleanupContainerRepositoryWorker).to receive(:perform_async)
|
|
|
|
.with(maintainer.id, root_repository.id, worker_params)
|
|
|
|
|
|
|
|
subject
|
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(:accepted)
|
|
|
|
end
|
|
|
|
end
|
2020-06-23 00:09:42 +05:30
|
|
|
|
|
|
|
context 'with invalid regex' do
|
|
|
|
let(:invalid_regex) { '*v10.' }
|
|
|
|
let(:lease_key) { "container_repository:cleanup_tags:#{root_repository.id}" }
|
|
|
|
|
|
|
|
RSpec.shared_examples 'rejecting the invalid regex' do |param_name|
|
|
|
|
it 'does not enqueue a job' do
|
|
|
|
expect(CleanupContainerRepositoryWorker).not_to receive(:perform_async)
|
|
|
|
|
|
|
|
subject
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'returning response status', :bad_request
|
|
|
|
|
|
|
|
it 'returns an error message' do
|
|
|
|
subject
|
|
|
|
|
|
|
|
expect(json_response['error']).to include("#{param_name} is an invalid regexp")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
before do
|
|
|
|
stub_last_activity_update
|
|
|
|
stub_exclusive_lease(lease_key, timeout: 1.hour)
|
|
|
|
end
|
|
|
|
|
|
|
|
%i[name_regex_delete name_regex name_regex_keep].each do |param_name|
|
|
|
|
context "for #{param_name}" do
|
|
|
|
let(:params) { { param_name => invalid_regex } }
|
|
|
|
|
|
|
|
it_behaves_like 'rejecting the invalid regex', param_name
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2019-03-02 22:35:43 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'GET /projects/:id/registry/repositories/:repository_id/tags/:tag_name' do
|
|
|
|
subject { get api("/projects/#{project.id}/registry/repositories/#{root_repository.id}/tags/rootA", api_user) }
|
|
|
|
|
2019-10-12 21:52:04 +05:30
|
|
|
it_behaves_like 'rejected container repository access', :guest, :forbidden
|
|
|
|
it_behaves_like 'rejected container repository access', :anonymous, :not_found
|
2019-03-02 22:35:43 +05:30
|
|
|
|
|
|
|
context 'for reporter' do
|
|
|
|
let(:api_user) { reporter }
|
|
|
|
|
|
|
|
before do
|
|
|
|
stub_container_registry_tags(repository: root_repository.path, tags: %w(rootA), with_manifest: true)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns a details of tag' do
|
|
|
|
subject
|
|
|
|
|
|
|
|
expect(json_response).to include(
|
|
|
|
'name' => 'rootA',
|
|
|
|
'digest' => 'sha256:4c8e63ca4cb663ce6c688cb06f1c372b088dac5b6d7ad7d49cd620d85cf72a15',
|
|
|
|
'revision' => 'd7a513a663c1a6dcdba9ed832ca53c02ac2af0c333322cd6ca92936d1d9917ac',
|
|
|
|
'total_size' => 2319870)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns a matching schema' do
|
|
|
|
subject
|
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
|
|
|
expect(response).to match_response_schema('registry/tag')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'DELETE /projects/:id/registry/repositories/:repository_id/tags/:tag_name' do
|
2019-12-21 20:55:43 +05:30
|
|
|
let(:service) { double('service') }
|
|
|
|
|
2019-03-02 22:35:43 +05:30
|
|
|
subject { delete api("/projects/#{project.id}/registry/repositories/#{root_repository.id}/tags/rootA", api_user) }
|
|
|
|
|
2019-10-12 21:52:04 +05:30
|
|
|
it_behaves_like 'rejected container repository access', :reporter, :forbidden
|
|
|
|
it_behaves_like 'rejected container repository access', :anonymous, :not_found
|
2019-03-02 22:35:43 +05:30
|
|
|
|
2021-01-29 00:20:46 +05:30
|
|
|
context 'for developer', :snowplow do
|
2019-09-04 21:01:54 +05:30
|
|
|
let(:api_user) { developer }
|
2019-03-02 22:35:43 +05:30
|
|
|
|
2019-12-21 20:55:43 +05:30
|
|
|
context 'when there are multiple tags' do
|
|
|
|
before do
|
|
|
|
stub_container_registry_tags(repository: root_repository.path, tags: %w(rootA rootB), with_manifest: true)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'properly removes tag' do
|
|
|
|
expect(service).to receive(:execute).with(root_repository) { { status: :success } }
|
|
|
|
expect(Projects::ContainerRepository::DeleteTagsService).to receive(:new).with(root_repository.project, api_user, tags: %w[rootA]) { service }
|
|
|
|
|
|
|
|
subject
|
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
2021-01-29 00:20:46 +05:30
|
|
|
expect_snowplow_event(category: described_class.name, action: 'delete_tag')
|
2019-12-21 20:55:43 +05:30
|
|
|
end
|
2019-03-02 22:35:43 +05:30
|
|
|
end
|
|
|
|
|
2019-12-21 20:55:43 +05:30
|
|
|
context 'when there\'s only one tag' do
|
|
|
|
before do
|
|
|
|
stub_container_registry_tags(repository: root_repository.path, tags: %w(rootA), with_manifest: true)
|
|
|
|
end
|
2019-03-02 22:35:43 +05:30
|
|
|
|
2019-12-21 20:55:43 +05:30
|
|
|
it 'properly removes tag' do
|
|
|
|
expect(service).to receive(:execute).with(root_repository) { { status: :success } }
|
|
|
|
expect(Projects::ContainerRepository::DeleteTagsService).to receive(:new).with(root_repository.project, api_user, tags: %w[rootA]) { service }
|
2019-03-02 22:35:43 +05:30
|
|
|
|
2019-12-21 20:55:43 +05:30
|
|
|
subject
|
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
2021-01-29 00:20:46 +05:30
|
|
|
expect_snowplow_event(category: described_class.name, action: 'delete_tag')
|
2019-12-21 20:55:43 +05:30
|
|
|
end
|
2019-03-02 22:35:43 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|