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

object_store_uploader_spec.rb « uploaders « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 2f52867bb91ecb732b9f4044816cd5f9ab66c13c (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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
require 'rails_helper'
require 'carrierwave/storage/fog'

describe ObjectStoreUploader do
  let(:uploader_class) { Class.new(described_class) }
  let(:object) { double }
  let(:uploader) { uploader_class.new(object, :file) }

  before do
    allow(object.class).to receive(:uploader_option).with(:file, :mount_on) { nil }
  end

  describe '#object_store' do
    it "calls artifacts_file_store on object" do
      expect(object).to receive(:file_store)

      uploader.object_store
    end

    context 'when store is null' do
      before do
        expect(object).to receive(:file_store).twice.and_return(nil)
      end

      it "returns LOCAL_STORE" do
        expect(uploader.real_object_store).to be_nil
        expect(uploader.object_store).to eq(described_class::LOCAL_STORE)
      end
    end

    context 'when value is set' do
      before do
        expect(object).to receive(:file_store).twice.and_return(described_class::REMOTE_STORE)
      end

      it "returns given value" do
        expect(uploader.real_object_store).not_to be_nil
        expect(uploader.object_store).to eq(described_class::REMOTE_STORE)
      end
    end
  end

  describe '#object_store=' do
    it "calls artifacts_file_store= on object" do
      expect(object).to receive(:file_store=).with(described_class::REMOTE_STORE)

      uploader.object_store = described_class::REMOTE_STORE
    end
  end

  describe '#file_storage?' do
    context 'when file storage is used' do
      before do
        expect(object).to receive(:file_store).and_return(described_class::LOCAL_STORE)
      end

      it { expect(uploader).to be_file_storage }
    end

    context 'when is remote storage' do
      before do
        uploader_class.storage_options double(
          object_store: double(enabled: true))
        expect(object).to receive(:file_store).and_return(described_class::REMOTE_STORE)
      end

      it { expect(uploader).not_to be_file_storage }
    end
  end

  describe '#file_cache_storage?' do
    context 'when file storage is used' do
      before do
        uploader_class.cache_storage(:file)
      end

      it { expect(uploader).to be_file_cache_storage }
    end

    context 'when is remote storage' do
      before do
        uploader_class.cache_storage(:fog)
      end

      it { expect(uploader).not_to be_file_cache_storage }
    end
  end

  context 'when using JobArtifactsUploader' do
    let(:artifact) { create(:ci_job_artifact, :archive, file_store: store) }
    let(:uploader) { artifact.file }

    context 'checking described_class' do
      let(:store) { described_class::LOCAL_STORE }

      it "uploader is of a described_class" do
        expect(uploader).to be_a(described_class)
      end

      it 'moves files locally' do
        expect(uploader.move_to_store).to be(true)
        expect(uploader.move_to_cache).to be(true)
      end
    end

    context 'when store is null' do
      let(:store) { nil }

      it "sets the store to LOCAL_STORE" do
        expect(artifact.file_store).to eq(described_class::LOCAL_STORE)
      end
    end

    describe '#use_file' do
      context 'when file is stored locally' do
        let(:store) { described_class::LOCAL_STORE }

        it "calls a regular path" do
          expect { |b| uploader.use_file(&b) }.not_to yield_with_args(/tmp\/cache/)
        end
      end

      context 'when file is stored remotely' do
        let(:store) { described_class::REMOTE_STORE }

        before do
          stub_artifacts_object_storage
        end

        it "calls a cache path" do
          expect { |b| uploader.use_file(&b) }.to yield_with_args(/tmp\/cache/)
        end
      end
    end

    describe '#migrate!' do
      let(:artifact) { create(:ci_job_artifact, :archive, file_store: store) }
      let(:uploader) { artifact.file }
      let(:store) { described_class::LOCAL_STORE }
      
      subject { uploader.migrate!(new_store) }

      context 'when using the same storage' do
        let(:new_store) { store }

        it "to not migrate the storage" do
          subject

          expect(uploader.object_store).to eq(store)
        end
      end

      context 'when migrating to local storage' do
        let(:store) { described_class::REMOTE_STORE }
        let(:new_store) { described_class::LOCAL_STORE }
        
        before do
          stub_artifacts_object_storage
        end

        it "local file does not exist" do
          expect(File.exist?(uploader.path)).to eq(false)
        end

        it "does migrate the file" do
          subject

          expect(uploader.object_store).to eq(new_store)
          expect(File.exist?(uploader.path)).to eq(true)
        end
      end

      context 'when migrating to remote storage' do
        let(:new_store) { described_class::REMOTE_STORE }
        let!(:current_path) { uploader.path }

        it "file does exist" do
          expect(File.exist?(current_path)).to eq(true)
        end
        
        context 'when storage is disabled' do
          before do
            stub_artifacts_object_storage(enabled: false) 
          end

          it "to raise an error" do
            expect { subject }.to raise_error(/Object Storage is not enabled/)
          end
        end

        context 'when storage is unlicensed' do
          before do
            stub_artifacts_object_storage(licensed: false)
          end

          it "raises an error" do
            expect { subject }.to raise_error(/Object Storage feature is missing/)
          end
        end

        context 'when credentials are set' do
          before do
            stub_artifacts_object_storage
          end

          it "does migrate the file" do
            subject

            expect(uploader.object_store).to eq(new_store)
            expect(File.exist?(current_path)).to eq(false)
          end

          it "does delete original file" do
            subject
    
            expect(File.exist?(current_path)).to eq(false)
          end

          context 'when subject save fails' do
            before do
              expect(artifact).to receive(:save!).and_raise(RuntimeError, "exception")
            end

            it "does catch an error" do
              expect { subject }.to raise_error(/exception/)
            end

            it "original file is not removed" do
              begin
                subject
              rescue
              end

              expect(File.exist?(current_path)).to eq(true)
            end
          end
        end
      end
    end
  end

  describe '#fog_directory' do
    let(:remote_directory) { 'directory' }

    before do
      uploader_class.storage_options double(
        object_store: double(remote_directory: remote_directory))
    end

    subject { uploader.fog_directory }

    it { is_expected.to eq(remote_directory) }
  end

  describe '#fog_credentials' do
    let(:connection) { 'connection' }

    before do
      uploader_class.storage_options double(
        object_store: double(connection: connection))
    end

    subject { uploader.fog_credentials }

    it { is_expected.to eq(connection) }
  end

  describe '#fog_public' do
    subject { uploader.fog_public }

    it { is_expected.to eq(false) }
  end

  describe '#verify_license!' do
    subject { uploader.verify_license!(nil) }

    context 'when using local storage' do
      before do
        expect(object).to receive(:file_store) { described_class::LOCAL_STORE }
      end

      it "does not raise an error" do
        expect { subject }.not_to raise_error
      end
    end

    context 'when using remote storage' do
      before do
        uploader_class.storage_options double(
          object_store: double(enabled: true))
        expect(object).to receive(:file_store) { described_class::REMOTE_STORE }
      end

      context 'feature is not available' do
        before do
          expect(License).to receive(:feature_available?).with(:object_storage) { false }
        end

        it "does raise an error" do
          expect { subject }.to raise_error(/Object Storage feature is missing/)
        end
      end

      context 'feature is available' do
        before do
          expect(License).to receive(:feature_available?).with(:object_storage) { true }
        end

        it "does not raise an error" do
          expect { subject }.not_to raise_error
        end
      end
    end
  end
end