summaryrefslogtreecommitdiff
path: root/spec/services/clusters/applications/prometheus_update_service_spec.rb
blob: 615bfc44045083e9c0410b9991ddea46ca9cbc50 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Clusters::Applications::PrometheusUpdateService do
  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) }

    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
    end

    context 'when prometheus is externally installed' do
      let(:application) { create(:clusters_applications_prometheus, :externally_installed, cluster: cluster) }

      it 'raises NotImplementedError' do
        expect { service.execute }.to raise_error(NotImplementedError)
      end
    end

    context 'when prometheus is a Clusters::Applications::Prometheus' do
      let!(:patch_command) { application.patch_command(empty_alerts_values_update_yaml) }

      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)
      end

      context 'when there are no errors' do
        before do
          expect(helm_client).to receive(:update).with(patch_command)

          allow(::ClusterWaitForAppUpdateWorker)
            .to receive(:perform_in)
            .and_return(nil)
        end

        it 'make the application updating' do
          expect(application.cluster).not_to be_nil

          service.execute

          expect(application).to be_updating
        end

        it 'updates current config' do
          prometheus_config_service = spy(:prometheus_config_service)

          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))

          service.execute
        end

        it 'schedules async update status check' do
          expect(::ClusterWaitForAppUpdateWorker).to receive(:perform_in).once

          service.execute
        end
      end

      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

        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
      end

      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)

          service.execute

          expect(application).to be_update_errored
        end
      end
    end
  end
end