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

abort_pipelines_service_spec.rb « ci « services « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: db25faff70fa15d15977db6fb56768e158631be9 (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Ci::AbortPipelinesService do
  let_it_be(:user) { create(:user) }
  let_it_be(:project) { create(:project, namespace: user.namespace) }

  let_it_be(:cancelable_pipeline, reload: true) { create(:ci_pipeline, :running, project: project, user: user) }
  let_it_be(:manual_pipeline, reload: true) { create(:ci_pipeline, status: :manual, project: project, user: user) }
  let_it_be(:other_users_pipeline, reload: true) { create(:ci_pipeline, :running, project: project, user: create(:user)) } # not this user's pipeline

  let_it_be(:cancelable_build, reload: true) { create(:ci_build, :running, pipeline: cancelable_pipeline) }
  let_it_be(:non_cancelable_build, reload: true) { create(:ci_build, :success, pipeline: cancelable_pipeline) }
  let_it_be(:cancelable_stage, reload: true) { create(:ci_stage_entity, name: 'stageA', status: :running, pipeline: cancelable_pipeline, project: project) }
  let_it_be(:non_cancelable_stage, reload: true) { create(:ci_stage_entity, name: 'stageB', status: :success, pipeline: cancelable_pipeline, project: project) }

  let_it_be(:manual_pipeline_cancelable_build, reload: true) { create(:ci_build, :created, pipeline: manual_pipeline) }
  let_it_be(:manual_pipeline_non_cancelable_build, reload: true) { create(:ci_build, :manual, pipeline: manual_pipeline) }
  let_it_be(:manual_pipeline_cancelable_stage, reload: true) { create(:ci_stage_entity, name: 'stageA', status: :created, pipeline: manual_pipeline, project: project) }
  let_it_be(:manual_pipeline_non_cancelable_stage, reload: true) { create(:ci_stage_entity, name: 'stageB', status: :success, pipeline: manual_pipeline, project: project) }

  describe '#execute' do
    def expect_correct_pipeline_cancellations
      expect(cancelable_pipeline.finished_at).not_to be_nil
      expect(cancelable_pipeline).to be_failed

      expect(manual_pipeline.finished_at).not_to be_nil
      expect(manual_pipeline).to be_failed
    end

    def expect_correct_stage_cancellations
      expect(cancelable_pipeline.stages - [non_cancelable_stage]).to all(be_failed)
      expect(manual_pipeline.stages - [manual_pipeline_non_cancelable_stage]).to all(be_failed)

      expect(non_cancelable_stage).not_to be_failed
      expect(manual_pipeline_non_cancelable_stage).not_to be_failed
    end

    def expect_correct_build_cancellations
      expect(cancelable_build).to be_failed
      expect(cancelable_build.finished_at).not_to be_nil

      expect(manual_pipeline_cancelable_build).to be_failed
      expect(manual_pipeline_cancelable_build.finished_at).not_to be_nil

      expect(non_cancelable_build).not_to be_failed
      expect(manual_pipeline_non_cancelable_build).not_to be_failed
    end

    def expect_correct_cancellations
      expect_correct_pipeline_cancellations
      expect_correct_stage_cancellations
      expect_correct_build_cancellations
    end

    context 'with project pipelines' do
      def abort_project_pipelines
        described_class.new.execute(project.all_pipelines, :project_deleted)
      end

      it 'fails all running pipelines and related jobs' do
        expect(abort_project_pipelines).to be_success

        expect_correct_cancellations

        expect(other_users_pipeline.status).to eq('failed')
        expect(other_users_pipeline.failure_reason).to eq('project_deleted')
        expect(other_users_pipeline.stages.map(&:status)).to all(eq('failed'))
      end

      it 'avoids N+1 queries' do
        control_count = ActiveRecord::QueryRecorder.new { abort_project_pipelines }.count

        pipelines = create_list(:ci_pipeline, 5, :running, project: project)
        create_list(:ci_build, 5, :running, pipeline: pipelines.first)

        expect { abort_project_pipelines }.not_to exceed_query_limit(control_count)
      end

      context 'with live build logs' do
        before do
          create(:ci_build_trace_chunk, build: cancelable_build)
        end

        it 'makes failed builds with stale trace visible' do
          expect(Ci::Build.with_stale_live_trace.count).to eq 0

          travel_to(2.days.ago) do
            abort_project_pipelines
          end

          expect(Ci::Build.with_stale_live_trace.count).to eq 1
        end
      end
    end

    context 'with user pipelines' do
      def abort_user_pipelines
        described_class.new.execute(user.pipelines, :user_blocked)
      end

      it 'fails all running pipelines and related jobs' do
        expect(abort_user_pipelines).to be_success

        expect_correct_cancellations

        expect(other_users_pipeline.status).not_to eq('failed')
      end

      it 'avoids N+1 queries' do
        control_count = ActiveRecord::QueryRecorder.new { abort_user_pipelines }.count

        pipelines = create_list(:ci_pipeline, 5, :running, project: project, user: user)
        create_list(:ci_build, 5, :running, pipeline: pipelines.first)

        expect { abort_user_pipelines }.not_to exceed_query_limit(control_count)
      end
    end
  end
end