Commit 4fdcaca6 authored by Grzegorz Bizon's avatar Grzegorz Bizon

Fix container registry blob specs

parent 7db1f226
require 'spec_helper' require 'spec_helper'
describe ContainerRegistry::Blob do describe ContainerRegistry::Blob do
let(:digest) { 'sha256:0123456789012345' } let(:group) { create(:group, name: 'group') }
let(:project) { create(:project, path: 'test', group: group) }
let(:repository) do
create(:container_repository, name: 'image',
tags: %w[latest rc1],
project: project)
end
let(:config) do let(:config) do
{ { 'digest' => 'sha256:0123456789012345',
'digest' => digest,
'mediaType' => 'binary', 'mediaType' => 'binary',
'size' => 1000 'size' => 1000 }
}
end end
let(:token) { 'token' }
let(:group) { create(:group, name: 'group') } let(:blob) { described_class.new(repository, config) }
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(:blob) { repository.blob(config) }
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(blob).to respond_to(:repository) } it { expect(blob).to respond_to(:repository) }
it { expect(blob).to delegate_method(:registry).to(:repository) } it { expect(blob).to delegate_method(:registry).to(:repository) }
it { expect(blob).to delegate_method(:client).to(:repository) } it { expect(blob).to delegate_method(:client).to(:repository) }
context '#path' do describe '#path' do
subject { blob.path } it 'returns a valid path to the blob' do
expect(blob.path).to eq('group/test/image@sha256:0123456789012345')
it { is_expected.to eq('example.com/group/test@sha256:0123456789012345') } end
end end
context '#digest' do describe '#digest' do
subject { blob.digest } it 'return correct digest value' do
expect(blob.digest).to eq 'sha256:0123456789012345'
it { is_expected.to eq(digest) } end
end end
context '#type' do describe '#type' do
subject { blob.type } it 'returns a correct type' do
expect(blob.type).to eq 'binary'
it { is_expected.to eq('binary') } end
end end
context '#revision' do describe '#revision' do
subject { blob.revision } it 'returns a correct blob SHA' do
expect(blob.revision).to eq '0123456789012345'
it { is_expected.to eq('0123456789012345') } end
end end
context '#short_revision' do describe '#short_revision' do
subject { blob.short_revision } it 'return a short SHA' do
expect(blob.short_revision).to eq '012345678'
it { is_expected.to eq('012345678') } end
end end
context '#delete' do describe '#delete' do
before do before do
stub_request(:delete, 'http://example.com/v2/group/test/blobs/sha256:0123456789012345'). stub_request(:delete, 'http://registry.gitlab/v2/group/test/image/blobs/sha256:0123456789012345')
to_return(status: 200) .to_return(status: 200)
end end
subject { blob.delete } it 'returns true when blob has been successfuly deleted' do
expect(blob.delete).to be_truthy
it { is_expected.to be_truthy } end
end end
context '#data' do describe '#data' do
let(:data) { '{"key":"value"}' }
subject { blob.data }
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:0123456789012345'). stub_request(:get, 'http://registry.gitlab/v2/group/test/image/blobs/sha256:0123456789012345').
to_return( to_return(
status: 200, status: 200,
headers: { 'Content-Type' => 'application/json' }, headers: { 'Content-Type' => 'application/json' },
body: data) body: '{"key":"value"}')
end end
it { is_expected.to eq(data) } it 'returns a correct blob data' do
expect(blob.data).to eq '{"key":"value"}'
end
end end
context 'when externally stored' do context 'when externally stored' do
let(:location) { 'http://external.com/blob/file' }
before do before do
stub_request(:get, 'http://example.com/v2/group/test/blobs/sha256:0123456789012345'). stub_request(:get, 'http://registry.gitlab/v2/group/test/image/blobs/sha256:0123456789012345')
with(headers: { 'Authorization' => "bearer #{token}" }). .with(headers: { 'Authorization' => 'bearer token' })
to_return( .to_return(
status: 307, status: 307,
headers: { 'Location' => location }) headers: { 'Location' => location })
end end
context 'for a valid address' do context 'for a valid address' do
let(:location) { 'http://external.com/blob/file' }
before do before do
stub_request(:get, location). stub_request(:get, location).
with(headers: { 'Authorization' => nil }). with(headers: { 'Authorization' => nil }).
to_return( to_return(
status: 200, status: 200,
headers: { 'Content-Type' => 'application/json' }, headers: { 'Content-Type' => 'application/json' },
body: data) body: '{"key":"value"}')
end end
it { is_expected.to eq(data) } it 'returns correct data' do
expect(blob.data).to eq '{"key":"value"}'
end
end end
context 'for invalid file' do context 'for invalid file' do
let(:location) { 'file:///etc/passwd' } let(:location) { 'file:///etc/passwd' }
it { expect{ subject }.to raise_error(ArgumentError, 'invalid address') } it 'raises an error' do
expect { blob.data }.to raise_error(ArgumentError, 'invalid address')
end
end end
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