summaryrefslogtreecommitdiff
path: root/spec/services/clusters/applications/prometheus_update_service_spec.rb
blob: 076ff0210c9836e955124fd6dee9eb2ea21df855 (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
# 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!(:patch_command) { application.patch_command(empty_alerts_values_update_yaml) }
    let(:helm_client) { instance_double(::Gitlab::Kubernetes::Helm::API) }

    subject(:service) { described_class.new(application, project) }

    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