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

420 lines
12 KiB
Ruby
Raw Normal View History

2018-03-17 18:26:18 +05:30
require 'spec_helper'
describe Clusters::Platforms::Kubernetes, :use_clean_rails_memory_store_caching do
include KubernetesHelpers
include ReactiveCachingHelpers
it { is_expected.to belong_to(:cluster) }
it { is_expected.to be_kind_of(Gitlab::Kubernetes) }
it { is_expected.to be_kind_of(ReactiveCaching) }
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(:project).to(:cluster) }
it { is_expected.to delegate_method(:enabled?).to(:cluster) }
it { is_expected.to delegate_method(:managed?).to(:cluster) }
it { is_expected.to delegate_method(:kubernetes_namespace).to(:cluster) }
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
context 'when namespace includes upper case' do
let(:kubernetes) { create(:cluster_platform_kubernetes, :configured, namespace: namespace) }
let(:namespace) { 'ABC' }
it 'converts to lower case' do
expect(kubernetes.namespace).to eq('abc')
end
end
end
describe 'validation' do
subject { kubernetes.valid? }
context 'when validates namespace' do
let(:kubernetes) { build(:cluster_platform_kubernetes, :configured, namespace: namespace) }
context 'when namespace is blank' do
let(:namespace) { '' }
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
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
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) }
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
2018-03-17 18:26:18 +05:30
describe '#actual_namespace' do
2018-12-13 13:39:08 +05:30
let(:cluster) { create(:cluster, :project) }
2018-03-17 18:26:18 +05:30
let(:project) { cluster.project }
2018-12-13 13:39:08 +05:30
let(:platform) do
create(:cluster_platform_kubernetes,
cluster: cluster,
namespace: namespace)
end
subject { platform.actual_namespace }
context 'with a namespace assigned' do
2018-03-17 18:26:18 +05:30
let(:namespace) { 'namespace-123' }
it { is_expected.to eq(namespace) }
end
2018-12-13 13:39:08 +05:30
context 'with no namespace assigned' do
2018-03-17 18:26:18 +05:30
let(:namespace) { nil }
2018-12-13 13:39:08 +05:30
context 'when kubernetes namespace is present' do
let(:kubernetes_namespace) { create(:cluster_kubernetes_namespace, cluster: cluster) }
2018-03-17 18:26:18 +05:30
2018-12-13 13:39:08 +05:30
before do
kubernetes_namespace
end
2018-03-17 18:26:18 +05:30
2018-12-13 13:39:08 +05:30
it { is_expected.to eq(kubernetes_namespace.namespace) }
end
2018-03-17 18:26:18 +05:30
2018-12-13 13:39:08 +05:30
context 'when kubernetes namespace is not present' do
it { is_expected.to eq("#{project.path}-#{project.id}") }
end
2018-03-17 18:26:18 +05:30
end
end
describe '#predefined_variables' do
let!(:cluster) { create(:cluster, :project, platform_kubernetes: kubernetes) }
2018-12-13 13:39:08 +05:30
let(:kubernetes) { create(:cluster_platform_kubernetes, api_url: api_url, ca_cert: ca_pem) }
2018-03-17 18:26:18 +05:30
let(:api_url) { 'https://kube.domain.com' }
let(:ca_pem) { 'CA PEM DATA' }
2018-12-13 13:39:08 +05:30
subject { kubernetes.predefined_variables(project: cluster.project) }
2018-03-17 18:26:18 +05:30
shared_examples 'setting variables' do
it 'sets the variables' do
2018-12-13 13:39:08 +05:30
expect(subject).to include(
2018-03-17 18:26:18 +05:30
{ key: 'KUBE_URL', value: api_url, public: true },
{ key: 'KUBE_CA_PEM', value: ca_pem, public: true },
{ key: 'KUBE_CA_PEM_FILE', value: ca_pem, public: true, file: true }
)
end
end
2018-12-13 13:39:08 +05:30
context 'kubernetes namespace is created with no service account token' do
let!(:kubernetes_namespace) { create(:cluster_kubernetes_namespace, cluster: cluster) }
it_behaves_like 'setting variables'
it 'sets KUBE_TOKEN' do
expect(subject).to include(
{ key: 'KUBE_TOKEN', value: kubernetes.token, public: false }
)
end
end
context 'kubernetes namespace is created with no service account token' do
let!(:kubernetes_namespace) { create(:cluster_kubernetes_namespace, :with_token, cluster: cluster) }
it_behaves_like 'setting variables'
it 'sets KUBE_TOKEN' do
expect(subject).to include(
{ key: 'KUBE_TOKEN', value: kubernetes_namespace.service_account_token, public: false }
)
end
end
2018-03-17 18:26:18 +05:30
context 'namespace is provided' do
let(:namespace) { 'my-project' }
before do
kubernetes.namespace = namespace
end
it_behaves_like 'setting variables'
2018-12-13 13:39:08 +05:30
it 'sets KUBE_TOKEN' do
expect(subject).to include(
{ key: 'KUBE_TOKEN', value: kubernetes.token, public: false }
)
end
2018-03-17 18:26:18 +05:30
end
context 'no namespace provided' do
let(:namespace) { kubernetes.actual_namespace }
it_behaves_like 'setting variables'
2018-12-13 13:39:08 +05:30
it 'sets KUBE_TOKEN' do
expect(subject).to include(
{ key: 'KUBE_TOKEN', value: kubernetes.token, public: false }
)
2018-03-17 18:26:18 +05:30
end
end
2019-02-15 15:39:39 +05:30
context 'group level cluster' do
let!(:cluster) { create(:cluster, :group, platform_kubernetes: kubernetes) }
let(:project) { create(:project, group: cluster.group) }
subject { kubernetes.predefined_variables(project: project) }
context 'no kubernetes namespace for the project' do
it_behaves_like 'setting variables'
it 'does not return KUBE_TOKEN' do
expect(subject).not_to include(
{ key: 'KUBE_TOKEN', value: kubernetes.token, public: false }
)
end
end
context 'kubernetes namespace exists for the project' do
let!(:kubernetes_namespace) { create(:cluster_kubernetes_namespace, :with_token, cluster: cluster, project: project) }
it_behaves_like 'setting variables'
it 'sets KUBE_TOKEN' do
expect(subject).to include(
{ key: 'KUBE_TOKEN', value: kubernetes_namespace.service_account_token, public: false }
)
end
end
end
2019-03-02 22:35:43 +05:30
context 'with a domain' do
let!(:cluster) do
create(:cluster, :provided_by_gcp, :with_domain,
platform_kubernetes: kubernetes)
end
it 'sets KUBE_INGRESS_BASE_DOMAIN' do
expect(subject).to include(
{ key: 'KUBE_INGRESS_BASE_DOMAIN', value: cluster.domain, public: true }
)
end
end
2018-03-17 18:26:18 +05:30
end
describe '#terminals' do
subject { service.terminals(environment) }
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") }
context 'with invalid pods' do
it 'returns no terminals' do
stub_reactive_cache(service, pods: [{ "bad" => "pod" }])
is_expected.to be_empty
end
end
context 'with valid pods' do
let(:pod) { kube_pod(app: environment.slug) }
2019-02-15 15:39:39 +05:30
let(:pod_with_no_terminal) { kube_pod(app: environment.slug, status: "Pending") }
2018-03-17 18:26:18 +05:30
let(:terminals) { kube_terminals(service, pod) }
before do
stub_reactive_cache(
service,
2019-02-15 15:39:39 +05:30
pods: [pod, pod, pod_with_no_terminal, kube_pod(app: "should-be-filtered-out")]
2018-03-17 18:26:18 +05:30
)
end
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
describe '#calculate_reactive_cache' do
subject { service.calculate_reactive_cache }
let!(:cluster) { create(:cluster, :project, enabled: enabled, platform_kubernetes: service) }
let(:service) { create(:cluster_platform_kubernetes, :configured) }
let(:enabled) { true }
context 'when cluster is disabled' do
let(:enabled) { false }
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
stub_kubeclient_pods
2018-11-08 19:23:39 +05:30
stub_kubeclient_deployments
2018-03-17 18:26:18 +05:30
end
2018-11-08 19:23:39 +05:30
it { is_expected.to include(pods: [kube_pod]) }
2018-03-17 18:26:18 +05:30
end
context 'when kubernetes responds with 500s' do
before do
stub_kubeclient_pods(status: 500)
2018-11-08 19:23:39 +05:30
stub_kubeclient_deployments(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
stub_kubeclient_pods(status: 404)
2018-11-08 19:23:39 +05:30
stub_kubeclient_deployments(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
end
2018-12-13 13:39:08 +05:30
describe '#update_kubernetes_namespace' do
let(:cluster) { create(:cluster, :provided_by_gcp) }
let(:platform) { cluster.platform }
context 'when namespace is updated' do
it 'should call ConfigureWorker' do
2019-02-15 15:39:39 +05:30
expect(ClusterConfigureWorker).to receive(:perform_async).with(cluster.id).once
2018-12-13 13:39:08 +05:30
platform.namespace = 'new-namespace'
platform.save
end
end
context 'when namespace is not updated' do
it 'should not call ConfigureWorker' do
2019-02-15 15:39:39 +05:30
expect(ClusterConfigureWorker).not_to receive(:perform_async)
2018-12-13 13:39:08 +05:30
platform.username = "new-username"
platform.save
end
end
end
2018-03-17 18:26:18 +05:30
end