debian-mirror-gitlab/spec/models/clusters/platforms/kubernetes_spec.rb

458 lines
15 KiB
Ruby
Raw Normal View History

2019-07-07 11:18:12 +05:30
# frozen_string_literal: true
2018-03-17 18:26:18 +05:30
require 'spec_helper'
2020-07-28 23:09:34 +05:30
RSpec.describe Clusters::Platforms::Kubernetes do
2018-03-17 18:26:18 +05:30
include KubernetesHelpers
it { is_expected.to belong_to(:cluster) }
it { is_expected.to be_kind_of(Gitlab::Kubernetes) }
it { is_expected.to respond_to :ca_pem }
2018-12-13 13:39:08 +05:30
it { is_expected.to validate_exclusion_of(:namespace).in_array(%w(gitlab-managed-apps)) }
it { is_expected.to validate_presence_of(:api_url) }
it { is_expected.to validate_presence_of(:token) }
it { is_expected.to delegate_method(:enabled?).to(:cluster) }
2019-07-07 11:18:12 +05:30
it { is_expected.to delegate_method(:provided_by_user?).to(:cluster) }
2018-12-13 13:39:08 +05:30
2019-02-15 15:39:39 +05:30
it_behaves_like 'having unique enum values'
2018-03-17 18:26:18 +05:30
describe 'before_validation' do
2019-12-21 20:55:43 +05:30
let(:kubernetes) { create(:cluster_platform_kubernetes, :configured, namespace: namespace) }
2018-03-17 18:26:18 +05:30
context 'when namespace includes upper case' do
let(:namespace) { 'ABC' }
it 'converts to lower case' do
expect(kubernetes.namespace).to eq('abc')
end
end
2019-12-21 20:55:43 +05:30
context 'when namespace is blank' do
let(:namespace) { '' }
it 'nullifies the namespace' do
expect(kubernetes.namespace).to be_nil
end
end
2018-03-17 18:26:18 +05:30
end
describe 'validation' do
subject { kubernetes.valid? }
context 'when validates namespace' do
let(:kubernetes) { build(:cluster_platform_kubernetes, :configured, namespace: namespace) }
2019-12-21 20:55:43 +05:30
context 'when namespace is nil' do
let(:namespace) { nil }
2018-03-17 18:26:18 +05:30
it { is_expected.to be_truthy }
end
context 'when namespace is longer than 63' do
let(:namespace) { 'a' * 64 }
it { is_expected.to be_falsey }
end
context 'when namespace includes invalid character' do
let(:namespace) { '!!!!!!' }
it { is_expected.to be_falsey }
end
context 'when namespace is vaild' do
let(:namespace) { 'namespace-123' }
it { is_expected.to be_truthy }
end
2019-02-15 15:39:39 +05:30
context 'for group cluster' do
let(:namespace) { 'namespace-123' }
let(:cluster) { build(:cluster, :group, :provided_by_user) }
let(:kubernetes) { cluster.platform_kubernetes }
before do
kubernetes.namespace = namespace
end
it { is_expected.to be_falsey }
end
2018-03-17 18:26:18 +05:30
end
context 'when validates api_url' do
let(:kubernetes) { build(:cluster_platform_kubernetes, :configured) }
before do
kubernetes.api_url = api_url
end
context 'when api_url is invalid url' do
let(:api_url) { '!!!!!!' }
it { expect(kubernetes.save).to be_falsey }
end
context 'when api_url is nil' do
let(:api_url) { nil }
it { expect(kubernetes.save).to be_falsey }
end
context 'when api_url is valid url' do
let(:api_url) { 'https://111.111.111.111' }
it { expect(kubernetes.save).to be_truthy }
end
2019-03-13 22:55:13 +05:30
context 'when api_url is localhost' do
let(:api_url) { 'http://localhost:22' }
it { expect(kubernetes.save).to be_falsey }
context 'Application settings allows local requests' do
before do
allow(ApplicationSetting)
.to receive(:current)
2019-10-12 21:52:04 +05:30
.and_return(ApplicationSetting.build_from_defaults(allow_local_requests_from_web_hooks_and_services: true))
2019-03-13 22:55:13 +05:30
end
it { expect(kubernetes.save).to be_truthy }
end
end
2018-03-17 18:26:18 +05:30
end
context 'when validates token' do
let(:kubernetes) { build(:cluster_platform_kubernetes, :configured) }
before do
kubernetes.token = token
end
context 'when token is nil' do
let(:token) { nil }
it { expect(kubernetes.save).to be_falsey }
end
end
2018-12-13 13:39:08 +05:30
2019-07-07 11:18:12 +05:30
context 'ca_cert' do
let(:kubernetes) { build(:cluster_platform_kubernetes, ca_pem: ca_pem) }
context 'with a valid certificate' do
let(:ca_pem) { File.read(Rails.root.join('spec/fixtures/clusters/sample_cert.pem')) }
it { is_expected.to be_truthy }
end
context 'with an invalid certificate' do
let(:ca_pem) { "invalid" }
it { is_expected.to be_falsey }
context 'but the certificate is not being updated' do
before do
allow(kubernetes).to receive(:ca_cert_changed?).and_return(false)
end
it { is_expected.to be_truthy }
end
end
context 'with no certificate' do
let(:ca_pem) { "" }
it { is_expected.to be_truthy }
end
end
2018-12-13 13:39:08 +05:30
describe 'when using reserved namespaces' do
subject { build(:cluster_platform_kubernetes, namespace: namespace) }
context 'when no namespace is manually assigned' do
let(:namespace) { nil }
it { is_expected.to be_valid }
end
context 'when no reserved namespace is assigned' do
let(:namespace) { 'my-namespace' }
it { is_expected.to be_valid }
end
context 'when reserved namespace is assigned' do
let(:namespace) { 'gitlab-managed-apps' }
it { is_expected.not_to be_valid }
end
end
2018-03-17 18:26:18 +05:30
end
2018-11-20 20:47:30 +05:30
describe '#kubeclient' do
2018-12-13 13:39:08 +05:30
let(:cluster) { create(:cluster, :project) }
let(:kubernetes) { build(:cluster_platform_kubernetes, :configured, namespace: 'a-namespace', cluster: cluster) }
2018-11-20 20:47:30 +05:30
subject { kubernetes.kubeclient }
2018-12-13 13:39:08 +05:30
before do
create(:cluster_kubernetes_namespace,
cluster: kubernetes.cluster,
cluster_project: kubernetes.cluster.cluster_project,
project: kubernetes.cluster.cluster_project.project)
end
2018-11-20 20:47:30 +05:30
it { is_expected.to be_an_instance_of(Gitlab::Kubernetes::KubeClient) }
2020-07-28 23:09:34 +05:30
context 'ca_pem is a single certificate' do
let(:ca_pem) { File.read(Rails.root.join('spec/fixtures/clusters/ca_certificate.pem')) }
let(:kubernetes) do
build(:cluster_platform_kubernetes,
:configured,
namespace: 'a-namespace',
cluster: cluster,
ca_pem: ca_pem)
end
it 'adds it to cert_store' do
cert = OpenSSL::X509::Certificate.new(ca_pem)
cert_store = kubernetes.kubeclient.kubeclient_options[:ssl_options][:cert_store]
expect(cert_store.verify(cert)).to be true
end
end
context 'ca_pem is a chain' do
let(:cert_chain) { File.read(Rails.root.join('spec/fixtures/clusters/chain_certificates.pem')) }
let(:kubernetes) do
build(:cluster_platform_kubernetes,
:configured,
namespace: 'a-namespace',
cluster: cluster,
ca_pem: cert_chain)
end
it 'includes chain of certificates' do
cert1_file = File.read(Rails.root.join('spec/fixtures/clusters/root_certificate.pem'))
cert1 = OpenSSL::X509::Certificate.new(cert1_file)
cert2_file = File.read(Rails.root.join('spec/fixtures/clusters/intermediate_certificate.pem'))
cert2 = OpenSSL::X509::Certificate.new(cert2_file)
cert3_file = File.read(Rails.root.join('spec/fixtures/clusters/ca_certificate.pem'))
cert3 = OpenSSL::X509::Certificate.new(cert3_file)
cert_store = kubernetes.kubeclient.kubeclient_options[:ssl_options][:cert_store]
expect(cert_store.verify(cert1)).to be true
expect(cert_store.verify(cert2)).to be true
expect(cert_store.verify(cert3)).to be true
end
end
2018-11-20 20:47:30 +05:30
end
describe '#rbac?' do
let(:kubernetes) { build(:cluster_platform_kubernetes, :configured) }
2019-02-15 15:39:39 +05:30
subject { kubernetes.rbac? }
2018-11-20 20:47:30 +05:30
2019-02-15 15:39:39 +05:30
it { is_expected.to be_truthy }
2018-11-20 20:47:30 +05:30
end
2019-10-12 21:52:04 +05:30
describe '#predefined_variables' do
let(:project) { create(:project) }
let(:cluster) { create(:cluster, :group, platform_kubernetes: platform) }
let(:platform) { create(:cluster_platform_kubernetes) }
let(:persisted_namespace) { create(:cluster_kubernetes_namespace, project: project, cluster: cluster) }
2019-09-04 21:01:54 +05:30
2019-10-12 21:52:04 +05:30
let(:environment_name) { 'env/production' }
let(:environment_slug) { Gitlab::Slug::Environment.new(environment_name).generate }
2018-03-17 18:26:18 +05:30
2019-10-12 21:52:04 +05:30
subject { platform.predefined_variables(project: project, environment_name: environment_name) }
2018-03-17 18:26:18 +05:30
2019-10-12 21:52:04 +05:30
before do
allow(Clusters::KubernetesNamespaceFinder).to receive(:new)
2019-12-21 20:55:43 +05:30
.with(cluster, project: project, environment_name: environment_name)
2019-10-12 21:52:04 +05:30
.and_return(double(execute: persisted_namespace))
2018-03-17 18:26:18 +05:30
end
2019-10-12 21:52:04 +05:30
it { is_expected.to include(key: 'KUBE_URL', value: platform.api_url, public: true) }
2018-12-13 13:39:08 +05:30
2019-10-12 21:52:04 +05:30
context 'platform has a CA certificate' do
let(:ca_pem) { File.read(Rails.root.join('spec/fixtures/clusters/sample_cert.pem')) }
let(:platform) { create(:cluster_platform_kubernetes, ca_cert: ca_pem) }
2018-12-13 13:39:08 +05:30
2019-10-12 21:52:04 +05:30
it { is_expected.to include(key: 'KUBE_CA_PEM', value: ca_pem, public: true) }
it { is_expected.to include(key: 'KUBE_CA_PEM_FILE', value: ca_pem, public: true, file: true) }
2018-12-13 13:39:08 +05:30
end
2019-12-21 20:55:43 +05:30
context 'cluster is managed by project' do
before do
allow(Gitlab::Kubernetes::DefaultNamespace).to receive(:new)
.with(cluster, project: project).and_return(double(from_environment_name: namespace))
allow(platform).to receive(:kubeconfig).with(namespace).and_return('kubeconfig')
end
let(:cluster) { create(:cluster, :group, platform_kubernetes: platform, management_project: project) }
let(:namespace) { 'kubernetes-namespace' }
let(:kubeconfig) { 'kubeconfig' }
it { is_expected.to include(key: 'KUBE_TOKEN', value: platform.token, public: false, masked: true) }
it { is_expected.to include(key: 'KUBE_NAMESPACE', value: namespace) }
it { is_expected.to include(key: 'KUBECONFIG', value: kubeconfig, public: false, file: true) }
end
2019-10-12 21:52:04 +05:30
context 'kubernetes namespace exists' do
let(:variable) { Hash(key: :fake_key, value: 'fake_value') }
let(:namespace_variables) { Gitlab::Ci::Variables::Collection.new([variable]) }
2018-12-13 13:39:08 +05:30
2019-10-12 21:52:04 +05:30
before do
expect(persisted_namespace).to receive(:predefined_variables).and_return(namespace_variables)
2018-12-13 13:39:08 +05:30
end
2019-09-04 21:01:54 +05:30
2019-10-12 21:52:04 +05:30
it { is_expected.to include(variable) }
2018-12-13 13:39:08 +05:30
end
2019-10-12 21:52:04 +05:30
context 'kubernetes namespace does not exist' do
let(:persisted_namespace) { nil }
let(:namespace) { 'kubernetes-namespace' }
let(:kubeconfig) { 'kubeconfig' }
2019-07-31 22:56:46 +05:30
2019-10-12 21:52:04 +05:30
before do
allow(Gitlab::Kubernetes::DefaultNamespace).to receive(:new)
.with(cluster, project: project).and_return(double(from_environment_name: namespace))
allow(platform).to receive(:kubeconfig).with(namespace).and_return(kubeconfig)
2019-02-15 15:39:39 +05:30
end
2019-10-12 21:52:04 +05:30
it { is_expected.not_to include(key: 'KUBE_TOKEN', value: platform.token, public: false, masked: true) }
it { is_expected.not_to include(key: 'KUBE_NAMESPACE', value: namespace) }
it { is_expected.not_to include(key: 'KUBECONFIG', value: kubeconfig, public: false, file: true) }
2019-02-15 15:39:39 +05:30
2019-10-12 21:52:04 +05:30
context 'cluster is unmanaged' do
let(:cluster) { create(:cluster, :group, :not_managed, platform_kubernetes: platform) }
2019-02-15 15:39:39 +05:30
2019-10-12 21:52:04 +05:30
it { is_expected.to include(key: 'KUBE_TOKEN', value: platform.token, public: false, masked: true) }
it { is_expected.to include(key: 'KUBE_NAMESPACE', value: namespace) }
it { is_expected.to include(key: 'KUBECONFIG', value: kubeconfig, public: false, file: true) }
2020-01-01 13:55:28 +05:30
context 'custom namespace is provided' do
let(:custom_namespace) { 'custom-namespace' }
subject do
platform.predefined_variables(
project: project,
environment_name: environment_name,
kubernetes_namespace: custom_namespace
)
end
before do
allow(platform).to receive(:kubeconfig).with(custom_namespace).and_return(kubeconfig)
end
it { is_expected.to include(key: 'KUBE_TOKEN', value: platform.token, public: false, masked: true) }
it { is_expected.to include(key: 'KUBE_NAMESPACE', value: custom_namespace) }
it { is_expected.to include(key: 'KUBECONFIG', value: kubeconfig, public: false, file: true) }
end
2019-02-15 15:39:39 +05:30
end
end
2019-03-02 22:35:43 +05:30
2019-10-12 21:52:04 +05:30
context 'cluster variables' do
let(:variable) { Hash(key: :fake_key, value: 'fake_value') }
let(:cluster_variables) { Gitlab::Ci::Variables::Collection.new([variable]) }
2019-03-02 22:35:43 +05:30
2019-10-12 21:52:04 +05:30
before do
expect(cluster).to receive(:predefined_variables).and_return(cluster_variables)
2019-03-02 22:35:43 +05:30
end
2019-10-12 21:52:04 +05:30
it { is_expected.to include(variable) }
2019-03-02 22:35:43 +05:30
end
2018-03-17 18:26:18 +05:30
end
describe '#terminals' do
2019-09-30 21:07:59 +05:30
subject { service.terminals(environment, pods: pods) }
2018-03-17 18:26:18 +05:30
let!(:cluster) { create(:cluster, :project, platform_kubernetes: service) }
let(:project) { cluster.project }
let(:service) { create(:cluster_platform_kubernetes, :configured) }
let(:environment) { build(:environment, project: project, name: "env", slug: "env-000000") }
2019-09-30 21:07:59 +05:30
let(:pods) { [{ "bad" => "pod" }] }
2018-03-17 18:26:18 +05:30
context 'with invalid pods' do
it 'returns no terminals' do
is_expected.to be_empty
end
end
context 'with valid pods' do
2019-10-12 21:52:04 +05:30
let(:pod) { kube_pod(environment_slug: environment.slug, namespace: cluster.kubernetes_namespace_for(environment), project_slug: project.full_path_slug) }
2019-07-07 11:18:12 +05:30
let(:pod_with_no_terminal) { kube_pod(environment_slug: environment.slug, project_slug: project.full_path_slug, status: "Pending") }
2018-03-17 18:26:18 +05:30
let(:terminals) { kube_terminals(service, pod) }
2019-09-30 21:07:59 +05:30
let(:pods) { [pod, pod, pod_with_no_terminal, kube_pod(environment_slug: "should-be-filtered-out")] }
2018-03-17 18:26:18 +05:30
it 'returns terminals' do
is_expected.to eq(terminals + terminals)
end
it 'uses max session time from settings' do
stub_application_setting(terminal_max_session_time: 600)
times = subject.map { |terminal| terminal[:max_session_time] }
expect(times).to eq [600, 600, 600, 600]
end
end
end
2019-09-30 21:07:59 +05:30
describe '#calculate_reactive_cache_for' do
2018-03-17 18:26:18 +05:30
let(:service) { create(:cluster_platform_kubernetes, :configured) }
2020-03-13 15:44:24 +05:30
let(:expected_pod_cached_data) do
kube_pod.tap { |kp| kp['metadata'].delete('namespace') }
end
2020-10-24 23:57:45 +05:30
2020-03-13 15:44:24 +05:30
let(:namespace) { "project-namespace" }
2021-01-03 14:25:43 +05:30
let(:environment) { instance_double(Environment, deployment_namespace: namespace, project: service.cluster.project) }
2019-09-30 21:07:59 +05:30
subject { service.calculate_reactive_cache_for(environment) }
2018-03-17 18:26:18 +05:30
2019-09-30 21:07:59 +05:30
context 'when the kubernetes integration is disabled' do
before do
allow(service).to receive(:enabled?).and_return(false)
end
2018-03-17 18:26:18 +05:30
it { is_expected.to be_nil }
end
2018-11-08 19:23:39 +05:30
context 'when kubernetes responds with valid pods and deployments' do
2018-03-17 18:26:18 +05:30
before do
2019-09-04 21:01:54 +05:30
stub_kubeclient_pods(namespace)
stub_kubeclient_deployments(namespace)
2021-01-03 14:25:43 +05:30
stub_kubeclient_ingresses(namespace)
2018-03-17 18:26:18 +05:30
end
2020-03-13 15:44:24 +05:30
it { is_expected.to include(pods: [expected_pod_cached_data]) }
2018-03-17 18:26:18 +05:30
end
context 'when kubernetes responds with 500s' do
before do
2019-09-04 21:01:54 +05:30
stub_kubeclient_pods(namespace, status: 500)
stub_kubeclient_deployments(namespace, status: 500)
2021-01-03 14:25:43 +05:30
stub_kubeclient_ingresses(namespace, status: 500)
2018-03-17 18:26:18 +05:30
end
2018-05-09 12:01:36 +05:30
it { expect { subject }.to raise_error(Kubeclient::HttpError) }
2018-03-17 18:26:18 +05:30
end
context 'when kubernetes responds with 404s' do
before do
2019-09-04 21:01:54 +05:30
stub_kubeclient_pods(namespace, status: 404)
stub_kubeclient_deployments(namespace, status: 404)
2021-01-03 14:25:43 +05:30
stub_kubeclient_ingresses(namespace, status: 404)
2018-03-17 18:26:18 +05:30
end
2018-11-08 19:23:39 +05:30
it { is_expected.to include(pods: []) }
2018-03-17 18:26:18 +05:30
end
2018-12-13 13:39:08 +05:30
end
2018-03-17 18:26:18 +05:30
end