summaryrefslogtreecommitdiff
path: root/spec/services/clusters/aws/verify_provision_status_service_spec.rb
blob: b62b0875bf32d19c3f91324e8bf490a9314e6c76 (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
# frozen_string_literal: true

require 'spec_helper'

describe Clusters::Aws::VerifyProvisionStatusService do
  describe '#execute' do
    let(:provider) { create(:cluster_provider_aws) }

    let(:stack) { double(stack_status: stack_status, creation_time: creation_time) }
    let(:creation_time) { 1.minute.ago }

    subject { described_class.new.execute(provider) }

    before do
      allow(provider.api_client).to receive(:describe_stacks)
        .with(stack_name: provider.cluster.name)
        .and_return(double(stacks: [stack]))
    end

    shared_examples 'provision error' do |message|
      it "sets the status to :errored with an appropriate error message" do
        subject

        expect(provider).to be_errored
        expect(provider.status_reason).to include(message)
      end
    end

    context 'stack creation is still in progress' do
      let(:stack_status) { 'CREATE_IN_PROGRESS' }
      let(:verify_service) { double(execute: true) }

      it 'schedules a worker to check again later' do
        expect(WaitForClusterCreationWorker).to receive(:perform_in)
          .with(described_class::POLL_INTERVAL, provider.cluster_id)

        subject
      end

      context 'stack creation is taking too long' do
        let(:creation_time) { 1.hour.ago }

        include_examples 'provision error', 'Kubernetes cluster creation time exceeds timeout'
      end
    end

    context 'stack creation is complete' do
      let(:stack_status) { 'CREATE_COMPLETE' }
      let(:finalize_service) { double(execute: true) }

      it 'finalizes creation' do
        expect(Clusters::Aws::FinalizeCreationService).to receive(:new).and_return(finalize_service)
        expect(finalize_service).to receive(:execute).with(provider).once

        subject
      end
    end

    context 'stack creation failed' do
      let(:stack_status) { 'CREATE_FAILED' }

      include_examples 'provision error', 'Unexpected status'
    end

    context 'error communicating with CloudFormation API' do
      let(:stack_status) { 'CREATE_IN_PROGRESS' }

      before do
        allow(provider.api_client).to receive(:describe_stacks)
          .and_raise(Aws::CloudFormation::Errors::ServiceError.new(double, 'Error message'))
      end

      include_examples 'provision error', 'Amazon CloudFormation request failed'
    end
  end
end