2021-12-11 22:18:48 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
RSpec.describe GoogleCloud::ServiceAccountsService do
|
|
|
|
let(:service) { described_class.new(project) }
|
|
|
|
|
|
|
|
describe 'find_for_project' do
|
2022-01-26 12:08:38 +05:30
|
|
|
let_it_be(:project) { create(:project) }
|
|
|
|
|
2021-12-11 22:18:48 +05:30
|
|
|
context 'when a project does not have GCP service account vars' do
|
|
|
|
before do
|
|
|
|
project.variables.build(key: 'blah', value: 'foo', environment_scope: 'world')
|
|
|
|
project.save!
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns an empty list' do
|
|
|
|
expect(service.find_for_project.length).to eq(0)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when a project has GCP service account ci vars' do
|
|
|
|
before do
|
2022-01-26 12:08:38 +05:30
|
|
|
project.variables.build(protected: true, environment_scope: '*', key: 'GCP_PROJECT_ID', value: 'prj1')
|
|
|
|
project.variables.build(protected: true, environment_scope: '*', key: 'GCP_SERVICE_ACCOUNT_KEY', value: 'mock')
|
|
|
|
project.variables.build(protected: true, environment_scope: 'staging', key: 'GCP_PROJECT_ID', value: 'prj2')
|
|
|
|
project.variables.build(protected: true, environment_scope: 'staging', key: 'GCP_SERVICE_ACCOUNT', value: 'mock')
|
|
|
|
project.variables.build(protected: true, environment_scope: 'production', key: 'GCP_PROJECT_ID', value: 'prj3')
|
|
|
|
project.variables.build(protected: true, environment_scope: 'production', key: 'GCP_SERVICE_ACCOUNT', value: 'mock')
|
|
|
|
project.variables.build(protected: true, environment_scope: 'production', key: 'GCP_SERVICE_ACCOUNT_KEY', value: 'mock')
|
2021-12-11 22:18:48 +05:30
|
|
|
project.save!
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns a list of service accounts' do
|
|
|
|
list = service.find_for_project
|
|
|
|
|
|
|
|
aggregate_failures 'testing list of service accounts' do
|
|
|
|
expect(list.length).to eq(3)
|
|
|
|
|
2022-05-07 20:08:51 +05:30
|
|
|
expect(list.first[:ref]).to eq('*')
|
2021-12-11 22:18:48 +05:30
|
|
|
expect(list.first[:gcp_project]).to eq('prj1')
|
|
|
|
expect(list.first[:service_account_exists]).to eq(false)
|
|
|
|
expect(list.first[:service_account_key_exists]).to eq(true)
|
|
|
|
|
2022-05-07 20:08:51 +05:30
|
|
|
expect(list.second[:ref]).to eq('staging')
|
2021-12-11 22:18:48 +05:30
|
|
|
expect(list.second[:gcp_project]).to eq('prj2')
|
|
|
|
expect(list.second[:service_account_exists]).to eq(true)
|
|
|
|
expect(list.second[:service_account_key_exists]).to eq(false)
|
|
|
|
|
2022-05-07 20:08:51 +05:30
|
|
|
expect(list.third[:ref]).to eq('production')
|
2021-12-11 22:18:48 +05:30
|
|
|
expect(list.third[:gcp_project]).to eq('prj3')
|
|
|
|
expect(list.third[:service_account_exists]).to eq(true)
|
|
|
|
expect(list.third[:service_account_key_exists]).to eq(true)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2022-01-26 12:08:38 +05:30
|
|
|
|
|
|
|
describe 'add_for_project' do
|
|
|
|
let_it_be(:project) { create(:project) }
|
|
|
|
|
|
|
|
it 'saves GCP creds as project CI vars' do
|
2022-03-02 08:16:31 +05:30
|
|
|
service.add_for_project('env_1', 'gcp_prj_id_1', 'srv_acc_1', 'srv_acc_key_1', true)
|
|
|
|
service.add_for_project('env_2', 'gcp_prj_id_2', 'srv_acc_2', 'srv_acc_key_2', false)
|
2022-01-26 12:08:38 +05:30
|
|
|
|
|
|
|
list = service.find_for_project
|
|
|
|
|
|
|
|
aggregate_failures 'testing list of service accounts' do
|
|
|
|
expect(list.length).to eq(2)
|
|
|
|
|
2022-05-07 20:08:51 +05:30
|
|
|
expect(list.first[:ref]).to eq('env_1')
|
2022-01-26 12:08:38 +05:30
|
|
|
expect(list.first[:gcp_project]).to eq('gcp_prj_id_1')
|
|
|
|
expect(list.first[:service_account_exists]).to eq(true)
|
|
|
|
expect(list.first[:service_account_key_exists]).to eq(true)
|
|
|
|
|
2022-05-07 20:08:51 +05:30
|
|
|
expect(list.second[:ref]).to eq('env_2')
|
2022-01-26 12:08:38 +05:30
|
|
|
expect(list.second[:gcp_project]).to eq('gcp_prj_id_2')
|
|
|
|
expect(list.second[:service_account_exists]).to eq(true)
|
|
|
|
expect(list.second[:service_account_key_exists]).to eq(true)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'replaces previously stored CI vars with new CI vars' do
|
2022-03-02 08:16:31 +05:30
|
|
|
service.add_for_project('env_1', 'new_project', 'srv_acc_1', 'srv_acc_key_1', false)
|
2022-01-26 12:08:38 +05:30
|
|
|
|
|
|
|
list = service.find_for_project
|
|
|
|
|
|
|
|
aggregate_failures 'testing list of service accounts' do
|
|
|
|
expect(list.length).to eq(2)
|
|
|
|
|
|
|
|
# asserting that the first service account is replaced
|
2022-05-07 20:08:51 +05:30
|
|
|
expect(list.first[:ref]).to eq('env_1')
|
2022-01-26 12:08:38 +05:30
|
|
|
expect(list.first[:gcp_project]).to eq('new_project')
|
|
|
|
expect(list.first[:service_account_exists]).to eq(true)
|
|
|
|
expect(list.first[:service_account_key_exists]).to eq(true)
|
|
|
|
|
2022-05-07 20:08:51 +05:30
|
|
|
expect(list.second[:ref]).to eq('env_2')
|
2022-01-26 12:08:38 +05:30
|
|
|
expect(list.second[:gcp_project]).to eq('gcp_prj_id_2')
|
|
|
|
expect(list.second[:service_account_exists]).to eq(true)
|
|
|
|
expect(list.second[:service_account_key_exists]).to eq(true)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-03-02 08:16:31 +05:30
|
|
|
it 'underlying project CI vars must be protected as per value' do
|
|
|
|
service.add_for_project('env_1', 'gcp_prj_id_1', 'srv_acc_1', 'srv_acc_key_1', true)
|
|
|
|
service.add_for_project('env_2', 'gcp_prj_id_2', 'srv_acc_2', 'srv_acc_key_2', false)
|
|
|
|
|
|
|
|
expect(project.variables[0].protected).to eq(true)
|
|
|
|
expect(project.variables[1].protected).to eq(true)
|
|
|
|
expect(project.variables[2].protected).to eq(true)
|
|
|
|
expect(project.variables[3].protected).to eq(false)
|
|
|
|
expect(project.variables[4].protected).to eq(false)
|
|
|
|
expect(project.variables[5].protected).to eq(false)
|
2022-01-26 12:08:38 +05:30
|
|
|
end
|
|
|
|
end
|
2021-12-11 22:18:48 +05:30
|
|
|
end
|