summaryrefslogtreecommitdiff
path: root/spec/services/ci/provision_cluster_service_spec.rb
blob: 5ce5c788314c2e47adbea44121fcb2dfe310d8ec (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
require 'spec_helper'

describe Ci::ProvisionClusterService do
  describe '#execute' do
    let(:cluster) { create(:gcp_cluster) }
    let(:operation) { spy }

    shared_examples 'error' do
      it 'sets an error to cluster object' do
        described_class.new.execute(cluster)

        expect(cluster.reload).to be_errored
      end
    end

    context 'when suceeded to request provision' do
      before do
        allow_any_instance_of(GoogleApi::CloudPlatform::Client)
          .to receive(:projects_zones_clusters_create).and_return(operation)
      end

      context 'when operation status is RUNNING' do
        before do
          allow(operation).to receive(:status).and_return('RUNNING')
        end

        context 'when suceeded to parse gcp operation id' do
          before do
            allow_any_instance_of(GoogleApi::CloudPlatform::Client)
              .to receive(:parse_operation_id).and_return('operation-123')
          end

          context 'when cluster status is scheduled' do
            before do
              allow_any_instance_of(GoogleApi::CloudPlatform::Client)
                .to receive(:parse_operation_id).and_return('operation-123')
            end

            it 'schedules a worker for status minitoring' do
              expect(WaitForClusterCreationWorker).to receive(:perform_in)

              described_class.new.execute(cluster)
            end
          end

          context 'when cluster status is creating' do
            before do
              cluster.make_creating!
            end

            it_behaves_like 'error'
          end
        end

        context 'when failed to parse gcp operation id' do
          before do
            allow_any_instance_of(GoogleApi::CloudPlatform::Client)
              .to receive(:parse_operation_id).and_return(nil)
          end

          it_behaves_like 'error'
        end
      end

      context 'when operation status is others' do
        before do
          allow(operation).to receive(:status).and_return('others')
        end

        it_behaves_like 'error'
      end
    end

    context 'when failed to request provision' do
      let(:error) { Google::Apis::ServerError.new('a') }

      before do
        allow_any_instance_of(GoogleApi::CloudPlatform::Client)
          .to receive(:projects_zones_clusters_create).and_raise(error)
      end

      it_behaves_like 'error'
    end
  end
end