summaryrefslogtreecommitdiff
path: root/spec/features/projects/pipelines/pipeline_spec.rb
blob: d6067e229523900e1a704f3926d86a66712caaff (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
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe 'Pipeline', :js, feature_category: :projects do
  include RoutesHelpers
  include ProjectForksHelper
  include ::ExclusiveLeaseHelpers

  let_it_be(:project) { create(:project) }

  let(:user) { create(:user) }
  let(:role) { :developer }

  before do
    sign_in(user)
    project.add_role(user, role)
  end

  shared_context 'pipeline builds' do
    let!(:build_passed) do
      create(:ci_build, :success,
             pipeline: pipeline, stage: 'build', stage_idx: 0, name: 'build')
    end

    let!(:build_failed) do
      create(:ci_build, :failed,
             pipeline: pipeline, stage: 'test', stage_idx: 1, name: 'test')
    end

    let!(:build_preparing) do
      create(:ci_build, :preparing,
             pipeline: pipeline, stage: 'deploy', stage_idx: 2, name: 'prepare')
    end

    let!(:build_running) do
      create(:ci_build, :running,
             pipeline: pipeline, stage: 'deploy', stage_idx: 3, name: 'deploy')
    end

    let!(:build_manual) do
      create(:ci_build, :manual,
             pipeline: pipeline, stage: 'deploy', stage_idx: 3, name: 'manual-build')
    end

    let!(:build_scheduled) do
      create(:ci_build, :scheduled,
             pipeline: pipeline, stage: 'deploy', stage_idx: 3, name: 'delayed-job')
    end

    let!(:build_external) do
      create(:generic_commit_status, status: 'success',
                                     pipeline: pipeline,
                                     name: 'jenkins',
                                     stage: 'external',
                                     ref: 'master',
                                     target_url: 'http://gitlab.com/status')
    end
  end

  describe 'GET /:project/-/pipelines/:id' do
    include_context 'pipeline builds'

    let_it_be(:group) { create(:group) }
    let_it_be(:project, reload: true) { create(:project, :repository, group: group) }

    let(:pipeline) do
      create(:ci_pipeline, name: 'Build pipeline', project: project, ref: 'master', sha: project.commit.id, user: user)
    end

    subject(:visit_pipeline) { visit project_pipeline_path(project, pipeline) }

    it 'shows the pipeline graph' do
      visit_pipeline

      expect(page).to have_selector('.js-pipeline-graph')
      expect(page).to have_content('build')
      expect(page).to have_content('test')
      expect(page).to have_content('deploy')
      expect(page).to have_content('Retry')
      expect(page).to have_content('Cancel running')
    end

    it 'shows link to the pipeline ref' do
      visit_pipeline

      expect(page).to have_link(pipeline.ref)
    end

    it 'shows the pipeline information' do
      visit_pipeline

      within '.pipeline-info' do
        expect(page).to have_content("#{pipeline.statuses.count} jobs " \
                                      "for #{pipeline.ref}")
        expect(page).to have_link(pipeline.ref,
          href: project_commits_path(pipeline.project, pipeline.ref))
      end
    end

    context 'with pipeline_name feature flag enabled' do
      before do
        stub_feature_flags(pipeline_name: true)
      end

      it 'displays pipeline name instead of commit title' do
        visit_pipeline

        within 'h3' do
          expect(page).to have_content(pipeline.name)
        end

        within '.well-segment[data-testid="commit-row"]' do
          expect(page).to have_content(project.commit.title)
          expect(page).to have_content(project.commit.short_id)
        end
      end
    end

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

      it 'displays commit title' do
        visit_pipeline

        within 'h3' do
          expect(page).not_to have_content(pipeline.name)
          expect(page).to have_content(project.commit.title)
        end

        within '.well-segment[data-testid="commit-row"]' do
          expect(page).not_to have_content(project.commit.title)
          expect(page).to have_content(project.commit.short_id)
        end
      end
    end

    describe 'related merge requests' do
      context 'when there are no related merge requests' do
        it 'shows a "no related merge requests" message' do
          visit_pipeline

          within '.related-merge-request-info' do
            expect(page).to have_content('No related merge requests found.')
          end
        end
      end

      context 'when there is one related merge request' do
        let!(:merge_request) do
          create(:merge_request,
            source_project: project,
            source_branch: pipeline.ref)
        end

        it 'shows a link to the merge request' do
          visit_pipeline

          within '.related-merge-requests' do
            expect(page).to have_content('1 related merge request: ')
            expect(page).to have_selector('.js-truncated-mr-list')
            expect(page).to have_link("#{merge_request.to_reference} #{merge_request.title}")

            expect(page).not_to have_selector('.js-full-mr-list')
            expect(page).not_to have_selector('.text-expander')
          end
        end
      end

      context 'when there are two related merge requests' do
        let!(:merge_request1) do
          create(:merge_request,
            source_project: project,
            source_branch: pipeline.ref)
        end

        let!(:merge_request2) do
          create(:merge_request,
            source_project: project,
            source_branch: pipeline.ref,
            target_branch: 'fix')
        end

        it 'links to the most recent related merge request' do
          visit_pipeline

          within '.related-merge-requests' do
            expect(page).to have_content('2 related merge requests: ')
            expect(page).to have_link("#{merge_request2.to_reference} #{merge_request2.title}")
            expect(page).to have_selector('.text-expander')
            expect(page).to have_selector('.js-full-mr-list', visible: false)
          end
        end

        it 'expands to show links to all related merge requests' do
          visit_pipeline

          within '.related-merge-requests' do
            find('.text-expander').click

            expect(page).to have_selector('.js-full-mr-list', visible: true)

            pipeline.all_merge_requests.map do |merge_request|
              expect(page).to have_link(href: project_merge_request_path(project, merge_request))
            end
          end
        end
      end
    end

    describe 'pipelines details view' do
      let!(:status) { create(:user_status, user: pipeline.user, emoji: 'smirk', message: 'Authoring this object') }

      it 'pipeline header shows the user status and emoji' do
        visit project_pipeline_path(project, pipeline)

        within '[data-testid="ci-header-content"]' do
          expect(page).to have_selector("[data-testid='#{status.message}']")
          expect(page).to have_selector("[data-name='#{status.emoji}']")
        end
      end
    end

    describe 'pipeline graph' do
      context 'when pipeline has running builds' do
        before do
          visit_pipeline
        end

        it 'shows a running icon and a cancel action for the running build' do
          page.within('#ci-badge-deploy') do
            expect(page).to have_selector('.js-ci-status-icon-running')
            expect(page).to have_selector('.js-icon-cancel')
            expect(page).to have_content('deploy')
          end
        end

        it 'cancels the running build and shows retry button', :sidekiq_might_not_need_inline do
          find('#ci-badge-deploy .ci-action-icon-container').click

          page.within('#ci-badge-deploy') do
            expect(page).to have_css('.js-icon-retry')
          end
        end
      end

      context 'when pipeline has preparing builds' do
        before do
          visit_pipeline
        end

        it 'shows a preparing icon and a cancel action' do
          page.within('#ci-badge-prepare') do
            expect(page).to have_selector('.js-ci-status-icon-preparing')
            expect(page).to have_selector('.js-icon-cancel')
            expect(page).to have_content('prepare')
          end
        end

        it 'cancels the preparing build and shows retry button', :sidekiq_might_not_need_inline do
          find('#ci-badge-deploy .ci-action-icon-container').click

          page.within('#ci-badge-deploy') do
            expect(page).to have_css('.js-icon-retry')
          end
        end
      end

      context 'when pipeline has successful builds' do
        before do
          visit_pipeline
        end

        it 'shows the success icon and a retry action for the successful build' do
          page.within('#ci-badge-build') do
            expect(page).to have_selector('.js-ci-status-icon-success')
            expect(page).to have_content('build')
          end

          page.within('#ci-badge-build .ci-action-icon-container.js-icon-retry') do
            expect(page).to have_selector('svg')
          end
        end

        it 'is possible to retry the success job', :sidekiq_might_not_need_inline do
          find('#ci-badge-build .ci-action-icon-container').click
          wait_for_requests

          expect(page).not_to have_content('Retry job')
          within('.js-pipeline-header-container') do
            expect(page).to have_selector('.js-ci-status-icon-running')
          end
        end
      end

      context 'when pipeline has a delayed job' do
        before do
          visit_pipeline
        end

        let(:project) { create(:project, :repository, group: group) }

        it 'shows the scheduled icon and an unschedule action for the delayed job' do
          page.within('#ci-badge-delayed-job') do
            expect(page).to have_selector('.js-ci-status-icon-scheduled')
            expect(page).to have_content('delayed-job')
          end

          page.within('#ci-badge-delayed-job .ci-action-icon-container.js-icon-time-out') do
            expect(page).to have_selector('svg')
          end
        end

        it 'unschedules the delayed job and shows play button as a manual job', :sidekiq_might_not_need_inline do
          find('#ci-badge-delayed-job .ci-action-icon-container').click

          page.within('#ci-badge-delayed-job') do
            expect(page).to have_css('.js-icon-play')
          end
        end
      end

      context 'when pipeline has failed builds' do
        before do
          visit_pipeline
        end

        it 'shows the failed icon and a retry action for the failed build' do
          page.within('#ci-badge-test') do
            expect(page).to have_selector('.js-ci-status-icon-failed')
            expect(page).to have_content('test')
          end

          page.within('#ci-badge-test .ci-action-icon-container.js-icon-retry') do
            expect(page).to have_selector('svg')
          end
        end

        it 'is possible to retry the failed build', :sidekiq_might_not_need_inline do
          find('#ci-badge-test .ci-action-icon-container').click
          wait_for_requests

          expect(page).not_to have_content('Retry job')
          within('.js-pipeline-header-container') do
            expect(page).to have_selector('.js-ci-status-icon-running')
          end
        end

        it 'includes the failure reason' do
          page.within('#ci-badge-test') do
            build_link = page.find('.js-pipeline-graph-job-link')
            expect(build_link['title']).to eq('test - failed - (unknown failure)')
          end
        end
      end

      context 'when pipeline has manual jobs' do
        before do
          visit_pipeline
        end

        it 'shows the skipped icon and a play action for the manual build' do
          page.within('#ci-badge-manual-build') do
            expect(page).to have_selector('.js-ci-status-icon-manual')
            expect(page).to have_content('manual')
          end

          page.within('#ci-badge-manual-build .ci-action-icon-container.js-icon-play') do
            expect(page).to have_selector('svg')
          end
        end

        it 'is possible to play the manual job', :sidekiq_might_not_need_inline do
          find('#ci-badge-manual-build .ci-action-icon-container').click
          wait_for_requests

          expect(page).not_to have_content('Play job')
          within('.js-pipeline-header-container') do
            expect(page).to have_selector('.js-ci-status-icon-running')
          end
        end
      end

      context 'when pipeline has external job' do
        before do
          visit_pipeline
        end

        it 'shows the success icon and the generic comit status build' do
          expect(page).to have_selector('.js-ci-status-icon-success')
          expect(page).to have_content('jenkins')
          expect(page).to have_link('jenkins', href: 'http://gitlab.com/status')
        end
      end

      context 'when pipeline has a downstream pipeline' do
        let(:downstream_project) { create(:project, :repository, group: group) }
        let(:downstream_pipeline) do
          create(:ci_pipeline,
          status,
          user: user,
          project: downstream_project,
          ref: 'master',
          sha: downstream_project.commit.id,
          child_of: pipeline)
        end

        let!(:build) { create(:ci_build, status, pipeline: downstream_pipeline, user: user) }

        before do
          downstream_pipeline.project.add_developer(user)
        end

        context 'and user has permission' do
          before do
            visit_pipeline
          end

          context 'with a successful downstream' do
            let(:status) { :success }

            it 'does not show the cancel or retry action' do
              expect(page).to have_selector('.ci-status-icon-success')
              expect(page).not_to have_selector('button[aria-label="Retry downstream pipeline"]')
              expect(page).not_to have_selector('button[aria-label="Cancel downstream pipeline"]')
            end
          end

          context 'with a running downstream' do
            let(:status) { :running }

            it 'shows the cancel action' do
              expect(page).to have_selector('button[aria-label="Cancel downstream pipeline"]')
            end

            context 'when canceling', :sidekiq_inline do
              before do
                find('button[aria-label="Cancel downstream pipeline"]').click
                wait_for_requests
              end

              it 'shows the pipeline as canceled with the retry action' do
                expect(page).to have_selector('button[aria-label="Retry downstream pipeline"]')
                expect(page).to have_selector('.ci-status-icon-canceled')
              end
            end
          end

          context 'with a failed downstream' do
            let(:status) { :failed }

            it 'indicates that pipeline can be retried' do
              expect(page).to have_selector('button[aria-label="Retry downstream pipeline"]')
            end

            context 'when retrying' do
              before do
                find('button[aria-label="Retry downstream pipeline"]').click
                wait_for_requests
              end

              it 'shows running pipeline with the cancel action' do
                expect(page).to have_selector('.ci-status-icon-running')
                expect(page).to have_selector('button[aria-label="Cancel downstream pipeline"]')
              end
            end
          end

          context 'with a canceled downstream' do
            let(:status) { :canceled }

            it 'indicates that pipeline can be retried' do
              expect(page).to have_selector('button[aria-label="Retry downstream pipeline"]')
            end

            context 'when retrying' do
              before do
                find('button[aria-label="Retry downstream pipeline"]').click
                wait_for_requests
              end

              it 'shows running pipeline with the cancel action' do
                expect(page).to have_selector('.ci-status-icon-running')
                expect(page).to have_selector('button[aria-label="Cancel downstream pipeline"]')
              end
            end
          end
        end

        context 'when user does not have permissions' do
          let(:status) { :failed }

          before do
            new_user = create(:user)
            project.add_role(new_user, :guest)
            downstream_project.add_role(new_user, :guest)
            sign_in(new_user)

            visit_pipeline
          end

          it 'does not show the retry button' do
            expect(page).to have_selector('.ci-status-icon-failed')
            expect(page).not_to have_selector('button[aria-label="Retry downstream pipeline"]')
          end
        end
      end
    end

    context 'when the pipeline has manual stage' do
      before do
        create(:ci_build, :manual, pipeline: pipeline, stage_idx: 10, stage: 'publish', name: 'CentOS')
        create(:ci_build, :manual, pipeline: pipeline, stage_idx: 10, stage: 'publish', name: 'Debian')
        create(:ci_build, :manual, pipeline: pipeline, stage_idx: 10, stage: 'publish', name: 'OpenSUDE')

        # force to update stages statuses
        Ci::ProcessPipelineService.new(pipeline).execute

        visit_pipeline
      end

      it 'displays play all button' do
        expect(page).to have_selector('.js-stage-action')
      end
    end

    context 'page tabs' do
      before do
        visit_pipeline
      end

      it 'shows Pipeline, Jobs, DAG and Failed Jobs tabs with link' do
        expect(page).to have_link('Pipeline')
        expect(page).to have_link('Jobs')
        expect(page).to have_link('Needs')
        expect(page).to have_link('Failed Jobs')
      end

      it 'shows counter in Jobs tab' do
        expect(page.find('[data-testid="builds-counter"]').text).to eq(pipeline.total_size.to_s)
      end

      context 'without permission to access builds' do
        let(:project) { create(:project, :public, :repository, public_builds: false) }
        let(:role) { :guest }

        it 'does not show the pipeline details page' do
          expect(page).to have_content('Not Found')
        end
      end
    end

    describe 'test tabs' do
      let(:pipeline) { create(:ci_pipeline, :with_test_reports, :with_report_results, project: project) }

      before do
        visit_pipeline
        wait_for_requests
      end

      context 'with test reports' do
        it 'shows badge counter in Tests tab' do
          expect(page.find('[data-testid="tests-counter"]').text).to eq(pipeline.test_report_summary.total[:count].to_s)
        end

        it 'calls summary.json endpoint', :js do
          find('.gl-tab-nav-item', text: 'Tests').click

          expect(page).to have_content('Jobs')
          expect(page).to have_selector('[data-testid="tests-detail"]', visible: :all)
        end
      end

      context 'without test reports' do
        let(:pipeline) { create(:ci_pipeline, project: project) }

        it 'shows zero' do
          expect(page.find('[data-testid="tests-counter"]', visible: :all).text).to eq("0")
        end
      end
    end

    context 'retrying jobs' do
      before do
        visit_pipeline
      end

      it { expect(page).not_to have_content('retried') }

      context 'when retrying' do
        before do
          find('[data-testid="retryPipeline"]').click
          wait_for_requests
        end

        it 'does not show a "Retry" button', :sidekiq_might_not_need_inline do
          expect(page).not_to have_content('Retry')
        end

        it 'shows running status in pipeline header', :sidekiq_might_not_need_inline do
          within('.js-pipeline-header-container') do
            expect(page).to have_selector('.js-ci-status-icon-running')
          end
        end
      end
    end

    context 'canceling jobs' do
      before do
        visit_pipeline
      end

      it { expect(page).not_to have_selector('.ci-canceled') }

      context 'when canceling' do
        before do
          click_on 'Cancel running'
        end

        it 'does not show a "Cancel running" button', :sidekiq_might_not_need_inline do
          expect(page).not_to have_content('Cancel running')
        end
      end
    end

    context 'when user can not delete' do
      before do
        visit_pipeline
      end

      it { expect(page).not_to have_button('Delete') }
    end

    context 'when deleting' do
      before do
        group.add_owner(user)

        visit_pipeline

        click_button 'Delete'
        click_button 'Delete pipeline'
      end

      it 'redirects to pipeline overview page', :sidekiq_inline do
        expect(page).to have_content('The pipeline has been deleted')
        expect(page).to have_current_path(project_pipelines_path(project), ignore_query: true)
      end
    end

    context 'when pipeline ref does not exist in repository anymore' do
      let(:pipeline) do
        create(:ci_empty_pipeline, project: project,
                                   ref: 'non-existent',
                                   sha: project.commit.id,
                                   user: user)
      end

      before do
        visit_pipeline
      end

      it 'does not render link to the pipeline ref' do
        expect(page).not_to have_link(pipeline.ref)
        expect(page).to have_content(pipeline.ref)
      end

      it 'does not render render raw HTML to the pipeline ref' do
        page.within '.pipeline-info' do
          expect(page).not_to have_content('<span class="ref-name"')
        end
      end
    end

    context 'when pipeline is detached merge request pipeline' do
      let(:source_project) { project }
      let(:target_project) { project }

      let(:merge_request) do
        create(:merge_request,
          :with_detached_merge_request_pipeline,
          source_project: source_project,
          target_project: target_project)
      end

      let(:pipeline) do
        merge_request.all_pipelines.last
      end

      it 'shows the pipeline information' do
        visit_pipeline

        within '.pipeline-info' do
          expect(page).to have_content("#{pipeline.statuses.count} jobs " \
                                       "for !#{merge_request.iid} " \
                                       "with #{merge_request.source_branch}")
          expect(page).to have_link("!#{merge_request.iid}",
            href: project_merge_request_path(project, merge_request))
          expect(page).to have_link(merge_request.source_branch,
            href: project_commits_path(merge_request.source_project, merge_request.source_branch))
        end
      end

      context 'when source branch does not exist' do
        before do
          project.repository.rm_branch(user, merge_request.source_branch)
        end

        it 'does not link to the source branch commit path' do
          visit_pipeline

          within '.pipeline-info' do
            expect(page).not_to have_link(merge_request.source_branch)
            expect(page).to have_content(merge_request.source_branch)
          end
        end
      end

      context 'when source project is a forked project' do
        let(:source_project) { fork_project(project, user, repository: true) }

        before do
          visit project_pipeline_path(source_project, pipeline)
        end

        it 'shows the pipeline information', :sidekiq_might_not_need_inline do
          within '.pipeline-info' do
            expect(page).to have_content("#{pipeline.statuses.count} jobs " \
                                         "for !#{merge_request.iid} " \
                                         "with #{merge_request.source_branch}")
            expect(page).to have_link("!#{merge_request.iid}",
              href: project_merge_request_path(project, merge_request))
            expect(page).to have_link(merge_request.source_branch,
              href: project_commits_path(merge_request.source_project, merge_request.source_branch))
          end
        end
      end
    end

    context 'when pipeline is merge request pipeline' do
      let(:project) { create(:project, :repository, group: group) }
      let(:source_project) { project }
      let(:target_project) { project }

      let(:merge_request) do
        create(:merge_request,
          :with_merge_request_pipeline,
          source_project: source_project,
          target_project: target_project,
          merge_sha: project.commit.id)
      end

      let(:pipeline) do
        merge_request.all_pipelines.last
      end

      before do
        pipeline.update!(user: user)
      end

      it 'shows the pipeline information' do
        visit_pipeline

        within '.pipeline-info' do
          expect(page).to have_content("#{pipeline.statuses.count} jobs " \
                                       "for !#{merge_request.iid} " \
                                       "with #{merge_request.source_branch} " \
                                       "into #{merge_request.target_branch}")
          expect(page).to have_link("!#{merge_request.iid}",
            href: project_merge_request_path(project, merge_request))
          expect(page).to have_link(merge_request.source_branch,
            href: project_commits_path(merge_request.source_project, merge_request.source_branch))
          expect(page).to have_link(merge_request.target_branch,
            href: project_commits_path(merge_request.target_project, merge_request.target_branch))
        end
      end

      context 'when target branch does not exist' do
        before do
          project.repository.rm_branch(user, merge_request.target_branch)
        end

        it 'does not link to the target branch commit path' do
          visit_pipeline

          within '.pipeline-info' do
            expect(page).not_to have_link(merge_request.target_branch)
            expect(page).to have_content(merge_request.target_branch)
          end
        end
      end

      context 'when source project is a forked project' do
        let(:source_project) { fork_project(project, user, repository: true) }

        before do
          visit project_pipeline_path(source_project, pipeline)
        end

        it 'shows the pipeline information', :sidekiq_might_not_need_inline do
          within '.pipeline-info' do
            expect(page).to have_content("#{pipeline.statuses.count} jobs " \
                                       "for !#{merge_request.iid} " \
                                       "with #{merge_request.source_branch} " \
                                       "into #{merge_request.target_branch}")
            expect(page).to have_link("!#{merge_request.iid}",
              href: project_merge_request_path(project, merge_request))
            expect(page).to have_link(merge_request.source_branch,
              href: project_commits_path(merge_request.source_project, merge_request.source_branch))
            expect(page).to have_link(merge_request.target_branch,
              href: project_commits_path(merge_request.target_project, merge_request.target_branch))
          end
        end
      end
    end
  end

  context 'when user does not have access to read jobs' do
    before do
      project.update!(public_builds: false)
    end

    describe 'GET /:project/-/pipelines/:id' do
      include_context 'pipeline builds'

      let_it_be(:project) { create(:project, :repository) }

      let(:pipeline) { create(:ci_pipeline, project: project, ref: 'master', sha: project.commit.id, user: user) }

      before do
        visit project_pipeline_path(project, pipeline)
      end

      it 'shows the pipeline graph' do
        expect(page).to have_selector('.js-pipeline-graph')
        expect(page).to have_content('build')
        expect(page).to have_content('test')
        expect(page).to have_content('deploy')
        expect(page).to have_content('Retry')
        expect(page).to have_content('Cancel running')
      end

      it 'does not link to job' do
        expect(page).not_to have_selector('.js-pipeline-graph-job-link')
      end
    end
  end

  context 'when a bridge job exists' do
    include_context 'pipeline builds'

    let(:project) { create(:project, :repository) }
    let(:downstream) { create(:project, :repository) }

    let(:pipeline) do
      create(:ci_pipeline, project: project,
                           ref: 'master',
                           sha: project.commit.id,
                           user: user)
    end

    let!(:bridge) do
      create(:ci_bridge, pipeline: pipeline,
                         name: 'cross-build',
                         user: user,
                         downstream: downstream)
    end

    describe 'GET /:project/-/pipelines/:id' do
      before do
        visit project_pipeline_path(project, pipeline)
      end

      it 'shows the pipeline with a bridge job' do
        expect(page).to have_selector('.js-pipeline-graph')
        expect(page).to have_content('cross-build')
      end

      context 'when a scheduled pipeline is created by a blocked user' do
        let(:project)  { create(:project, :repository) }

        let(:schedule) do
          create(:ci_pipeline_schedule,
            project: project,
            owner: project.first_owner,
            description: 'blocked user schedule'
          ).tap do |schedule|
            schedule.update_column(:next_run_at, 1.minute.ago)
          end
        end

        before do
          schedule.owner.block!
          PipelineScheduleWorker.new.perform
        end

        it 'displays the PipelineSchedule in an inactive state' do
          stub_feature_flags(pipeline_schedules_vue: false)

          visit project_pipeline_schedules_path(project)
          page.click_link('Inactive')

          expect(page).to have_selector('table.ci-table > tbody > tr > td', text: 'blocked user schedule')
        end

        it 'does not create a new Pipeline' do
          visit project_pipelines_path(project)

          expect(page).not_to have_selector('.ci-table')
          expect(schedule.last_pipeline).to be_nil
        end
      end
    end

    describe 'GET /:project/-/pipelines/:id/builds' do
      before do
        visit builds_project_pipeline_path(project, pipeline)
      end

      it 'shows a bridge job on a list' do
        expect(page).to have_content('cross-build')
        expect(page).to have_content(bridge.id)
      end
    end
  end

  context 'when build requires resource', :sidekiq_inline do
    let_it_be(:project) { create(:project, :repository) }

    let(:pipeline) { create(:ci_pipeline, project: project) }
    let(:resource_group) { create(:ci_resource_group, project: project) }

    let!(:test_job) do
      create(:ci_build, :pending, stage: 'test', name: 'test',
                                  stage_idx: 1, pipeline: pipeline, project: project)
    end

    let!(:deploy_job) do
      create(:ci_build, :created, stage: 'deploy', name: 'deploy',
                                  stage_idx: 2, pipeline: pipeline, project: project, resource_group: resource_group)
    end

    describe 'GET /:project/-/pipelines/:id' do
      subject { visit project_pipeline_path(project, pipeline) }

      it 'shows deploy job as created' do
        subject

        within('.js-pipeline-header-container') do
          expect(page).to have_content('pending')
        end

        within('.js-pipeline-graph') do
          within(all('[data-testid="stage-column"]')[0]) do
            expect(page).to have_content('test')
            expect(page).to have_css('.ci-status-icon-pending')
          end

          within(all('[data-testid="stage-column"]')[1]) do
            expect(page).to have_content('deploy')
            expect(page).to have_css('.ci-status-icon-created')
          end
        end
      end

      context 'when test job succeeded' do
        before do
          test_job.success!
        end

        it 'shows deploy job as pending' do
          subject

          within('.js-pipeline-header-container') do
            expect(page).to have_content('running')
          end

          within('.js-pipeline-graph') do
            within(all('[data-testid="stage-column"]')[0]) do
              expect(page).to have_content('test')
              expect(page).to have_css('.ci-status-icon-success')
            end

            within(all('[data-testid="stage-column"]')[1]) do
              expect(page).to have_content('deploy')
              expect(page).to have_css('.ci-status-icon-pending')
            end
          end
        end
      end

      context 'when test job succeeded but there are no available resources' do
        let(:another_job) { create(:ci_build, :running, project: project, resource_group: resource_group) }

        before do
          resource_group.assign_resource_to(another_job)
          test_job.success!
        end

        it 'shows deploy job as waiting for resource' do
          subject

          within('.js-pipeline-header-container') do
            expect(page).to have_content('waiting')
          end

          within('.js-pipeline-graph') do
            within(all('[data-testid="stage-column"]')[1]) do
              expect(page).to have_content('deploy')
              expect(page).to have_css('.ci-status-icon-waiting-for-resource')
            end
          end
        end

        context 'when resource is released from another job' do
          before do
            another_job.success!
          end

          it 'shows deploy job as pending' do
            subject

            within('.js-pipeline-header-container') do
              expect(page).to have_content('running')
            end

            within('.js-pipeline-graph') do
              within(all('[data-testid="stage-column"]')[1]) do
                expect(page).to have_content('deploy')
                expect(page).to have_css('.ci-status-icon-pending')
              end
            end
          end
        end

        context 'when deploy job is a bridge to trigger a downstream pipeline' do
          let!(:deploy_job) do
            create(:ci_bridge, :created,
              stage: 'deploy',
              name: 'deploy',
              stage_idx: 2,
              pipeline: pipeline,
              project: project,
              resource_group: resource_group
            )
          end

          it 'shows deploy job as waiting for resource' do
            subject

            within('.js-pipeline-header-container') do
              expect(page).to have_content('waiting')
            end

            within('.js-pipeline-graph') do
              within(all('[data-testid="stage-column"]')[1]) do
                expect(page).to have_content('deploy')
                expect(page).to have_css('.ci-status-icon-waiting-for-resource')
              end
            end
          end
        end
      end
    end
  end

  describe 'GET /:project/-/pipelines/:id/builds' do
    include_context 'pipeline builds'

    let_it_be(:project) { create(:project, :repository) }

    let(:pipeline) { create(:ci_pipeline, project: project, ref: 'master', sha: project.commit.id) }

    before do
      visit builds_project_pipeline_path(project, pipeline)
    end

    it 'shows a list of jobs' do
      expect(page).to have_content('Test')
      expect(page).to have_content(build_passed.id)
      expect(page).to have_content('Deploy')
      expect(page).to have_content(build_failed.id)
      expect(page).to have_content(build_running.id)
      expect(page).to have_content(build_external.id)
      expect(page).to have_content('Retry')
      expect(page).to have_content('Cancel running')
      expect(page).to have_button('Play')
    end

    context 'page tabs' do
      it 'shows Pipeline, Jobs and DAG tabs with link' do
        expect(page).to have_link('Pipeline')
        expect(page).to have_link('Jobs')
        expect(page).to have_link('Needs')
      end

      it 'shows counter in Jobs tab' do
        expect(page.find('[data-testid="builds-counter"]').text).to eq(pipeline.total_size.to_s)
      end
    end

    context 'retrying jobs' do
      it { expect(page).not_to have_content('retried') }

      context 'when retrying' do
        before do
          find('[data-testid="retry"]', match: :first).click
        end

        it 'does not show a "Retry" button', :sidekiq_might_not_need_inline do
          expect(page).not_to have_content('Retry')
        end
      end
    end

    context 'canceling jobs' do
      it { expect(page).not_to have_selector('.ci-canceled') }

      context 'when canceling' do
        before do
          click_on 'Cancel running'
        end

        it 'does not show a "Cancel running" button', :sidekiq_might_not_need_inline do
          expect(page).not_to have_content('Cancel running')
        end
      end
    end

    context 'playing manual job' do
      before do
        within '[data-testid="jobs-tab-table"]' do
          click_button('Play')

          wait_for_requests
        end
      end

      it { expect(build_manual.reload).to be_pending }
    end

    context 'when user unschedules a delayed job' do
      before do
        within '[data-testid="jobs-tab-table"]' do
          click_button('Unschedule')
        end
      end

      it 'unschedules the delayed job and shows play button as a manual job' do
        expect(page).to have_button('Play')
        expect(page).not_to have_button('Unschedule')
      end
    end
  end

  describe 'GET /:project/-/pipelines/:id/failures' do
    let(:pipeline) { create(:ci_pipeline, project: project, ref: 'master', sha: '1234') }
    let(:pipeline_failures_page) { failures_project_pipeline_path(project, pipeline) }
    let!(:failed_build) { create(:ci_build, :failed, pipeline: pipeline) }

    subject { visit pipeline_failures_page }

    context 'with failed build' do
      before do
        failed_build.trace.set('4 examples, 1 failure')
      end

      it 'lists failed builds' do
        subject

        expect(page).to have_content(failed_build.name)
        expect(page).to have_content(failed_build.stage_name)
      end

      it 'shows build failure logs' do
        subject

        expect(page).to have_content('4 examples, 1 failure')
      end

      it 'shows the failure reason' do
        subject

        expect(page).to have_content('There is an unknown failure, please try again')
      end

      context 'when user does not have permission to retry build' do
        it 'shows retry button for failed build' do
          subject

          page.within(find('[data-testid="tab-failures"]', match: :first)) do
            expect(page).not_to have_button('Retry')
          end
        end
      end

      context 'when user does have permission to retry build' do
        before do
          create(:protected_branch, :developers_can_merge,
                 name: pipeline.ref, project: project)
        end

        it 'shows retry button for failed build' do
          subject

          page.within(find('[data-testid="tab-failures"]', match: :first)) do
            expect(page).to have_button('Retry')
          end
        end
      end
    end

    context 'when missing build logs' do
      it 'lists failed builds' do
        subject

        expect(page).to have_content(failed_build.name)
        expect(page).to have_content(failed_build.stage_name)
      end

      it 'does not show log' do
        subject

        expect(page).to have_content('No job log')
      end
    end

    context 'without permission to access builds' do
      let(:role) { :guest }

      before do
        project.update!(public_builds: false)
      end

      context 'when accessing failed jobs page' do
        it 'renders a 404 page' do
          requests = inspect_requests { subject }

          expect(page).to have_title('Not Found')
          expect(requests.first.status_code).to eq(404)
        end
      end
    end

    context 'without failures' do
      before do
        failed_build.update!(status: :success)
      end

      it 'does not show the failure tab' do
        subject

        expect(page).not_to have_content('Failed Jobs')
      end

      it 'displays the pipeline graph' do
        subject

        expect(page).to have_current_path(pipeline_path(pipeline), ignore_query: true)
        expect(page).to have_selector('.js-pipeline-graph')
      end
    end
  end

  describe 'GET /:project/-/pipelines/:id/dag' do
    include_context 'pipeline builds'

    let_it_be(:project) { create(:project, :repository) }

    let(:pipeline) { create(:ci_pipeline, project: project, ref: 'master', sha: project.commit.id) }

    before do
      visit dag_project_pipeline_path(project, pipeline)
    end

    context 'page tabs' do
      it 'shows Pipeline, Jobs and DAG tabs with link' do
        expect(page).to have_link('Pipeline')
        expect(page).to have_link('Jobs')
        expect(page).to have_link('Needs')
      end
    end
  end

  context 'when user sees pipeline flags in a pipeline detail page' do
    let_it_be(:project) { create(:project, :repository) }

    context 'when pipeline is latest' do
      include_context 'pipeline builds'

      let(:pipeline) do
        create(:ci_pipeline,
               project: project,
               ref: 'master',
               sha: project.commit.id,
               user: user)
      end

      before do
        visit project_pipeline_path(project, pipeline)
      end

      it 'contains badge that indicates it is the latest build' do
        page.within(all('.well-segment')[1]) do
          expect(page).to have_content 'latest'
        end
      end
    end

    context 'when pipeline has configuration errors' do
      let(:pipeline) do
        create(:ci_pipeline,
               :invalid,
               project: project,
               ref: 'master',
               sha: project.commit.id,
               user: user)
      end

      before do
        visit project_pipeline_path(project, pipeline)
      end

      it 'contains badge that indicates errors' do
        page.within(all('.well-segment')[1]) do
          expect(page).to have_content 'yaml invalid'
        end
      end

      it 'contains badge with tooltip which contains error' do
        expect(pipeline).to have_yaml_errors

        page.within(all('.well-segment')[1]) do
          expect(page).to have_selector(
            %Q{span[title="#{pipeline.yaml_errors}"]})
        end
      end

      it 'contains badge that indicates failure reason' do
        expect(page).to have_content 'error'
      end

      it 'contains badge with tooltip which contains failure reason' do
        expect(pipeline.failure_reason?).to eq true

        page.within(all('.well-segment')[1]) do
          expect(page).to have_selector(
            %Q{span[title="#{pipeline.present.failure_reason}"]})
        end
      end

      it 'contains a pipeline header with title' do
        expect(page).to have_content "Pipeline ##{pipeline.id}"
      end
    end

    context 'when pipeline is stuck' do
      include_context 'pipeline builds'

      let(:pipeline) do
        create(:ci_pipeline,
               project: project,
               ref: 'master',
               sha: project.commit.id,
               user: user)
      end

      before do
        create(:ci_build, :pending, pipeline: pipeline)
        visit project_pipeline_path(project, pipeline)
      end

      it 'contains badge that indicates being stuck' do
        page.within(all('.well-segment')[1]) do
          expect(page).to have_content 'stuck'
        end
      end
    end

    context 'when pipeline uses auto devops' do
      include_context 'pipeline builds'

      let(:project) { create(:project, :repository, auto_devops_attributes: { enabled: true }) }
      let(:pipeline) do
        create(:ci_pipeline,
               :auto_devops_source,
               project: project,
               ref: 'master',
               sha: project.commit.id,
               user: user)
      end

      before do
        visit project_pipeline_path(project, pipeline)
      end

      it 'contains badge that indicates using auto devops' do
        page.within(all('.well-segment')[1]) do
          expect(page).to have_content 'Auto DevOps'
        end
      end
    end

    context 'when pipeline runs in a merge request context' do
      include_context 'pipeline builds'

      let(:pipeline) do
        create(:ci_pipeline,
               source: :merge_request_event,
               project: merge_request.source_project,
               ref: 'feature',
               sha: merge_request.diff_head_sha,
               user: user,
               merge_request: merge_request)
      end

      let(:merge_request) do
        create(:merge_request,
               source_project: project,
               source_branch: 'feature',
               target_project: project,
               target_branch: 'master')
      end

      before do
        visit project_pipeline_path(project, pipeline)
      end

      it 'contains badge that indicates detached merge request pipeline' do
        page.within(all('.well-segment')[1]) do
          expect(page).to have_content 'merge request'
        end
      end
    end
  end
end