summaryrefslogtreecommitdiff
path: root/spec/services/dependency_proxy/find_or_create_manifest_service_spec.rb
blob: ef608c9b113b46d895f50d00935c8695ad548cae (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
# frozen_string_literal: true
require 'spec_helper'

RSpec.describe DependencyProxy::FindOrCreateManifestService do
  include DependencyProxyHelpers

  let_it_be(:image) { 'alpine' }
  let_it_be(:tag) { 'latest' }
  let_it_be(:dependency_proxy_manifest) { create(:dependency_proxy_manifest, file_name: "#{image}:#{tag}.json") }

  let(:manifest) { dependency_proxy_manifest.file.read }
  let(:group) { dependency_proxy_manifest.group }
  let(:token) { Digest::SHA256.hexdigest('123') }
  let(:headers) do
    {
      DependencyProxy::Manifest::DIGEST_HEADER => dependency_proxy_manifest.digest,
      'content-type' => dependency_proxy_manifest.content_type
    }
  end

  describe '#execute' do
    subject { described_class.new(group, image, tag, token).execute }

    shared_examples 'downloading the manifest' do
      it 'downloads manifest from remote registry if there is no cached one', :aggregate_failures do
        expect { subject }.to change { group.dependency_proxy_manifests.count }.by(1)
        expect(subject[:status]).to eq(:success)
        expect(subject[:manifest]).to be_a(DependencyProxy::Manifest)
        expect(subject[:manifest]).to be_persisted
        expect(subject[:from_cache]).to eq false
      end
    end

    shared_examples 'returning no manifest' do
      it 'returns a nil manifest' do
        expect(subject[:status]).to eq(:success)
        expect(subject[:from_cache]).to eq false
        expect(subject[:manifest]).to be_nil
      end
    end

    context 'when no manifest exists' do
      let_it_be(:image) { 'new-image' }

      context 'successful head request' do
        before do
          stub_manifest_head(image, tag, headers: headers)
          stub_manifest_download(image, tag, headers: headers)
        end

        it_behaves_like 'returning no manifest'

        context 'with dependency_proxy_manifest_workhorse feature disabled' do
          before do
            stub_feature_flags(dependency_proxy_manifest_workhorse: false)
          end

          it_behaves_like 'downloading the manifest'
        end
      end

      context 'failed head request' do
        before do
          stub_manifest_head(image, tag, status: :error)
          stub_manifest_download(image, tag, headers: headers)
        end

        it_behaves_like 'returning no manifest'

        context 'with dependency_proxy_manifest_workhorse feature disabled' do
          before do
            stub_feature_flags(dependency_proxy_manifest_workhorse: false)
          end

          it_behaves_like 'downloading the manifest'
        end
      end
    end

    context 'when manifest exists' do
      before do
        stub_manifest_head(image, tag, headers: headers)
      end

      shared_examples 'using the cached manifest' do
        it 'uses cached manifest instead of downloading one', :aggregate_failures do
          expect { subject }.to change { dependency_proxy_manifest.reload.read_at }

          expect(subject[:status]).to eq(:success)
          expect(subject[:manifest]).to be_a(DependencyProxy::Manifest)
          expect(subject[:manifest]).to eq(dependency_proxy_manifest)
          expect(subject[:from_cache]).to eq true
        end
      end

      it_behaves_like 'using the cached manifest'

      context 'when digest is stale' do
        let(:digest) { 'new-digest' }
        let(:content_type) { 'new-content-type' }

        before do
          stub_manifest_head(image, tag, headers: { DependencyProxy::Manifest::DIGEST_HEADER => digest, 'content-type' => content_type })
          stub_manifest_download(image, tag, headers: { DependencyProxy::Manifest::DIGEST_HEADER => digest, 'content-type' => content_type })
        end

        it_behaves_like 'returning no manifest'

        context 'with dependency_proxy_manifest_workhorse feature disabled' do
          before do
            stub_feature_flags(dependency_proxy_manifest_workhorse: false)
          end

          it 'downloads the new manifest and updates the existing record', :aggregate_failures do
            expect(subject[:status]).to eq(:success)
            expect(subject[:manifest]).to eq(dependency_proxy_manifest)
            expect(subject[:manifest].content_type).to eq(content_type)
            expect(subject[:manifest].digest).to eq(digest)
            expect(subject[:from_cache]).to eq false
          end
        end
      end

      context 'when the cached manifest is expired' do
        before do
          dependency_proxy_manifest.update_column(:status, DependencyProxy::Manifest.statuses[:expired])
          stub_manifest_head(image, tag, headers: headers)
          stub_manifest_download(image, tag, headers: headers)
        end

        it_behaves_like 'returning no manifest'

        context 'with dependency_proxy_manifest_workhorse feature disabled' do
          before do
            stub_feature_flags(dependency_proxy_manifest_workhorse: false)
          end

          it_behaves_like 'downloading the manifest'
        end
      end

      context 'failed connection' do
        before do
          expect(DependencyProxy::HeadManifestService).to receive(:new).and_raise(Net::OpenTimeout)
        end

        it_behaves_like 'using the cached manifest'

        context 'and no manifest is cached' do
          let_it_be(:image) { 'new-image' }

          it 'returns an error', :aggregate_failures do
            expect(subject[:status]).to eq(:error)
            expect(subject[:http_status]).to eq(503)
            expect(subject[:message]).to eq('Failed to download the manifest from the external registry')
          end
        end
      end
    end
  end
end