summaryrefslogtreecommitdiff
path: root/spec/uploaders/object_storage_spec.rb
blob: 1566021934a64dfa765ddf4bbb82952b16936420 (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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
# frozen_string_literal: true

require 'spec_helper'
require 'carrierwave/storage/fog'

class Implementation < GitlabUploader
  include ObjectStorage::Concern
  include ::RecordsUploads::Concern
  prepend ::ObjectStorage::Extension::RecordsUploads

  storage_location :uploads

  private

  # user/:id
  def dynamic_segment
    File.join(model.class.underscore, model.id.to_s)
  end
end

# TODO: Update feature_category once object storage group ownership has been determined.
RSpec.describe ObjectStorage, :clean_gitlab_redis_shared_state, feature_category: :shared do
  let(:uploader_class) { Implementation }
  let(:object) { build_stubbed(:user) }
  let(:file_column) { :file }
  let(:uploader) { uploader_class.new(object, file_column) }

  describe '#object_store=' do
    before do
      allow(uploader_class).to receive(:object_store_enabled?).and_return(true)
    end

    it "reload the local storage" do
      uploader.object_store = described_class::Store::LOCAL
      expect(uploader.file_storage?).to be_truthy
    end

    it "reload the REMOTE storage" do
      uploader.object_store = described_class::Store::REMOTE
      expect(uploader.file_storage?).to be_falsey
    end

    context 'object_store is Store::LOCAL' do
      before do
        uploader.object_store = described_class::Store::LOCAL
      end

      describe '#store_dir' do
        it 'is the composition of (base_dir, dynamic_segment)' do
          expect(uploader.store_dir).to start_with("uploads/-/system/user/")
        end
      end

      describe '#store_path' do
        subject { uploader.store_path('filename') }

        it 'uses store_dir' do
          expect(subject).to eq("uploads/-/system/user/#{object.id}/filename")
        end

        context 'when a bucket prefix is configured' do
          before do
            allow(uploader_class).to receive(:object_store_options) do
              double(
                bucket_prefix: 'my/prefix'
              )
            end
          end

          it 'uses store_dir and ignores prefix' do
            expect(subject).to eq("uploads/-/system/user/#{object.id}/filename")
          end
        end
      end
    end

    context 'object_store is Store::REMOTE' do
      before do
        uploader.object_store = described_class::Store::REMOTE
      end

      describe '#store_dir' do
        it 'is the composition of (dynamic_segment)' do
          expect(uploader.store_dir).to start_with("user/")
        end
      end

      describe '#store_path' do
        subject { uploader.store_path('filename') }

        it 'uses store_dir' do
          expect(subject).to eq("user/#{object.id}/filename")
        end

        context 'when a bucket prefix is configured' do
          before do
            allow(uploader_class).to receive(:object_store_options) do
              double(
                bucket_prefix: 'my/prefix'
              )
            end
          end

          it 'uses the prefix and store_dir' do
            expect(subject).to eq("my/prefix/user/#{object.id}/filename")
          end
        end

        context 'when model has final path defined for the file column' do
          # Changing this to `foo` to make a point that not all uploaders are mounted
          # as `file`. They can be mounted as different names, for example, `avatar`.
          let(:file_column) { :foo }

          before do
            allow(object).to receive(:foo_final_path).and_return('123-final-path')
          end

          it 'uses the final path instead' do
            expect(subject).to eq('123-final-path')
          end

          context 'and a bucket prefix is configured' do
            before do
              allow(uploader_class).to receive(:object_store_options) do
                double(
                  bucket_prefix: 'my/prefix'
                )
              end
            end

            it 'uses the prefix with the final path' do
              expect(subject).to eq("my/prefix/123-final-path")
            end
          end
        end
      end
    end
  end

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

    it "delegates to <mount>_store on model" do
      expect(object).to receive(:file_store)

      subject
    end

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

      it "uses Store::LOCAL" do
        is_expected.to eq(described_class::Store::LOCAL)
      end
    end

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

      it "returns the given value" do
        is_expected.to eq(described_class::Store::REMOTE)
      end
    end
  end

  describe '#file_cache_storage?' do
    context 'when file storage is used' do
      before do
        expect(uploader_class).to receive(:cache_storage) { CarrierWave::Storage::File }
      end

      it { expect(uploader).to be_file_cache_storage }
    end

    context 'when is remote storage' do
      before do
        expect(uploader_class).to receive(:cache_storage) { CarrierWave::Storage::Fog }
      end

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

  # this means the model shall include
  #   include RecordsUpload::Concern
  #   prepend ObjectStorage::Extension::RecordsUploads
  # the object_store persistence is delegated to the `Upload` model.
  #
  context 'when persist_object_store? is false' do
    let(:object) { create(:project, :with_avatar) }
    let(:uploader) { object.avatar }

    it { expect(object).to be_a(Avatarable) }
    it { expect(uploader.persist_object_store?).to be_falsey }

    describe 'delegates the object_store logic to the `Upload` model' do
      it 'sets @upload to the found `upload`' do
        expect(uploader.upload).to eq(uploader.upload)
      end

      it 'sets @object_store to the `Upload` value' do
        expect(uploader.object_store).to eq(uploader.upload.store)
      end
    end

    describe '#migrate!' do
      let(:new_store) { ObjectStorage::Store::REMOTE }

      before do
        stub_uploads_object_storage(uploader: AvatarUploader)
      end

      subject { uploader.migrate!(new_store) }

      it 'persist @object_store to the recorded upload' do
        subject

        expect(uploader.upload.store).to eq(new_store)
      end

      describe 'fails' do
        it 'is handled gracefully' do
          store = uploader.object_store
          expect_next_instance_of(Upload) do |instance|
            expect(instance).to receive(:save!).and_raise("An error")
          end

          expect { subject }.to raise_error("An error")
          expect(uploader.exists?).to be_truthy
          expect(uploader.upload.store).to eq(store)
        end
      end
    end
  end

  # this means the model holds an <mounted_as>_store attribute directly
  # and do not delegate the object_store persistence to the `Upload` model.
  #
  context 'persist_object_store? is true' do
    context 'when using JobArtifactsUploader' do
      let(:store) { described_class::Store::LOCAL }
      let(:object) { create(:ci_job_artifact, :archive, file_store: store) }
      let(:uploader) { object.file }

      context 'checking described_class' do
        it "uploader include described_class::Concern" do
          expect(uploader).to be_a(described_class::Concern)
        end
      end

      describe '#use_file' do
        context 'when file is stored locally' do
          it "calls a regular path" do
            expect { |b| uploader.use_file(&b) }.not_to yield_with_args(%r[tmp/cache])
          end
        end

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

          before do
            stub_artifacts_object_storage
          end

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

          it "cleans up the cached file" do
            cached_path = ''

            uploader.use_file do |path|
              cached_path = path

              expect(File.exist?(cached_path)).to be_truthy
            end

            expect(File.exist?(cached_path)).to be_falsey
          end
        end
      end

      describe '#use_open_file' do
        context 'when file is stored locally' do
          it "returns the file unlinked" do
            expect { |b| uploader.use_open_file(&b) }.to yield_with_args(
              satisfying do |f|
                expect(f).to be_an_instance_of(ObjectStorage::Concern::OpenFile)
                expect(f.file_path).to be_nil
              end
            )
          end

          it "returns the file not unlinked" do
            expect { |b| uploader.use_open_file(unlink_early: false, &b) }.to yield_with_args(
              satisfying do |f|
                expect(f).to be_an_instance_of(ObjectStorage::Concern::OpenFile)
                expect(File.exist?(f.file_path)).to be_truthy
              end
            )
          end
        end

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

          before do
            stub_artifacts_object_storage

            # We need to check the Host header not including the port because AWS does not accept
            stub_request(:get, %r{s3.amazonaws.com/#{uploader.path}})
              .with { |request| !request.headers['Host'].to_s.include?(':443') }
              .to_return(status: 200, body: '')
          end

          it "returns the file" do
            expect { |b| uploader.use_open_file(&b) }.to yield_with_args(an_instance_of(ObjectStorage::Concern::OpenFile))
          end
        end
      end

      describe '#migrate!' do
        subject { uploader.migrate!(new_store) }

        shared_examples "updates the underlying <mounted>_store" do
          it do
            subject

            expect(object.file_store).to eq(new_store)
          end
        end

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

          it "to not migrate the storage" do
            subject

            expect(uploader).not_to receive(:store!)
            expect(uploader.object_store).to eq(store)
          end
        end

        context 'when migrating to local storage' do
          let(:store) { described_class::Store::REMOTE }
          let(:new_store) { described_class::Store::LOCAL }

          before do
            stub_artifacts_object_storage
          end

          include_examples "updates the underlying <mounted>_store"

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

          it "remote file exist" do
            expect(uploader.file.exists?).to be_truthy
          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::Store::REMOTE }
          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 for JobArtifactUploader/)
            end
          end

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

            include_examples "updates the underlying <mounted>_store"

            it "does migrate the file" do
              subject

              expect(uploader.object_store).to eq(new_store)
            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(uploader).to receive(:persist_object_store!).and_raise(RuntimeError, "exception")
              end

              it "original file is not removed" do
                expect { subject }.to raise_error(/exception/)

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

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

    before do
      allow(uploader_class).to receive(:options) do
        double(object_store: double(remote_directory: remote_directory))
      end
    end

    subject { uploader.fog_directory }

    it { is_expected.to eq(remote_directory) }
  end

  context 'when file is in use' do
    def when_file_is_in_use
      uploader.use_file do
        yield
      end
    end

    it 'cannot migrate' do
      when_file_is_in_use do
        expect(uploader).not_to receive(:unsafe_migrate!)

        expect { uploader.migrate!(described_class::Store::REMOTE) }.to raise_error(ObjectStorage::ExclusiveLeaseTaken)
      end
    end

    it 'cannot use_file' do
      when_file_is_in_use do
        expect(uploader).not_to receive(:unsafe_use_file)

        expect { uploader.use_file }.to raise_error(ObjectStorage::ExclusiveLeaseTaken)
      end
    end

    it 'can still migrate other files of the same model' do
      uploader2 = uploader_class.new(object, :file)
      uploader2.upload = create(:upload)
      uploader.upload = create(:upload)

      when_file_is_in_use do
        expect(uploader2).to receive(:unsafe_migrate!)

        uploader2.migrate!(described_class::Store::REMOTE)
      end
    end
  end

  describe '#fog_credentials' do
    let(:connection) { GitlabSettings::Options.build("provider" => "AWS") }

    before do
      allow(uploader_class).to receive(:options) do
        double(object_store: double(connection: connection))
      end
    end

    subject { uploader.fog_credentials }

    it { is_expected.to eq(provider: 'AWS') }
  end

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

    it { is_expected.to eq(nil) }
  end

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

    it { is_expected.to eq({}) }

    context 'with encryption configured' do
      let(:raw_options) do
        {
          "enabled" => true,
          "connection" => { "provider" => 'AWS' },
          "storage_options" => { "server_side_encryption" => "AES256" }
        }
      end

      let(:options) { GitlabSettings::Options.build(raw_options) }

      before do
        allow(uploader_class).to receive(:options) do
          double(object_store: options)
        end
      end

      it { is_expected.to eq({ "x-amz-server-side-encryption" => "AES256" }) }
    end
  end

  describe '.workhorse_authorize' do
    let(:has_length) { true }
    let(:maximum_size) { nil }
    let(:use_final_store_path) { false }

    subject do
      uploader_class.workhorse_authorize(
        has_length: has_length,
        maximum_size: maximum_size,
        use_final_store_path: use_final_store_path
      )
    end

    context 'when FIPS is enabled', :fips_mode do
      it 'response enables FIPS' do
        expect(subject[:UploadHashFunctions]).to match_array(%w[sha1 sha256 sha512])
      end
    end

    context 'when FIPS is disabled' do
      it 'response disables FIPS' do
        expect(subject[:UploadHashFunctions]).to be nil
      end
    end

    shared_examples 'returns the maximum size given' do
      it "returns temporary path" do
        expect(subject[:MaximumSize]).to eq(maximum_size)
      end
    end

    shared_examples 'uses local storage' do
      it_behaves_like 'returns the maximum size given' do
        it "returns temporary path" do
          is_expected.to have_key(:TempPath)

          expect(subject[:TempPath]).to start_with(uploader_class.root)
          expect(subject[:TempPath]).to include(described_class::TMP_UPLOAD_PATH)
        end
      end
    end

    shared_examples 'uses remote storage' do
      it_behaves_like 'returns the maximum size given' do
        it "returns remote object properties for a temporary upload" do
          is_expected.to have_key(:RemoteObject)

          expect(subject[:RemoteObject]).to have_key(:ID)
          expect(subject[:RemoteObject]).to include(Timeout: a_kind_of(Integer))
          expect(subject[:RemoteObject][:Timeout]).to be(ObjectStorage::DirectUpload::TIMEOUT)

          upload_path = File.join(described_class::TMP_UPLOAD_PATH, subject[:RemoteObject][:ID])

          expect(subject[:RemoteObject][:GetURL]).to include(upload_path)
          expect(subject[:RemoteObject][:DeleteURL]).to include(upload_path)
          expect(subject[:RemoteObject][:StoreURL]).to include(upload_path)
          expect(subject[:RemoteObject][:SkipDelete]).to eq(false)

          ::Gitlab::Redis::SharedState.with do |redis|
            expect(redis.hlen(ObjectStorage::PendingDirectUpload::KEY)).to be_zero
          end
        end
      end
    end

    shared_examples 'uses remote storage with multipart uploads' do
      it_behaves_like 'uses remote storage' do
        it "returns multipart upload" do
          is_expected.to have_key(:RemoteObject)

          expect(subject[:RemoteObject]).to have_key(:MultipartUpload)
          expect(subject[:RemoteObject][:MultipartUpload]).to have_key(:PartSize)

          upload_path = File.join(described_class::TMP_UPLOAD_PATH, subject[:RemoteObject][:ID])

          expect(subject[:RemoteObject][:MultipartUpload][:PartURLs]).to all(include(upload_path))
          expect(subject[:RemoteObject][:MultipartUpload][:CompleteURL]).to include(upload_path)
          expect(subject[:RemoteObject][:MultipartUpload][:AbortURL]).to include(upload_path)
        end
      end
    end

    shared_examples 'uses remote storage without multipart uploads' do
      it_behaves_like 'uses remote storage' do
        it "does not return multipart upload" do
          is_expected.to have_key(:RemoteObject)
          expect(subject[:RemoteObject]).not_to have_key(:MultipartUpload)
        end
      end
    end

    shared_examples 'handling object storage final upload path' do |multipart|
      context 'when use_final_store_path is true' do
        let(:use_final_store_path) { true }
        let(:final_store_path) { File.join('@final', 'abc', '123', 'somefilename') }
        let(:escaped_path) { escape_path(final_store_path) }

        before do
          stub_object_storage_multipart_init_with_final_store_path("#{storage_url}#{final_store_path}") if multipart

          allow(uploader_class).to receive(:generate_final_store_path).and_return(final_store_path)
        end

        it 'uses the full path instead of the temporary one' do
          expect(subject[:RemoteObject][:ID]).to eq(final_store_path)

          expect(subject[:RemoteObject][:GetURL]).to include(escaped_path)
          expect(subject[:RemoteObject][:StoreURL]).to include(escaped_path)

          if multipart
            expect(subject[:RemoteObject][:MultipartUpload][:PartURLs]).to all(include(escaped_path))
            expect(subject[:RemoteObject][:MultipartUpload][:CompleteURL]).to include(escaped_path)
            expect(subject[:RemoteObject][:MultipartUpload][:AbortURL]).to include(escaped_path)
          end

          expect(subject[:RemoteObject][:SkipDelete]).to eq(true)

          expect(
            ObjectStorage::PendingDirectUpload.exists?(uploader_class.storage_location_identifier, final_store_path)
          ).to eq(true)
        end

        context 'and bucket prefix is configured' do
          let(:prefixed_final_store_path) { "my/prefix/#{final_store_path}" }
          let(:escaped_path) { escape_path(prefixed_final_store_path) }

          before do
            allow(uploader_class.object_store_options).to receive(:bucket_prefix).and_return('my/prefix')

            if multipart
              stub_object_storage_multipart_init_with_final_store_path("#{storage_url}#{prefixed_final_store_path}")
            end
          end

          it 'sets the remote object ID to the final path without prefix' do
            expect(subject[:RemoteObject][:ID]).to eq(final_store_path)
          end

          it 'returns the final path with prefix' do
            expect(subject[:RemoteObject][:GetURL]).to include(escaped_path)
            expect(subject[:RemoteObject][:StoreURL]).to include(escaped_path)

            if multipart
              expect(subject[:RemoteObject][:MultipartUpload][:PartURLs]).to all(include(escaped_path))
              expect(subject[:RemoteObject][:MultipartUpload][:CompleteURL]).to include(escaped_path)
              expect(subject[:RemoteObject][:MultipartUpload][:AbortURL]).to include(escaped_path)
            end
          end

          it 'creates the pending upload entry without the prefix' do
            is_expected.to have_key(:RemoteObject)

            expect(
              ObjectStorage::PendingDirectUpload.exists?(uploader_class.storage_location_identifier, final_store_path)
            ).to eq(true)
          end
        end
      end

      def escape_path(path)
        # This is what the private method Fog::AWS::Storage#object_to_path will do to the object name
        Fog::AWS.escape(path).gsub('%2F', '/')
      end
    end

    context 'when object storage is disabled' do
      before do
        allow(Gitlab.config.uploads.object_store).to receive(:enabled) { false }
      end

      it_behaves_like 'uses local storage'
    end

    context 'when object storage is enabled' do
      before do
        allow(Gitlab.config.uploads.object_store).to receive(:enabled) { true }
      end

      context 'when direct upload is enabled' do
        before do
          allow(Gitlab.config.uploads.object_store).to receive(:direct_upload) { true }
        end

        context 'uses AWS' do
          let(:storage_url) { "https://uploads.s3.eu-central-1.amazonaws.com/" }
          let(:credentials) do
            {
              provider: "AWS",
              aws_access_key_id: "AWS_ACCESS_KEY_ID",
              aws_secret_access_key: "AWS_SECRET_ACCESS_KEY",
              region: "eu-central-1"
            }
          end

          before do
            expect_next_instance_of(ObjectStorage::Config) do |instance|
              allow(instance).to receive(:credentials).and_return(credentials)
            end
          end

          context 'for known length' do
            it_behaves_like 'uses remote storage without multipart uploads' do
              it 'returns links for S3' do
                expect(subject[:RemoteObject][:GetURL]).to start_with(storage_url)
                expect(subject[:RemoteObject][:DeleteURL]).to start_with(storage_url)
                expect(subject[:RemoteObject][:StoreURL]).to start_with(storage_url)
              end
            end

            it_behaves_like 'handling object storage final upload path'
          end

          context 'for unknown length' do
            let(:has_length) { false }
            let(:maximum_size) { 1.gigabyte }

            before do
              stub_object_storage_multipart_init(storage_url)
            end

            it_behaves_like 'uses remote storage with multipart uploads' do
              it 'returns links for S3' do
                expect(subject[:RemoteObject][:GetURL]).to start_with(storage_url)
                expect(subject[:RemoteObject][:DeleteURL]).to start_with(storage_url)
                expect(subject[:RemoteObject][:StoreURL]).to start_with(storage_url)
                expect(subject[:RemoteObject][:MultipartUpload][:PartURLs]).to all(start_with(storage_url))
                expect(subject[:RemoteObject][:MultipartUpload][:CompleteURL]).to start_with(storage_url)
                expect(subject[:RemoteObject][:MultipartUpload][:AbortURL]).to start_with(storage_url)
              end
            end

            it_behaves_like 'handling object storage final upload path', :multipart
          end
        end

        context 'uses Google' do
          let(:storage_url) { "https://storage.googleapis.com/uploads/" }
          let(:credentials) do
            {
              provider: "Google",
              google_storage_access_key_id: 'ACCESS_KEY_ID',
              google_storage_secret_access_key: 'SECRET_ACCESS_KEY'
            }
          end

          before do
            expect_next_instance_of(ObjectStorage::Config) do |instance|
              allow(instance).to receive(:credentials).and_return(credentials)
            end
          end

          context 'for known length' do
            it_behaves_like 'uses remote storage without multipart uploads' do
              it 'returns links for Google Cloud' do
                expect(subject[:RemoteObject][:GetURL]).to start_with(storage_url)
                expect(subject[:RemoteObject][:DeleteURL]).to start_with(storage_url)
                expect(subject[:RemoteObject][:StoreURL]).to start_with(storage_url)
              end
            end

            it_behaves_like 'handling object storage final upload path'
          end

          context 'for unknown length' do
            let(:has_length) { false }
            let(:maximum_size) { 1.gigabyte }

            it_behaves_like 'uses remote storage without multipart uploads' do
              it 'returns links for Google Cloud' do
                expect(subject[:RemoteObject][:GetURL]).to start_with(storage_url)
                expect(subject[:RemoteObject][:DeleteURL]).to start_with(storage_url)
                expect(subject[:RemoteObject][:StoreURL]).to start_with(storage_url)
              end
            end

            it_behaves_like 'handling object storage final upload path'
          end
        end

        context 'uses GDK/minio' do
          let(:storage_url) { "http://minio:9000/uploads/" }
          let(:credentials) do
            { provider: "AWS",
              aws_access_key_id: "AWS_ACCESS_KEY_ID",
              aws_secret_access_key: "AWS_SECRET_ACCESS_KEY",
              endpoint: 'http://minio:9000',
              path_style: true,
              region: "gdk" }
          end

          before do
            expect_next_instance_of(ObjectStorage::Config) do |instance|
              allow(instance).to receive(:credentials).and_return(credentials)
            end
          end

          context 'for known length' do
            it_behaves_like 'uses remote storage without multipart uploads' do
              it 'returns links for S3' do
                expect(subject[:RemoteObject][:GetURL]).to start_with(storage_url)
                expect(subject[:RemoteObject][:DeleteURL]).to start_with(storage_url)
                expect(subject[:RemoteObject][:StoreURL]).to start_with(storage_url)
              end
            end

            it_behaves_like 'handling object storage final upload path'
          end

          context 'for unknown length' do
            let(:has_length) { false }
            let(:maximum_size) { 1.gigabyte }

            before do
              stub_object_storage_multipart_init(storage_url)
            end

            it_behaves_like 'uses remote storage with multipart uploads' do
              it 'returns links for S3' do
                expect(subject[:RemoteObject][:GetURL]).to start_with(storage_url)
                expect(subject[:RemoteObject][:DeleteURL]).to start_with(storage_url)
                expect(subject[:RemoteObject][:StoreURL]).to start_with(storage_url)
                expect(subject[:RemoteObject][:MultipartUpload][:PartURLs]).to all(start_with(storage_url))
                expect(subject[:RemoteObject][:MultipartUpload][:CompleteURL]).to start_with(storage_url)
                expect(subject[:RemoteObject][:MultipartUpload][:AbortURL]).to start_with(storage_url)
              end
            end

            it_behaves_like 'handling object storage final upload path', :multipart
          end
        end
      end

      context 'when direct upload is disabled' do
        before do
          allow(Gitlab.config.uploads.object_store).to receive(:direct_upload) { false }
        end

        it_behaves_like 'uses local storage'
      end
    end
  end

  describe '#cache!' do
    subject do
      uploader.cache!(uploaded_file)
    end

    context 'when local file is used' do
      let(:temp_file) { Tempfile.new("test") }

      before do
        FileUtils.touch(temp_file)
      end

      after do
        FileUtils.rm_f(temp_file)
      end

      context 'when valid file is used' do
        context 'when valid file is specified' do
          let(:uploaded_file) { temp_file }

          it 'properly caches the file' do
            subject

            expect(uploader).to be_exists
            expect(uploader.path).to start_with(uploader_class.root)
            expect(uploader.filename).to eq(File.basename(uploaded_file.path))
          end

          context 'when object storage and direct upload is specified' do
            before do
              stub_uploads_object_storage(uploader_class, enabled: true, direct_upload: true)
            end

            context 'when file is stored' do
              subject do
                uploader.store!(uploaded_file)
              end

              it 'file to be remotely stored in permament location' do
                subject

                expect(uploader).to be_exists
                expect(uploader).not_to be_cached
                expect(uploader).not_to be_file_storage
                expect(uploader.path).not_to be_nil
                expect(uploader.path).not_to include('tmp/upload')
                expect(uploader.path).not_to include('tmp/cache')
                expect(uploader.object_store).to eq(described_class::Store::REMOTE)
              end
            end
          end

          context 'when object storage and direct upload is not used' do
            before do
              stub_uploads_object_storage(uploader_class, enabled: true, direct_upload: false)
            end

            context 'when file is stored' do
              subject do
                uploader.store!(uploaded_file)
              end

              it 'file to be remotely stored in permament location' do
                subject

                expect(uploader).to be_exists
                expect(uploader).not_to be_cached
                expect(uploader).to be_file_storage
                expect(uploader.path).not_to be_nil
                expect(uploader.path).not_to include('tmp/upload')
                expect(uploader.path).not_to include('tmp/cache')
                expect(uploader.object_store).to eq(described_class::Store::LOCAL)
              end
            end
          end
        end
      end
    end

    context 'when remote file is used' do
      let(:temp_file) { Tempfile.new("test") }

      let!(:fog_connection) do
        stub_uploads_object_storage(uploader_class)
      end

      before do
        FileUtils.touch(temp_file)
      end

      after do
        FileUtils.rm_f(temp_file)
      end

      context 'when valid file is used' do
        context 'when invalid file is specified' do
          let(:uploaded_file) do
            UploadedFile.new(temp_file.path, remote_id: "../test/123123")
          end

          it 'raises an error' do
            expect { subject }.to raise_error(uploader_class::RemoteStoreError, /Bad file path/)
          end
        end

        context 'when non existing file is specified' do
          let(:uploaded_file) do
            UploadedFile.new(temp_file.path, remote_id: "test/123123")
          end

          it 'raises an error' do
            expect { subject }.to raise_error(uploader_class::RemoteStoreError, /Missing file/)
          end
        end

        context 'when empty remote_id is specified' do
          let(:uploaded_file) do
            UploadedFile.new(temp_file.path, remote_id: '')
          end

          it 'uses local storage' do
            subject

            expect(uploader).to be_file_storage
            expect(uploader.object_store).to eq(described_class::Store::LOCAL)
          end
        end

        context 'when valid file is specified' do
          let(:uploaded_file) do
            UploadedFile.new(temp_file.path, filename: "my_file.txt", remote_id: "test/123123")
          end

          let!(:fog_file) do
            fog_connection.directories.new(key: 'uploads').files.create( # rubocop:disable Rails/SaveBang
              key: 'tmp/uploads/test/123123',
              body: 'content'
            )
          end

          it 'file to be cached and remote stored' do
            expect { subject }.not_to raise_error

            expect(uploader).to be_exists
            expect(uploader).to be_cached
            expect(uploader.cache_only).to be_falsey
            expect(uploader).not_to be_file_storage
            expect(uploader.path).not_to be_nil
            expect(uploader.path).to include('tmp/uploads')
            expect(uploader.path).not_to include('tmp/cache')
            expect(uploader.object_store).to eq(described_class::Store::REMOTE)
          end

          context 'when file is stored' do
            subject do
              uploader.store!(uploaded_file)
            end

            it 'file to be remotely stored in permament location' do
              subject

              expect(uploader).to be_exists
              expect(uploader).not_to be_cached
              expect(uploader).not_to be_file_storage
              expect(uploader.path).not_to be_nil
              expect(uploader.path).not_to include('tmp/upload')
              expect(uploader.path).not_to include('tmp/cache')
              expect(uploader.url).to include('/my_file.txt')
              expect(uploader.object_store).to eq(described_class::Store::REMOTE)
            end
          end

          context 'when uploaded file remote_id matches a pending direct upload entry' do
            let(:object) { build_stubbed(:ci_job_artifact) }
            let(:final_path) { '@final/test/123123' }
            let(:fog_config) { Gitlab.config.uploads.object_store }
            let(:bucket) { 'uploads' }
            let(:uploaded_file) { UploadedFile.new(temp_file.path, filename: "my_file.txt", remote_id: final_path) }
            let(:fog_file_path) { final_path }

            let(:fog_connection_2) do
              stub_object_storage_uploader(
                config: fog_config,
                uploader: uploader_class,
                direct_upload: true
              )
            end

            let!(:fog_file_2) do
              fog_connection_2.directories.new(key: bucket).files.create( # rubocop:disable Rails/SaveBang
                key: fog_file_path,
                body: 'content'
              )
            end

            before do
              ObjectStorage::PendingDirectUpload.prepare(
                uploader_class.storage_location_identifier,
                final_path
              )
            end

            it 'file to be cached and remote stored with final path set' do
              expect { subject }.not_to raise_error

              expect(uploader).to be_exists
              expect(uploader).to be_cached
              expect(uploader.cache_only).to be_falsey
              expect(uploader).not_to be_file_storage
              expect(uploader.path).to eq(uploaded_file.remote_id)
              expect(uploader.object_store).to eq(described_class::Store::REMOTE)

              expect(object.file_final_path).to eq(uploaded_file.remote_id)
            end

            context 'when bucket prefix is configured' do
              let(:fog_config) do
                Gitlab.config.uploads.object_store.tap do |config|
                  config[:remote_directory] = 'main-bucket'
                  config[:bucket_prefix] = 'uploads'
                end
              end

              let(:bucket) { 'main-bucket' }
              let(:fog_file_path) { "uploads/#{final_path}" }

              it 'stores the file final path in the db without the prefix' do
                expect { subject }.not_to raise_error

                expect(uploader.store_path).to eq("uploads/#{final_path}")
                expect(object.file_final_path).to eq(final_path)
              end
            end

            context 'when file is stored' do
              subject do
                uploader.store!(uploaded_file)
              end

              it 'file to be remotely stored in permament location' do
                subject

                expect(uploader).to be_exists
                expect(uploader).not_to be_cached
                expect(uploader.path).to eq(uploaded_file.remote_id)
              end

              it 'does not trigger Carrierwave copy and delete because it is already in the final location' do
                expect_next_instance_of(CarrierWave::Storage::Fog::File) do |instance|
                  expect(instance).not_to receive(:copy_to)
                  expect(instance).not_to receive(:delete)
                end

                subject
              end
            end
          end
        end
      end
    end
  end

  describe '#retrieve_from_store!' do
    context 'uploaders that includes the RecordsUploads extension' do
      [:group, :project, :user].each do |model|
        context "for #{model}s" do
          let(:models) { create_list(model, 3, :with_avatar).map(&:reload) }
          let(:avatars) { models.map(&:avatar) }

          it 'batches fetching uploads from the database' do
            # Ensure that these are all created and fully loaded before we start
            # running queries for avatars
            models

            expect { avatars }.not_to exceed_query_limit(1)
          end

          it 'does not attempt to replace methods' do
            models.each do |model|
              expect(model.avatar.upload).to receive(:method_missing).and_call_original

              model.avatar.upload.path
            end
          end

          it 'fetches a unique upload for each model' do
            expect(avatars.map(&:url).uniq).to eq(avatars.map(&:url))
            expect(avatars.map(&:upload).uniq).to eq(avatars.map(&:upload))
          end
        end
      end
    end

    describe 'filename' do
      let(:model) { create(:ci_job_artifact, :remote_store, :archive) }

      before do
        stub_artifacts_object_storage
      end

      shared_examples 'ensuring correct filename' do
        it 'uses the original filename' do
          expect(model.reload.file.filename).to eq('ci_build_artifacts.zip')
        end
      end

      context 'when model has final path defined for the file column' do
        before do
          model.update_column(:file_final_path, 'some/final/path/abc-123')
        end

        it_behaves_like 'ensuring correct filename'
      end

      context 'when model has no final path defined for the file column' do
        it_behaves_like 'ensuring correct filename'
      end
    end
  end

  describe '.generate_final_store_path' do
    subject(:final_path) { uploader_class.generate_final_store_path }

    before do
      allow(Digest::SHA2).to receive(:hexdigest).and_return('somehash1234')
    end

    it 'returns the generated hashed path' do
      expect(final_path).to eq('@final/so/me/hash1234')
    end
  end

  describe 'OpenFile' do
    subject { ObjectStorage::Concern::OpenFile.new(file) }

    let(:file) { double(read: true, size: true, path: true) }

    it 'delegates read and size methods' do
      expect(subject.read).to eq(true)
      expect(subject.size).to eq(true)
    end

    it 'does not delegate path method' do
      expect { subject.path }.to raise_error(NoMethodError)
    end
  end
end