summaryrefslogtreecommitdiff
path: root/spec/workers/wait_for_cluster_creation_worker_spec.rb
blob: 6d82981287f5a33487394aa349629fc22cdde6a8 (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
require 'spec_helper'

describe WaitForClusterCreationWorker do
  describe '#perform' do
    context 'when cluster exists' do
      let(:cluster) { create(:gcp_cluster) }
      let(:operation) { double }

      before do
        allow(operation).to receive(:status).and_return(status)
        allow(operation).to receive(:start_time).and_return(1.minute.ago)
        allow(operation).to receive(:status_message).and_return('error')
        allow_any_instance_of(Ci::FetchGcpOperationService).to receive(:execute).and_yield(operation)
      end

      context 'when operation status is RUNNING' do
        let(:status) { 'RUNNING' }

        it 'reschedules worker' do
          expect(described_class).to receive(:perform_in)

          described_class.new.perform(cluster.id)
        end

        context 'when operation timeout' do
          before do
            allow(operation).to receive(:start_time).and_return(30.minutes.ago)
          end

          it 'sets an error message on cluster' do
            described_class.new.perform(cluster.id)

            expect(cluster.reload).to be_errored
          end
        end
      end

      context 'when operation status is DONE' do
        let(:status) { 'DONE' }

        it 'finalizes cluster creation' do
          expect_any_instance_of(Ci::FinalizeClusterCreationService).to receive(:execute)

          described_class.new.perform(cluster.id)
        end
      end

      context 'when operation status is others' do
        let(:status) { 'others' }

        it 'sets an error message on cluster' do
          described_class.new.perform(cluster.id)

          expect(cluster.reload).to be_errored
        end
      end
    end

    context 'when cluster does not exist' do
      it 'does not provision a cluster' do
        expect_any_instance_of(Ci::FetchGcpOperationService).not_to receive(:execute)

        described_class.new.perform(1234)
      end
    end
  end
end