Commit af42dd29 authored by Grzegorz Bizon's avatar Grzegorz Bizon

Fix specs for container repository tags

parent dcd2eeb1
class ContainerRepository < ActiveRecord::Base class ContainerRepository < ActiveRecord::Base
belongs_to :project belongs_to :project
delegate :client, to: :registry
validates :manifest, presence: true validates :manifest, presence: true
validates :name, presence: true validates :name, length: { minimum: 0, allow_nil: false }
delegate :client, to: :registry
before_destroy :delete_tags before_destroy :delete_tags
def registry def registry
...@@ -17,7 +19,7 @@ class ContainerRepository < ActiveRecord::Base ...@@ -17,7 +19,7 @@ class ContainerRepository < ActiveRecord::Base
end end
def path def path
@path ||= "#{project.full_path}/#{name}" @path ||= [project.full_path, name].select(&:present?).join('/')
end end
def tag(tag) def tag(tag)
......
...@@ -38,11 +38,11 @@ module ContainerRegistry ...@@ -38,11 +38,11 @@ module ContainerRegistry
end end
def delete def delete
client.delete_blob(repository.name_with_namespace, digest) client.delete_blob(repository.path, digest)
end end
def data def data
@data ||= client.blob(repository.name_with_namespace, digest, type) @data ||= client.blob(repository.path, digest, type)
end end
end end
end end
...@@ -22,7 +22,7 @@ module ContainerRegistry ...@@ -22,7 +22,7 @@ module ContainerRegistry
end end
def manifest def manifest
@manifest ||= client.repository_manifest(repository.name_with_namespace, name) @manifest ||= client.repository_manifest(repository.path, name)
end end
def path def path
...@@ -38,7 +38,7 @@ module ContainerRegistry ...@@ -38,7 +38,7 @@ module ContainerRegistry
def digest def digest
return @digest if defined?(@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 end
def config_blob def config_blob
...@@ -80,7 +80,7 @@ module ContainerRegistry ...@@ -80,7 +80,7 @@ module ContainerRegistry
def delete def delete
return unless digest return unless digest
client.delete_repository_tag(repository.name_with_namespace, digest) client.delete_repository_tag(repository.path, digest)
end end
end end
end end
FactoryGirl.define do FactoryGirl.define do
factory :container_repository do factory :container_repository do
name "test_container_image" name 'test_container_image'
project project
transient do transient do
tags ['tag'] tags []
end end
after(:build) do |image, evaluator| after(:build) do |repository, evaluator|
# if evaluator.tags.to_a.any? if evaluator.tags.any?
# allow(Gitlab.config.registry).to receive(:enabled).and_return(true) allow(repository.client)
# allow(Auth::ContainerRegistryAuthenticationService) .to receive(:repository_tags)
# .to receive(:full_access_token).and_return('token') .and_return({
# allow(image.client).to receive(:repository_tags).and_return({ name: repository.path,
# name: image.name_with_namespace, tags: evaluator.tags
# tags: evaluator.tags })
# }) end
# end
end end
end end
end end
...@@ -3,30 +3,58 @@ require 'spec_helper' ...@@ -3,30 +3,58 @@ require 'spec_helper'
describe ContainerRegistry::Tag do describe ContainerRegistry::Tag do
let(:group) { create(:group, name: 'group') } let(:group) { create(:group, name: 'group') }
let(:project) { create(:project, path: 'test', group: group) } let(:project) { create(:project, path: 'test', group: group) }
let(:example_host) { 'example.com' }
let(:registry_url) { 'http://' + example_host } let(:repository) do
let(:repository) { create(:container_repository, name: '', project: project) } create(:container_repository, name: '', tags: %w[latest], project: project)
let(:tag) { repository.tag('tag') } end
let(:headers) { { 'Accept' => 'application/vnd.docker.distribution.manifest.v2+json' } }
# 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 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 end
it { expect(tag).to respond_to(:repository) } it { expect(tag).to respond_to(:repository) }
it { expect(tag).to delegate_method(:registry).to(:repository) } it { expect(tag).to delegate_method(:registry).to(:repository) }
it { expect(tag).to delegate_method(:client).to(:repository) } it { expect(tag).to delegate_method(:client).to(:repository) }
context '#path' do describe '#path' do
subject { tag.path } 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 end
context 'manifest processing' do context 'manifest processing' do
context 'schema v1' do context 'schema v1' do
before 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). with(headers: headers).
to_return( to_return(
status: 200, status: 200,
...@@ -63,7 +91,7 @@ describe ContainerRegistry::Tag do ...@@ -63,7 +91,7 @@ describe ContainerRegistry::Tag do
context 'schema v2' do context 'schema v2' do
before 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). with(headers: headers).
to_return( to_return(
status: 200, status: 200,
...@@ -100,7 +128,7 @@ describe ContainerRegistry::Tag do ...@@ -100,7 +128,7 @@ describe ContainerRegistry::Tag do
context 'when locally stored' do context 'when locally stored' do
before 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' }). with(headers: { 'Accept' => 'application/octet-stream' }).
to_return( to_return(
status: 200, status: 200,
...@@ -112,7 +140,7 @@ describe ContainerRegistry::Tag do ...@@ -112,7 +140,7 @@ describe ContainerRegistry::Tag do
context 'when externally stored' do context 'when externally stored' do
before 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' }). with(headers: { 'Accept' => 'application/octet-stream' }).
to_return( to_return(
status: 307, status: 307,
...@@ -132,7 +160,7 @@ describe ContainerRegistry::Tag do ...@@ -132,7 +160,7 @@ describe ContainerRegistry::Tag do
context 'manifest digest' do context 'manifest digest' do
before 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). with(headers: headers).
to_return(status: 200, headers: { 'Docker-Content-Digest' => 'sha256:digest' }) to_return(status: 200, headers: { 'Docker-Content-Digest' => 'sha256:digest' })
end end
...@@ -145,7 +173,7 @@ describe ContainerRegistry::Tag do ...@@ -145,7 +173,7 @@ describe ContainerRegistry::Tag do
context '#delete' do context '#delete' do
before 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). with(headers: headers).
to_return(status: 200) to_return(status: 200)
end 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