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

container_repository_spec.rb « models « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 7c0ae51223ba67f40e3b358a7f2232c24b7cc725 (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
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe ContainerRepository, :aggregate_failures do
  using RSpec::Parameterized::TableSyntax

  let(:group) { create(:group, name: 'group') }
  let(:project) { create(:project, path: 'test', group: group) }

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

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

    stub_request(:get, 'http://registry.gitlab/v2/group/test/my_image/tags/list')
      .with(headers: { 'Accept' => ContainerRegistry::Client::ACCEPTED_TYPES.join(', ') })
      .to_return(
        status: 200,
        body: Gitlab::Json.dump(tags: ['test_tag']),
        headers: { 'Content-Type' => 'application/json' })
  end

  it_behaves_like 'having unique enum values'

  describe 'associations' do
    it 'belongs to the project' do
      expect(repository).to belong_to(:project)
    end
  end

  describe 'validations' do
    it { is_expected.to validate_presence_of(:migration_retries_count) }
    it { is_expected.to validate_numericality_of(:migration_retries_count).is_greater_than_or_equal_to(0) }

    it { is_expected.to validate_inclusion_of(:migration_aborted_in_state).in_array(described_class::ABORTABLE_MIGRATION_STATES) }
    it { is_expected.to allow_value(nil).for(:migration_aborted_in_state) }

    context 'migration_state' do
      it { is_expected.to validate_presence_of(:migration_state) }
      it { is_expected.to validate_inclusion_of(:migration_state).in_array(described_class::MIGRATION_STATES) }

      describe 'pre_importing' do
        it 'validates expected attributes' do
          expect(build(:container_repository, migration_state: 'pre_importing')).to be_invalid
          expect(build(:container_repository, :pre_importing)).to be_valid
        end
      end

      describe 'pre_import_done' do
        it 'validates expected attributes' do
          expect(build(:container_repository, migration_state: 'pre_import_done')).to be_invalid
          expect(build(:container_repository, :pre_import_done)).to be_valid
        end
      end

      describe 'importing' do
        it 'validates expected attributes' do
          expect(build(:container_repository, migration_state: 'importing')).to be_invalid
          expect(build(:container_repository, :importing)).to be_valid
        end
      end

      describe 'import_skipped' do
        it 'validates expected attributes' do
          expect(build(:container_repository, migration_state: 'import_skipped')).to be_invalid
          expect(build(:container_repository, :import_skipped)).to be_valid
        end
      end

      describe 'import_aborted' do
        it 'validates expected attributes' do
          expect(build(:container_repository, migration_state: 'import_aborted')).to be_invalid
          expect(build(:container_repository, :import_aborted)).to be_valid
        end
      end
    end
  end

  context ':migration_state state_machine' do
    shared_examples 'no action when feature flag is disabled' do
      context 'feature flag disabled' do
        before do
          stub_feature_flags(container_registry_migration_phase2_enabled: false)
        end

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

    shared_examples 'transitioning to pre_importing', skip_pre_import_success: true do
      before do
        repository.update_column(:migration_pre_import_done_at, Time.zone.now)
      end

      it_behaves_like 'no action when feature flag is disabled'

      context 'successful pre_import request' do
        it 'sets migration_pre_import_started_at and resets migration_pre_import_done_at' do
          expect(repository).to receive(:migration_pre_import).and_return(:ok)

          expect { subject }.to change { repository.reload.migration_pre_import_started_at }
            .and change { repository.migration_pre_import_done_at }.to(nil)

          expect(repository).to be_pre_importing
        end
      end

      context 'failed pre_import request' do
        it 'sets migration_pre_import_started_at and resets migration_pre_import_done_at' do
          expect(repository).to receive(:migration_pre_import).and_return(:error)

          expect { subject }.to change { repository.reload.migration_pre_import_started_at }
            .and change { repository.migration_aborted_at }
            .and change { repository.migration_pre_import_done_at }.to(nil)

          expect(repository.migration_aborted_in_state).to eq('pre_importing')
          expect(repository).to be_import_aborted
        end
      end
    end

    shared_examples 'transitioning to importing', skip_import_success: true do
      before do
        repository.update_columns(migration_import_done_at: Time.zone.now)
      end

      context 'successful import request' do
        it 'sets migration_import_started_at and resets migration_import_done_at' do
          expect(repository).to receive(:migration_import).and_return(:ok)

          expect { subject }.to change { repository.reload.migration_import_started_at }
            .and change { repository.migration_import_done_at }.to(nil)

          expect(repository).to be_importing
        end
      end

      context 'failed import request' do
        it 'sets migration_import_started_at and resets migration_import_done_at' do
          expect(repository).to receive(:migration_import).and_return(:error)

          expect { subject }.to change { repository.reload.migration_import_started_at }
            .and change { repository.migration_aborted_at }

          expect(repository.migration_aborted_in_state).to eq('importing')
          expect(repository).to be_import_aborted
        end
      end
    end

    shared_examples 'transitioning out of import_aborted' do
      it 'resets migration_aborted_at and migration_aborted_in_state' do
        expect { subject }.to change { repository.reload.migration_aborted_in_state }.to(nil)
          .and change { repository.migration_aborted_at }.to(nil)
      end
    end

    shared_examples 'transitioning from allowed states' do |allowed_states|
      described_class::MIGRATION_STATES.each do |state|
        result = allowed_states.include?(state)

        context "when transitioning from #{state}" do
          let(:repository) { create(:container_repository, state.to_sym) }

          it "returns #{result}" do
            expect(subject).to eq(result)
          end
        end
      end
    end

    shared_examples 'queueing the next import' do
      it 'starts the worker' do
        expect(::ContainerRegistry::Migration::EnqueuerWorker).to receive(:perform_async)

        subject
      end
    end

    describe '#start_pre_import' do
      let_it_be_with_reload(:repository) { create(:container_repository) }

      subject { repository.start_pre_import }

      before do |example|
        unless example.metadata[:skip_pre_import_success]
          allow(repository).to receive(:migration_pre_import).and_return(:ok)
        end
      end

      it_behaves_like 'transitioning from allowed states', %w[default]
      it_behaves_like 'transitioning to pre_importing'
    end

    describe '#retry_pre_import' do
      let_it_be_with_reload(:repository) { create(:container_repository, :import_aborted) }

      subject { repository.retry_pre_import }

      before do |example|
        unless example.metadata[:skip_pre_import_success]
          allow(repository).to receive(:migration_pre_import).and_return(:ok)
        end
      end

      it_behaves_like 'transitioning from allowed states', %w[import_aborted]
      it_behaves_like 'transitioning to pre_importing'
      it_behaves_like 'transitioning out of import_aborted'
    end

    describe '#finish_pre_import' do
      let_it_be_with_reload(:repository) { create(:container_repository, :pre_importing) }

      subject { repository.finish_pre_import }

      it_behaves_like 'transitioning from allowed states', %w[pre_importing import_aborted]

      it 'sets migration_pre_import_done_at' do
        expect { subject }.to change { repository.reload.migration_pre_import_done_at }

        expect(repository).to be_pre_import_done
      end
    end

    describe '#start_import' do
      let_it_be_with_reload(:repository) { create(:container_repository, :pre_import_done) }

      subject { repository.start_import }

      before do |example|
        unless example.metadata[:skip_import_success]
          allow(repository).to receive(:migration_import).and_return(:ok)
        end
      end

      it_behaves_like 'transitioning from allowed states', %w[pre_import_done]
      it_behaves_like 'transitioning to importing'
    end

    describe '#retry_import' do
      let_it_be_with_reload(:repository) { create(:container_repository, :import_aborted) }

      subject { repository.retry_import }

      before do |example|
        unless example.metadata[:skip_import_success]
          allow(repository).to receive(:migration_import).and_return(:ok)
        end
      end

      it_behaves_like 'transitioning from allowed states', %w[import_aborted]
      it_behaves_like 'transitioning to importing'
      it_behaves_like 'no action when feature flag is disabled'
    end

    describe '#finish_import' do
      let_it_be_with_reload(:repository) { create(:container_repository, :importing) }

      subject { repository.finish_import }

      it_behaves_like 'transitioning from allowed states', %w[importing import_aborted]
      it_behaves_like 'queueing the next import'

      it 'sets migration_import_done_at and queues the next import' do
        expect { subject }.to change { repository.reload.migration_import_done_at }

        expect(repository).to be_import_done
      end
    end

    describe '#already_migrated' do
      let_it_be_with_reload(:repository) { create(:container_repository) }

      subject { repository.already_migrated }

      it_behaves_like 'transitioning from allowed states', %w[default]

      it 'sets migration_import_done_at' do
        subject

        expect(repository).to be_import_done
      end
    end

    describe '#abort_import' do
      let_it_be_with_reload(:repository) { create(:container_repository, :importing) }

      subject { repository.abort_import }

      it_behaves_like 'transitioning from allowed states', ContainerRepository::ABORTABLE_MIGRATION_STATES
      it_behaves_like 'queueing the next import'

      it 'sets migration_aborted_at and migration_aborted_at, increments the retry count, and queues the next import' do
        expect { subject }.to change { repository.migration_aborted_at }
          .and change { repository.reload.migration_retries_count }.by(1)

        expect(repository.migration_aborted_in_state).to eq('importing')
        expect(repository).to be_import_aborted
      end
    end

    describe '#skip_import' do
      let_it_be_with_reload(:repository) { create(:container_repository) }

      subject { repository.skip_import(reason: :too_many_retries) }

      it_behaves_like 'transitioning from allowed states', ContainerRepository::ABORTABLE_MIGRATION_STATES

      it 'sets migration_skipped_at and migration_skipped_reason' do
        expect { subject }.to change { repository.reload.migration_skipped_at }

        expect(repository.migration_skipped_reason).to eq('too_many_retries')
        expect(repository).to be_import_skipped
      end

      it 'raises and error if a reason is not given' do
        expect { repository.skip_import }.to raise_error(ArgumentError)
      end
    end

    describe '#finish_pre_import_and_start_import' do
      let_it_be_with_reload(:repository) { create(:container_repository, :pre_importing) }

      subject { repository.finish_pre_import_and_start_import }

      before do |example|
        unless example.metadata[:skip_import_success]
          allow(repository).to receive(:migration_import).and_return(:ok)
        end
      end

      it_behaves_like 'transitioning from allowed states', %w[pre_importing import_aborted]
      it_behaves_like 'transitioning to importing'
    end
  end

  context 'when triggering registry API requests' do
    let(:repository_state) { nil }
    let(:repository) { create(:container_repository, repository_state) }

    shared_examples 'a state machine configured with use_transactions: false' do
      it 'executes the registry API request outside of a transaction', :delete do
        expect(repository).to receive(:save).and_call_original do
          expect(ApplicationRecord.connection.transaction_open?).to be true
        end

        expect(repository).to receive(:try_import) do
          expect(ApplicationRecord.connection.transaction_open?).to be false
        end

        subject
      end
    end

    context 'when responding to a start_pre_import event' do
      subject { repository.start_pre_import }

      it_behaves_like 'a state machine configured with use_transactions: false'
    end

    context 'when responding to a retry_pre_import event' do
      let(:repository_state) { :import_aborted }

      subject { repository.retry_pre_import }

      it_behaves_like 'a state machine configured with use_transactions: false'
    end

    context 'when responding to a start_import event' do
      let(:repository_state) { :pre_import_done }

      subject { repository.start_import }

      it_behaves_like 'a state machine configured with use_transactions: false'
    end

    context 'when responding to a retry_import event' do
      let(:repository_state) { :import_aborted }

      subject { repository.retry_import }

      it_behaves_like 'a state machine configured with use_transactions: false'
    end
  end

  describe '#retry_aborted_migration' do
    subject { repository.retry_aborted_migration }

    shared_examples 'no action' do
      it 'does nothing' do
        expect { subject }.not_to change { repository.reload.migration_state }

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

    shared_examples 'retrying the pre_import' do
      it 'retries the pre_import' do
        expect(repository).to receive(:migration_pre_import).and_return(:ok)

        expect { subject }.to change { repository.reload.migration_state }.to('pre_importing')
      end
    end

    shared_examples 'retrying the import' do
      it 'retries the import' do
        expect(repository).to receive(:migration_import).and_return(:ok)

        expect { subject }.to change { repository.reload.migration_state }.to('importing')
      end
    end

    context 'when migration_state is not aborted' do
      it_behaves_like 'no action'
    end

    context 'when migration_state is aborted' do
      before do
        repository.abort_import

        allow(repository.gitlab_api_client)
            .to receive(:import_status).with(repository.path).and_return(client_response)
      end

      context 'native response' do
        let(:client_response) { 'native' }

        it 'raises an error' do
          expect { subject }.to raise_error(described_class::NativeImportError)
        end
      end

      context 'import_in_progress response' do
        let(:client_response) { 'import_in_progress' }

        it_behaves_like 'no action'
      end

      context 'import_complete response' do
        let(:client_response) { 'import_complete' }

        it 'finishes the import' do
          expect { subject }.to change { repository.reload.migration_state }.to('import_done')
        end
      end

      context 'import_failed response' do
        let(:client_response) { 'import_failed' }

        it_behaves_like 'retrying the import'
      end

      context 'pre_import_in_progress response' do
        let(:client_response) { 'pre_import_in_progress' }

        it_behaves_like 'no action'
      end

      context 'pre_import_complete response' do
        let(:client_response) { 'pre_import_complete' }

        it 'finishes the pre_import and starts the import' do
          expect(repository).to receive(:finish_pre_import).and_call_original
          expect(repository).to receive(:migration_import).and_return(:ok)

          expect { subject }.to change { repository.reload.migration_state }.to('importing')
        end
      end

      context 'pre_import_failed response' do
        let(:client_response) { 'pre_import_failed' }

        it_behaves_like 'retrying the pre_import'
      end

      context 'error response' do
        let(:client_response) { 'error' }

        context 'migration_pre_import_done_at is NULL' do
          it_behaves_like 'retrying the pre_import'
        end

        context 'migration_pre_import_done_at is not NULL' do
          before do
            repository.update_columns(
              migration_pre_import_started_at: 5.minutes.ago,
              migration_pre_import_done_at: Time.zone.now
            )
          end

          it_behaves_like 'retrying the import'
        end
      end
    end
  end

  describe '#tag' do
    it 'has a test tag' do
      expect(repository.tag('test')).not_to be_nil
    end
  end

  describe '#path' do
    context 'when project path does not contain uppercase letters' do
      it 'returns a full path to the repository' do
        expect(repository.path).to eq('group/test/my_image')
      end
    end

    context 'when path contains uppercase letters' do
      let(:project) { create(:project, :repository, path: 'MY_PROJECT', group: group) }

      it 'returns a full path without capital letters' do
        expect(repository.path).to eq('group/my_project/my_image')
      end
    end
  end

  describe '#manifest' do
    it 'returns non-empty manifest' do
      expect(repository.manifest).not_to be_nil
    end
  end

  describe '#valid?' do
    it 'is a valid repository' do
      expect(repository).to be_valid
    end
  end

  describe '#tags' do
    it 'returns non-empty tags list' do
      expect(repository.tags).not_to be_empty
    end
  end

  describe '#tags_count' do
    it 'returns the count of tags' do
      expect(repository.tags_count).to eq(1)
    end
  end

  describe '#has_tags?' do
    it 'has tags' do
      expect(repository).to have_tags
    end
  end

  describe '#delete_tags!' do
    let(:repository) do
      create(:container_repository, name: 'my_image',
                                    tags: { latest: '123', rc1: '234' },
                                    project: project)
    end

    context 'when action succeeds' do
      it 'returns status that indicates success' do
        expect(repository.client)
          .to receive(:delete_repository_tag_by_digest)
          .twice
          .and_return(true)

        expect(repository.delete_tags!).to be_truthy
      end
    end

    context 'when action fails' do
      it 'returns status that indicates failure' do
        expect(repository.client)
          .to receive(:delete_repository_tag_by_digest)
          .twice
          .and_return(false)

        expect(repository.delete_tags!).to be_falsey
      end
    end
  end

  describe '#delete_tag_by_name' do
    let(:repository) do
      create(:container_repository, name: 'my_image',
                                    tags: { latest: '123', rc1: '234' },
                                    project: project)
    end

    context 'when action succeeds' do
      it 'returns status that indicates success' do
        expect(repository.client)
          .to receive(:delete_repository_tag_by_name)
          .with(repository.path, "latest")
          .and_return(true)

        expect(repository.delete_tag_by_name('latest')).to be_truthy
      end
    end

    context 'when action fails' do
      it 'returns status that indicates failure' do
        expect(repository.client)
          .to receive(:delete_repository_tag_by_name)
          .with(repository.path, "latest")
          .and_return(false)

        expect(repository.delete_tag_by_name('latest')).to be_falsey
      end
    end
  end

  describe '#location' do
    context 'when registry is running on a custom port' do
      before do
        stub_container_registry_config(enabled: true,
                                       api_url: 'http://registry.gitlab:5000',
                                       host_port: 'registry.gitlab:5000')
      end

      it 'returns a full location of the repository' do
        expect(repository.location)
          .to eq 'registry.gitlab:5000/group/test/my_image'
      end
    end
  end

  describe '#root_repository?' do
    context 'when repository is a root repository' do
      let(:repository) { create(:container_repository, :root) }

      it 'returns true' do
        expect(repository).to be_root_repository
      end
    end

    context 'when repository is not a root repository' do
      it 'returns false' do
        expect(repository).not_to be_root_repository
      end
    end
  end

  describe '#start_expiration_policy!' do
    subject { repository.start_expiration_policy! }

    it 'sets the expiration policy started at to now' do
      freeze_time do
        expect { subject }
          .to change { repository.expiration_policy_started_at }.from(nil).to(Time.zone.now)
      end
    end
  end

  describe '#reset_expiration_policy_started_at!' do
    subject { repository.reset_expiration_policy_started_at! }

    before do
      repository.start_expiration_policy!
    end

    it 'resets the expiration policy started at' do
      started_at = repository.expiration_policy_started_at

      expect(started_at).not_to be_nil
      expect { subject }
          .to change { repository.expiration_policy_started_at }.from(started_at).to(nil)
    end
  end

  context 'registry migration' do
    shared_examples 'handling the migration step' do |step|
      let(:client_response) { :foobar }

      before do
        allow(repository.gitlab_api_client).to receive(:supports_gitlab_api?).and_return(true)
      end

      it 'returns the same response as the client' do
        expect(repository.gitlab_api_client)
          .to receive(step).with(repository.path).and_return(client_response)
        expect(subject).to eq(client_response)
      end

      context 'when the gitlab_api feature is not supported' do
        before do
          allow(repository.gitlab_api_client).to receive(:supports_gitlab_api?).and_return(false)
        end

        it 'returns :error' do
          expect(repository.gitlab_api_client).not_to receive(step)

          expect(subject).to eq(:error)
        end
      end

      context 'too many imports' do
        it 'raises an error when it receives too_many_imports as a response' do
          expect(repository.gitlab_api_client)
            .to receive(step).with(repository.path).and_return(:too_many_imports)
          expect { subject }.to raise_error(described_class::TooManyImportsError)
        end
      end
    end

    describe '#migration_pre_import' do
      subject { repository.migration_pre_import }

      it_behaves_like 'handling the migration step', :pre_import_repository
    end

    describe '#migration_import' do
      subject { repository.migration_import }

      it_behaves_like 'handling the migration step', :import_repository
    end
  end

  describe '.build_from_path' do
    let(:registry_path) do
      ContainerRegistry::Path.new(project.full_path + '/some/image')
    end

    let(:repository) do
      described_class.build_from_path(registry_path)
    end

    it 'fabricates repository assigned to a correct project' do
      expect(repository.project).to eq project
    end

    it 'fabricates repository with a correct name' do
      expect(repository.name).to eq 'some/image'
    end

    it 'is not persisted' do
      expect(repository).not_to be_persisted
    end
  end

  describe '.find_or_create_from_path' do
    let(:repository) do
      described_class.find_or_create_from_path(ContainerRegistry::Path.new(path))
    end

    let(:repository_path) { ContainerRegistry::Path.new(path) }

    context 'when received multi-level repository path' do
      let(:path) { project.full_path + '/some/image' }

      it 'fabricates repository assigned to a correct project' do
        expect(repository.project).to eq project
      end

      it 'fabricates repository with a correct name' do
        expect(repository.name).to eq 'some/image'
      end
    end

    context 'when path is too long' do
      let(:path) do
        project.full_path + '/a/b/c/d/e/f/g/h/i/j/k/l/n/o/p/s/t/u/x/y/z'
      end

      it 'does not create repository and raises error' do
        expect { repository }.to raise_error(
          ContainerRegistry::Path::InvalidRegistryPathError)
      end
    end

    context 'when received multi-level repository with nested groups' do
      let(:group) { create(:group, :nested, name: 'nested') }
      let(:path) { project.full_path + '/some/image' }

      it 'fabricates repository assigned to a correct project' do
        expect(repository.project).to eq project
      end

      it 'fabricates repository with a correct name' do
        expect(repository.name).to eq 'some/image'
      end

      it 'has path including a nested group' do
        expect(repository.path).to include 'nested/test/some/image'
      end
    end

    context 'when received root repository path' do
      let(:path) { project.full_path }

      it 'fabricates repository assigned to a correct project' do
        expect(repository.project).to eq project
      end

      it 'fabricates repository with an empty name' do
        expect(repository.name).to be_empty
      end
    end

    context 'when repository already exists' do
      let(:path) { project.full_path + '/some/image' }

      it 'returns the existing repository' do
        container_repository = create(:container_repository, project: project, name: 'some/image')

        expect(repository.id).to eq(container_repository.id)
      end
    end

    context 'when many of the same repository are created at the same time' do
      let(:path) { ContainerRegistry::Path.new(project.full_path + '/some/image') }

      it 'does not throw validation errors and only creates one repository' do
        expect { repository_creation_race(path) }.to change { described_class.count }.by(1)
      end

      it 'retrieves a persisted repository for all concurrent calls' do
        repositories = repository_creation_race(path).map(&:value)

        expect(repositories).to all(be_persisted)
      end
    end

    def repository_creation_race(path)
      # create a race condition - structure from https://blog.arkency.com/2015/09/testing-race-conditions/
      wait_for_it = true

      threads = Array.new(10) do |i|
        Thread.new do
          true while wait_for_it

          described_class.find_or_create_from_path(path)
        end
      end
      wait_for_it = false
      threads.each(&:join)
    end
  end

  describe '.find_by_path' do
    let_it_be(:container_repository) { create(:container_repository) }
    let_it_be(:repository_path) { container_repository.project.full_path }

    let(:path) { ContainerRegistry::Path.new(repository_path + '/' + container_repository.name) }

    subject { described_class.find_by_path(path) }

    context 'when repository exists' do
      it 'finds the repository' do
        expect(subject).to eq(container_repository)
      end
    end

    context 'when repository does not exist' do
      let(:path) { ContainerRegistry::Path.new(repository_path + '/some/image') }

      it 'returns nil' do
        expect(subject).to be_nil
      end
    end
  end

  describe '.find_by_path!' do
    let_it_be(:container_repository) { create(:container_repository) }
    let_it_be(:repository_path) { container_repository.project.full_path }

    let(:path) { ContainerRegistry::Path.new(repository_path + '/' + container_repository.name) }

    subject { described_class.find_by_path!(path) }

    context 'when repository exists' do
      it 'finds the repository' do
        expect(subject).to eq(container_repository)
      end
    end

    context 'when repository does not exist' do
      let(:path) { ContainerRegistry::Path.new(repository_path + '/some/image') }

      it 'raises an exception' do
        expect { subject }.to raise_error(ActiveRecord::RecordNotFound)
      end
    end
  end

  describe '.build_root_repository' do
    let(:repository) do
      described_class.build_root_repository(project)
    end

    it 'fabricates a root repository object' do
      expect(repository).to be_root_repository
    end

    it 'assignes it to the correct project' do
      expect(repository.project).to eq project
    end

    it 'does not persist it' do
      expect(repository).not_to be_persisted
    end
  end

  describe '.for_group_and_its_subgroups' do
    subject { described_class.for_group_and_its_subgroups(test_group) }

    context 'in a group' do
      let(:test_group) { group }

      it { is_expected.to contain_exactly(repository) }
    end

    context 'with a subgroup' do
      let_it_be(:test_group) { create(:group) }
      let_it_be(:another_project) { create(:project, path: 'test', group: test_group) }
      let_it_be(:project3) { create(:project, :container_registry_disabled, path: 'test3', group: test_group) }

      let_it_be(:another_repository) do
        create(:container_repository, name: 'my_image', project: another_project)
      end

      let_it_be(:repository3) do
        create(:container_repository, name: 'my_image3', project: project3)
      end

      before do
        group.parent = test_group
        group.save!
      end

      it { is_expected.to contain_exactly(repository, another_repository) }
    end

    context 'group without container_repositories' do
      let(:test_group) { create(:group) }

      it { is_expected.to eq([]) }
    end
  end

  describe '.search_by_name' do
    let!(:another_repository) do
      create(:container_repository, name: 'my_foo_bar', project: project)
    end

    subject { described_class.search_by_name('my_image') }

    it { is_expected.to contain_exactly(repository) }
  end

  describe '.for_project_id' do
    subject { described_class.for_project_id(project.id) }

    it { is_expected.to contain_exactly(repository) }
  end

  describe '.expiration_policy_started_at_nil_or_before' do
    let_it_be(:repository1) { create(:container_repository, expiration_policy_started_at: nil) }
    let_it_be(:repository2) { create(:container_repository, expiration_policy_started_at: 1.day.ago) }
    let_it_be(:repository3) { create(:container_repository, expiration_policy_started_at: 2.hours.ago) }
    let_it_be(:repository4) { create(:container_repository, expiration_policy_started_at: 1.week.ago) }

    subject { described_class.expiration_policy_started_at_nil_or_before(3.hours.ago) }

    it { is_expected.to contain_exactly(repository1, repository2, repository4) }
  end

  describe '.with_migration_import_started_at_nil_or_before' do
    let_it_be(:repository1) { create(:container_repository, migration_import_started_at: 5.minutes.ago) }
    let_it_be(:repository2) { create(:container_repository, migration_import_started_at: nil) }
    let_it_be(:repository3) { create(:container_repository, migration_import_started_at: 10.minutes.ago) }

    subject { described_class.with_migration_import_started_at_nil_or_before(7.minutes.ago) }

    it { is_expected.to contain_exactly(repository2, repository3) }
  end

  describe '.with_migration_pre_import_started_at_nil_or_before' do
    let_it_be(:repository1) { create(:container_repository, migration_pre_import_started_at: 5.minutes.ago) }
    let_it_be(:repository2) { create(:container_repository, migration_pre_import_started_at: nil) }
    let_it_be(:repository3) { create(:container_repository, migration_pre_import_started_at: 10.minutes.ago) }

    subject { described_class.with_migration_pre_import_started_at_nil_or_before(7.minutes.ago) }

    it { is_expected.to contain_exactly(repository2, repository3) }
  end

  describe '.with_migration_pre_import_done_at_nil_or_before' do
    let_it_be(:repository1) { create(:container_repository, migration_pre_import_done_at: 5.minutes.ago) }
    let_it_be(:repository2) { create(:container_repository, migration_pre_import_done_at: nil) }
    let_it_be(:repository3) { create(:container_repository, migration_pre_import_done_at: 10.minutes.ago) }

    subject { described_class.with_migration_pre_import_done_at_nil_or_before(7.minutes.ago) }

    it { is_expected.to contain_exactly(repository2, repository3) }
  end

  describe '.with_stale_ongoing_cleanup' do
    let_it_be(:repository1) { create(:container_repository, :cleanup_ongoing, expiration_policy_started_at: 1.day.ago) }
    let_it_be(:repository2) { create(:container_repository, :cleanup_ongoing, expiration_policy_started_at: 25.minutes.ago) }
    let_it_be(:repository3) { create(:container_repository, :cleanup_ongoing, expiration_policy_started_at: 1.week.ago) }
    let_it_be(:repository4) { create(:container_repository, :cleanup_unscheduled, expiration_policy_started_at: 25.minutes.ago) }

    subject { described_class.with_stale_ongoing_cleanup(27.minutes.ago) }

    it { is_expected.to contain_exactly(repository1, repository3) }
  end

  describe '.waiting_for_cleanup' do
    let_it_be(:repository_cleanup_scheduled) { create(:container_repository, :cleanup_scheduled) }
    let_it_be(:repository_cleanup_unfinished) { create(:container_repository, :cleanup_unfinished) }
    let_it_be(:repository_cleanup_ongoing) { create(:container_repository, :cleanup_ongoing) }

    subject { described_class.waiting_for_cleanup }

    it { is_expected.to contain_exactly(repository_cleanup_scheduled, repository_cleanup_unfinished) }
  end

  describe '.exists_by_path?' do
    it 'returns true for known container repository paths' do
      path = ContainerRegistry::Path.new("#{project.full_path}/#{repository.name}")
      expect(described_class.exists_by_path?(path)).to be_truthy
    end

    it 'returns false for unknown container repository paths' do
      path = ContainerRegistry::Path.new('you/dont/know/me')
      expect(described_class.exists_by_path?(path)).to be_falsey
    end
  end

  describe '.with_enabled_policy' do
    let_it_be(:repository) { create(:container_repository) }
    let_it_be(:repository2) { create(:container_repository) }

    subject { described_class.with_enabled_policy }

    before do
      repository.project.container_expiration_policy.update!(enabled: true)
    end

    it { is_expected.to eq([repository]) }
  end

  describe '#migration_in_active_state?' do
    subject { container_repository.migration_in_active_state? }

    described_class::MIGRATION_STATES.each do |state|
      context "when in #{state} migration_state" do
        let(:container_repository) { create(:container_repository, state.to_sym)}

        it { is_expected.to eq(state == 'importing' || state == 'pre_importing') }
      end
    end
  end

  describe '#migration_importing?' do
    subject { container_repository.migration_importing? }

    described_class::MIGRATION_STATES.each do |state|
      context "when in #{state} migration_state" do
        let(:container_repository) { create(:container_repository, state.to_sym)}

        it { is_expected.to eq(state == 'importing') }
      end
    end
  end

  describe '#migration_pre_importing?' do
    subject { container_repository.migration_pre_importing? }

    described_class::MIGRATION_STATES.each do |state|
      context "when in #{state} migration_state" do
        let(:container_repository) { create(:container_repository, state.to_sym)}

        it { is_expected.to eq(state == 'pre_importing') }
      end
    end
  end

  describe '#try_import' do
    let_it_be_with_reload(:container_repository) { create(:container_repository) }

    let(:response) { nil }

    subject do
      container_repository.try_import do
        container_repository.foo
      end
    end

    before do
      allow(container_repository).to receive(:foo).and_return(response)
    end

    context 'successful request' do
      let(:response) { :ok }

      it { is_expected.to eq(true) }
    end

    context 'TooManyImportsError' do
      before do
        stub_application_setting(container_registry_import_start_max_retries: 3)
        allow(container_repository).to receive(:foo).and_raise(described_class::TooManyImportsError)
      end

      it 'tries again exponentially and aborts the migration' do
        expect(container_repository).to receive(:sleep).with(a_value_within(0.01).of(0.1))
        expect(container_repository).to receive(:sleep).with(a_value_within(0.01).of(0.2))
        expect(container_repository).to receive(:sleep).with(a_value_within(0.01).of(0.3))

        expect(subject).to eq(false)

        expect(container_repository).to be_import_aborted
      end
    end

    context 'other response' do
      let(:response) { :error }

      it 'aborts the migration' do
        expect(subject).to eq(false)

        expect(container_repository).to be_import_aborted
      end
    end

    context 'with no block given' do
      it 'raises an error' do
        expect { container_repository.try_import }.to raise_error(ArgumentError)
      end
    end
  end

  context 'with repositories' do
    let_it_be_with_reload(:repository) { create(:container_repository, :cleanup_unscheduled) }
    let_it_be(:other_repository) { create(:container_repository, :cleanup_unscheduled) }

    let(:policy) { repository.project.container_expiration_policy }

    before do
      ContainerExpirationPolicy.update_all(enabled: true)
    end

    describe '.requiring_cleanup' do
      subject { described_class.requiring_cleanup }

      context 'with next_run_at in the future' do
        before do
          policy.update_column(:next_run_at, 10.minutes.from_now)
        end

        it { is_expected.to eq([]) }
      end

      context 'with next_run_at in the past' do
        before do
          policy.update_column(:next_run_at, 10.minutes.ago)
        end

        it { is_expected.to eq([repository]) }
      end

      context 'with repository cleanup started at after policy next run at' do
        before do
          repository.update!(expiration_policy_started_at: policy.next_run_at + 5.minutes)
        end

        it { is_expected.to eq([]) }
      end
    end

    describe '.with_unfinished_cleanup' do
      subject { described_class.with_unfinished_cleanup }

      it { is_expected.to eq([]) }

      context 'with an unfinished repository' do
        before do
          repository.cleanup_unfinished!
        end

        it { is_expected.to eq([repository]) }
      end
    end

    describe '.recently_done_migration_step' do
      let_it_be(:import_done_repository) { create(:container_repository, :import_done, migration_pre_import_done_at: 3.days.ago, migration_import_done_at: 2.days.ago) }
      let_it_be(:import_aborted_repository) { create(:container_repository, :import_aborted, migration_pre_import_done_at: 5.days.ago, migration_aborted_at: 1.day.ago) }
      let_it_be(:pre_import_done_repository) { create(:container_repository, :pre_import_done, migration_pre_import_done_at: 1.hour.ago) }

      subject { described_class.recently_done_migration_step }

      it 'returns completed imports by done_at date' do
        expect(subject.to_a).to eq([pre_import_done_repository, import_aborted_repository, import_done_repository])
      end
    end

    describe '.ready_for_import' do
      include_context 'importable repositories'

      subject { described_class.ready_for_import }

      before do
        stub_application_setting(container_registry_import_target_plan: project.namespace.actual_plan_name)
      end

      it 'works' do
        expect(subject).to contain_exactly(valid_container_repository, valid_container_repository2)
      end
    end

    describe '#last_import_step_done_at' do
      let_it_be(:aborted_at) { Time.zone.now - 1.hour }
      let_it_be(:pre_import_done_at) { Time.zone.now - 2.hours }

      subject { repository.last_import_step_done_at }

      before do
        repository.update_columns(
          migration_pre_import_done_at: pre_import_done_at,
          migration_aborted_at: aborted_at
        )
      end

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

  describe '#external_import_status' do
    subject { repository.external_import_status }

    it 'returns the response from the client' do
      expect(repository.gitlab_api_client).to receive(:import_status).with(repository.path).and_return('test')

      expect(subject).to eq('test')
    end
  end

  describe '.with_stale_migration' do
    let_it_be(:repository) { create(:container_repository) }
    let_it_be(:stale_pre_importing_old_timestamp) { create(:container_repository, :pre_importing, migration_pre_import_started_at: 10.minutes.ago) }
    let_it_be(:stale_pre_importing_nil_timestamp) { create(:container_repository, :pre_importing).tap { |r| r.update_column(:migration_pre_import_started_at, nil) } }
    let_it_be(:stale_pre_importing_recent_timestamp) { create(:container_repository, :pre_importing, migration_pre_import_started_at: 2.minutes.ago) }

    let_it_be(:stale_pre_import_done_old_timestamp) { create(:container_repository, :pre_import_done, migration_pre_import_done_at: 10.minutes.ago) }
    let_it_be(:stale_pre_import_done_nil_timestamp) { create(:container_repository, :pre_import_done).tap { |r| r.update_column(:migration_pre_import_done_at, nil) } }
    let_it_be(:stale_pre_import_done_recent_timestamp) { create(:container_repository, :pre_import_done, migration_pre_import_done_at: 2.minutes.ago) }

    let_it_be(:stale_importing_old_timestamp) { create(:container_repository, :importing, migration_import_started_at: 10.minutes.ago) }
    let_it_be(:stale_importing_nil_timestamp) { create(:container_repository, :importing).tap { |r| r.update_column(:migration_import_started_at, nil) } }
    let_it_be(:stale_importing_recent_timestamp) { create(:container_repository, :importing, migration_import_started_at: 2.minutes.ago) }

    let(:stale_migrations) do
      [
        stale_pre_importing_old_timestamp,
        stale_pre_importing_nil_timestamp,
        stale_pre_import_done_old_timestamp,
        stale_pre_import_done_nil_timestamp,
        stale_importing_old_timestamp,
        stale_importing_nil_timestamp
      ]
    end

    subject { described_class.with_stale_migration(5.minutes.ago) }

    it { is_expected.to contain_exactly(*stale_migrations) }
  end
end