summaryrefslogtreecommitdiff
path: root/spec/services/clusters/cleanup/project_namespace_service_spec.rb
blob: 8d3ae217a9fa9d051ce60772a4b431f491b40b61 (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
118
119
120
121
122
123
124
125
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Clusters::Cleanup::ProjectNamespaceService do
  describe '#execute' do
    subject { service.execute }

    let!(:service) { described_class.new(cluster) }
    let!(:cluster) { create(:cluster, :with_environments, :cleanup_removing_project_namespaces) }
    let!(:logger) { service.send(:logger) }
    let(:log_meta) do
      {
        service: described_class.name,
        cluster_id: cluster.id,
        execution_count: 0
      }
    end

    let(:kubeclient_instance_double) do
      instance_double(Gitlab::Kubernetes::KubeClient, delete_namespace: nil, delete_service_account: nil)
    end

    before do
      allow_any_instance_of(Clusters::Cluster).to receive(:kubeclient).and_return(kubeclient_instance_double)
    end

    context 'when cluster has namespaces to be deleted' do
      it 'deletes namespaces from cluster' do
        expect(kubeclient_instance_double).to receive(:delete_namespace)
          .with cluster.kubernetes_namespaces[0].namespace
        expect(kubeclient_instance_double).to receive(:delete_namespace)
          .with(cluster.kubernetes_namespaces[1].namespace)

        subject
      end

      it 'deletes namespaces from database' do
        expect { subject }.to change { cluster.kubernetes_namespaces.exists? }.from(true).to(false)
      end

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

      it 'logs all events' do
        expect(logger).to receive(:info)
          .with(
            log_meta.merge(
              event: :deleting_project_namespace,
              namespace: cluster.kubernetes_namespaces[0].namespace))
        expect(logger).to receive(:info)
          .with(
            log_meta.merge(
              event: :deleting_project_namespace,
              namespace: cluster.kubernetes_namespaces[1].namespace))

        subject
      end

      context 'when cluster.kubeclient is nil' do
        let(:kubeclient_instance_double) { nil }

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

        it 'deletes namespaces from database' do
          expect { subject }.to change { cluster.kubernetes_namespaces.exists? }.from(true).to(false)
        end
      end
    end

    context 'when cluster has no namespaces' do
      let!(:cluster) { create(:cluster, :cleanup_removing_project_namespaces) }

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

        subject
      end

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

      it 'does not try to delete namespaces' do
        expect(kubeclient_instance_double).not_to receive(:delete_namespace)

        subject
      end
    end

    context 'when there is a Kubeclient::HttpError' do
      let(:kubeclient_instance_double) do
        instance_double(Gitlab::Kubernetes::KubeClient)
      end

      ['Unauthorized', 'forbidden', 'Certificate verify Failed'].each do |message|
        it 'schedules ::ServiceAccountWorker with accepted errors' do
          allow(kubeclient_instance_double)
            .to receive(:delete_namespace)
            .and_raise(Kubeclient::HttpError.new(401, message, nil))

          expect(Clusters::Cleanup::ServiceAccountWorker).to receive(:perform_async).with(cluster.id)

          subject
        end
      end

      it 'raises error with unaccepted errors' do
        allow(kubeclient_instance_double)
          .to receive(:delete_namespace)
          .and_raise(Kubeclient::HttpError.new(401, 'unexpected message', nil))

        expect { subject }.to raise_error(Kubeclient::HttpError)
      end
    end
  end
end