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.1 KiB
Ruby
Raw Normal View History

2021-12-11 22:18:48 +05:30
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Ci::GenerateKubeconfigService do
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)
create(:agent_project_authorization, agent: agent, project: project)
end
let_it_be(:group_agent_authorization) do
agent = create(:cluster_agent, project: agent_project)
create(:agent_group_authorization, agent: agent, group: group)
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(
::Clusters::Agents::FilterAuthorizationsService,
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)
allow(::Clusters::Agents::FilterAuthorizationsService).to receive(:new).and_return(filter_service)
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
expect(::Clusters::Agents::FilterAuthorizationsService).to receive(:new).with(
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
expect(::Clusters::Agents::FilterAuthorizationsService).to receive(:new).with(
pipeline.cluster_agent_authorizations,
environment: 'production'
)
execute
end
2021-12-11 22:18:48 +05:30
end
end
end