debian-mirror-gitlab/spec/models/clusters/kubernetes_namespace_spec.rb

115 lines
3.9 KiB
Ruby
Raw Normal View History

2018-12-13 13:39:08 +05:30
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Clusters::KubernetesNamespace, type: :model do
it { is_expected.to belong_to(:cluster_project) }
it { is_expected.to belong_to(:project) }
it { is_expected.to belong_to(:cluster) }
it { is_expected.to have_one(:platform_kubernetes) }
describe 'has_service_account_token' do
subject { described_class.has_service_account_token }
context 'namespace has service_account_token' do
let!(:namespace) { create(:cluster_kubernetes_namespace, :with_token) }
it { is_expected.to include(namespace) }
end
context 'namespace has no service_account_token' do
let!(:namespace) { create(:cluster_kubernetes_namespace) }
it { is_expected.not_to include(namespace) }
end
end
2019-12-21 20:55:43 +05:30
describe '.with_environment_name' do
2019-10-12 21:52:04 +05:30
let(:cluster) { create(:cluster, :group) }
2019-12-21 20:55:43 +05:30
let(:environment) { create(:environment, name: name) }
2018-12-13 13:39:08 +05:30
2019-12-21 20:55:43 +05:30
let(:name) { 'production' }
2018-12-13 13:39:08 +05:30
2019-12-21 20:55:43 +05:30
subject { described_class.with_environment_name(name) }
2018-12-13 13:39:08 +05:30
2019-10-12 21:52:04 +05:30
context 'there is no associated environment' do
let!(:namespace) { create(:cluster_kubernetes_namespace, cluster: cluster, project: environment.project) }
2018-12-13 13:39:08 +05:30
2019-10-12 21:52:04 +05:30
it { is_expected.to be_empty }
2018-12-13 13:39:08 +05:30
end
2019-10-12 21:52:04 +05:30
context 'there is an assicated environment' do
let!(:namespace) do
create(
:cluster_kubernetes_namespace,
cluster: cluster,
project: environment.project,
environment: environment
)
2018-12-13 13:39:08 +05:30
end
2019-12-21 20:55:43 +05:30
context 'with a matching name' do
2019-10-12 21:52:04 +05:30
it { is_expected.to eq [namespace] }
2018-12-13 13:39:08 +05:30
end
2019-12-21 20:55:43 +05:30
context 'without a matching name' do
let(:environment) { create(:environment, name: 'staging') }
2018-12-13 13:39:08 +05:30
2019-10-12 21:52:04 +05:30
it { is_expected.to be_empty }
2018-12-13 13:39:08 +05:30
end
end
2019-10-12 21:52:04 +05:30
end
2018-12-13 13:39:08 +05:30
2019-10-12 21:52:04 +05:30
describe 'namespace uniqueness validation' do
2020-11-24 15:15:51 +05:30
let_it_be(:cluster) { create(:cluster, :project, :provided_by_gcp) }
let(:kubernetes_namespace) { build(:cluster_kubernetes_namespace, cluster: cluster, namespace: 'my-namespace') }
2018-12-13 13:39:08 +05:30
2019-10-12 21:52:04 +05:30
subject { kubernetes_namespace }
2018-12-13 13:39:08 +05:30
2019-10-12 21:52:04 +05:30
context 'when cluster is using the namespace' do
before do
create(:cluster_kubernetes_namespace,
cluster: kubernetes_namespace.cluster,
environment: kubernetes_namespace.environment,
namespace: 'my-namespace')
2018-12-13 13:39:08 +05:30
end
2019-10-12 21:52:04 +05:30
it { is_expected.not_to be_valid }
end
context 'when cluster is not using the namespace' do
it { is_expected.to be_valid }
2018-12-13 13:39:08 +05:30
end
end
describe '#predefined_variables' do
let(:kubernetes_namespace) { create(:cluster_kubernetes_namespace, cluster: cluster, service_account_token: token) }
let(:cluster) { create(:cluster, :project, platform_kubernetes: platform) }
let(:platform) { create(:cluster_platform_kubernetes, api_url: api_url, ca_cert: ca_pem, token: token) }
let(:api_url) { 'https://kube.domain.com' }
2019-07-07 11:18:12 +05:30
let(:ca_pem) { File.read(Rails.root.join('spec/fixtures/clusters/sample_cert.pem')) }
2018-12-13 13:39:08 +05:30
let(:token) { 'token' }
let(:kubeconfig) do
config_file = expand_fixture_path('config/kubeconfig.yml')
config = YAML.safe_load(File.read(config_file))
config.dig('users', 0, 'user')['token'] = token
config.dig('contexts', 0, 'context')['namespace'] = kubernetes_namespace.namespace
config.dig('clusters', 0, 'cluster')['certificate-authority-data'] =
Base64.strict_encode64(ca_pem)
YAML.dump(config)
end
it 'sets the variables' do
expect(kubernetes_namespace.predefined_variables).to include(
{ key: 'KUBE_SERVICE_ACCOUNT', value: kubernetes_namespace.service_account_name, public: true },
{ key: 'KUBE_NAMESPACE', value: kubernetes_namespace.namespace, public: true },
2019-07-07 11:18:12 +05:30
{ key: 'KUBE_TOKEN', value: kubernetes_namespace.service_account_token, public: false, masked: true },
2018-12-13 13:39:08 +05:30
{ key: 'KUBECONFIG', value: kubeconfig, public: false, file: true }
)
end
end
end