Welcome to mirror list, hosted at ThFree Co, Russian Federation.

app_service_spec.rb « cleanup « clusters « services « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: ba1be7448a4412537fe1f5692d311589f5bc87d8 (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
112
113
114
115
116
117
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Clusters::Cleanup::AppService do
  describe '#execute' do
    let!(:cluster) { create(:cluster, :project, :cleanup_uninstalling_applications, provider_type: :gcp) }
    let(:service) { described_class.new(cluster) }
    let(:logger) { service.send(:logger) }
    let(:log_meta) do
      {
        service: described_class.name,
        cluster_id: cluster.id,
        execution_count: 0
      }
    end

    subject { service.execute }

    shared_examples 'does not reschedule itself' do
      it 'does not reschedule itself' do
        expect(Clusters::Cleanup::AppWorker).not_to receive(:perform_in)
      end
    end

    context 'when cluster has no applications available or transitioning applications' do
      it_behaves_like 'does not reschedule itself'

      it 'transitions cluster to cleanup_removing_project_namespaces' do
        expect { subject }
          .to change { cluster.reload.cleanup_status_name }
          .from(:cleanup_uninstalling_applications)
          .to(:cleanup_removing_project_namespaces)
      end

      it 'schedules Clusters::Cleanup::ProjectNamespaceWorker' do
        expect(Clusters::Cleanup::ProjectNamespaceWorker).to receive(:perform_async).with(cluster.id)
        subject
      end

      it 'logs all events' do
        expect(logger).to receive(:info)
          .with(log_meta.merge(event: :schedule_remove_project_namespaces))

        subject
      end
    end

    context 'when cluster has uninstallable applications' do
      shared_examples 'reschedules itself' do
        it 'reschedules itself' do
          expect(Clusters::Cleanup::AppWorker)
            .to receive(:perform_in)
            .with(1.minute, cluster.id, 1)

          subject
        end
      end

      context 'has applications with dependencies' do
        let!(:helm) { create(:clusters_applications_helm, :installed, cluster: cluster) }
        let!(:ingress) { create(:clusters_applications_ingress, :installed, cluster: cluster) }
        let!(:cert_manager) { create(:clusters_applications_cert_manager, :installed, cluster: cluster) }
        let!(:jupyter) { create(:clusters_applications_jupyter, :installed, cluster: cluster) }

        it_behaves_like 'reschedules itself'

        it 'only uninstalls apps that are not dependencies for other installed apps' do
          expect(Clusters::Applications::UninstallWorker)
            .not_to receive(:perform_async).with(helm.name, helm.id)

          expect(Clusters::Applications::UninstallWorker)
            .not_to receive(:perform_async).with(ingress.name, ingress.id)

          expect(Clusters::Applications::UninstallWorker)
            .to receive(:perform_async).with(cert_manager.name, cert_manager.id)
            .and_call_original

          expect(Clusters::Applications::UninstallWorker)
            .to receive(:perform_async).with(jupyter.name, jupyter.id)
            .and_call_original

          subject
        end

        it 'logs application uninstalls and next execution' do
          expect(logger).to receive(:info)
            .with(log_meta.merge(event: :uninstalling_app, application: kind_of(String))).twice
          expect(logger).to receive(:info)
            .with(log_meta.merge(event: :scheduling_execution, next_execution: 1))

          subject
        end

        context 'cluster is not cleanup_uninstalling_applications' do
          let!(:cluster) { create(:cluster, :project, provider_type: :gcp) }

          it_behaves_like 'does not reschedule itself'
        end
      end

      context 'when applications are still uninstalling/scheduled/depending on others' do
        let!(:helm) { create(:clusters_applications_helm, :installed, cluster: cluster) }
        let!(:ingress) { create(:clusters_applications_ingress, :scheduled, cluster: cluster) }
        let!(:runner) { create(:clusters_applications_runner, :uninstalling, cluster: cluster) }

        it_behaves_like 'reschedules itself'

        it 'does not call the uninstallation service' do
          expect(Clusters::Applications::UninstallWorker).not_to receive(:new)

          subject
        end
      end
    end
  end
end