2019-03-30 03:23:56 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-03-23 09:37:17 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-24 14:09:03 -04:00
|
|
|
RSpec.describe ContainerRepository do
|
2017-03-23 09:37:17 -04:00
|
|
|
let(:group) { create(:group, name: 'group') }
|
2017-08-22 19:19:35 -04:00
|
|
|
let(:project) { create(:project, path: 'test', group: group) }
|
2017-03-23 09:37:17 -04:00
|
|
|
|
2017-04-10 07:01:10 -04:00
|
|
|
let(:repository) do
|
2017-03-23 09:37:17 -04:00
|
|
|
create(:container_repository, name: 'my_image', project: project)
|
|
|
|
end
|
|
|
|
|
|
|
|
before do
|
|
|
|
stub_container_registry_config(enabled: true,
|
|
|
|
api_url: 'http://registry.gitlab',
|
|
|
|
host_port: 'registry.gitlab')
|
|
|
|
|
|
|
|
stub_request(:get, 'http://registry.gitlab/v2/group/test/my_image/tags/list')
|
2019-07-25 00:44:07 -04:00
|
|
|
.with(headers: { 'Accept' => ContainerRegistry::Client::ACCEPTED_TYPES.join(', ') })
|
2017-03-23 09:37:17 -04:00
|
|
|
.to_return(
|
2017-03-23 09:57:33 -04:00
|
|
|
status: 200,
|
2020-04-30 14:09:38 -04:00
|
|
|
body: Gitlab::Json.dump(tags: ['test_tag']),
|
2017-03-23 09:57:33 -04:00
|
|
|
headers: { 'Content-Type' => 'application/json' })
|
2017-03-23 09:37:17 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
describe 'associations' do
|
|
|
|
it 'belongs to the project' do
|
2017-04-10 07:01:10 -04:00
|
|
|
expect(repository).to belong_to(:project)
|
2017-03-23 09:37:17 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-03-30 05:07:58 -04:00
|
|
|
describe '.exists_by_path?' do
|
|
|
|
it 'returns true for known container repository paths' do
|
|
|
|
path = ContainerRegistry::Path.new("#{project.full_path}/#{repository.name}")
|
|
|
|
expect(described_class.exists_by_path?(path)).to be_truthy
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns false for unknown container repository paths' do
|
|
|
|
path = ContainerRegistry::Path.new('you/dont/know/me')
|
|
|
|
expect(described_class.exists_by_path?(path)).to be_falsey
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-03-23 09:37:17 -04:00
|
|
|
describe '#tag' do
|
|
|
|
it 'has a test tag' do
|
2017-04-10 07:01:10 -04:00
|
|
|
expect(repository.tag('test')).not_to be_nil
|
2017-03-23 09:37:17 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#path' do
|
2017-04-13 05:54:02 -04:00
|
|
|
context 'when project path does not contain uppercase letters' do
|
|
|
|
it 'returns a full path to the repository' do
|
|
|
|
expect(repository.path).to eq('group/test/my_image')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when path contains uppercase letters' do
|
2017-08-01 14:51:52 -04:00
|
|
|
let(:project) { create(:project, :repository, path: 'MY_PROJECT', group: group) }
|
2017-04-13 05:54:02 -04:00
|
|
|
|
|
|
|
it 'returns a full path without capital letters' do
|
|
|
|
expect(repository.path).to eq('group/my_project/my_image')
|
|
|
|
end
|
2017-03-23 09:37:17 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#manifest' do
|
2017-04-10 07:01:10 -04:00
|
|
|
it 'returns non-empty manifest' do
|
|
|
|
expect(repository.manifest).not_to be_nil
|
|
|
|
end
|
2017-03-23 09:37:17 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
describe '#valid?' do
|
2017-04-10 07:01:10 -04:00
|
|
|
it 'is a valid repository' do
|
|
|
|
expect(repository).to be_valid
|
|
|
|
end
|
2017-03-23 09:37:17 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
describe '#tags' do
|
2017-04-10 07:01:10 -04:00
|
|
|
it 'returns non-empty tags list' do
|
|
|
|
expect(repository.tags).not_to be_empty
|
|
|
|
end
|
2017-03-23 09:37:17 -04:00
|
|
|
end
|
|
|
|
|
2020-05-25 17:08:00 -04:00
|
|
|
describe '#tags_count' do
|
|
|
|
it 'returns the count of tags' do
|
|
|
|
expect(repository.tags_count).to eq(1)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-04-03 04:57:12 -04:00
|
|
|
describe '#has_tags?' do
|
|
|
|
it 'has tags' do
|
2017-04-10 07:01:10 -04:00
|
|
|
expect(repository).to have_tags
|
2017-04-03 04:57:12 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-03-31 05:54:09 -04:00
|
|
|
describe '#delete_tags!' do
|
2017-04-10 07:01:10 -04:00
|
|
|
let(:repository) do
|
2017-03-31 05:54:09 -04:00
|
|
|
create(:container_repository, name: 'my_image',
|
2019-10-16 11:06:17 -04:00
|
|
|
tags: { latest: '123', rc1: '234' },
|
2017-03-31 05:54:09 -04:00
|
|
|
project: project)
|
2017-03-23 09:37:17 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when action succeeds' do
|
2017-03-31 05:54:09 -04:00
|
|
|
it 'returns status that indicates success' do
|
2017-04-10 07:01:10 -04:00
|
|
|
expect(repository.client)
|
2020-02-12 10:09:37 -05:00
|
|
|
.to receive(:delete_repository_tag_by_digest)
|
2019-10-16 11:06:17 -04:00
|
|
|
.twice
|
2017-03-23 09:37:17 -04:00
|
|
|
.and_return(true)
|
|
|
|
|
2017-04-10 07:01:10 -04:00
|
|
|
expect(repository.delete_tags!).to be_truthy
|
2017-03-23 09:37:17 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when action fails' do
|
2017-03-31 05:54:09 -04:00
|
|
|
it 'returns status that indicates failure' do
|
2017-04-10 07:01:10 -04:00
|
|
|
expect(repository.client)
|
2020-02-12 10:09:37 -05:00
|
|
|
.to receive(:delete_repository_tag_by_digest)
|
2019-10-16 11:06:17 -04:00
|
|
|
.twice
|
2017-03-23 09:37:17 -04:00
|
|
|
.and_return(false)
|
|
|
|
|
2017-04-10 07:01:10 -04:00
|
|
|
expect(repository.delete_tags!).to be_falsey
|
2017-03-23 09:37:17 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2017-03-27 10:04:38 -04:00
|
|
|
|
2020-02-12 10:09:37 -05:00
|
|
|
describe '#delete_tag_by_name' do
|
|
|
|
let(:repository) do
|
|
|
|
create(:container_repository, name: 'my_image',
|
|
|
|
tags: { latest: '123', rc1: '234' },
|
|
|
|
project: project)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when action succeeds' do
|
|
|
|
it 'returns status that indicates success' do
|
|
|
|
expect(repository.client)
|
|
|
|
.to receive(:delete_repository_tag_by_name)
|
|
|
|
.with(repository.path, "latest")
|
|
|
|
.and_return(true)
|
|
|
|
|
|
|
|
expect(repository.delete_tag_by_name('latest')).to be_truthy
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when action fails' do
|
|
|
|
it 'returns status that indicates failure' do
|
|
|
|
expect(repository.client)
|
|
|
|
.to receive(:delete_repository_tag_by_name)
|
|
|
|
.with(repository.path, "latest")
|
|
|
|
.and_return(false)
|
|
|
|
|
|
|
|
expect(repository.delete_tag_by_name('latest')).to be_falsey
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-04-10 06:57:19 -04:00
|
|
|
describe '#location' do
|
|
|
|
context 'when registry is running on a custom port' do
|
|
|
|
before do
|
|
|
|
stub_container_registry_config(enabled: true,
|
|
|
|
api_url: 'http://registry.gitlab:5000',
|
|
|
|
host_port: 'registry.gitlab:5000')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns a full location of the repository' do
|
2017-04-10 07:01:10 -04:00
|
|
|
expect(repository.location)
|
2017-04-10 06:57:19 -04:00
|
|
|
.to eq 'registry.gitlab:5000/group/test/my_image'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-04-03 06:49:54 -04:00
|
|
|
describe '#root_repository?' do
|
|
|
|
context 'when repository is a root repository' do
|
|
|
|
let(:repository) { create(:container_repository, :root) }
|
|
|
|
|
|
|
|
it 'returns true' do
|
|
|
|
expect(repository).to be_root_repository
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when repository is not a root repository' do
|
|
|
|
it 'returns false' do
|
2017-04-10 07:01:10 -04:00
|
|
|
expect(repository).not_to be_root_repository
|
2017-04-03 06:49:54 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '.build_from_path' do
|
2017-04-03 07:05:46 -04:00
|
|
|
let(:registry_path) do
|
|
|
|
ContainerRegistry::Path.new(project.full_path + '/some/image')
|
|
|
|
end
|
2017-04-03 06:49:54 -04:00
|
|
|
|
|
|
|
let(:repository) do
|
2017-04-03 07:05:46 -04:00
|
|
|
described_class.build_from_path(registry_path)
|
2017-04-03 06:49:54 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'fabricates repository assigned to a correct project' do
|
|
|
|
expect(repository.project).to eq project
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'fabricates repository with a correct name' do
|
|
|
|
expect(repository.name).to eq 'some/image'
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'is not persisted' do
|
|
|
|
expect(repository).not_to be_persisted
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-03-31 06:27:05 -04:00
|
|
|
describe '.create_from_path!' do
|
|
|
|
let(:repository) do
|
|
|
|
described_class.create_from_path!(ContainerRegistry::Path.new(path))
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:repository_path) { ContainerRegistry::Path.new(path) }
|
|
|
|
|
2017-03-27 10:04:38 -04:00
|
|
|
context 'when received multi-level repository path' do
|
2017-03-31 06:27:05 -04:00
|
|
|
let(:path) { project.full_path + '/some/image' }
|
2017-03-27 10:04:38 -04:00
|
|
|
|
2017-03-31 06:27:05 -04:00
|
|
|
it 'fabricates repository assigned to a correct project' do
|
2017-03-27 10:04:38 -04:00
|
|
|
expect(repository.project).to eq project
|
|
|
|
end
|
|
|
|
|
2017-03-31 06:27:05 -04:00
|
|
|
it 'fabricates repository with a correct name' do
|
|
|
|
expect(repository.name).to eq 'some/image'
|
2017-03-27 10:04:38 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-03-31 06:27:05 -04:00
|
|
|
context 'when path is too long' do
|
|
|
|
let(:path) do
|
|
|
|
project.full_path + '/a/b/c/d/e/f/g/h/i/j/k/l/n/o/p/s/t/u/x/y/z'
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not create repository and raises error' do
|
|
|
|
expect { repository }.to raise_error(
|
|
|
|
ContainerRegistry::Path::InvalidRegistryPathError)
|
|
|
|
end
|
2017-03-27 10:04:38 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when received multi-level repository with nested groups' do
|
2017-03-31 06:27:05 -04:00
|
|
|
let(:group) { create(:group, :nested, name: 'nested') }
|
|
|
|
let(:path) { project.full_path + '/some/image' }
|
|
|
|
|
|
|
|
it 'fabricates repository assigned to a correct project' do
|
|
|
|
expect(repository.project).to eq project
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'fabricates repository with a correct name' do
|
|
|
|
expect(repository.name).to eq 'some/image'
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'has path including a nested group' do
|
|
|
|
expect(repository.path).to include 'nested/test/some/image'
|
|
|
|
end
|
2017-03-27 10:04:38 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when received root repository path' do
|
2017-03-31 06:27:05 -04:00
|
|
|
let(:path) { project.full_path }
|
|
|
|
|
|
|
|
it 'fabricates repository assigned to a correct project' do
|
|
|
|
expect(repository.project).to eq project
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'fabricates repository with an empty name' do
|
|
|
|
expect(repository.name).to be_empty
|
|
|
|
end
|
2017-03-27 10:04:38 -04:00
|
|
|
end
|
|
|
|
end
|
2017-04-04 06:57:38 -04:00
|
|
|
|
|
|
|
describe '.build_root_repository' do
|
|
|
|
let(:repository) do
|
|
|
|
described_class.build_root_repository(project)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'fabricates a root repository object' do
|
|
|
|
expect(repository).to be_root_repository
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'assignes it to the correct project' do
|
|
|
|
expect(repository.project).to eq project
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not persist it' do
|
|
|
|
expect(repository).not_to be_persisted
|
|
|
|
end
|
|
|
|
end
|
2019-11-04 10:07:36 -05:00
|
|
|
|
|
|
|
describe '.for_group_and_its_subgroups' do
|
|
|
|
subject { described_class.for_group_and_its_subgroups(test_group) }
|
|
|
|
|
|
|
|
context 'in a group' do
|
|
|
|
let(:test_group) { group }
|
|
|
|
|
|
|
|
it { is_expected.to contain_exactly(repository) }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with a subgroup' do
|
|
|
|
let(:test_group) { create(:group) }
|
|
|
|
let(:another_project) { create(:project, path: 'test', group: test_group) }
|
|
|
|
|
|
|
|
let(:another_repository) do
|
|
|
|
create(:container_repository, name: 'my_image', project: another_project)
|
|
|
|
end
|
|
|
|
|
|
|
|
before do
|
|
|
|
group.parent = test_group
|
|
|
|
group.save
|
|
|
|
end
|
|
|
|
|
|
|
|
it { is_expected.to contain_exactly(repository, another_repository) }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'group without container_repositories' do
|
|
|
|
let(:test_group) { create(:group) }
|
|
|
|
|
|
|
|
it { is_expected.to eq([]) }
|
|
|
|
end
|
|
|
|
end
|
2020-04-27 20:09:33 -04:00
|
|
|
|
|
|
|
describe '.search_by_name' do
|
|
|
|
let!(:another_repository) do
|
|
|
|
create(:container_repository, name: 'my_foo_bar', project: project)
|
|
|
|
end
|
|
|
|
|
|
|
|
subject { described_class.search_by_name('my_image') }
|
|
|
|
|
|
|
|
it { is_expected.to contain_exactly(repository) }
|
|
|
|
end
|
2017-03-23 09:37:17 -04:00
|
|
|
end
|