Commit af42dd29 authored by Grzegorz Bizon's avatar Grzegorz Bizon

Fix specs for container repository tags

parent dcd2eeb1
class ContainerRepository < ActiveRecord::Base
belongs_to :project
delegate :client, to: :registry
validates :manifest, presence: true
validates :name, presence: true
validates :name, length: { minimum: 0, allow_nil: false }
delegate :client, to: :registry
before_destroy :delete_tags
def registry
......@@ -17,7 +19,7 @@ class ContainerRepository < ActiveRecord::Base
end
def path
@path ||= "#{project.full_path}/#{name}"
@path ||= [project.full_path, name].select(&:present?).join('/')
end
def tag(tag)
......
......@@ -38,11 +38,11 @@ module ContainerRegistry
end
def delete
client.delete_blob(repository.name_with_namespace, digest)
client.delete_blob(repository.path, digest)
end
def data
@data ||= client.blob(repository.name_with_namespace, digest, type)
@data ||= client.blob(repository.path, digest, type)
end
end
end
......@@ -22,7 +22,7 @@ module ContainerRegistry
end
def manifest
@manifest ||= client.repository_manifest(repository.name_with_namespace, name)
@manifest ||= client.repository_manifest(repository.path, name)
end
def path
......@@ -38,7 +38,7 @@ module ContainerRegistry
def digest
return @digest if defined?(@digest)
@digest = client.repository_tag_digest(repository.name_with_namespace, name)
@digest = client.repository_tag_digest(repository.path, name)
end
def config_blob
......@@ -80,7 +80,7 @@ module ContainerRegistry
def delete
return unless digest
client.delete_repository_tag(repository.name_with_namespace, digest)
client.delete_repository_tag(repository.path, digest)
end
end
end
FactoryGirl.define do
factory :container_repository do
name "test_container_image"
name 'test_container_image'
project
transient do
tags ['tag']
tags []
end
after(:build) do |image, evaluator|
# if evaluator.tags.to_a.any?
# allow(Gitlab.config.registry).to receive(:enabled).and_return(true)
# allow(Auth::ContainerRegistryAuthenticationService)
# .to receive(:full_access_token).and_return('token')
# allow(image.client).to receive(:repository_tags).and_return({
# name: image.name_with_namespace,
# tags: evaluator.tags
# })
# end
after(:build) do |repository, evaluator|
if evaluator.tags.any?
allow(repository.client)
.to receive(:repository_tags)
.and_return({
name: repository.path,
tags: evaluator.tags
})
end
end
end
end
......@@ -3,30 +3,58 @@ require 'spec_helper'
describe ContainerRegistry::Tag do
let(:group) { create(:group, name: 'group') }
let(:project) { create(:project, path: 'test', group: group) }
let(:example_host) { 'example.com' }
let(:registry_url) { 'http://' + example_host }
let(:repository) { create(:container_repository, name: '', project: project) }
let(:tag) { repository.tag('tag') }
let(:headers) { { 'Accept' => 'application/vnd.docker.distribution.manifest.v2+json' } }
let(:repository) do
create(:container_repository, name: '', tags: %w[latest], project: project)
end
# TODO, move stubs to helper with this header
let(:headers) do
{ 'Accept' => 'application/vnd.docker.distribution.manifest.v2+json' }
end
let(:tag) { described_class.new(repository, 'tag') }
before do
stub_container_registry_config(enabled: true, api_url: registry_url, host_port: example_host)
stub_container_registry_config(enabled: true,
api_url: 'http://registry.gitlab',
host_port: 'registry.gitlab')
end
it { expect(tag).to respond_to(:repository) }
it { expect(tag).to delegate_method(:registry).to(:repository) }
it { expect(tag).to delegate_method(:client).to(:repository) }
context '#path' do
subject { tag.path }
describe '#path' do
context 'when tag belongs to zero-level repository' do
let(:repository) do
create(:container_repository, name: '',
tags: %w[rc1],
project: project)
end
it { is_expected.to eq('example.com/group/test:tag') }
it 'returns path to the image' do
expect(tag.path).to eq('group/test:tag')
end
end
context 'when tag belongs to first-level repository' do
let(:repository) do
create(:container_repository, name: 'my_image',
tags: %w[latest],
project: project)
end
it 'returns path to the image' do
expect(tag.path).to eq('group/test/my_image:tag')
end
end
end
context 'manifest processing' do
context 'schema v1' do
before do
stub_request(:get, 'http://example.com/v2/group/test/manifests/tag').
stub_request(:get, 'http://registry.gitlab/v2/group/test/manifests/tag').
with(headers: headers).
to_return(
status: 200,
......@@ -63,7 +91,7 @@ describe ContainerRegistry::Tag do
context 'schema v2' do
before do
stub_request(:get, 'http://example.com/v2/group/test/manifests/tag').
stub_request(:get, 'http://registry.gitlab/v2/group/test/manifests/tag').
with(headers: headers).
to_return(
status: 200,
......@@ -100,7 +128,7 @@ describe ContainerRegistry::Tag do
context 'when locally stored' do
before do
stub_request(:get, 'http://example.com/v2/group/test/blobs/sha256:d7a513a663c1a6dcdba9ed832ca53c02ac2af0c333322cd6ca92936d1d9917ac').
stub_request(:get, 'http://registry.gitlab/v2/group/test/blobs/sha256:d7a513a663c1a6dcdba9ed832ca53c02ac2af0c333322cd6ca92936d1d9917ac').
with(headers: { 'Accept' => 'application/octet-stream' }).
to_return(
status: 200,
......@@ -112,7 +140,7 @@ describe ContainerRegistry::Tag do
context 'when externally stored' do
before do
stub_request(:get, 'http://example.com/v2/group/test/blobs/sha256:d7a513a663c1a6dcdba9ed832ca53c02ac2af0c333322cd6ca92936d1d9917ac').
stub_request(:get, 'http://registry.gitlab/v2/group/test/blobs/sha256:d7a513a663c1a6dcdba9ed832ca53c02ac2af0c333322cd6ca92936d1d9917ac').
with(headers: { 'Accept' => 'application/octet-stream' }).
to_return(
status: 307,
......@@ -132,7 +160,7 @@ describe ContainerRegistry::Tag do
context 'manifest digest' do
before do
stub_request(:head, 'http://example.com/v2/group/test/manifests/tag').
stub_request(:head, 'http://registry.gitlab/v2/group/test/manifests/tag').
with(headers: headers).
to_return(status: 200, headers: { 'Docker-Content-Digest' => 'sha256:digest' })
end
......@@ -145,7 +173,7 @@ describe ContainerRegistry::Tag do
context '#delete' do
before do
stub_request(:delete, 'http://example.com/v2/group/test/manifests/sha256:digest').
stub_request(:delete, 'http://registry.gitlab/v2/group/test/manifests/sha256:digest').
with(headers: headers).
to_return(status: 200)
end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment