2019-07-31 22:56:46 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
require 'spec_helper'
|
|
|
|
|
2019-03-02 22:35:43 +05:30
|
|
|
describe Clusters::Applications::CheckInstallationProgressService, '#execute' do
|
2018-03-17 18:26:18 +05:30
|
|
|
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 }
|
|
|
|
|
2019-01-03 12:48:30 +05:30
|
|
|
shared_examples 'a not yet terminated installation' do |a_phase|
|
|
|
|
let(:phase) { a_phase }
|
2018-12-23 12:14:25 +05:30
|
|
|
|
2019-02-15 15:39:39 +05:30
|
|
|
before do
|
2019-12-04 20:38:33 +05:30
|
|
|
expect(service).to receive(:pod_phase).once.and_return(phase)
|
2019-02-15 15:39:39 +05:30
|
|
|
end
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
context "when phase is #{a_phase}" do
|
2019-07-31 22:56:46 +05:30
|
|
|
context 'when not timed_out' do
|
2018-03-17 18:26:18 +05:30
|
|
|
it 'reschedule a new check' do
|
|
|
|
expect(ClusterWaitForAppInstallationWorker).to receive(:perform_in).once
|
|
|
|
expect(service).not_to receive(:remove_installation_pod)
|
|
|
|
|
2019-03-02 22:35:43 +05:30
|
|
|
expect do
|
|
|
|
service.execute
|
|
|
|
|
|
|
|
application.reload
|
|
|
|
end.not_to change(application, :status)
|
2018-03-17 18:26:18 +05:30
|
|
|
|
|
|
|
expect(application.status_reason).to be_nil
|
|
|
|
end
|
|
|
|
end
|
2019-03-02 22:35:43 +05:30
|
|
|
end
|
|
|
|
end
|
2018-03-17 18:26:18 +05:30
|
|
|
|
2019-07-07 11:18:12 +05:30
|
|
|
shared_examples 'error handling' do
|
2019-03-02 22:35:43 +05:30
|
|
|
context 'when installation raises a Kubeclient::HttpError' do
|
|
|
|
let(:cluster) { create(:cluster, :provided_by_user, :project) }
|
2019-07-07 11:18:12 +05:30
|
|
|
let(:logger) { service.send(:logger) }
|
|
|
|
let(:error) { Kubeclient::HttpError.new(401, 'Unauthorized', nil) }
|
2018-03-17 18:26:18 +05:30
|
|
|
|
2019-03-02 22:35:43 +05:30
|
|
|
before do
|
|
|
|
application.update!(cluster: cluster)
|
2018-03-17 18:26:18 +05:30
|
|
|
|
2019-12-04 20:38:33 +05:30
|
|
|
expect(service).to receive(:pod_phase).and_raise(error)
|
2019-07-07 11:18:12 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
include_examples 'logs kubernetes errors' do
|
|
|
|
let(:error_name) { 'Kubeclient::HttpError' }
|
|
|
|
let(:error_message) { 'Unauthorized' }
|
|
|
|
let(:error_code) { 401 }
|
2019-03-02 22:35:43 +05:30
|
|
|
end
|
2018-03-17 18:26:18 +05:30
|
|
|
|
2019-03-02 22:35:43 +05:30
|
|
|
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
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
before do
|
|
|
|
allow(service).to receive(:installation_errors).and_return(errors)
|
|
|
|
allow(service).to receive(:remove_installation_pod).and_return(nil)
|
|
|
|
end
|
|
|
|
|
2019-03-02 22:35:43 +05:30
|
|
|
context 'when application is updating' do
|
|
|
|
let(:application) { create(:clusters_applications_helm, :updating) }
|
|
|
|
|
2019-07-07 11:18:12 +05:30
|
|
|
include_examples 'error handling'
|
2019-03-02 22:35:43 +05:30
|
|
|
|
|
|
|
RESCHEDULE_PHASES.each { |phase| it_behaves_like 'a not yet terminated installation', phase }
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
context 'when installation POD succeeded' do
|
|
|
|
let(:phase) { Gitlab::Kubernetes::Pod::SUCCEEDED }
|
2019-02-15 15:39:39 +05:30
|
|
|
before do
|
2019-12-04 20:38:33 +05:30
|
|
|
expect(service).to receive(:pod_phase).once.and_return(phase)
|
2019-02-15 15:39:39 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it 'removes the installation POD' do
|
|
|
|
expect(service).to receive(:remove_installation_pod).once
|
|
|
|
|
|
|
|
service.execute
|
|
|
|
end
|
2018-03-17 18:26:18 +05:30
|
|
|
|
|
|
|
it 'make the application installed' do
|
|
|
|
expect(ClusterWaitForAppInstallationWorker).not_to receive(:perform_in)
|
|
|
|
|
|
|
|
service.execute
|
|
|
|
|
2019-03-02 22:35:43 +05:30
|
|
|
expect(application).to be_updated
|
2018-03-17 18:26:18 +05:30
|
|
|
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' }
|
|
|
|
|
2019-02-15 15:39:39 +05:30
|
|
|
before do
|
2019-12-04 20:38:33 +05:30
|
|
|
expect(service).to receive(:pod_phase).once.and_return(phase)
|
2019-02-15 15:39:39 +05:30
|
|
|
end
|
2018-03-17 18:26:18 +05:30
|
|
|
|
|
|
|
it 'make the application errored' do
|
|
|
|
service.execute
|
|
|
|
|
2019-03-02 22:35:43 +05:30
|
|
|
expect(application).to be_update_errored
|
|
|
|
expect(application.status_reason).to eq('Operation failed. Check pod logs for install-helm for more details.')
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-03-02 22:35:43 +05:30
|
|
|
context 'when timed out' do
|
2019-07-31 22:56:46 +05:30
|
|
|
let(:application) { create(:clusters_applications_helm, :timed_out, :updating) }
|
2019-02-15 15:39:39 +05:30
|
|
|
|
2019-03-02 22:35:43 +05:30
|
|
|
before do
|
2019-12-04 20:38:33 +05:30
|
|
|
expect(service).to receive(:pod_phase).once.and_return(phase)
|
2019-03-02 22:35:43 +05:30
|
|
|
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
|
2019-07-07 11:18:12 +05:30
|
|
|
include_examples 'error handling'
|
2019-03-02 22:35:43 +05:30
|
|
|
|
|
|
|
RESCHEDULE_PHASES.each { |phase| it_behaves_like 'a not yet terminated installation', phase }
|
2019-02-15 15:39:39 +05:30
|
|
|
|
2019-03-02 22:35:43 +05:30
|
|
|
context 'when installation POD succeeded' do
|
|
|
|
let(:phase) { Gitlab::Kubernetes::Pod::SUCCEEDED }
|
2019-02-15 15:39:39 +05:30
|
|
|
before do
|
2019-12-04 20:38:33 +05:30
|
|
|
expect(service).to receive(:pod_phase).once.and_return(phase)
|
2019-03-02 22:35:43 +05:30
|
|
|
end
|
2019-02-15 15:39:39 +05:30
|
|
|
|
2019-03-02 22:35:43 +05:30
|
|
|
it 'removes the installation POD' do
|
2019-12-04 20:38:33 +05:30
|
|
|
expect_any_instance_of(Gitlab::Kubernetes::Helm::Api)
|
|
|
|
.to receive(:delete_pod!)
|
|
|
|
.with(kind_of(String))
|
|
|
|
.once
|
|
|
|
expect(service).to receive(:remove_installation_pod).and_call_original
|
2019-03-02 22:35:43 +05:30
|
|
|
|
|
|
|
service.execute
|
2019-02-15 15:39:39 +05:30
|
|
|
end
|
|
|
|
|
2019-03-02 22:35:43 +05:30
|
|
|
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
|
2019-12-04 20:38:33 +05:30
|
|
|
expect(service).to receive(:pod_phase).once.and_return(phase)
|
2019-03-02 22:35:43 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it 'make the application errored' do
|
2019-02-15 15:39:39 +05:30
|
|
|
service.execute
|
|
|
|
|
|
|
|
expect(application).to be_errored
|
2019-03-02 22:35:43 +05:30
|
|
|
expect(application.status_reason).to eq('Operation failed. Check pod logs for install-helm for more details.')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when timed out' do
|
2019-07-31 22:56:46 +05:30
|
|
|
let(:application) { create(:clusters_applications_helm, :timed_out) }
|
2019-03-02 22:35:43 +05:30
|
|
|
|
|
|
|
before do
|
2019-12-04 20:38:33 +05:30
|
|
|
expect(service).to receive(:pod_phase).once.and_return(phase)
|
2019-02-15 15:39:39 +05:30
|
|
|
end
|
|
|
|
|
2019-03-02 22:35:43 +05:30
|
|
|
it 'make the application errored' do
|
|
|
|
expect(ClusterWaitForAppInstallationWorker).not_to receive(:perform_in)
|
2019-02-15 15:39:39 +05:30
|
|
|
|
|
|
|
service.execute
|
2019-03-02 22:35:43 +05:30
|
|
|
|
|
|
|
expect(application).to be_errored
|
|
|
|
expect(application.status_reason).to eq('Operation timed out. Check pod logs for install-helm for more details.')
|
2019-02-15 15:39:39 +05:30
|
|
|
end
|
|
|
|
end
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
end
|