2018-10-16 20:03:59 +00:00
|
|
|
# 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) }
|
|
|
|
|
2018-11-13 10:27:21 +00:00
|
|
|
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
|
|
|
|
|
2018-10-16 20:03:59 +00:00
|
|
|
describe 'namespace uniqueness validation' do
|
|
|
|
let(:cluster_project) { create(:cluster_project) }
|
2018-11-02 15:46:15 +00:00
|
|
|
let(:kubernetes_namespace) { build(:cluster_kubernetes_namespace, namespace: 'my-namespace') }
|
2018-10-16 20:03:59 +00:00
|
|
|
|
2019-01-16 12:09:29 +00:00
|
|
|
subject { kubernetes_namespace }
|
2018-10-16 20:03:59 +00:00
|
|
|
|
|
|
|
context 'when cluster is using the namespace' do
|
|
|
|
before do
|
|
|
|
create(:cluster_kubernetes_namespace,
|
2018-11-02 15:46:15 +00:00
|
|
|
cluster: kubernetes_namespace.cluster,
|
|
|
|
namespace: 'my-namespace')
|
2018-10-16 20:03:59 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it { is_expected.not_to be_valid }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when cluster is not using the namespace' do
|
|
|
|
it { is_expected.to be_valid }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-11-14 17:55:38 +00:00
|
|
|
describe '#set_defaults' do
|
2018-11-02 15:46:15 +00:00
|
|
|
let(:kubernetes_namespace) { build(:cluster_kubernetes_namespace) }
|
|
|
|
let(:cluster) { kubernetes_namespace.cluster }
|
|
|
|
let(:platform) { kubernetes_namespace.platform_kubernetes }
|
2018-10-16 20:03:59 +00:00
|
|
|
|
2018-11-14 17:55:38 +00:00
|
|
|
subject { kubernetes_namespace.set_defaults }
|
2018-10-16 20:03:59 +00:00
|
|
|
|
2018-11-14 17:55:38 +00:00
|
|
|
describe '#namespace' do
|
2018-11-02 15:46:15 +00:00
|
|
|
before do
|
|
|
|
platform.update_column(:namespace, namespace)
|
|
|
|
end
|
2018-10-16 20:03:59 +00:00
|
|
|
|
|
|
|
context 'when platform has a namespace assigned' do
|
|
|
|
let(:namespace) { 'platform-namespace' }
|
|
|
|
|
2019-04-05 08:43:27 +00:00
|
|
|
it 'copies the namespace' do
|
2018-11-02 15:46:15 +00:00
|
|
|
subject
|
|
|
|
|
|
|
|
expect(kubernetes_namespace.namespace).to eq('platform-namespace')
|
2018-10-16 20:03:59 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when platform does not have namespace assigned' do
|
2018-11-02 15:46:15 +00:00
|
|
|
let(:project) { kubernetes_namespace.project }
|
2018-10-16 20:03:59 +00:00
|
|
|
let(:namespace) { nil }
|
2018-11-02 15:46:15 +00:00
|
|
|
let(:project_slug) { "#{project.path}-#{project.id}" }
|
2018-10-16 20:03:59 +00:00
|
|
|
|
2019-04-05 08:43:27 +00:00
|
|
|
it 'fallbacks to project namespace' do
|
2018-11-02 15:46:15 +00:00
|
|
|
subject
|
2018-10-16 20:03:59 +00:00
|
|
|
|
2018-11-02 15:46:15 +00:00
|
|
|
expect(kubernetes_namespace.namespace).to eq(project_slug)
|
2018-10-16 20:03:59 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-11-14 17:55:38 +00:00
|
|
|
describe '#service_account_name' do
|
2018-11-02 15:46:15 +00:00
|
|
|
let(:service_account_name) { "#{kubernetes_namespace.namespace}-service-account" }
|
2018-10-16 20:03:59 +00:00
|
|
|
|
2019-04-05 08:43:27 +00:00
|
|
|
it 'sets a service account name based on namespace' do
|
2018-11-02 15:46:15 +00:00
|
|
|
subject
|
|
|
|
|
|
|
|
expect(kubernetes_namespace.service_account_name).to eq(service_account_name)
|
2018-10-16 20:03:59 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2018-11-02 15:46:15 +00:00
|
|
|
|
|
|
|
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-02-07 04:56:08 +00:00
|
|
|
let(:ca_pem) { File.read(Rails.root.join('spec/fixtures/clusters/sample_cert.pem')) }
|
2018-11-02 15:46:15 +00:00
|
|
|
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-03-07 17:08:17 +00:00
|
|
|
{ key: 'KUBE_TOKEN', value: kubernetes_namespace.service_account_token, public: false, masked: true },
|
2018-11-02 15:46:15 +00:00
|
|
|
{ key: 'KUBECONFIG', value: kubeconfig, public: false, file: true }
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
2018-10-16 20:03:59 +00:00
|
|
|
end
|