debian-mirror-gitlab/spec/models/clusters/applications/prometheus_spec.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

350 lines
11 KiB
Ruby
Raw Normal View History

2019-07-07 11:18:12 +05:30
# frozen_string_literal: true
2019-12-04 20:38:33 +05:30
require 'spec_helper'
2018-03-17 18:26:18 +05:30
2020-07-28 23:09:34 +05:30
RSpec.describe Clusters::Applications::Prometheus do
2018-11-20 20:47:30 +05:30
include KubernetesHelpers
2020-03-13 15:44:24 +05:30
include StubRequests
2018-11-20 20:47:30 +05:30
2018-03-27 19:54:05 +05:30
include_examples 'cluster application core specs', :clusters_applications_prometheus
2018-12-13 13:39:08 +05:30
include_examples 'cluster application status specs', :clusters_applications_prometheus
2019-03-02 22:35:43 +05:30
include_examples 'cluster application version specs', :clusters_applications_prometheus
2019-02-15 15:39:39 +05:30
include_examples 'cluster application helm specs', :clusters_applications_prometheus
2019-03-02 22:35:43 +05:30
include_examples 'cluster application initial status specs'
2018-03-17 18:26:18 +05:30
2023-01-13 00:05:48 +05:30
describe 'default values' do
subject(:prometheus) { build(:clusters_applications_prometheus) }
it { expect(prometheus.alert_manager_token).to be_an_instance_of(String) }
it { expect(prometheus.version).to eq(described_class::VERSION) }
end
2019-07-31 22:56:46 +05:30
describe 'after_destroy' do
2021-06-08 01:23:25 +05:30
let(:cluster) { create(:cluster, :with_installed_helm) }
let(:application) { create(:clusters_applications_prometheus, :installed, cluster: cluster) }
2019-07-31 22:56:46 +05:30
2021-06-08 01:23:25 +05:30
it 'disables the corresponding integration' do
application.destroy!
2019-07-31 22:56:46 +05:30
2021-06-08 01:23:25 +05:30
expect(cluster.integration_prometheus).not_to be_enabled
2019-07-31 22:56:46 +05:30
end
end
2018-03-17 18:26:18 +05:30
describe 'transition to installed' do
let(:project) { create(:project) }
2020-01-01 13:55:28 +05:30
let(:cluster) { create(:cluster, :with_installed_helm) }
let(:application) { create(:clusters_applications_prometheus, :installing, cluster: cluster) }
2018-03-17 18:26:18 +05:30
2021-06-08 01:23:25 +05:30
it 'enables the corresponding integration' do
2020-01-01 13:55:28 +05:30
application.make_installed
2021-06-08 01:23:25 +05:30
expect(cluster.integration_prometheus).to be_enabled
2018-03-17 18:26:18 +05:30
end
end
2021-04-29 21:17:54 +05:30
describe 'transition to externally_installed' do
let(:project) { create(:project) }
let(:cluster) { create(:cluster, :with_installed_helm) }
let(:application) { create(:clusters_applications_prometheus, :installing, cluster: cluster) }
2021-06-08 01:23:25 +05:30
it 'enables the corresponding integration' do
2021-04-29 21:17:54 +05:30
application.make_externally_installed!
2021-06-08 01:23:25 +05:30
expect(cluster.integration_prometheus).to be_enabled
2021-04-29 21:17:54 +05:30
end
end
2020-04-22 19:07:51 +05:30
describe 'transition to updating' do
let(:project) { create(:project) }
let(:cluster) { create(:cluster, projects: [project]) }
subject { create(:clusters_applications_prometheus, :installed, cluster: cluster) }
it 'sets last_update_started_at to now' do
2020-11-24 15:15:51 +05:30
freeze_time do
2020-06-23 00:09:42 +05:30
expect { subject.make_updating }.to change { subject.reload.last_update_started_at }.to be_within(1.second).of(Time.current)
2020-04-22 19:07:51 +05:30
end
end
end
2021-06-08 01:23:25 +05:30
describe '#managed_prometheus?' do
subject { prometheus.managed_prometheus? }
let(:prometheus) { build(:clusters_applications_prometheus) }
it { is_expected.to be_truthy }
context 'externally installed' do
let(:prometheus) { build(:clusters_applications_prometheus, :externally_installed) }
it { is_expected.to be_falsey }
end
context 'uninstalled' do
let(:prometheus) { build(:clusters_applications_prometheus, :uninstalled) }
it { is_expected.to be_falsey }
end
end
2019-07-31 22:56:46 +05:30
describe '#can_uninstall?' do
let(:prometheus) { create(:clusters_applications_prometheus) }
subject { prometheus.can_uninstall? }
it { is_expected.to be_truthy }
end
2018-03-27 19:54:05 +05:30
describe '#prometheus_client' do
2021-04-29 21:17:54 +05:30
include_examples '#prometheus_client shared' do
let(:factory) { :clusters_applications_prometheus }
2018-03-17 18:26:18 +05:30
end
end
2018-03-27 19:54:05 +05:30
describe '#install_command' do
let(:prometheus) { create(:clusters_applications_prometheus) }
2018-11-20 20:47:30 +05:30
subject { prometheus.install_command }
2021-01-29 00:20:46 +05:30
it { is_expected.to be_an_instance_of(Gitlab::Kubernetes::Helm::V3::InstallCommand) }
2018-03-27 19:54:05 +05:30
2019-07-07 11:18:12 +05:30
it 'is initialized with 3 arguments' do
2018-11-20 20:47:30 +05:30
expect(subject.name).to eq('prometheus')
2020-10-24 23:57:45 +05:30
expect(subject.chart).to eq('prometheus/prometheus')
2020-11-24 15:15:51 +05:30
expect(subject.version).to eq('10.4.1')
2019-02-15 15:39:39 +05:30
expect(subject).to be_rbac
2018-11-20 20:47:30 +05:30
expect(subject.files).to eq(prometheus.files)
end
2019-02-15 15:39:39 +05:30
context 'on a non rbac enabled cluster' do
2018-11-20 20:47:30 +05:30
before do
2019-02-15 15:39:39 +05:30
prometheus.cluster.platform_kubernetes.abac!
2018-11-20 20:47:30 +05:30
end
2018-11-18 11:00:15 +05:30
2019-02-15 15:39:39 +05:30
it { is_expected.not_to be_rbac }
2018-11-18 11:00:15 +05:30
end
context 'application failed to install previously' do
let(:prometheus) { create(:clusters_applications_prometheus, :errored, version: '2.0.0') }
2019-07-07 11:18:12 +05:30
it 'is initialized with the locked version' do
2020-11-24 15:15:51 +05:30
expect(subject.version).to eq('10.4.1')
2018-11-18 11:00:15 +05:30
end
2018-03-27 19:54:05 +05:30
end
2019-02-15 15:39:39 +05:30
2019-07-07 11:18:12 +05:30
it 'does not install knative metrics' do
2019-10-12 21:52:04 +05:30
expect(subject.postinstall).to be_empty
2019-02-15 15:39:39 +05:30
end
context 'with knative installed' do
2023-01-13 00:05:48 +05:30
let(:knative) { create(:clusters_applications_knative, :updated) }
2019-02-15 15:39:39 +05:30
let(:prometheus) { create(:clusters_applications_prometheus, cluster: knative.cluster) }
subject { prometheus.install_command }
2019-07-07 11:18:12 +05:30
it 'installs knative metrics' do
2019-02-15 15:39:39 +05:30
expect(subject.postinstall).to include("kubectl apply -f #{Clusters::Applications::Knative::METRICS_CONFIG}")
end
end
2018-03-27 19:54:05 +05:30
end
2019-07-31 22:56:46 +05:30
describe '#uninstall_command' do
let(:prometheus) { create(:clusters_applications_prometheus) }
subject { prometheus.uninstall_command }
2021-01-29 00:20:46 +05:30
it { is_expected.to be_an_instance_of(Gitlab::Kubernetes::Helm::V3::DeleteCommand) }
2019-07-31 22:56:46 +05:30
it 'has the application name' do
expect(subject.name).to eq('prometheus')
end
it 'has files' do
expect(subject.files).to eq(prometheus.files)
end
it 'is rbac' do
expect(subject).to be_rbac
end
2019-12-21 20:55:43 +05:30
describe '#predelete' do
2023-01-13 00:05:48 +05:30
let(:knative) { create(:clusters_applications_knative, :updated) }
2019-12-21 20:55:43 +05:30
let(:prometheus) { create(:clusters_applications_prometheus, cluster: knative.cluster) }
subject { prometheus.uninstall_command.predelete }
it 'deletes knative metrics' do
metrics_config = Clusters::Applications::Knative::METRICS_CONFIG
is_expected.to include("kubectl delete -f #{metrics_config} --ignore-not-found")
end
end
2019-07-31 22:56:46 +05:30
context 'on a non rbac enabled cluster' do
before do
prometheus.cluster.platform_kubernetes.abac!
end
it { is_expected.not_to be_rbac }
end
end
2020-01-01 13:55:28 +05:30
describe '#patch_command' do
subject(:patch_command) { prometheus.patch_command(values) }
2019-03-02 22:35:43 +05:30
let(:prometheus) { build(:clusters_applications_prometheus) }
let(:values) { prometheus.values }
2021-01-29 00:20:46 +05:30
it { is_expected.to be_an_instance_of(::Gitlab::Kubernetes::Helm::V3::PatchCommand) }
2019-03-02 22:35:43 +05:30
2019-07-07 11:18:12 +05:30
it 'is initialized with 3 arguments' do
2020-01-01 13:55:28 +05:30
expect(patch_command.name).to eq('prometheus')
2020-10-24 23:57:45 +05:30
expect(patch_command.chart).to eq('prometheus/prometheus')
2020-11-24 15:15:51 +05:30
expect(patch_command.version).to eq('10.4.1')
2020-01-01 13:55:28 +05:30
expect(patch_command.files).to eq(prometheus.files)
2019-03-02 22:35:43 +05:30
end
end
describe '#update_in_progress?' do
context 'when app is updating' do
it 'returns true' do
cluster = create(:cluster)
prometheus_app = build(:clusters_applications_prometheus, :updating, cluster: cluster)
expect(prometheus_app.update_in_progress?).to be true
end
end
end
describe '#update_errored?' do
context 'when app errored' do
it 'returns true' do
cluster = create(:cluster)
prometheus_app = build(:clusters_applications_prometheus, :update_errored, cluster: cluster)
expect(prometheus_app.update_errored?).to be true
end
end
end
2018-11-18 11:00:15 +05:30
describe '#files' do
let(:application) { create(:clusters_applications_prometheus) }
let(:values) { subject[:'values.yaml'] }
subject { application.files }
2019-07-07 11:18:12 +05:30
it 'includes prometheus valid values' do
2018-11-18 11:00:15 +05:30
expect(values).to include('alertmanager')
expect(values).to include('kubeStateMetrics')
expect(values).to include('nodeExporter')
expect(values).to include('pushgateway')
expect(values).to include('serverFiles')
2018-03-27 19:54:05 +05:30
end
end
2019-03-02 22:35:43 +05:30
describe '#files_with_replaced_values' do
let(:application) { build(:clusters_applications_prometheus) }
let(:files) { application.files }
subject { application.files_with_replaced_values({ hello: :world }) }
it 'does not modify #files' do
2020-04-08 14:13:33 +05:30
expect(subject[:'values.yaml']).not_to eq(files[:'values.yaml'])
2019-03-02 22:35:43 +05:30
expect(files[:'values.yaml']).to eq(application.values)
end
it 'returns values.yaml with replaced values' do
expect(subject[:'values.yaml']).to eq({ hello: :world })
end
2020-04-08 14:13:33 +05:30
it 'uses values from #files, except for values.yaml' do
allow(application).to receive(:files).and_return({
'values.yaml': 'some value specific to files',
'file_a.txt': 'file_a',
'file_b.txt': 'file_b'
})
expect(subject.except(:'values.yaml')).to eq({
'file_a.txt': 'file_a',
'file_b.txt': 'file_b'
})
2019-03-02 22:35:43 +05:30
end
end
2020-01-01 13:55:28 +05:30
describe '#configured?' do
let(:prometheus) { create(:clusters_applications_prometheus, :installed, cluster: cluster) }
subject { prometheus.configured? }
context 'when a kubenetes client is present' do
let(:cluster) { create(:cluster, :project, :provided_by_gcp) }
it { is_expected.to be_truthy }
context 'when it is not availalble' do
let(:prometheus) { create(:clusters_applications_prometheus, cluster: cluster) }
it { is_expected.to be_falsey }
end
2020-03-13 15:44:24 +05:30
context 'when the kubernetes URL is blocked' do
before do
blocked_ip = '127.0.0.1' # localhost addresses are blocked by default
stub_all_dns(cluster.platform.api_url, ip_address: blocked_ip)
end
it { is_expected.to be_falsey }
end
2020-01-01 13:55:28 +05:30
end
context 'when a kubenetes client is not present' do
let(:cluster) { create(:cluster) }
it { is_expected.to be_falsy }
end
end
2020-04-08 14:13:33 +05:30
2020-04-22 19:07:51 +05:30
describe '#updated_since?' do
let(:cluster) { create(:cluster) }
let(:prometheus_app) { build(:clusters_applications_prometheus, cluster: cluster) }
2020-06-23 00:09:42 +05:30
let(:timestamp) { Time.current - 5.minutes }
2020-04-22 19:07:51 +05:30
around do |example|
2020-11-24 15:15:51 +05:30
freeze_time { example.run }
2020-04-22 19:07:51 +05:30
end
before do
2020-06-23 00:09:42 +05:30
prometheus_app.last_update_started_at = Time.current
2020-04-22 19:07:51 +05:30
end
context 'when app does not have status failed' do
it 'returns true when last update started after the timestamp' do
expect(prometheus_app.updated_since?(timestamp)).to be true
end
it 'returns false when last update started before the timestamp' do
2020-06-23 00:09:42 +05:30
expect(prometheus_app.updated_since?(Time.current + 5.minutes)).to be false
2020-04-22 19:07:51 +05:30
end
end
context 'when app has status failed' do
it 'returns false when last update started after the timestamp' do
prometheus_app.status = 6
expect(prometheus_app.updated_since?(timestamp)).to be false
end
end
end
2020-04-08 14:13:33 +05:30
describe 'alert manager token' do
subject { create(:clusters_applications_prometheus) }
2021-06-08 01:23:25 +05:30
it 'is autogenerated on creation' do
expect(subject.alert_manager_token).to match(/\A\h{32}\z/)
expect(subject.encrypted_alert_manager_token).not_to be_nil
expect(subject.encrypted_alert_manager_token_iv).not_to be_nil
2020-04-08 14:13:33 +05:30
end
end
2018-03-17 18:26:18 +05:30
end