Welcome to mirror list, hosted at ThFree Co, Russian Federation.

tag_spec.rb « container_registry « lib « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: cb5c6a60e1dd8b60c00ce2515a0a629cf6f9249e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe ContainerRegistry::Tag do
  let(:group) { create(:group, name: 'group') }
  let(:project) { create(:project, path: 'test', group: group) }

  let(:repository) do
    create(:container_repository, name: '', project: project)
  end

  let(:headers) do
    { 'Accept' => ContainerRegistry::Client::ACCEPTED_TYPES.join(', ') }
  end

  let(:tag) { described_class.new(repository, 'tag') }

  before do
    stub_container_registry_config(enabled: true,
                                   api_url: 'http://registry.gitlab',
                                   host_port: 'registry.gitlab')
  end

  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) }

  describe '#path' do
    context 'when tag belongs to zero-level repository' do
      let(:repository) do
        create(:container_repository, name: '',
                                      tags: %w[rc1],
                                      project: project)
      end

      it 'returns path to the image' do
        expect(tag.path).to eq('group/test:tag')
      end
    end

    context 'when tag belongs to first-level repository' do
      let(:repository) do
        create(:container_repository, name: 'my_image',
                                      tags: %w[tag],
                                      project: project)
      end

      it 'returns path to the image' do
        expect(tag.path).to eq('group/test/my_image:tag')
      end
    end
  end

  describe '#location' do
    it 'returns a full location of the tag' do
      expect(tag.location)
        .to eq 'registry.gitlab/group/test:tag'
    end
  end

  context 'manifest processing' do
    shared_examples 'using the value manually set on created_at' do
      let(:value) { 5.seconds.ago }

      before do
        tag.created_at = value
      end

      it 'does not use the config' do
        expect(tag).not_to receive(:config)

        expect(subject).to eq(value)
      end
    end

    context 'schema v1' do
      before do
        stub_request(:get, 'http://registry.gitlab/v2/group/test/manifests/tag')
          .with(headers: headers)
          .to_return(
            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

      describe '#layers' do
        subject { tag.layers }

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

      describe '#total_size' do
        subject { tag.total_size }

        it { is_expected.to be_nil }
      end

      context 'config processing' do
        describe '#config' do
          subject { tag.config }

          it { is_expected.to be_nil }
        end

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

          it { is_expected.to be_nil }

          it_behaves_like 'using the value manually set on created_at'
        end
      end
    end

    context 'image is a helm chart' do
      before do
        stub_request(:get, 'http://registry.gitlab/v2/group/test/manifests/tag')
          .with(headers: headers)
          .to_return(
            status: 200,
            body: File.read(Rails.root + 'spec/fixtures/container_registry/tag_manifest_helm.json'),
            headers: { 'Content-Type' => 'application/vnd.docker.distribution.manifest.v2+json' })

        stub_request(:get, 'http://registry.gitlab/v2/group/test/blobs/sha256:65a07b841ece031e6d0ec5eb948eacb17aa6d7294cdeb01d5348e86242951487')
          .with(headers: { 'Accept' => 'application/vnd.cncf.helm.config.v1+json' })
          .to_return(
            status: 200,
            body: File.read(Rails.root + 'spec/fixtures/container_registry/config_blob_helm.json'))
      end

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

        it { is_expected.to be_nil }

        it_behaves_like 'using the value manually set on created_at'
      end
    end

    context 'schema v2' do
      before do
        stub_request(:get, 'http://registry.gitlab/v2/group/test/manifests/tag')
          .with(headers: headers)
          .to_return(
            status: 200,
            body: File.read(Rails.root + 'spec/fixtures/container_registry/tag_manifest.json'),
            headers: { 'Content-Type' => 'application/vnd.docker.distribution.manifest.v2+json' })
      end

      describe '#layers' do
        subject { tag.layers }

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

      describe '#total_size' do
        subject { tag.total_size }

        it { is_expected.to eq(2319870) }
      end

      context 'config processing' do
        shared_examples 'a processable' do
          describe '#config' do
            subject { tag.config }

            it { is_expected.not_to be_nil }
          end

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

            it { is_expected.not_to be_nil }

            it_behaves_like 'using the value manually set on created_at'
          end
        end

        context 'when locally stored' do
          before do
            stub_request(:get, 'http://registry.gitlab/v2/group/test/blobs/sha256:d7a513a663c1a6dcdba9ed832ca53c02ac2af0c333322cd6ca92936d1d9917ac')
              .with(headers: { 'Accept' => 'application/octet-stream' })
              .to_return(
                status: 200,
                body: File.read(Rails.root + 'spec/fixtures/container_registry/config_blob.json'))
          end

          it_behaves_like 'a processable'
        end

        context 'when externally stored' do
          before do
            stub_request(:get, 'http://registry.gitlab/v2/group/test/blobs/sha256:d7a513a663c1a6dcdba9ed832ca53c02ac2af0c333322cd6ca92936d1d9917ac')
              .with(headers: { 'Accept' => 'application/octet-stream' })
              .to_return(
                status: 307,
                headers: { 'Location' => 'http://external.com/blob/file' })

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

          it_behaves_like 'a processable'
        end

        describe '#force_created_at_from_iso8601' do
          subject { tag.force_created_at_from_iso8601(input) }

          shared_examples 'setting and caching the created_at value' do
            it 'sets and caches the created_at value' do
              expect(tag).not_to receive(:config)

              subject

              expect(tag.created_at).to eq(expected_value)
            end
          end

          context 'with a valid input' do
            let(:input) { 2.days.ago.iso8601 }
            let(:expected_value) { DateTime.iso8601(input) }

            it_behaves_like 'setting and caching the created_at value'
          end

          context 'with a nil input' do
            let(:input) { nil }
            let(:expected_value) { nil }

            it_behaves_like 'setting and caching the created_at value'
          end

          context 'with an invalid input' do
            let(:input) { 'not a timestamp' }
            let(:expected_value) { nil }

            it_behaves_like 'setting and caching the created_at value'
          end
        end

        describe 'updated_at=' do
          subject do
            tag.updated_at = input
            tag.updated_at
          end

          context 'with a valid input' do
            let(:input) { 2.days.ago.iso8601 }

            it { is_expected.to eq(DateTime.iso8601(input)) }
          end

          context 'with a nil input' do
            let(:input) { nil }

            it { is_expected.to eq(nil) }
          end

          context 'with an invalid input' do
            let(:input) { 'not a timestamp' }

            it { is_expected.to eq(nil) }
          end
        end
      end
    end
  end

  context 'with stubbed digest' do
    before do
      stub_request(:head, 'http://registry.gitlab/v2/group/test/manifests/tag')
        .with(headers: headers)
        .to_return(status: 200, headers: { DependencyProxy::Manifest::DIGEST_HEADER => 'sha256:digest' })
    end

    describe '#digest' do
      it 'returns a correct tag digest' do
        expect(tag.digest).to eq 'sha256:digest'
      end
    end

    describe '#unsafe_delete' do
      before do
        stub_request(:delete, 'http://registry.gitlab/v2/group/test/manifests/sha256:digest')
          .with(headers: headers)
          .to_return(status: 200)
      end

      it 'correctly deletes the tag' do
        expect(tag.unsafe_delete).to be_truthy
      end
    end
  end
end