Commit cb3324d3 authored by Sean McGivern's avatar Sean McGivern

Merge branch 'refactor/get_rid_of_schedule_installation_service' into 'master'

Get rid of ScheduleInstallationService

See merge request gitlab-org/gitlab-ce!25420
parents b7a3c8f6 4c974f50
...@@ -27,9 +27,11 @@ module Clusters ...@@ -27,9 +27,11 @@ module Clusters
application.oauth_application = create_oauth_application(application, request) application.oauth_application = create_oauth_application(application, request)
end end
application.save! worker = application.updateable? ? ClusterUpgradeAppWorker : ClusterInstallAppWorker
Clusters::Applications::ScheduleInstallationService.new(application).execute application.make_scheduled!
worker.perform_async(application.name, application.id)
end end
end end
......
# frozen_string_literal: true
module Clusters
module Applications
class ScheduleInstallationService
attr_reader :application
def initialize(application)
@application = application
end
def execute
application.updateable? ? schedule_upgrade : schedule_install
end
private
def schedule_upgrade
application.make_scheduled!
ClusterUpgradeAppWorker.perform_async(application.name, application.id)
end
def schedule_install
application.make_scheduled!
ClusterInstallAppWorker.perform_async(application.name, application.id)
end
end
end
end
require 'spec_helper' require 'spec_helper'
describe Gitlab::Profiler do describe Gitlab::Profiler do
RSpec::Matchers.define_negated_matcher :not_change, :change
let(:null_logger) { Logger.new('/dev/null') } let(:null_logger) { Logger.new('/dev/null') }
let(:private_token) { 'private' } let(:private_token) { 'private' }
...@@ -187,7 +185,7 @@ describe Gitlab::Profiler do ...@@ -187,7 +185,7 @@ describe Gitlab::Profiler do
end end
it 'does not modify the standard Rails loggers' do it 'does not modify the standard Rails loggers' do
expect { described_class.with_custom_logger(nil) { } } expect { described_class.with_custom_logger(nil) {} }
.to not_change { ActiveRecord::Base.logger } .to not_change { ActiveRecord::Base.logger }
.and not_change { ActionController::Base.logger } .and not_change { ActionController::Base.logger }
.and not_change { ActiveSupport::LogSubscriber.colorize_logging } .and not_change { ActiveSupport::LogSubscriber.colorize_logging }
...@@ -204,7 +202,7 @@ describe Gitlab::Profiler do ...@@ -204,7 +202,7 @@ describe Gitlab::Profiler do
end end
it 'cleans up ApplicationController afterwards' do it 'cleans up ApplicationController afterwards' do
expect { described_class.with_user(user) { } } expect { described_class.with_user(user) {} }
.to not_change { ActionController.instance_methods(false) } .to not_change { ActionController.instance_methods(false) }
end end
end end
...@@ -213,7 +211,7 @@ describe Gitlab::Profiler do ...@@ -213,7 +211,7 @@ describe Gitlab::Profiler do
it 'does not define methods on ApplicationController' do it 'does not define methods on ApplicationController' do
expect(ApplicationController).not_to receive(:define_method) expect(ApplicationController).not_to receive(:define_method)
described_class.with_user(nil) { } described_class.with_user(nil) {}
end end
end end
end end
......
...@@ -26,12 +26,6 @@ describe Clusters::Applications::CreateService do ...@@ -26,12 +26,6 @@ describe Clusters::Applications::CreateService do
end.to change(cluster, :application_helm) end.to change(cluster, :application_helm)
end end
it 'schedules an install via worker' do
expect(ClusterInstallAppWorker).to receive(:perform_async).with('helm', anything).once
subject
end
context 'application already installed' do context 'application already installed' do
let!(:application) { create(:clusters_applications_helm, :installed, cluster: cluster) } let!(:application) { create(:clusters_applications_helm, :installed, cluster: cluster) }
...@@ -42,88 +36,101 @@ describe Clusters::Applications::CreateService do ...@@ -42,88 +36,101 @@ describe Clusters::Applications::CreateService do
end end
it 'schedules an upgrade for the application' do it 'schedules an upgrade for the application' do
expect(Clusters::Applications::ScheduleInstallationService).to receive(:new).with(application).and_call_original expect(ClusterUpgradeAppWorker).to receive(:perform_async)
subject subject
end end
end end
context 'cert manager application' do context 'known applications' do
let(:params) do
{
application: 'cert_manager',
email: 'test@example.com'
}
end
before do before do
allow_any_instance_of(Clusters::Applications::ScheduleInstallationService).to receive(:execute) create(:clusters_applications_helm, :installed, cluster: cluster)
end end
it 'creates the application' do context 'cert manager application' do
expect do let(:params) do
subject {
application: 'cert_manager',
email: 'test@example.com'
}
end
cluster.reload before do
end.to change(cluster, :application_cert_manager) expect_any_instance_of(Clusters::Applications::CertManager)
end .to receive(:make_scheduled!)
.and_call_original
end
it 'sets the email' do it 'creates the application' do
expect(subject.email).to eq('test@example.com') expect do
end subject
end
context 'jupyter application' do cluster.reload
let(:params) do end.to change(cluster, :application_cert_manager)
{ end
application: 'jupyter',
hostname: 'example.com'
}
end
before do it 'sets the email' do
allow_any_instance_of(Clusters::Applications::ScheduleInstallationService).to receive(:execute) expect(subject.email).to eq('test@example.com')
end
end end
it 'creates the application' do context 'jupyter application' do
expect do let(:params) do
subject {
application: 'jupyter',
hostname: 'example.com'
}
end
cluster.reload before do
end.to change(cluster, :application_jupyter) create(:clusters_applications_ingress, :installed, external_ip: "127.0.0.0", cluster: cluster)
end expect_any_instance_of(Clusters::Applications::Jupyter)
.to receive(:make_scheduled!)
.and_call_original
end
it 'sets the hostname' do it 'creates the application' do
expect(subject.hostname).to eq('example.com') expect do
end subject
it 'sets the oauth_application' do cluster.reload
expect(subject.oauth_application).to be_present end.to change(cluster, :application_jupyter)
end end
end
context 'knative application' do it 'sets the hostname' do
let(:params) do expect(subject.hostname).to eq('example.com')
{ end
application: 'knative',
hostname: 'example.com'
}
end
before do it 'sets the oauth_application' do
allow_any_instance_of(Clusters::Applications::ScheduleInstallationService).to receive(:execute) expect(subject.oauth_application).to be_present
end
end end
it 'creates the application' do context 'knative application' do
expect do let(:params) do
subject {
application: 'knative',
hostname: 'example.com'
}
end
cluster.reload before do
end.to change(cluster, :application_knative) expect_any_instance_of(Clusters::Applications::Knative)
end .to receive(:make_scheduled!)
.and_call_original
end
it 'sets the hostname' do it 'creates the application' do
expect(subject.hostname).to eq('example.com') 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
end end
end end
...@@ -140,19 +147,21 @@ describe Clusters::Applications::CreateService do ...@@ -140,19 +147,21 @@ describe Clusters::Applications::CreateService do
using RSpec::Parameterized::TableSyntax using RSpec::Parameterized::TableSyntax
before do where(:application, :association, :allowed, :pre_create_helm) do
allow_any_instance_of(Clusters::Applications::ScheduleInstallationService).to receive(:execute) 'helm' | :application_helm | true | false
end 'ingress' | :application_ingress | true | true
'runner' | :application_runner | false | true
where(:application, :association, :allowed) do 'jupyter' | :application_jupyter | false | true
'helm' | :application_helm | true 'prometheus' | :application_prometheus | false | true
'ingress' | :application_ingress | true
'runner' | :application_runner | false
'jupyter' | :application_jupyter | false
'prometheus' | :application_prometheus | false
end end
with_them do with_them do
before do
klass = "Clusters::Applications::#{application.titleize}"
allow_any_instance_of(klass.constantize).to receive(:make_scheduled!).and_call_original
create(:clusters_applications_helm, :installed, cluster: cluster) if pre_create_helm
end
let(:params) { { application: application } } let(:params) { { application: application } }
it 'executes for each application' do it 'executes for each application' do
...@@ -168,5 +177,68 @@ describe Clusters::Applications::CreateService do ...@@ -168,5 +177,68 @@ describe Clusters::Applications::CreateService do
end end
end end
end end
context 'when application is installable' do
shared_examples 'installable applications' do
it 'makes the application scheduled' do
expect do
subject
end.to change { Clusters::Applications::Helm.with_status(:scheduled).count }.by(1)
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
let(:application) { create(:clusters_applications_helm, :installable, cluster: cluster) }
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
let!(:application) { create(:clusters_applications_helm, :installing, cluster: cluster) }
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|
let(:application) { create(:clusters_applications_helm, status, cluster: cluster) }
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
end end
end end
require 'spec_helper'
describe Clusters::Applications::ScheduleInstallationService do
def count_scheduled
application&.class&.with_status(:scheduled)&.count || 0
end
shared_examples 'a failing service' do
it 'raise an exception' do
expect(ClusterInstallAppWorker).not_to receive(:perform_async)
count_before = count_scheduled
expect { service.execute }.to raise_error(StandardError)
expect(count_scheduled).to eq(count_before)
end
end
describe '#execute' do
let(:service) { described_class.new(application) }
context 'when application is installable' do
let(:application) { create(:clusters_applications_helm, :installable) }
it 'make the application scheduled' do
expect(ClusterInstallAppWorker).to receive(:perform_async).with(application.name, kind_of(Numeric)).once
expect { service.execute }.to change { application.class.with_status(:scheduled).count }.by(1)
end
end
context 'when installation is already in progress' do
let(:application) { create(:clusters_applications_helm, :installing) }
it_behaves_like 'a failing service'
end
context 'when application is nil' do
let(:application) { nil }
it_behaves_like 'a failing service'
end
context 'when application cannot be persisted' do
let(:application) { create(:clusters_applications_helm) }
before do
expect(application).to receive(:make_scheduled!).once.and_raise(ActiveRecord::RecordInvalid)
end
it_behaves_like 'a failing service'
end
context 'when application is installed' do
let(:application) { create(:clusters_applications_helm, :installed) }
it 'schedules an upgrade via worker' do
expect(ClusterUpgradeAppWorker).to receive(:perform_async).with(application.name, application.id).once
service.execute
expect(application).to be_scheduled
end
end
context 'when application is updated' do
let(:application) { create(:clusters_applications_helm, :updated) }
it 'schedules an upgrade via worker' do
expect(ClusterUpgradeAppWorker).to receive(:perform_async).with(application.name, application.id).once
service.execute
expect(application).to be_scheduled
end
end
end
end
# frozen_string_literal: true
RSpec::Matchers.define_negated_matcher :not_change, :change
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment