debian-mirror-gitlab/spec/services/ci/generate_kubeconfig_service_spec.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

102 lines
3.2 KiB
Ruby
Raw Normal View History

2021-12-11 22:18:48 +05:30
# frozen_string_literal: true
require 'spec_helper'
2023-06-20 00:43:36 +05:30
RSpec.describe Ci::GenerateKubeconfigService, feature_category: :deployment_management do
2021-12-11 22:18:48 +05:30
describe '#execute' do
2023-03-04 22:38:38 +05:30
let_it_be(:group) { create(:group) }
let_it_be(:project) { create(:project, group: group) }
let_it_be(:pipeline) { create(:ci_empty_pipeline, project: project) }
let_it_be(:build) { create(:ci_build, project: project, pipeline: pipeline) }
2021-12-11 22:18:48 +05:30
2023-03-04 22:38:38 +05:30
let_it_be(:agent_project) { create(:project, group: group, name: 'project-containing-agent-config') }
2021-12-11 22:18:48 +05:30
2023-03-04 22:38:38 +05:30
let_it_be(:project_agent_authorization) do
agent = create(:cluster_agent, project: agent_project)
2023-06-20 00:43:36 +05:30
create(:agent_ci_access_project_authorization, agent: agent, project: project)
2023-03-04 22:38:38 +05:30
end
let_it_be(:group_agent_authorization) do
agent = create(:cluster_agent, project: agent_project)
2023-06-20 00:43:36 +05:30
create(:agent_ci_access_group_authorization, agent: agent, group: group)
2023-03-04 22:38:38 +05:30
end
let(:template) do
instance_double(
Gitlab::Kubernetes::Kubeconfig::Template,
add_cluster: nil,
add_user: nil,
add_context: nil
)
end
let(:agent_authorizations) { [project_agent_authorization, group_agent_authorization] }
let(:filter_service) do
instance_double(
2023-06-20 00:43:36 +05:30
::Clusters::Agents::Authorizations::CiAccess::FilterService,
2023-03-04 22:38:38 +05:30
execute: agent_authorizations
)
end
subject(:execute) { described_class.new(pipeline, token: build.token, environment: nil).execute }
2021-12-11 22:18:48 +05:30
before do
2023-03-04 22:38:38 +05:30
allow(Gitlab::Kubernetes::Kubeconfig::Template).to receive(:new).and_return(template)
2023-06-20 00:43:36 +05:30
allow(::Clusters::Agents::Authorizations::CiAccess::FilterService).to receive(:new).and_return(filter_service)
2023-03-04 22:38:38 +05:30
end
it 'returns a Kubeconfig Template' do
expect(execute).to eq(template)
2021-12-11 22:18:48 +05:30
end
2023-03-04 22:38:38 +05:30
it 'adds a cluster' do
2021-12-11 22:18:48 +05:30
expect(template).to receive(:add_cluster).with(
name: 'gitlab',
url: Gitlab::Kas.tunnel_url
).once
2023-03-04 22:38:38 +05:30
execute
end
2021-12-11 22:18:48 +05:30
2023-03-04 22:38:38 +05:30
it "filters the pipeline's agents by `nil` environment" do
2023-06-20 00:43:36 +05:30
expect(::Clusters::Agents::Authorizations::CiAccess::FilterService).to receive(:new).with(
2023-03-04 22:38:38 +05:30
pipeline.cluster_agent_authorizations,
environment: nil
2021-12-11 22:18:48 +05:30
)
2023-03-04 22:38:38 +05:30
execute
end
it 'adds user and context for all eligible agents', :aggregate_failures do
agent_authorizations.each do |authorization|
expect(template).to receive(:add_user).with(
name: "agent:#{authorization.agent.id}",
token: "ci:#{authorization.agent.id}:#{build.token}"
)
expect(template).to receive(:add_context).with(
name: "#{agent_project.full_path}:#{authorization.agent.name}",
namespace: 'production',
cluster: 'gitlab',
user: "agent:#{authorization.agent.id}"
)
end
execute
end
context 'when environment is specified' do
subject(:execute) { described_class.new(pipeline, token: build.token, environment: 'production').execute }
it "filters the pipeline's agents by the specified environment" do
2023-06-20 00:43:36 +05:30
expect(::Clusters::Agents::Authorizations::CiAccess::FilterService).to receive(:new).with(
2023-03-04 22:38:38 +05:30
pipeline.cluster_agent_authorizations,
environment: 'production'
)
execute
end
2021-12-11 22:18:48 +05:30
end
end
end