tag_spec.rb 4.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
require 'spec_helper'

describe ContainerRegistry::Tag do
  let(:registry) { ContainerRegistry::Registry.new('http://example.com') }
  let(:repository) { registry.repository('group/test') }
  let(:tag) { repository.tag('tag') }
  let(:headers) { { 'Accept' => 'application/vnd.docker.distribution.manifest.v2+json' } }

  it { expect(tag).to respond_to(:repository) }
  it { expect(tag).to delegate_method(:registry).to(:repository) }
  it { expect(tag).to delegate_method(:client).to(:repository) }

  context '#path' do
    subject { tag.path }

    it { is_expected.to eq('example.com/group/test:tag') }
  end

  context 'manifest processing' do
20 21
    context 'schema v1' do
      before do
22 23 24
        stub_request(:get, 'http://example.com/v2/group/test/manifests/tag').
          with(headers: headers).
          to_return(
25 26 27 28
            status: 200,
            body: File.read(Rails.root + 'spec/fixtures/container_registry/tag_manifest_1.json'),
            headers: { 'Content-Type' => 'application/vnd.docker.distribution.manifest.v1+prettyjws' })
      end
29

30 31
      context '#layers' do
        subject { tag.layers }
32

33 34 35 36 37
        it { expect(subject.length).to eq(1) }
      end

      context '#total_size' do
        subject { tag.total_size }
38

39 40
        it { is_expected.to be_nil }
      end
41

42 43 44 45 46 47 48 49 50 51 52 53 54
      context 'config processing' do
        context '#config' do
          subject { tag.config }

          it { is_expected.to be_nil }
        end

        context '#created_at' do
          subject { tag.created_at }

          it { is_expected.to be_nil }
        end
      end
55 56
    end

57
    context 'schema v2' do
58
      before do
59 60 61
        stub_request(:get, 'http://example.com/v2/group/test/manifests/tag').
          with(headers: headers).
          to_return(
62
            status: 200,
63 64
            body: File.read(Rails.root + 'spec/fixtures/container_registry/tag_manifest.json'),
            headers: { 'Content-Type' => 'application/vnd.docker.distribution.manifest.v2+json' })
65 66
      end

67 68
      context '#layers' do
        subject { tag.layers }
69

70
        it { expect(subject.length).to eq(1) }
71 72
      end

73 74 75 76 77 78 79
      context '#total_size' do
        subject { tag.total_size }

        it { is_expected.to eq(2319870) }
      end

      context 'config processing' do
80 81 82
        shared_examples 'a processable' do
          context '#config' do
            subject { tag.config }
83

84 85 86 87 88
            it { is_expected.not_to be_nil }
          end

          context '#created_at' do
            subject { tag.created_at }
89

90 91
            it { is_expected.not_to be_nil }
          end
92 93
        end

94 95
        context 'when locally stored' do
          before do
96 97 98
            stub_request(:get, 'http://example.com/v2/group/test/blobs/sha256:d7a513a663c1a6dcdba9ed832ca53c02ac2af0c333322cd6ca92936d1d9917ac').
              with(headers: { 'Accept' => 'application/octet-stream' }).
              to_return(
99 100 101 102 103 104
                status: 200,
                body: File.read(Rails.root + 'spec/fixtures/container_registry/config_blob.json'))
          end

          it_behaves_like 'a processable'
        end
105

106 107
        context 'when externally stored' do
          before do
108 109 110
            stub_request(:get, 'http://example.com/v2/group/test/blobs/sha256:d7a513a663c1a6dcdba9ed832ca53c02ac2af0c333322cd6ca92936d1d9917ac').
              with(headers: { 'Accept' => 'application/octet-stream' }).
              to_return(
111 112 113
                status: 307,
                headers: { 'Location' => 'http://external.com/blob/file' })

114 115
            stub_request(:get, 'http://external.com/blob/file').
              to_return(
116 117 118 119 120
                status: 200,
                body: File.read(Rails.root + 'spec/fixtures/container_registry/config_blob.json'))
          end

          it_behaves_like 'a processable'
121
        end
122 123 124 125 126 127
      end
    end
  end

  context 'manifest digest' do
    before do
128 129 130
      stub_request(:head, 'http://example.com/v2/group/test/manifests/tag').
        with(headers: headers).
        to_return(status: 200, headers: { 'Docker-Content-Digest' => 'sha256:digest' })
131 132 133 134 135 136 137 138 139 140
    end

    context '#digest' do
      subject { tag.digest }

      it { is_expected.to eq('sha256:digest') }
    end

    context '#delete' do
      before do
141 142 143
        stub_request(:delete, 'http://example.com/v2/group/test/manifests/sha256:digest').
          with(headers: headers).
          to_return(status: 200)
144 145 146 147 148 149 150 151
      end

      subject { tag.delete }

      it { is_expected.to be_truthy }
    end
  end
end