summaryrefslogtreecommitdiff
path: root/spec/ee/spec/services/geo/file_download_service_spec.rb
blob: 4fb0d89dbde8417fd0da4edd99c61a5194f5bb79 (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
require 'spec_helper'

describe Geo::FileDownloadService do
  include ::EE::GeoHelpers

  set(:primary)  { create(:geo_node, :primary) }
  set(:secondary) { create(:geo_node) }

  before do
    stub_current_geo_node(secondary)

    allow_any_instance_of(Gitlab::ExclusiveLease).to receive(:try_obtain).and_return(true)
  end

  describe '#execute' do
    context 'user avatar' do
      let(:user) { create(:user, avatar: fixture_file_upload(Rails.root + 'spec/fixtures/dk.png', 'image/png')) }
      let(:upload) { Upload.find_by(model: user, uploader: 'AvatarUploader') }

      subject(:execute!) { described_class.new(:avatar, upload.id).execute }

      it 'downloads a user avatar' do
        stub_transfer(Gitlab::Geo::FileTransfer, 100)

        expect { execute! }.to change { Geo::FileRegistry.synced.count }.by(1)
      end

      it 'registers when the download fails' do
        stub_transfer(Gitlab::Geo::FileTransfer, -1)

        expect { execute! }.to change { Geo::FileRegistry.failed.count }.by(1)
        expect(Geo::FileRegistry.last.retry_count).to eq(1)
        expect(Geo::FileRegistry.last.retry_at).to be_present
      end

      it 'registers when the download fails with some other error' do
        stub_transfer(Gitlab::Geo::FileTransfer, nil)

        expect { execute! }.to change { Geo::FileRegistry.failed.count }.by(1)
      end
    end

    context 'group avatar' do
      let(:group) { create(:group, avatar: fixture_file_upload(Rails.root + 'spec/fixtures/dk.png', 'image/png')) }
      let(:upload) { Upload.find_by(model: group, uploader: 'AvatarUploader') }

      subject(:execute!) { described_class.new(:avatar, upload.id).execute }

      it 'downloads a group avatar' do
        stub_transfer(Gitlab::Geo::FileTransfer, 100)

        expect { execute! }.to change { Geo::FileRegistry.synced.count }.by(1)
      end

      it 'registers when the download fails' do
        stub_transfer(Gitlab::Geo::FileTransfer, -1)

        expect { execute! }.to change { Geo::FileRegistry.failed.count }.by(1)
      end
    end

    context 'project avatar' do
      let(:project) { create(:project, avatar: fixture_file_upload(Rails.root + 'spec/fixtures/dk.png', 'image/png')) }
      let(:upload) { Upload.find_by(model: project, uploader: 'AvatarUploader') }

      subject(:execute!) { described_class.new(:avatar, upload.id).execute }

      it 'downloads a project avatar' do
        stub_transfer(Gitlab::Geo::FileTransfer, 100)

        expect { execute! }.to change { Geo::FileRegistry.synced.count }.by(1)
      end

      it 'registers when the download fails' do
        stub_transfer(Gitlab::Geo::FileTransfer, -1)

        expect { execute! }.to change { Geo::FileRegistry.failed.count }.by(1)
      end
    end

    context 'with an attachment' do
      let(:note) { create(:note, :with_attachment) }
      let(:upload) { Upload.find_by(model: note, uploader: 'AttachmentUploader') }

      subject(:execute!) { described_class.new(:attachment, upload.id).execute }

      it 'downloads the attachment' do
        stub_transfer(Gitlab::Geo::FileTransfer, 100)

        expect { execute! }.to change { Geo::FileRegistry.synced.count }.by(1)
      end

      it 'registers when the download fails' do
        stub_transfer(Gitlab::Geo::FileTransfer, -1)

        expect { execute! }.to change { Geo::FileRegistry.failed.count }.by(1)
      end
    end

    context 'with a snippet' do
      let(:upload) { create(:upload, :personal_snippet_upload) }

      subject(:execute!) { described_class.new(:personal_file, upload.id).execute }

      it 'downloads the file' do
        stub_transfer(Gitlab::Geo::FileTransfer, 100)

        expect { execute! }.to change { Geo::FileRegistry.synced.count }.by(1)
      end

      it 'registers when the download fails' do
        stub_transfer(Gitlab::Geo::FileTransfer, -1)

        expect { execute! }.to change { Geo::FileRegistry.failed.count }.by(1)
      end
    end

    context 'with file upload' do
      let(:project) { create(:project) }
      let(:upload) { Upload.find_by(model: project, uploader: 'FileUploader') }

      subject { described_class.new(:file, upload.id) }

      before do
        FileUploader.new(project).store!(fixture_file_upload(Rails.root + 'spec/fixtures/dk.png', 'image/png'))
      end

      it 'downloads the file' do
        stub_transfer(Gitlab::Geo::FileTransfer, 100)

        expect { subject.execute }.to change { Geo::FileRegistry.synced.count }.by(1)
      end

      it 'registers when the download fails' do
        stub_transfer(Gitlab::Geo::FileTransfer, -1)

        expect { subject.execute }.to change { Geo::FileRegistry.failed.count }.by(1)
      end
    end

    context 'with namespace file upload' do
      let(:group) { create(:group) }
      let(:upload) { Upload.find_by(model: group, uploader: 'NamespaceFileUploader') }

      subject { described_class.new(:file, upload.id) }

      before do
        NamespaceFileUploader.new(group).store!(fixture_file_upload(Rails.root + 'spec/fixtures/dk.png', 'image/png'))
      end

      it 'downloads the file' do
        stub_transfer(Gitlab::Geo::FileTransfer, 100)

        expect { subject.execute }.to change { Geo::FileRegistry.synced.count }.by(1)
      end

      it 'registers when the download fails' do
        stub_transfer(Gitlab::Geo::FileTransfer, -1)

        expect { subject.execute }.to change { Geo::FileRegistry.failed.count }.by(1)
      end
    end

    context 'LFS object' do
      let(:lfs_object) { create(:lfs_object) }

      subject { described_class.new(:lfs, lfs_object.id) }

      it 'downloads an LFS object' do
        stub_transfer(Gitlab::Geo::LfsTransfer, 100)

        expect { subject.execute }.to change { Geo::FileRegistry.synced.count }.by(1)
      end

      it 'registers when the download fails' do
        stub_transfer(Gitlab::Geo::LfsTransfer, -1)

        expect { subject.execute }.to change { Geo::FileRegistry.failed.count }.by(1)
      end

      it 'logs a message' do
        stub_transfer(Gitlab::Geo::LfsTransfer, 100)

        expect(Gitlab::Geo::Logger).to receive(:info).with(hash_including(:message, :download_time_s, success: true, bytes_downloaded: 100)).and_call_original

        subject.execute
      end
    end

    context 'job artifacts' do
      let(:job_artifact) { create(:ci_job_artifact) }

      subject { described_class.new(:job_artifact, job_artifact.id) }

      it 'downloads a job artifact' do
        stub_transfer(Gitlab::Geo::JobArtifactTransfer, 100)

        expect { subject.execute }.to change { Geo::FileRegistry.synced.count }.by(1)
      end

      it 'registers when the download fails' do
        stub_transfer(Gitlab::Geo::JobArtifactTransfer, -1)

        expect { subject.execute }.to change { Geo::FileRegistry.failed.count }.by(1)
      end

      it 'logs a message' do
        stub_transfer(Gitlab::Geo::JobArtifactTransfer, 100)

        expect(Gitlab::Geo::Logger).to receive(:info).with(hash_including(:message, :download_time_s, success: true, bytes_downloaded: 100)).and_call_original

        subject.execute
      end
    end

    context 'bad object type' do
      it 'raises an error' do
        expect { described_class.new(:bad, 1).execute }.to raise_error(NameError)
      end
    end

    def stub_transfer(kls, result)
      instance = double("(instance of #{kls})", download_from_primary: result)
      allow(kls).to receive(:new).and_return(instance)
    end
  end
end