BigW Consortium Gitlab

Commit f451173a by Grzegorz Bizon

Fix specs for container repository model class

parent ea16ea5b
class ContainerRepository < ActiveRecord::Base class ContainerRepository < ActiveRecord::Base
belongs_to :project belongs_to :project
delegate :client, to: :registry
delegate :container_registry, to: :project
delegate :client, to: :container_registry
validates :manifest, presence: true validates :manifest, presence: true
validates :name, presence: true
before_destroy :delete_tags before_destroy :delete_tags
def registry def registry
# TODO, container registry with image access level @registry ||= begin
token = Auth::ContainerRegistryAuthenticationService.image_token(self) token = Auth::ContainerRegistryAuthenticationService.full_access_token(path)
url = Gitlab.config.registry.api_url
host_port = Gitlab.config.registry.host_port
ContainerRegistry::Registry.new(url, token: token, path: host_port)
end
end
def path
@path ||= "#{project.full_path}/#{name}"
end end
def tag(tag) def tag(tag)
......
FactoryGirl.define do
factory :container_image do
name "test_container_image"
project
transient do
tags ['tag']
stubbed true
end
after(:build) do |image, evaluator|
if evaluator.stubbed
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
end
end
end
FactoryGirl.define do
factory :container_repository do
name "test_container_image"
project
transient do
tags ['tag']
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
end
end
end
require 'spec_helper'
describe ContainerImage 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(:container_image) { create(:container_image, name: '', project: project, stubbed: false) }
before do
stub_container_registry_config(enabled: true, api_url: registry_url, host_port: example_host)
stub_request(:get, 'http://example.com/v2/group/test/tags/list').
with(headers: { 'Accept' => 'application/vnd.docker.distribution.manifest.v2+json' }).
to_return(
status: 200,
body: JSON.dump(tags: ['test']),
headers: { 'Content-Type' => 'application/json' })
end
it { expect(container_image).to respond_to(:project) }
it { expect(container_image).to delegate_method(:container_registry).to(:project) }
it { expect(container_image).to delegate_method(:client).to(:container_registry) }
it { expect(container_image.tag('test')).not_to be_nil }
context '#path' do
subject { container_image.path }
it { is_expected.to eq('example.com/group/test') }
end
context 'manifest processing' do
context '#manifest' do
subject { container_image.manifest }
it { is_expected.not_to be_nil }
end
context '#valid?' do
subject { container_image.valid? }
it { is_expected.to be_truthy }
end
context '#tags' do
subject { container_image.tags }
it { is_expected.not_to be_empty }
end
end
context '#delete_tags' do
let(:tag) { ContainerRegistry::Tag.new(container_image, 'tag') }
before do
expect(container_image).to receive(:tags).twice.and_return([tag])
expect(tag).to receive(:digest).and_return('sha256:4c8e63ca4cb663ce6c688cb06f1c3672a172b088dac5b6d7ad7d49cd620d85cf')
end
subject { container_image.delete_tags }
context 'succeeds' do
before { expect(container_image.client).to receive(:delete_repository_tag).and_return(true) }
it { is_expected.to be_truthy }
end
context 'any fails' do
before { expect(container_image.client).to receive(:delete_repository_tag).and_return(false) }
it { is_expected.to be_falsey }
end
end
end
require 'spec_helper'
describe ContainerRepository do
let(:group) { create(:group, name: 'group') }
let(:project) { create(:project, path: 'test', group: group) }
let(:container_repository) do
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')
.with(headers: {
'Accept' => 'application/vnd.docker.distribution.manifest.v2+json' })
.to_return(
status: 200,
body: JSON.dump(tags: ['test_tag']),
headers: { 'Content-Type' => 'application/json' })
end
describe 'associations' do
it 'belongs to the project' do
expect(container_repository).to belong_to(:project)
end
end
describe '#tag' do
it 'has a test tag' do
expect(container_repository.tag('test')).not_to be_nil
end
end
describe '#path' do
it 'returns a full path to the repository' do
expect(container_repository.path).to eq('group/test/my_image')
end
end
describe '#manifest' do
subject { container_repository.manifest }
it { is_expected.not_to be_nil }
end
describe '#valid?' do
subject { container_repository.valid? }
it { is_expected.to be_truthy }
end
describe '#tags' do
subject { container_repository.tags }
it { is_expected.not_to be_empty }
end
# TODO, improve these specs
#
describe '#delete_tags' do
let(:tag) { ContainerRegistry::Tag.new(container_repository, 'tag') }
before do
allow(container_repository).to receive(:tags).twice.and_return([tag])
allow(tag).to receive(:digest)
.and_return('sha256:4c8e63ca4cb663ce6c688cb06f1c3672a172b088dac5b6d7ad7d49cd620d85cf')
end
context 'when action succeeds' do
before do
allow(container_repository.client)
.to receive(:delete_repository_tag)
.and_return(true)
end
it 'returns status that indicates success' do
expect(container_repository.delete_tags).to be_truthy
end
end
context 'when action fails' do
before do
allow(container_repository.client)
.to receive(:delete_repository_tag)
.and_return(false)
end
it 'returns status that indicates failure' do
expect(container_repository.delete_tags).to be_falsey
end
end
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