Commit 22139d81 authored by Douwe Maan's avatar Douwe Maan

Merge branch 'dz-refactor-cluster-apps-schedule' into 'master'

Refactor code around scheduling cluster installations

See merge request gitlab-org/gitlab-ce!19108
parents 063ccb3e cf996685
...@@ -5,9 +5,10 @@ class Projects::Clusters::ApplicationsController < Projects::ApplicationControll ...@@ -5,9 +5,10 @@ class Projects::Clusters::ApplicationsController < Projects::ApplicationControll
before_action :authorize_create_cluster!, only: [:create] before_action :authorize_create_cluster!, only: [:create]
def create def create
Clusters::Applications::ScheduleInstallationService.new(project, current_user, application = @application_class.find_or_create_by!(cluster: @cluster)
application_class: @application_class,
cluster: @cluster).execute Clusters::Applications::ScheduleInstallationService.new(project, current_user).execute(application)
head :no_content head :no_content
rescue StandardError rescue StandardError
head :bad_request head :bad_request
......
module Clusters module Clusters
module Applications module Applications
class ScheduleInstallationService < ::BaseService class ScheduleInstallationService < ::BaseService
def execute def execute(application)
application_class.find_or_create_by!(cluster: cluster).try do |application| application.make_scheduled!
application.make_scheduled!
ClusterInstallAppWorker.perform_async(application.name, application.id)
end
end
private
def application_class
params[:application_class]
end
def cluster ClusterInstallAppWorker.perform_async(application.name, application.id)
params[:cluster]
end end
end end
end end
......
...@@ -2,7 +2,7 @@ require 'spec_helper' ...@@ -2,7 +2,7 @@ require 'spec_helper'
describe Clusters::Applications::ScheduleInstallationService do describe Clusters::Applications::ScheduleInstallationService do
def count_scheduled def count_scheduled
application_class&.with_status(:scheduled)&.count || 0 application&.class&.with_status(:scheduled)&.count || 0
end end
shared_examples 'a failing service' do shared_examples 'a failing service' do
...@@ -10,45 +10,42 @@ describe Clusters::Applications::ScheduleInstallationService do ...@@ -10,45 +10,42 @@ describe Clusters::Applications::ScheduleInstallationService do
expect(ClusterInstallAppWorker).not_to receive(:perform_async) expect(ClusterInstallAppWorker).not_to receive(:perform_async)
count_before = count_scheduled count_before = count_scheduled
expect { service.execute }.to raise_error(StandardError) expect { service.execute(application) }.to raise_error(StandardError)
expect(count_scheduled).to eq(count_before) expect(count_scheduled).to eq(count_before)
end end
end end
describe '#execute' do describe '#execute' do
let(:application_class) { Clusters::Applications::Helm } let(:project) { double(:project) }
let(:cluster) { create(:cluster, :project, :provided_by_gcp) } let(:service) { described_class.new(project, nil) }
let(:project) { cluster.project }
let(:service) { described_class.new(project, nil, cluster: cluster, application_class: application_class) }
it 'creates a new application' do context 'when application is installable' do
allow(ClusterInstallAppWorker).to receive(:perform_async) let(:application) { create(:clusters_applications_helm, :installable) }
expect { service.execute }.to change { application_class.count }.by(1) it 'make the application scheduled' do
end expect(ClusterInstallAppWorker).to receive(:perform_async).with(application.name, kind_of(Numeric)).once
it 'make the application scheduled' do
expect(ClusterInstallAppWorker).to receive(:perform_async).with(application_class.application_name, kind_of(Numeric)).once
expect { service.execute }.to change { application_class.with_status(:scheduled).count }.by(1) expect { service.execute(application) }.to change { application.class.with_status(:scheduled).count }.by(1)
end
end end
context 'when installation is already in progress' do context 'when installation is already in progress' do
let(:application) { create(:clusters_applications_helm, :installing) } let(:application) { create(:clusters_applications_helm, :installing) }
let(:cluster) { application.cluster }
it_behaves_like 'a failing service' it_behaves_like 'a failing service'
end end
context 'when application_class is nil' do context 'when application is nil' do
let(:application_class) { nil } let(:application) { nil }
it_behaves_like 'a failing service' it_behaves_like 'a failing service'
end end
context 'when application cannot be persisted' do context 'when application cannot be persisted' do
let(:application) { create(:clusters_applications_helm) }
before do before do
expect_any_instance_of(application_class).to receive(:make_scheduled!).once.and_raise(ActiveRecord::RecordInvalid) expect(application).to receive(:make_scheduled!).once.and_raise(ActiveRecord::RecordInvalid)
end end
it_behaves_like 'a failing service' it_behaves_like 'a failing service'
......
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