summaryrefslogtreecommitdiff
path: root/spec/services/clusters/applications/check_installation_progress_service_spec.rb
blob: 1cca89d31d73df3ddc11dd4c23ccfef9b743a8ca (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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
require 'spec_helper'

describe Clusters::Applications::CheckInstallationProgressService, '#execute' do
  RESCHEDULE_PHASES = Gitlab::Kubernetes::Pod::PHASES - [Gitlab::Kubernetes::Pod::SUCCEEDED, Gitlab::Kubernetes::Pod::FAILED].freeze

  let(:application) { create(:clusters_applications_helm, :installing) }
  let(:service) { described_class.new(application) }
  let(:phase) { Gitlab::Kubernetes::Pod::UNKNOWN }
  let(:errors) { nil }

  shared_examples 'a not yet terminated installation' do |a_phase|
    let(:phase) { a_phase }

    before do
      expect(service).to receive(:installation_phase).once.and_return(phase)
    end

    context "when phase is #{a_phase}" do
      context 'when not timeouted' do
        it 'reschedule a new check' do
          expect(ClusterWaitForAppInstallationWorker).to receive(:perform_in).once
          expect(service).not_to receive(:remove_installation_pod)

          expect do
            service.execute

            application.reload
          end.not_to change(application, :status)

          expect(application.status_reason).to be_nil
        end
      end
    end
  end

  shared_examples 'error handling' do
    context 'when installation raises a Kubeclient::HttpError' do
      let(:cluster) { create(:cluster, :provided_by_user, :project) }
      let(:logger) { service.send(:logger) }
      let(:error) { Kubeclient::HttpError.new(401, 'Unauthorized', nil) }

      before do
        application.update!(cluster: cluster)

        expect(service).to receive(:installation_phase).and_raise(error)
      end

      include_examples 'logs kubernetes errors' do
        let(:error_name) { 'Kubeclient::HttpError' }
        let(:error_message) { 'Unauthorized' }
        let(:error_code) { 401 }
      end

      it 'shows the response code from the error' do
        service.execute

        expect(application).to be_errored.or(be_update_errored)
        expect(application.status_reason).to eq('Kubernetes error: 401')
      end
    end
  end

  before do
    allow(service).to receive(:installation_errors).and_return(errors)
    allow(service).to receive(:remove_installation_pod).and_return(nil)
  end

  context 'when application is updating' do
    let(:application) { create(:clusters_applications_helm, :updating) }

    include_examples 'error handling'

    RESCHEDULE_PHASES.each { |phase| it_behaves_like 'a not yet terminated installation', phase }

    context 'when installation POD succeeded' do
      let(:phase) { Gitlab::Kubernetes::Pod::SUCCEEDED }
      before do
        expect(service).to receive(:installation_phase).once.and_return(phase)
      end

      it 'removes the installation POD' do
        expect(service).to receive(:remove_installation_pod).once

        service.execute
      end

      it 'make the application installed' do
        expect(ClusterWaitForAppInstallationWorker).not_to receive(:perform_in)

        service.execute

        expect(application).to be_updated
        expect(application.status_reason).to be_nil
      end
    end

    context 'when installation POD failed' do
      let(:phase) { Gitlab::Kubernetes::Pod::FAILED }
      let(:errors) { 'test installation failed' }

      before do
        expect(service).to receive(:installation_phase).once.and_return(phase)
      end

      it 'make the application errored' do
        service.execute

        expect(application).to be_update_errored
        expect(application.status_reason).to eq('Operation failed. Check pod logs for install-helm for more details.')
      end
    end

    context 'when timed out' do
      let(:application) { create(:clusters_applications_helm, :timeouted, :updating) }

      before do
        expect(service).to receive(:installation_phase).once.and_return(phase)
      end

      it 'make the application errored' do
        expect(ClusterWaitForAppInstallationWorker).not_to receive(:perform_in)

        service.execute

        expect(application).to be_update_errored
        expect(application.status_reason).to eq('Operation timed out. Check pod logs for install-helm for more details.')
      end
    end
  end

  context 'when application is installing' do
    include_examples 'error handling'

    RESCHEDULE_PHASES.each { |phase| it_behaves_like 'a not yet terminated installation', phase }

    context 'when installation POD succeeded' do
      let(:phase) { Gitlab::Kubernetes::Pod::SUCCEEDED }
      before do
        expect(service).to receive(:installation_phase).once.and_return(phase)
      end

      it 'removes the installation POD' do
        expect(service).to receive(:remove_installation_pod).once

        service.execute
      end

      it 'make the application installed' do
        expect(ClusterWaitForAppInstallationWorker).not_to receive(:perform_in)

        service.execute

        expect(application).to be_installed
        expect(application.status_reason).to be_nil
      end
    end

    context 'when installation POD failed' do
      let(:phase) { Gitlab::Kubernetes::Pod::FAILED }
      let(:errors) { 'test installation failed' }

      before do
        expect(service).to receive(:installation_phase).once.and_return(phase)
      end

      it 'make the application errored' do
        service.execute

        expect(application).to be_errored
        expect(application.status_reason).to eq('Operation failed. Check pod logs for install-helm for more details.')
      end
    end

    context 'when timed out' do
      let(:application) { create(:clusters_applications_helm, :timeouted) }

      before do
        expect(service).to receive(:installation_phase).once.and_return(phase)
      end

      it 'make the application errored' do
        expect(ClusterWaitForAppInstallationWorker).not_to receive(:perform_in)

        service.execute

        expect(application).to be_errored
        expect(application.status_reason).to eq('Operation timed out. Check pod logs for install-helm for more details.')
      end
    end
  end
end