Commit 4934f607 authored by Shinya Maeda's avatar Shinya Maeda

specs for workers.

parent d6744d98
...@@ -4,8 +4,8 @@ class ClusterProvisionWorker ...@@ -4,8 +4,8 @@ class ClusterProvisionWorker
def perform(cluster_id) def perform(cluster_id)
Clusters::Cluster.find_by_id(cluster_id).try do |cluster| Clusters::Cluster.find_by_id(cluster_id).try do |cluster|
cluster.provider_gcp.try do |provider| cluster.provider.try do |provider|
Clusters::Gcp::ProvisionService.new.execute(provider) Clusters::Gcp::ProvisionService.new.execute(provider) if cluster.gcp?
end end
end end
end end
......
...@@ -4,8 +4,8 @@ class WaitForClusterCreationWorker ...@@ -4,8 +4,8 @@ class WaitForClusterCreationWorker
def perform(cluster_id) def perform(cluster_id)
Clusters::Cluster.find_by_id(cluster_id).try do |cluster| Clusters::Cluster.find_by_id(cluster_id).try do |cluster|
cluster.provider_gcp.try do |provider| cluster.provider.try do |provider|
Clusters::Gcp::VerifyProvisionStatusService.new.execute(provider) Clusters::Gcp::VerifyProvisionStatusService.new.execute(provider) if cluster.gcp?
end end
end end
end end
......
...@@ -2,11 +2,22 @@ require 'spec_helper' ...@@ -2,11 +2,22 @@ require 'spec_helper'
describe ClusterProvisionWorker do describe ClusterProvisionWorker do
describe '#perform' do describe '#perform' do
context 'when cluster exists' do context 'when provider type is gcp' do
let(:cluster) { create(:gcp_cluster) } let(:cluster) { create(:cluster, provider_type: :gcp, provider_gcp: provider) }
let(:provider) { create(:provider_gcp, :scheduled) }
it 'provision a cluster' do it 'provision a cluster' do
expect_any_instance_of(Ci::ProvisionClusterService).to receive(:execute) expect_any_instance_of(Clusters::Gcp::ProvisionService).to receive(:execute)
described_class.new.perform(cluster.id)
end
end
context 'when provider type is user' do
let(:cluster) { create(:cluster, provider_type: :user) }
it 'does not provision a cluster' do
expect_any_instance_of(Clusters::Gcp::ProvisionService).not_to receive(:execute)
described_class.new.perform(cluster.id) described_class.new.perform(cluster.id)
end end
...@@ -14,7 +25,7 @@ describe ClusterProvisionWorker do ...@@ -14,7 +25,7 @@ describe ClusterProvisionWorker do
context 'when cluster does not exist' do context 'when cluster does not exist' do
it 'does not provision a cluster' do it 'does not provision a cluster' do
expect_any_instance_of(Ci::ProvisionClusterService).not_to receive(:execute) expect_any_instance_of(Clusters::Gcp::ProvisionService).not_to receive(:execute)
described_class.new.perform(123) described_class.new.perform(123)
end end
......
...@@ -2,65 +2,32 @@ require 'spec_helper' ...@@ -2,65 +2,32 @@ require 'spec_helper'
describe WaitForClusterCreationWorker do describe WaitForClusterCreationWorker do
describe '#perform' do describe '#perform' do
context 'when cluster exists' do context 'when provider type is gcp' do
let(:cluster) { create(:gcp_cluster) } let(:cluster) { create(:cluster, provider_type: :gcp, provider_gcp: provider) }
let(:operation) { double } let(:provider) { create(:provider_gcp, :creating) }
before do it 'provision a cluster' do
allow(operation).to receive(:status).and_return(status) expect_any_instance_of(Clusters::Gcp::VerifyProvisionStatusService).to receive(:execute)
allow(operation).to receive(:start_time).and_return(1.minute.ago)
allow(operation).to receive(:status_message).and_return('error')
allow_any_instance_of(Ci::FetchGcpOperationService).to receive(:execute).and_yield(operation)
end
context 'when operation status is RUNNING' do
let(:status) { 'RUNNING' }
it 'reschedules worker' do
expect(described_class).to receive(:perform_in)
described_class.new.perform(cluster.id)
end
context 'when operation timeout' do
before do
allow(operation).to receive(:start_time).and_return(30.minutes.ago.utc)
end
it 'sets an error message on cluster' do
described_class.new.perform(cluster.id)
expect(cluster.reload).to be_errored described_class.new.perform(cluster.id)
end
end
end
context 'when operation status is DONE' do
let(:status) { 'DONE' }
it 'finalizes cluster creation' do
expect_any_instance_of(Ci::FinalizeClusterCreationService).to receive(:execute)
described_class.new.perform(cluster.id)
end
end end
end
context 'when operation status is others' do context 'when provider type is user' do
let(:status) { 'others' } let(:cluster) { create(:cluster, provider_type: :user) }
it 'sets an error message on cluster' do it 'does not provision a cluster' do
described_class.new.perform(cluster.id) expect_any_instance_of(Clusters::Gcp::VerifyProvisionStatusService).not_to receive(:execute)
expect(cluster.reload).to be_errored described_class.new.perform(cluster.id)
end
end end
end end
context 'when cluster does not exist' do context 'when cluster does not exist' do
it 'does not provision a cluster' do it 'does not provision a cluster' do
expect_any_instance_of(Ci::FetchGcpOperationService).not_to receive(:execute) expect_any_instance_of(Clusters::Gcp::VerifyProvisionStatusService).not_to receive(:execute)
described_class.new.perform(1234) described_class.new.perform(123)
end end
end end
end end
......
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