gcp_spec.rb 4.89 KB
Newer Older
1 2 3 4 5 6 7
require 'spec_helper'

describe Clusters::Providers::Gcp do
  it { is_expected.to belong_to(:cluster) }
  it { is_expected.to validate_presence_of(:zone) }

  describe 'default_value_for' do
8
    let(:gcp) { build(:cluster_provider_gcp) }
9 10 11 12

    it "has default value" do
      expect(gcp.zone).to eq('us-central1-a')
      expect(gcp.num_nodes).to eq(3)
13
      expect(gcp.machine_type).to eq('n1-standard-2')
14 15 16 17 18 19 20
    end
  end

  describe 'validation' do
    subject { gcp.valid? }

    context 'when validates gcp_project_id' do
21
      let(:gcp) { build(:cluster_provider_gcp, gcp_project_id: gcp_project_id) }
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

      context 'when gcp_project_id is shorter than 1' do
        let(:gcp_project_id) { '' }

        it { is_expected.to be_falsey }
      end

      context 'when gcp_project_id is longer than 63' do
        let(:gcp_project_id) { 'a' * 64 }

        it { is_expected.to be_falsey }
      end

      context 'when gcp_project_id includes invalid character' do
        let(:gcp_project_id) { '!!!!!!' }

        it { is_expected.to be_falsey }
      end

      context 'when gcp_project_id is valid' do
        let(:gcp_project_id) { 'gcp-project-1' }

        it { is_expected.to be_truthy }
      end
    end

    context 'when validates num_nodes' do
49
      let(:gcp) { build(:cluster_provider_gcp, num_nodes: num_nodes) }
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

      context 'when num_nodes is string' do
        let(:num_nodes) { 'A3' }

        it { is_expected.to be_falsey }
      end

      context 'when num_nodes is nil' do
        let(:num_nodes) { nil }

        it { is_expected.to be_falsey }
      end

      context 'when num_nodes is smaller than 1' do
        let(:num_nodes) { 0 }

        it { is_expected.to be_falsey }
      end

      context 'when num_nodes is valid' do
        let(:num_nodes) { 3 }

        it { is_expected.to be_truthy }
      end
    end
  end

77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
  describe '#legacy_abac?' do
    let(:gcp) { build(:cluster_provider_gcp) }

    subject { gcp }

    it 'should default to true' do
      is_expected.to be_legacy_abac
    end

    context 'legacy_abac is set to false' do
      let(:gcp) { build(:cluster_provider_gcp, legacy_abac: false) }

      it 'is false' do
        is_expected.not_to be_legacy_abac
      end
    end
  end

95 96
  describe '#state_machine' do
    context 'when any => [:created]' do
97
      let(:gcp) { build(:cluster_provider_gcp, :creating) }
98 99 100 101 102 103 104 105 106 107 108 109 110

      before do
        gcp.make_created
      end

      it 'nullify access_token and operation_id' do
        expect(gcp.access_token).to be_nil
        expect(gcp.operation_id).to be_nil
        expect(gcp).to be_created
      end
    end

    context 'when any => [:creating]' do
111
      let(:gcp) { build(:cluster_provider_gcp) }
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136

      context 'when operation_id is present' do
        let(:operation_id) { 'operation-xxx' }

        before do
          gcp.make_creating(operation_id)
        end

        it 'sets operation_id' do
          expect(gcp.operation_id).to eq(operation_id)
          expect(gcp).to be_creating
        end
      end

      context 'when operation_id is nil' do
        let(:operation_id) { nil }

        it 'raises an error' do
          expect { gcp.make_creating(operation_id) }
            .to raise_error('operation_id is required')
        end
      end
    end

    context 'when any => [:errored]' do
137
      let(:gcp) { build(:cluster_provider_gcp, :creating) }
138 139 140 141 142 143 144 145 146 147 148 149
      let(:status_reason) { 'err msg' }

      it 'nullify access_token and operation_id' do
        gcp.make_errored(status_reason)

        expect(gcp.access_token).to be_nil
        expect(gcp.operation_id).to be_nil
        expect(gcp.status_reason).to eq(status_reason)
        expect(gcp).to be_errored
      end

      context 'when status_reason is nil' do
150
        let(:gcp) { build(:cluster_provider_gcp, :errored) }
151 152 153 154 155 156 157 158 159 160 161 162 163 164

        it 'does not set status_reason' do
          gcp.make_errored(nil)

          expect(gcp.status_reason).not_to be_nil
        end
      end
    end
  end

  describe '#on_creation?' do
    subject { gcp.on_creation? }

    context 'when status is creating' do
165
      let(:gcp) { create(:cluster_provider_gcp, :creating) }
166 167 168 169 170

      it { is_expected.to be_truthy }
    end

    context 'when status is created' do
171
      let(:gcp) { create(:cluster_provider_gcp, :created) }
172 173 174 175 176 177 178 179 180

      it { is_expected.to be_falsey }
    end
  end

  describe '#api_client' do
    subject { gcp.api_client }

    context 'when status is creating' do
181
      let(:gcp) { build(:cluster_provider_gcp, :creating) }
182 183 184 185 186 187 188 189

      it 'returns Cloud Platform API clinet' do
        expect(subject).to be_an_instance_of(GoogleApi::CloudPlatform::Client)
        expect(subject.access_token).to eq(gcp.access_token)
      end
    end

    context 'when status is created' do
190
      let(:gcp) { build(:cluster_provider_gcp, :created) }
191 192 193 194 195

      it { is_expected.to be_nil }
    end

    context 'when status is errored' do
196
      let(:gcp) { build(:cluster_provider_gcp, :errored) }
197 198 199 200 201

      it { is_expected.to be_nil }
    end
  end
end