2019-10-12 21:52:04 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-06-02 11:05:42 +05:30
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
RSpec.describe ContainerRegistry::Blob do
|
2017-08-17 22:00:37 +05:30
|
|
|
let(:group) { create(:group, name: 'group') }
|
2017-09-10 17:25:29 +05:30
|
|
|
let(:project) { create(:project, path: 'test', group: group) }
|
2017-08-17 22:00:37 +05:30
|
|
|
|
|
|
|
let(:repository) do
|
|
|
|
create(:container_repository, name: 'image',
|
|
|
|
tags: %w[latest rc1],
|
|
|
|
project: project)
|
|
|
|
end
|
|
|
|
|
2016-06-02 11:05:42 +05:30
|
|
|
let(:config) do
|
2017-08-17 22:00:37 +05:30
|
|
|
{ 'digest' => 'sha256:0123456789012345',
|
2016-06-02 11:05:42 +05:30
|
|
|
'mediaType' => 'binary',
|
2017-08-17 22:00:37 +05:30
|
|
|
'size' => 1000 }
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:blob) { described_class.new(repository, config) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
stub_container_registry_config(enabled: true,
|
|
|
|
api_url: 'http://registry.gitlab',
|
|
|
|
host_port: 'registry.gitlab')
|
2016-06-02 11:05:42 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
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-08-17 22:00:37 +05:30
|
|
|
describe '#path' do
|
|
|
|
it 'returns a valid path to the blob' do
|
|
|
|
expect(blob.path).to eq('group/test/image@sha256:0123456789012345')
|
|
|
|
end
|
2016-06-02 11:05:42 +05:30
|
|
|
end
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
describe '#digest' do
|
|
|
|
it 'return correct digest value' do
|
|
|
|
expect(blob.digest).to eq 'sha256:0123456789012345'
|
|
|
|
end
|
2016-06-02 11:05:42 +05:30
|
|
|
end
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
describe '#type' do
|
|
|
|
it 'returns a correct type' do
|
|
|
|
expect(blob.type).to eq 'binary'
|
|
|
|
end
|
2016-06-02 11:05:42 +05:30
|
|
|
end
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
describe '#revision' do
|
|
|
|
it 'returns a correct blob SHA' do
|
|
|
|
expect(blob.revision).to eq '0123456789012345'
|
|
|
|
end
|
2016-06-02 11:05:42 +05:30
|
|
|
end
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
describe '#short_revision' do
|
|
|
|
it 'return a short SHA' do
|
|
|
|
expect(blob.short_revision).to eq '012345678'
|
|
|
|
end
|
2016-06-02 11:05:42 +05:30
|
|
|
end
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
describe '#delete' do
|
2016-06-02 11:05:42 +05:30
|
|
|
before do
|
2017-08-17 22:00:37 +05:30
|
|
|
stub_request(:delete, 'http://registry.gitlab/v2/group/test/image/blobs/sha256:0123456789012345')
|
|
|
|
.to_return(status: 200)
|
2016-06-02 11:05:42 +05:30
|
|
|
end
|
|
|
|
|
2018-12-13 13:39:08 +05:30
|
|
|
it 'returns true when blob has been successfully deleted' do
|
2017-08-17 22:00:37 +05:30
|
|
|
expect(blob.delete).to be_truthy
|
|
|
|
end
|
2016-06-02 11:05:42 +05:30
|
|
|
end
|
2016-08-24 12:49:21 +05:30
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
describe '#data' do
|
2016-08-24 12:49:21 +05:30
|
|
|
context 'when locally stored' do
|
|
|
|
before do
|
2017-09-10 17:25:29 +05:30
|
|
|
stub_request(:get, 'http://registry.gitlab/v2/group/test/image/blobs/sha256:0123456789012345')
|
|
|
|
.to_return(
|
2016-08-24 12:49:21 +05:30
|
|
|
status: 200,
|
|
|
|
headers: { 'Content-Type' => 'application/json' },
|
2017-08-17 22:00:37 +05:30
|
|
|
body: '{"key":"value"}')
|
2016-08-24 12:49:21 +05:30
|
|
|
end
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
it 'returns a correct blob data' do
|
|
|
|
expect(blob.data).to eq '{"key":"value"}'
|
|
|
|
end
|
2016-08-24 12:49:21 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
context 'when externally stored' do
|
2017-08-17 22:00:37 +05:30
|
|
|
let(:location) { 'http://external.com/blob/file' }
|
|
|
|
|
2016-08-24 12:49:21 +05:30
|
|
|
before do
|
2017-08-17 22:00:37 +05:30
|
|
|
stub_request(:get, 'http://registry.gitlab/v2/group/test/image/blobs/sha256:0123456789012345')
|
|
|
|
.with(headers: { 'Authorization' => 'bearer token' })
|
|
|
|
.to_return(
|
2016-08-24 12:49:21 +05:30
|
|
|
status: 307,
|
|
|
|
headers: { 'Location' => location })
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'for a valid address' do
|
|
|
|
before do
|
2017-09-10 17:25:29 +05:30
|
|
|
stub_request(:get, location)
|
|
|
|
.with { |request| !request.headers.include?('Authorization') }
|
|
|
|
.to_return(
|
2016-08-24 12:49:21 +05:30
|
|
|
status: 200,
|
|
|
|
headers: { 'Content-Type' => 'application/json' },
|
2017-08-17 22:00:37 +05:30
|
|
|
body: '{"key":"value"}')
|
2016-08-24 12:49:21 +05:30
|
|
|
end
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
it 'returns correct data' do
|
|
|
|
expect(blob.data).to eq '{"key":"value"}'
|
|
|
|
end
|
2016-08-24 12:49:21 +05:30
|
|
|
end
|
|
|
|
|
2019-10-12 21:52:04 +05:30
|
|
|
context 'for a relative address' do
|
|
|
|
before do
|
|
|
|
stub_request(:get, 'http://registry.gitlab/relative')
|
|
|
|
.with { |request| !request.headers.include?('Authorization') }
|
|
|
|
.to_return(
|
|
|
|
status: 200,
|
|
|
|
headers: { 'Content-Type' => 'application/json' },
|
|
|
|
body: '{"key":"value"}')
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:location) { '/relative' }
|
|
|
|
|
|
|
|
it 'returns correct data' do
|
|
|
|
expect(blob.data).to eq '{"key":"value"}'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-08-24 12:49:21 +05:30
|
|
|
context 'for invalid file' do
|
|
|
|
let(:location) { 'file:///etc/passwd' }
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
it 'raises an error' do
|
2019-10-12 21:52:04 +05:30
|
|
|
expect { blob.data }.to raise_error(ArgumentError, 'Invalid scheme for file:///etc/passwd')
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
2016-08-24 12:49:21 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2016-06-02 11:05:42 +05:30
|
|
|
end
|