Welcome to mirror list, hosted at ThFree Co, Russian Federation.

enable_cloudsql_service_spec.rb « google_cloud « services « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: f267f6d3bc2a24a578e62c6045958f8fbab1ea79 (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe GoogleCloud::EnableCloudsqlService do
  let_it_be(:project) { create(:project) }

  subject(:result) { described_class.new(project).execute }

  context 'when a project does not have any GCP_PROJECT_IDs configured' do
    it 'returns error' do
      message = 'No GCP projects found. Configure a service account or GCP_PROJECT_ID CI variable.'

      expect(result[:status]).to eq(:error)
      expect(result[:message]).to eq(message)
    end
  end

  context 'when a project has GCP_PROJECT_IDs configured' do
    before do
      project.variables.build(environment_scope: 'production', key: 'GCP_PROJECT_ID', value: 'prj-prod')
      project.variables.build(environment_scope: 'staging', key: 'GCP_PROJECT_ID', value: 'prj-staging')
      project.save!
    end

    after do
      project.variables.destroy_all # rubocop:disable Cop/DestroyAll
      project.save!
    end

    it 'enables cloudsql, compute and service networking Google APIs', :aggregate_failures do
      expect_next_instance_of(GoogleApi::CloudPlatform::Client) do |instance|
        expect(instance).to receive(:enable_cloud_sql_admin).with('prj-prod')
        expect(instance).to receive(:enable_compute).with('prj-prod')
        expect(instance).to receive(:enable_service_networking).with('prj-prod')
        expect(instance).to receive(:enable_cloud_sql_admin).with('prj-staging')
        expect(instance).to receive(:enable_compute).with('prj-staging')
        expect(instance).to receive(:enable_service_networking).with('prj-staging')
      end

      expect(result[:status]).to eq(:success)
    end

    context 'when Google APIs raise an error' do
      it 'returns error result' do
        allow_next_instance_of(GoogleApi::CloudPlatform::Client) do |instance|
          allow(instance).to receive(:enable_cloud_sql_admin).with('prj-prod')
          allow(instance).to receive(:enable_compute).with('prj-prod')
          allow(instance).to receive(:enable_service_networking).with('prj-prod')
          allow(instance).to receive(:enable_cloud_sql_admin).with('prj-staging')
          allow(instance).to receive(:enable_compute).with('prj-staging')
          allow(instance).to receive(:enable_service_networking).with('prj-staging')
                                                                .and_raise(Google::Apis::Error.new('error'))
        end

        expect(result[:status]).to eq(:error)
        expect(result[:message]).to eq('error')
      end
    end
  end
end