summaryrefslogtreecommitdiff
path: root/spec/services/clusters/gcp/verify_provision_status_service_spec.rb
blob: 2ee2fa51f6305bac277c40b86ae53fc02ae76b9e (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
require 'spec_helper'

describe Clusters::Gcp::VerifyProvisionStatusService do
  include GoogleApi::CloudPlatformHelpers

  describe '#execute' do
    let(:provider) { create(:cluster_provider_gcp, :creating) }
    let(:gcp_project_id) { provider.gcp_project_id }
    let(:zone) { provider.zone }
    let(:operation_id) { provider.operation_id }

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

        described_class.new.execute(provider)
      end
    end

    shared_examples 'finalize_creation' do
      it 'schedules a worker for status minitoring' do
        expect_any_instance_of(Clusters::Gcp::FinalizeCreationService).to receive(:execute)

        described_class.new.execute(provider)
      end
    end

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

        expect(provider.reload).to be_errored
      end
    end

    context 'when operation status is RUNNING' do
      before do
        stub_cloud_platform_get_zone_operation(
          gcp_project_id, zone, operation_id,
          {
            "status": 'RUNNING',
            "startTime": 1.minute.ago.strftime("%FT%TZ")
          } )
      end

      it_behaves_like 'continue_creation'

      context 'when cluster creation time exceeds timeout' do
        before do
          stub_cloud_platform_get_zone_operation(
            gcp_project_id, zone, operation_id,
            {
              "status": 'RUNNING',
              "startTime": 30.minutes.ago.strftime("%FT%TZ")
            } )
        end

        it_behaves_like 'error'
      end
    end

    context 'when operation status is PENDING' do
      before do
        stub_cloud_platform_get_zone_operation(
          gcp_project_id, zone, operation_id,
          {
            "status": 'PENDING',
            "startTime": 1.minute.ago.strftime("%FT%TZ")
          } )
      end

      it_behaves_like 'continue_creation'
    end

    context 'when operation status is DONE' do
      before do
        stub_cloud_platform_get_zone_operation(
          gcp_project_id, zone, operation_id,
          {
            "status": 'DONE'
          } )
      end

      it_behaves_like 'finalize_creation'
    end

    context 'when operation status is unexpected' do
      before do
        stub_cloud_platform_get_zone_operation(
          gcp_project_id, zone, operation_id,
          {
            "status": 'unexpected'
          } )
      end

      it_behaves_like 'error'
    end

    context 'when failed to get operation status' do
      before do
        stub_cloud_platform_get_zone_operation_error(gcp_project_id, zone, operation_id)
      end

      it_behaves_like 'error'
    end
  end
end