2019-10-12 21:52:04 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
RSpec.describe Gitlab::Kubernetes::DefaultNamespace do
|
2019-10-12 21:52:04 +05:30
|
|
|
let(:generator) { described_class.new(cluster, project: environment.project) }
|
|
|
|
|
|
|
|
describe '#from_environment_name' do
|
|
|
|
let(:cluster) { create(:cluster) }
|
|
|
|
let(:environment) { create(:environment) }
|
|
|
|
|
|
|
|
subject { generator.from_environment_name(environment.name) }
|
|
|
|
|
|
|
|
it 'generates a slug and passes it to #from_environment_slug' do
|
|
|
|
expect(Gitlab::Slug::Environment).to receive(:new)
|
|
|
|
.with(environment.name)
|
|
|
|
.and_return(double(generate: environment.slug))
|
|
|
|
|
|
|
|
expect(generator).to receive(:from_environment_slug)
|
|
|
|
.with(environment.slug)
|
|
|
|
.and_return(:mock_namespace)
|
|
|
|
|
|
|
|
expect(subject).to eq :mock_namespace
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#from_environment_slug' do
|
|
|
|
let(:platform) { create(:cluster_platform_kubernetes, namespace: platform_namespace) }
|
|
|
|
let(:cluster) { create(:cluster, platform_kubernetes: platform) }
|
|
|
|
let(:project) { create(:project, path: "Path-With-Capitals") }
|
|
|
|
let(:environment) { create(:environment, project: project) }
|
|
|
|
|
|
|
|
subject { generator.from_environment_slug(environment.slug) }
|
|
|
|
|
2021-10-27 15:23:28 +05:30
|
|
|
shared_examples_for 'handles very long project paths' do
|
|
|
|
before do
|
|
|
|
allow(project).to receive(:path).and_return 'x' * 100
|
|
|
|
end
|
|
|
|
|
|
|
|
it { is_expected.to satisfy { |s| s.length <= 63 } }
|
|
|
|
end
|
|
|
|
|
2019-10-12 21:52:04 +05:30
|
|
|
context 'namespace per environment is enabled' do
|
|
|
|
context 'platform namespace is specified' do
|
|
|
|
let(:platform_namespace) { 'platform-namespace' }
|
|
|
|
|
|
|
|
it { is_expected.to eq "#{platform_namespace}-#{environment.slug}" }
|
|
|
|
|
|
|
|
context 'cluster is unmanaged' do
|
|
|
|
let(:cluster) { create(:cluster, :not_managed, platform_kubernetes: platform) }
|
|
|
|
|
|
|
|
it { is_expected.to eq platform_namespace }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'platform namespace is blank' do
|
|
|
|
let(:platform_namespace) { nil }
|
|
|
|
|
2021-10-27 15:23:28 +05:30
|
|
|
it 'constructs a namespace from the project and environment slug' do
|
|
|
|
expect(subject).to eq "path-with-capitals-#{project.id}-#{environment.slug}"
|
2019-10-12 21:52:04 +05:30
|
|
|
end
|
2021-10-27 15:23:28 +05:30
|
|
|
|
|
|
|
it_behaves_like 'handles very long project paths'
|
2019-10-12 21:52:04 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'namespace per environment is disabled' do
|
|
|
|
let(:cluster) { create(:cluster, :namespace_per_environment_disabled, platform_kubernetes: platform) }
|
|
|
|
|
|
|
|
context 'platform namespace is specified' do
|
|
|
|
let(:platform_namespace) { 'platform-namespace' }
|
|
|
|
|
|
|
|
it { is_expected.to eq platform_namespace }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'platform namespace is blank' do
|
|
|
|
let(:platform_namespace) { nil }
|
|
|
|
|
2021-10-27 15:23:28 +05:30
|
|
|
it 'constructs a namespace from just the project' do
|
|
|
|
expect(subject).to eq "path-with-capitals-#{project.id}"
|
2019-10-12 21:52:04 +05:30
|
|
|
end
|
2021-10-27 15:23:28 +05:30
|
|
|
|
|
|
|
it_behaves_like 'handles very long project paths'
|
2019-10-12 21:52:04 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|