2016-05-17 14:20:11 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
describe ContainerRegistry::Blob do
|
2017-03-24 08:08:21 -04:00
|
|
|
let(:group) { create(:group, name: 'group') }
|
2017-08-02 15:55:11 -04:00
|
|
|
let(:project) { create(:project, path: 'test', group: group) }
|
2017-03-24 08:08:21 -04:00
|
|
|
|
|
|
|
let(:repository) do
|
|
|
|
create(:container_repository, name: 'image',
|
|
|
|
tags: %w[latest rc1],
|
|
|
|
project: project)
|
|
|
|
end
|
|
|
|
|
2016-05-17 14:20:11 -04:00
|
|
|
let(:config) do
|
2017-03-24 08:08:21 -04:00
|
|
|
{ 'digest' => 'sha256:0123456789012345',
|
2016-05-17 14:20:11 -04:00
|
|
|
'mediaType' => 'binary',
|
2017-03-24 08:08:21 -04:00
|
|
|
'size' => 1000 }
|
2016-05-17 14:20:11 -04:00
|
|
|
end
|
2016-12-15 22:24:05 -05:00
|
|
|
|
2017-03-24 08:08:21 -04:00
|
|
|
let(:blob) { described_class.new(repository, config) }
|
2016-05-17 14:20:11 -04:00
|
|
|
|
2016-12-15 22:24:05 -05:00
|
|
|
before do
|
2017-03-24 08:08:21 -04:00
|
|
|
stub_container_registry_config(enabled: true,
|
|
|
|
api_url: 'http://registry.gitlab',
|
|
|
|
host_port: 'registry.gitlab')
|
2016-12-15 22:24:05 -05:00
|
|
|
end
|
|
|
|
|
2016-05-17 14:20:11 -04:00
|
|
|
it { expect(blob).to respond_to(:repository) }
|
|
|
|
it { expect(blob).to delegate_method(:registry).to(:repository) }
|
|
|
|
it { expect(blob).to delegate_method(:client).to(:repository) }
|
|
|
|
|
2017-03-24 08:08:21 -04:00
|
|
|
describe '#path' do
|
|
|
|
it 'returns a valid path to the blob' do
|
|
|
|
expect(blob.path).to eq('group/test/image@sha256:0123456789012345')
|
|
|
|
end
|
2016-05-17 14:20:11 -04:00
|
|
|
end
|
|
|
|
|
2017-03-24 08:08:21 -04:00
|
|
|
describe '#digest' do
|
|
|
|
it 'return correct digest value' do
|
|
|
|
expect(blob.digest).to eq 'sha256:0123456789012345'
|
|
|
|
end
|
2016-05-17 14:20:11 -04:00
|
|
|
end
|
|
|
|
|
2017-03-24 08:08:21 -04:00
|
|
|
describe '#type' do
|
|
|
|
it 'returns a correct type' do
|
|
|
|
expect(blob.type).to eq 'binary'
|
|
|
|
end
|
2016-05-17 14:20:11 -04:00
|
|
|
end
|
|
|
|
|
2017-03-24 08:08:21 -04:00
|
|
|
describe '#revision' do
|
|
|
|
it 'returns a correct blob SHA' do
|
|
|
|
expect(blob.revision).to eq '0123456789012345'
|
|
|
|
end
|
2016-05-17 14:20:11 -04:00
|
|
|
end
|
|
|
|
|
2017-03-24 08:08:21 -04:00
|
|
|
describe '#short_revision' do
|
|
|
|
it 'return a short SHA' do
|
|
|
|
expect(blob.short_revision).to eq '012345678'
|
|
|
|
end
|
2016-05-17 14:20:11 -04:00
|
|
|
end
|
|
|
|
|
2017-03-24 08:08:21 -04:00
|
|
|
describe '#delete' do
|
2016-05-17 14:20:11 -04:00
|
|
|
before do
|
2017-03-24 08:08:21 -04:00
|
|
|
stub_request(:delete, 'http://registry.gitlab/v2/group/test/image/blobs/sha256:0123456789012345')
|
|
|
|
.to_return(status: 200)
|
2016-05-17 14:20:11 -04:00
|
|
|
end
|
|
|
|
|
2017-03-24 08:08:21 -04:00
|
|
|
it 'returns true when blob has been successfuly deleted' do
|
|
|
|
expect(blob.delete).to be_truthy
|
|
|
|
end
|
2016-05-17 14:20:11 -04:00
|
|
|
end
|
2016-06-30 08:49:58 -04:00
|
|
|
|
2017-03-24 08:08:21 -04:00
|
|
|
describe '#data' do
|
2016-06-30 08:49:58 -04:00
|
|
|
context 'when locally stored' do
|
|
|
|
before do
|
2017-06-21 09:48:12 -04:00
|
|
|
stub_request(:get, 'http://registry.gitlab/v2/group/test/image/blobs/sha256:0123456789012345')
|
|
|
|
.to_return(
|
2016-06-30 08:49:58 -04:00
|
|
|
status: 200,
|
|
|
|
headers: { 'Content-Type' => 'application/json' },
|
2017-03-24 08:08:21 -04:00
|
|
|
body: '{"key":"value"}')
|
2016-06-30 08:49:58 -04:00
|
|
|
end
|
|
|
|
|
2017-03-24 08:08:21 -04:00
|
|
|
it 'returns a correct blob data' do
|
|
|
|
expect(blob.data).to eq '{"key":"value"}'
|
|
|
|
end
|
2016-06-30 08:49:58 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when externally stored' do
|
2017-03-24 08:08:21 -04:00
|
|
|
let(:location) { 'http://external.com/blob/file' }
|
|
|
|
|
2016-06-30 08:49:58 -04:00
|
|
|
before do
|
2017-03-24 08:08:21 -04:00
|
|
|
stub_request(:get, 'http://registry.gitlab/v2/group/test/image/blobs/sha256:0123456789012345')
|
|
|
|
.with(headers: { 'Authorization' => 'bearer token' })
|
|
|
|
.to_return(
|
2016-06-30 08:49:58 -04:00
|
|
|
status: 307,
|
|
|
|
headers: { 'Location' => location })
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'for a valid address' do
|
|
|
|
before do
|
2017-06-21 09:48:12 -04:00
|
|
|
stub_request(:get, location)
|
|
|
|
.with { |request| !request.headers.include?('Authorization') }
|
|
|
|
.to_return(
|
2016-06-30 08:49:58 -04:00
|
|
|
status: 200,
|
|
|
|
headers: { 'Content-Type' => 'application/json' },
|
2017-03-24 08:08:21 -04:00
|
|
|
body: '{"key":"value"}')
|
2016-06-30 08:49:58 -04:00
|
|
|
end
|
|
|
|
|
2017-03-24 08:08:21 -04:00
|
|
|
it 'returns correct data' do
|
|
|
|
expect(blob.data).to eq '{"key":"value"}'
|
|
|
|
end
|
2016-06-30 08:49:58 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'for invalid file' do
|
|
|
|
let(:location) { 'file:///etc/passwd' }
|
|
|
|
|
2017-03-24 08:08:21 -04:00
|
|
|
it 'raises an error' do
|
|
|
|
expect { blob.data }.to raise_error(ArgumentError, 'invalid address')
|
|
|
|
end
|
2016-06-30 08:49:58 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2016-05-17 14:20:11 -04:00
|
|
|
end
|