diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2020-03-06 06:08:30 +0000 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2020-03-06 06:08:30 +0000 |
commit | ab128cc125f9db0c3a1bd48845f90c3d61ef42c9 (patch) | |
tree | c3a1fc2bc7a6109b35597fd6e7a0a11089af0ec4 /spec/models/clusters | |
parent | a6011c3d70e0e8ac318ba6629183c44f8614c4df (diff) | |
download | gitlab-ce-ab128cc125f9db0c3a1bd48845f90c3d61ef42c9.tar.gz |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'spec/models/clusters')
-rw-r--r-- | spec/models/clusters/cluster_spec.rb | 76 |
1 files changed, 38 insertions, 38 deletions
diff --git a/spec/models/clusters/cluster_spec.rb b/spec/models/clusters/cluster_spec.rb index 23592cb0c70..8d0ede3d9bd 100644 --- a/spec/models/clusters/cluster_spec.rb +++ b/spec/models/clusters/cluster_spec.rb @@ -674,59 +674,59 @@ describe Clusters::Cluster, :use_clean_rails_memory_store_caching do end describe '#kubernetes_namespace_for' do - let(:cluster) { create(:cluster, :group) } - let(:environment) { create(:environment, last_deployable: build) } - let(:build) { create(:ci_build) } + subject { cluster.kubernetes_namespace_for(environment, deployable: build) } - subject { cluster.kubernetes_namespace_for(environment) } + let(:environment_name) { 'the-environment-name' } + let(:environment) { create(:environment, name: environment_name, project: cluster.project, last_deployable: build) } + let(:build) { create(:ci_build, environment: environment_name, project: cluster.project) } + let(:cluster) { create(:cluster, :project, managed: managed_cluster) } + let(:managed_cluster) { true } + let(:default_namespace) { Gitlab::Kubernetes::DefaultNamespace.new(cluster, project: cluster.project).from_environment_slug(environment.slug) } + let(:build_options) { {} } - before do - expect(Clusters::KubernetesNamespaceFinder).to receive(:new) - .with(cluster, project: environment.project, environment_name: environment.name) - .and_return(double(execute: persisted_namespace)) - - allow(build).to receive(:expanded_kubernetes_namespace) - .and_return(ci_configured_namespace) + it 'validates the project id' do + environment.project_id = build.project_id + 1 + expect { subject }.to raise_error ArgumentError, 'environment.project_id must match deployable.project_id' end - context 'no persisted namespace exists and namespace is not specified in CI template' do - let(:persisted_namespace) { nil } - let(:ci_configured_namespace) { nil } + context 'when environment has no last_deployable' do + let(:build) { nil } - let(:namespace_generator) { double } - let(:default_namespace) { 'a-default-namespace' } + it { is_expected.to eq default_namespace } + end + context 'when cluster is managed' do before do - expect(Gitlab::Kubernetes::DefaultNamespace).to receive(:new) - .with(cluster, project: environment.project) - .and_return(namespace_generator) - expect(namespace_generator).to receive(:from_environment_slug) - .with(environment.slug) - .and_return(default_namespace) + build.options = { environment: { kubernetes: { namespace: 'ci yaml namespace' } } } end - it { is_expected.to eq default_namespace } - end - - context 'persisted namespace exists' do - let(:persisted_namespace) { create(:cluster_kubernetes_namespace) } - let(:ci_configured_namespace) { nil } + it 'returns the cached namespace if present, ignoring CI config' do + cached_namespace = create(:cluster_kubernetes_namespace, cluster: cluster, environment: environment, namespace: 'the name', service_account_token: 'some token') + expect(subject).to eq cached_namespace.namespace + end - it { is_expected.to eq persisted_namespace.namespace } + it 'returns the default namespace when no cached namespace, ignoring CI config' do + expect(subject).to eq default_namespace + end end - context 'namespace is specified in CI template' do - let(:persisted_namespace) { nil } - let(:ci_configured_namespace) { 'ci-configured-namespace' } + context 'when cluster is not managed' do + let(:managed_cluster) { false } - it { is_expected.to eq ci_configured_namespace } - end + it 'returns the cached namespace if present, regardless of CI config' do + cached_namespace = create(:cluster_kubernetes_namespace, cluster: cluster, environment: environment, namespace: 'the name', service_account_token: 'some token') + build.options = { environment: { kubernetes: { namespace: 'ci yaml namespace' } } } + expect(subject).to eq cached_namespace.namespace + end - context 'persisted namespace exists and namespace is also specifed in CI template' do - let(:persisted_namespace) { create(:cluster_kubernetes_namespace) } - let(:ci_configured_namespace) { 'ci-configured-namespace' } + it 'returns the CI YAML namespace when configured' do + build.options = { environment: { kubernetes: { namespace: 'ci yaml namespace' } } } + expect(subject).to eq 'ci yaml namespace' + end - it { is_expected.to eq persisted_namespace.namespace } + it 'returns the default namespace when no namespace is configured' do + expect(subject).to eq default_namespace + end end end |