2020-03-24 05:09:25 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-24 02:09:01 -04:00
|
|
|
RSpec.describe Clusters::Applications::PrometheusUpdateService do
|
2020-03-24 05:09:25 -04:00
|
|
|
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-04-21 20:10:01 -04:00
|
|
|
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-03-24 05:09:25 -04:00
|
|
|
end
|
|
|
|
|
2021-04-21 20:10:01 -04:00
|
|
|
context 'when prometheus is externally installed' do
|
|
|
|
let(:application) { create(:clusters_applications_prometheus, :externally_installed, cluster: cluster) }
|
2020-03-24 05:09:25 -04:00
|
|
|
|
2021-04-21 20:10:01 -04:00
|
|
|
it 'raises NotImplementedError' do
|
|
|
|
expect { service.execute }.to raise_error(NotImplementedError)
|
2020-03-24 05:09:25 -04:00
|
|
|
end
|
2021-04-21 20:10:01 -04:00
|
|
|
end
|
2020-03-24 05:09:25 -04:00
|
|
|
|
2021-04-21 20:10:01 -04:00
|
|
|
context 'when prometheus is a Clusters::Applications::Prometheus' do
|
|
|
|
let!(:patch_command) { application.patch_command(empty_alerts_values_update_yaml) }
|
2020-03-24 05:09:25 -04:00
|
|
|
|
2021-04-21 20:10:01 -04:00
|
|
|
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-03-24 05:09:25 -04:00
|
|
|
end
|
|
|
|
|
2021-04-21 20:10:01 -04:00
|
|
|
context 'when there are no errors' do
|
|
|
|
before do
|
|
|
|
expect(helm_client).to receive(:update).with(patch_command)
|
2020-03-24 05:09:25 -04:00
|
|
|
|
2021-04-21 20:10:01 -04:00
|
|
|
allow(::ClusterWaitForAppUpdateWorker)
|
|
|
|
.to receive(:perform_in)
|
|
|
|
.and_return(nil)
|
|
|
|
end
|
2020-03-24 05:09:25 -04:00
|
|
|
|
2021-04-21 20:10:01 -04:00
|
|
|
it 'make the application updating' do
|
|
|
|
expect(application.cluster).not_to be_nil
|
2020-03-24 05:09:25 -04:00
|
|
|
|
2021-04-21 20:10:01 -04:00
|
|
|
service.execute
|
2020-03-24 05:09:25 -04:00
|
|
|
|
2021-04-21 20:10:01 -04:00
|
|
|
expect(application).to be_updating
|
|
|
|
end
|
2020-03-24 05:09:25 -04:00
|
|
|
|
2021-04-21 20:10:01 -04:00
|
|
|
it 'updates current config' do
|
|
|
|
prometheus_config_service = spy(:prometheus_config_service)
|
2020-03-24 05:09:25 -04:00
|
|
|
|
2021-04-21 20:10:01 -04:00
|
|
|
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-03-24 05:09:25 -04:00
|
|
|
|
2021-04-21 20:10:01 -04:00
|
|
|
service.execute
|
|
|
|
end
|
2020-03-24 05:09:25 -04:00
|
|
|
|
2021-04-21 20:10:01 -04:00
|
|
|
it 'schedules async update status check' do
|
|
|
|
expect(::ClusterWaitForAppUpdateWorker).to receive(:perform_in).once
|
|
|
|
|
|
|
|
service.execute
|
|
|
|
end
|
2020-03-24 05:09:25 -04:00
|
|
|
end
|
|
|
|
|
2021-04-21 20:10:01 -04:00
|
|
|
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-03-24 05:09:25 -04:00
|
|
|
|
2021-04-21 20:10:01 -04:00
|
|
|
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-03-24 05:09:25 -04:00
|
|
|
end
|
|
|
|
|
2021-04-21 20:10:01 -04:00
|
|
|
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-03-24 05:09:25 -04:00
|
|
|
|
2021-04-21 20:10:01 -04:00
|
|
|
service.execute
|
2020-03-24 05:09:25 -04:00
|
|
|
|
2021-04-21 20:10:01 -04:00
|
|
|
expect(application).to be_update_errored
|
|
|
|
end
|
2020-03-24 05:09:25 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|