2020-04-22 19:07:51 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
RSpec.describe Clusters::Applications::PrometheusUpdateService do
|
2020-04-22 19:07:51 +05:30
|
|
|
describe '#execute' do
|
|
|
|
let(:project) { create(:project) }
|
|
|
|
let(:environment) { create(:environment, project: project) }
|
|
|
|
let(:cluster) { create(:cluster, :provided_by_user, :with_installed_helm, projects: [project]) }
|
|
|
|
let(:application) { create(:clusters_applications_prometheus, :installed, cluster: cluster) }
|
|
|
|
let(:empty_alerts_values_update_yaml) { "---\nalertmanager:\n enabled: false\nserverFiles:\n alerts: {}\n" }
|
|
|
|
let(:helm_client) { instance_double(::Gitlab::Kubernetes::Helm::API) }
|
|
|
|
|
|
|
|
subject(:service) { described_class.new(application, project) }
|
|
|
|
|
2021-06-08 01:23:25 +05:30
|
|
|
context 'when prometheus is a Clusters::Integrations::Prometheus' do
|
|
|
|
let(:application) { create(:clusters_integrations_prometheus, cluster: cluster) }
|
|
|
|
|
|
|
|
it 'raises NotImplementedError' do
|
|
|
|
expect { service.execute }.to raise_error(NotImplementedError)
|
|
|
|
end
|
2020-04-22 19:07:51 +05:30
|
|
|
end
|
|
|
|
|
2021-06-08 01:23:25 +05:30
|
|
|
context 'when prometheus is externally installed' do
|
|
|
|
let(:application) { create(:clusters_applications_prometheus, :externally_installed, cluster: cluster) }
|
2020-04-22 19:07:51 +05:30
|
|
|
|
2021-06-08 01:23:25 +05:30
|
|
|
it 'raises NotImplementedError' do
|
|
|
|
expect { service.execute }.to raise_error(NotImplementedError)
|
2020-04-22 19:07:51 +05:30
|
|
|
end
|
2021-06-08 01:23:25 +05:30
|
|
|
end
|
2020-04-22 19:07:51 +05:30
|
|
|
|
2021-06-08 01:23:25 +05:30
|
|
|
context 'when prometheus is a Clusters::Applications::Prometheus' do
|
|
|
|
let!(:patch_command) { application.patch_command(empty_alerts_values_update_yaml) }
|
2020-04-22 19:07:51 +05:30
|
|
|
|
2021-06-08 01:23:25 +05:30
|
|
|
before do
|
|
|
|
allow(service).to receive(:patch_command).with(empty_alerts_values_update_yaml).and_return(patch_command)
|
|
|
|
allow(service).to receive(:helm_api).and_return(helm_client)
|
2020-04-22 19:07:51 +05:30
|
|
|
end
|
|
|
|
|
2021-06-08 01:23:25 +05:30
|
|
|
context 'when there are no errors' do
|
|
|
|
before do
|
|
|
|
expect(helm_client).to receive(:update).with(patch_command)
|
2020-04-22 19:07:51 +05:30
|
|
|
|
2021-06-08 01:23:25 +05:30
|
|
|
allow(::ClusterWaitForAppUpdateWorker)
|
|
|
|
.to receive(:perform_in)
|
|
|
|
.and_return(nil)
|
|
|
|
end
|
2020-04-22 19:07:51 +05:30
|
|
|
|
2021-06-08 01:23:25 +05:30
|
|
|
it 'make the application updating' do
|
|
|
|
expect(application.cluster).not_to be_nil
|
2020-04-22 19:07:51 +05:30
|
|
|
|
2021-06-08 01:23:25 +05:30
|
|
|
service.execute
|
2020-04-22 19:07:51 +05:30
|
|
|
|
2021-06-08 01:23:25 +05:30
|
|
|
expect(application).to be_updating
|
|
|
|
end
|
2020-04-22 19:07:51 +05:30
|
|
|
|
2021-06-08 01:23:25 +05:30
|
|
|
it 'updates current config' do
|
|
|
|
prometheus_config_service = spy(:prometheus_config_service)
|
2020-04-22 19:07:51 +05:30
|
|
|
|
2021-06-08 01:23:25 +05:30
|
|
|
expect(Clusters::Applications::PrometheusConfigService)
|
|
|
|
.to receive(:new)
|
|
|
|
.with(project, cluster, application)
|
|
|
|
.and_return(prometheus_config_service)
|
|
|
|
|
|
|
|
expect(prometheus_config_service)
|
|
|
|
.to receive(:execute)
|
|
|
|
.and_return(YAML.safe_load(empty_alerts_values_update_yaml))
|
2020-04-22 19:07:51 +05:30
|
|
|
|
2021-06-08 01:23:25 +05:30
|
|
|
service.execute
|
|
|
|
end
|
2020-04-22 19:07:51 +05:30
|
|
|
|
2021-06-08 01:23:25 +05:30
|
|
|
it 'schedules async update status check' do
|
|
|
|
expect(::ClusterWaitForAppUpdateWorker).to receive(:perform_in).once
|
|
|
|
|
|
|
|
service.execute
|
|
|
|
end
|
2020-04-22 19:07:51 +05:30
|
|
|
end
|
|
|
|
|
2021-06-08 01:23:25 +05:30
|
|
|
context 'when k8s cluster communication fails' do
|
|
|
|
before do
|
|
|
|
error = ::Kubeclient::HttpError.new(500, 'system failure', nil)
|
|
|
|
allow(helm_client).to receive(:update).and_raise(error)
|
|
|
|
end
|
2020-04-22 19:07:51 +05:30
|
|
|
|
2021-06-08 01:23:25 +05:30
|
|
|
it 'make the application update errored' do
|
|
|
|
service.execute
|
|
|
|
|
|
|
|
expect(application).to be_update_errored
|
|
|
|
expect(application.status_reason).to match(/kubernetes error:/i)
|
|
|
|
end
|
2020-04-22 19:07:51 +05:30
|
|
|
end
|
|
|
|
|
2021-06-08 01:23:25 +05:30
|
|
|
context 'when application cannot be persisted' do
|
|
|
|
let(:application) { build(:clusters_applications_prometheus, :installed) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
allow(application).to receive(:make_updating!).once
|
|
|
|
.and_raise(ActiveRecord::RecordInvalid.new(application))
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'make the application update errored' do
|
|
|
|
expect(helm_client).not_to receive(:update)
|
2020-04-22 19:07:51 +05:30
|
|
|
|
2021-06-08 01:23:25 +05:30
|
|
|
service.execute
|
2020-04-22 19:07:51 +05:30
|
|
|
|
2021-06-08 01:23:25 +05:30
|
|
|
expect(application).to be_update_errored
|
|
|
|
end
|
2020-04-22 19:07:51 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|