BigW Consortium Gitlab

blob_spec.rb 3.33 KB
Newer Older
1 2 3
require 'spec_helper'

describe ContainerRegistry::Blob do
4
  let(:group) { create(:group, name: 'group') }
5
  let(:project) { create(:empty_project, path: 'test', group: group) }
6 7 8 9 10 11 12

  let(:repository) do
    create(:container_repository, name: 'image',
                                  tags: %w[latest rc1],
                                  project: project)
  end

13
  let(:config) do
14
    { 'digest' => 'sha256:0123456789012345',
15
      'mediaType' => 'binary',
16
      'size' => 1000 }
17
  end
18

19
  let(:blob) { described_class.new(repository, config) }
20

21
  before do
22 23 24
    stub_container_registry_config(enabled: true,
                                   api_url: 'http://registry.gitlab',
                                   host_port: 'registry.gitlab')
25 26
  end

27 28 29 30
  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) }

31 32 33 34
  describe '#path' do
    it 'returns a valid path to the blob' do
      expect(blob.path).to eq('group/test/image@sha256:0123456789012345')
    end
35 36
  end

37 38 39 40
  describe '#digest' do
    it 'return correct digest value' do
      expect(blob.digest).to eq 'sha256:0123456789012345'
    end
41 42
  end

43 44 45 46
  describe '#type' do
    it 'returns a correct type' do
      expect(blob.type).to eq 'binary'
    end
47 48
  end

49 50 51 52
  describe '#revision' do
    it 'returns a correct blob SHA' do
      expect(blob.revision).to eq '0123456789012345'
    end
53 54
  end

55 56 57 58
  describe '#short_revision' do
    it 'return a short SHA' do
      expect(blob.short_revision).to eq '012345678'
    end
59 60
  end

61
  describe '#delete' do
62
    before do
63 64
      stub_request(:delete, 'http://registry.gitlab/v2/group/test/image/blobs/sha256:0123456789012345')
        .to_return(status: 200)
65 66
    end

67 68 69
    it 'returns true when blob has been successfuly deleted' do
      expect(blob.delete).to be_truthy
    end
70
  end
71

72
  describe '#data' do
73 74
    context 'when locally stored' do
      before do
75
        stub_request(:get, 'http://registry.gitlab/v2/group/test/image/blobs/sha256:0123456789012345').
76 77 78
          to_return(
            status: 200,
            headers: { 'Content-Type' => 'application/json' },
79
            body: '{"key":"value"}')
80 81
      end

82 83 84
      it 'returns a correct blob data' do
        expect(blob.data).to eq '{"key":"value"}'
      end
85 86 87
    end

    context 'when externally stored' do
88 89
      let(:location) { 'http://external.com/blob/file' }

90
      before do
91 92 93
        stub_request(:get, 'http://registry.gitlab/v2/group/test/image/blobs/sha256:0123456789012345')
          .with(headers: { 'Authorization' => 'bearer token' })
          .to_return(
94 95 96 97 98 99 100 101 102 103 104
            status: 307,
            headers: { 'Location' => location })
      end

      context 'for a valid address' do
        before do
          stub_request(:get, location).
            with(headers: { 'Authorization' => nil }).
            to_return(
              status: 200,
              headers: { 'Content-Type' => 'application/json' },
105
              body: '{"key":"value"}')
106 107
        end

108 109 110
        it 'returns correct data' do
          expect(blob.data).to eq '{"key":"value"}'
        end
111 112 113 114 115
      end

      context 'for invalid file' do
        let(:location) { 'file:///etc/passwd' }

116 117 118
        it 'raises an error' do
          expect { blob.data }.to raise_error(ArgumentError, 'invalid address')
        end
119 120 121
      end
    end
  end
122
end