debian-mirror-gitlab/spec/services/clusters/applications/create_service_spec.rb

308 lines
8.6 KiB
Ruby
Raw Normal View History

2018-12-13 13:39:08 +05:30
# frozen_string_literal: true
require 'spec_helper'
2020-07-28 23:09:34 +05:30
RSpec.describe Clusters::Applications::CreateService do
2018-12-13 13:39:08 +05:30
include TestRequestHelpers
let(:cluster) { create(:cluster, :project, :provided_by_gcp) }
let(:user) { create(:user) }
2021-02-22 17:27:13 +05:30
let(:params) { { application: 'ingress' } }
2018-12-13 13:39:08 +05:30
let(:service) { described_class.new(cluster, user, params) }
describe '#execute' do
before do
allow(ClusterInstallAppWorker).to receive(:perform_async)
2019-03-02 22:35:43 +05:30
allow(ClusterUpgradeAppWorker).to receive(:perform_async)
2018-12-13 13:39:08 +05:30
end
subject { service.execute(test_request) }
it 'creates an application' do
expect do
subject
cluster.reload
2021-02-22 17:27:13 +05:30
end.to change(cluster, :application_ingress)
2018-12-13 13:39:08 +05:30
end
2019-03-02 22:35:43 +05:30
context 'application already installed' do
2021-02-22 17:27:13 +05:30
let!(:application) { create(:clusters_applications_ingress, :installed, cluster: cluster) }
2019-03-02 22:35:43 +05:30
it 'does not create a new application' do
expect do
subject
2021-02-22 17:27:13 +05:30
end.not_to change(Clusters::Applications::Ingress, :count)
2019-03-02 22:35:43 +05:30
end
it 'schedules an upgrade for the application' do
2019-07-07 11:18:12 +05:30
expect(ClusterUpgradeAppWorker).to receive(:perform_async)
2019-03-02 22:35:43 +05:30
subject
end
end
2019-07-07 11:18:12 +05:30
context 'known applications' do
2020-03-13 15:44:24 +05:30
context 'ingress application' do
let(:params) do
{
application: 'ingress',
modsecurity_enabled: true
}
end
before do
expect_any_instance_of(Clusters::Applications::Ingress)
.to receive(:make_scheduled!)
.and_call_original
end
it 'creates the application' do
expect do
subject
cluster.reload
end.to change(cluster, :application_ingress)
end
it 'sets modsecurity_enabled' do
expect(subject.modsecurity_enabled).to eq(true)
end
end
2019-07-07 11:18:12 +05:30
context 'cert manager application' do
let(:params) do
{
application: 'cert_manager',
email: 'test@example.com'
}
end
2019-02-15 15:39:39 +05:30
2019-07-07 11:18:12 +05:30
before do
expect_any_instance_of(Clusters::Applications::CertManager)
.to receive(:make_scheduled!)
.and_call_original
end
2018-12-13 13:39:08 +05:30
2019-07-07 11:18:12 +05:30
it 'creates the application' do
expect do
subject
2018-12-13 13:39:08 +05:30
2019-07-07 11:18:12 +05:30
cluster.reload
end.to change(cluster, :application_cert_manager)
end
2018-12-13 13:39:08 +05:30
2019-07-07 11:18:12 +05:30
it 'sets the email' do
expect(subject.email).to eq('test@example.com')
end
2019-05-30 16:15:17 +05:30
end
2018-12-13 13:39:08 +05:30
2019-07-07 11:18:12 +05:30
context 'jupyter application' do
let(:params) do
{
application: 'jupyter',
hostname: 'example.com'
}
end
2018-12-13 13:39:08 +05:30
2019-07-07 11:18:12 +05:30
before do
create(:clusters_applications_ingress, :installed, external_ip: "127.0.0.0", cluster: cluster)
expect_any_instance_of(Clusters::Applications::Jupyter)
.to receive(:make_scheduled!)
.and_call_original
end
2018-12-13 13:39:08 +05:30
2019-07-07 11:18:12 +05:30
it 'creates the application' do
expect do
subject
2018-12-13 13:39:08 +05:30
2019-07-07 11:18:12 +05:30
cluster.reload
end.to change(cluster, :application_jupyter)
end
2018-12-13 13:39:08 +05:30
2019-07-07 11:18:12 +05:30
it 'sets the hostname' do
expect(subject.hostname).to eq('example.com')
end
2018-12-13 13:39:08 +05:30
2019-07-07 11:18:12 +05:30
it 'sets the oauth_application' do
expect(subject.oauth_application).to be_present
end
2019-05-30 16:15:17 +05:30
end
2018-12-13 13:39:08 +05:30
2019-07-07 11:18:12 +05:30
context 'knative application' do
let(:params) do
{
application: 'knative',
2020-04-08 14:13:33 +05:30
hostname: 'example.com',
pages_domain_id: domain.id
2019-07-07 11:18:12 +05:30
}
end
2019-05-18 00:54:41 +05:30
2020-04-08 14:13:33 +05:30
let(:domain) { create(:pages_domain, :instance_serverless) }
let(:associate_domain_service) { double('AssociateDomainService') }
2019-07-07 11:18:12 +05:30
before do
expect_any_instance_of(Clusters::Applications::Knative)
.to receive(:make_scheduled!)
.and_call_original
end
2019-05-18 00:54:41 +05:30
2019-07-07 11:18:12 +05:30
it 'creates the application' do
expect do
subject
cluster.reload
end.to change(cluster, :application_knative)
end
it 'sets the hostname' do
expect(subject.hostname).to eq('example.com')
end
2020-04-08 14:13:33 +05:30
it 'executes AssociateDomainService' do
expect(Serverless::AssociateDomainService).to receive(:new) do |knative, args|
expect(knative).to be_a(Clusters::Applications::Knative)
expect(args[:pages_domain_id]).to eq(params[:pages_domain_id])
expect(args[:creator]).to eq(user)
associate_domain_service
end
expect(associate_domain_service).to receive(:execute)
subject
end
2018-12-13 13:39:08 +05:30
end
2019-12-26 22:10:19 +05:30
context 'elastic stack application' do
let(:params) do
{
2020-03-13 15:44:24 +05:30
application: 'elastic_stack'
2019-12-26 22:10:19 +05:30
}
end
before do
create(:clusters_applications_ingress, :installed, external_ip: "127.0.0.0", cluster: cluster)
expect_any_instance_of(Clusters::Applications::ElasticStack)
.to receive(:make_scheduled!)
.and_call_original
end
it 'creates the application' do
expect do
subject
cluster.reload
end.to change(cluster, :application_elastic_stack)
end
end
2018-12-13 13:39:08 +05:30
end
context 'invalid application' do
let(:params) { { application: 'non-existent' } }
it 'raises an error' do
expect { subject }.to raise_error(Clusters::Applications::CreateService::InvalidApplicationError)
end
end
2019-02-15 15:39:39 +05:30
context 'group cluster' do
let(:cluster) { create(:cluster, :provided_by_gcp, :group) }
using RSpec::Parameterized::TableSyntax
2021-02-22 17:27:13 +05:30
where(:application, :association, :allowed, :pre_create_ingress) do
'ingress' | :application_ingress | true | false
'runner' | :application_runner | true | false
'prometheus' | :application_prometheus | true | false
'jupyter' | :application_jupyter | true | true
2019-05-30 16:15:17 +05:30
end
2019-05-18 00:54:41 +05:30
2019-05-30 16:15:17 +05:30
with_them do
2019-07-07 11:18:12 +05:30
before do
klass = "Clusters::Applications::#{application.titleize}"
allow_any_instance_of(klass.constantize).to receive(:make_scheduled!).and_call_original
2019-12-04 20:38:33 +05:30
create(:clusters_applications_ingress, :installed, cluster: cluster, external_hostname: 'example.com') if pre_create_ingress
2019-07-07 11:18:12 +05:30
end
2019-02-15 15:39:39 +05:30
let(:params) { { application: application } }
it 'executes for each application' do
if allowed
expect do
subject
cluster.reload
end.to change(cluster, association)
else
expect { subject }.to raise_error(Clusters::Applications::CreateService::InvalidApplicationError)
end
end
end
end
2019-07-07 11:18:12 +05:30
context 'when application is installable' do
shared_examples 'installable applications' do
it 'makes the application scheduled' do
expect do
subject
2021-02-22 17:27:13 +05:30
end.to change { Clusters::Applications::Ingress.with_status(:scheduled).count }.by(1)
2019-07-07 11:18:12 +05:30
end
it 'schedules an install via worker' do
expect(ClusterInstallAppWorker)
.to receive(:perform_async)
.with(*worker_arguments)
.once
subject
end
end
context 'when application is associated with a cluster' do
2021-02-22 17:27:13 +05:30
let(:application) { create(:clusters_applications_ingress, :installable, cluster: cluster) }
2019-07-07 11:18:12 +05:30
let(:worker_arguments) { [application.name, application.id] }
it_behaves_like 'installable applications'
end
context 'when application is not associated with a cluster' do
let(:worker_arguments) { [params[:application], kind_of(Numeric)] }
it_behaves_like 'installable applications'
end
end
context 'when installation is already in progress' do
2021-02-22 17:27:13 +05:30
let!(:application) { create(:clusters_applications_ingress, :installing, cluster: cluster) }
2019-07-07 11:18:12 +05:30
it 'raises an exception' do
expect { subject }
.to raise_exception(StateMachines::InvalidTransition)
.and not_change(application.class.with_status(:scheduled), :count)
end
it 'does not schedule a cluster worker' do
expect(ClusterInstallAppWorker).not_to receive(:perform_async)
end
end
context 'when application is installed' do
%i(installed updated).each do |status|
2021-02-22 17:27:13 +05:30
let(:application) { create(:clusters_applications_ingress, status, cluster: cluster) }
2019-07-07 11:18:12 +05:30
it 'schedules an upgrade via worker' do
expect(ClusterUpgradeAppWorker)
.to receive(:perform_async)
.with(application.name, application.id)
.once
subject
expect(application.reload).to be_scheduled
end
end
end
2018-12-13 13:39:08 +05:30
end
end