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

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

require 'spec_helper'

RSpec.describe Ci::CancelPipelineWorker, :aggregate_failures, feature_category: :continuous_integration do
  let!(:pipeline) { create(:ci_pipeline, :running) }

  describe '#perform' do
    subject(:perform) { described_class.new.perform(pipeline.id, pipeline.id) }

    let(:cancel_service) { instance_double(::Ci::CancelPipelineService) }

    it 'cancels the pipeline' do
      allow(::Ci::Pipeline).to receive(:find_by_id).twice.and_return(pipeline)
      expect(::Ci::CancelPipelineService)
        .to receive(:new)
        .with(
          pipeline: pipeline,
          current_user: nil,
          auto_canceled_by_pipeline: pipeline,
          cascade_to_children: false)
        .and_return(cancel_service)

      expect(cancel_service).to receive(:force_execute)

      perform
    end

    context 'if pipeline is deleted' do
      subject(:perform) { described_class.new.perform(non_existing_record_id, pipeline.id) }

      it 'does not error' do
        expect(::Ci::CancelPipelineService).not_to receive(:new)

        perform
      end
    end

    context 'when auto_canceled_by_pipeline is deleted' do
      subject(:perform) { described_class.new.perform(pipeline.id, non_existing_record_id) }

      it 'does not error' do
        expect(::Ci::CancelPipelineService)
          .to receive(:new)
          .with(
            pipeline: an_instance_of(::Ci::Pipeline),
            current_user: nil,
            auto_canceled_by_pipeline: nil,
            cascade_to_children: false)
          .and_call_original

        perform
      end
    end

    describe 'with builds and state transition side effects', :sidekiq_inline do
      let!(:build) { create(:ci_build, :running, pipeline: pipeline) }

      it_behaves_like 'an idempotent worker', :sidekiq_inline do
        let(:job_args) { [pipeline.id, pipeline.id] }

        it 'cancels the pipeline' do
          perform

          pipeline.reload

          expect(pipeline).to be_canceled
          expect(pipeline.builds.first).to be_canceled
          expect(pipeline.builds.first.auto_canceled_by_id).to eq pipeline.id
          expect(pipeline.auto_canceled_by_id).to eq pipeline.id
        end
      end
    end
  end
end