Commit e710e766 authored by Shinya Maeda's avatar Shinya Maeda

fetch_gcp_operation_service_spec. finalize_cluster_creation_service_spec....

fetch_gcp_operation_service_spec. finalize_cluster_creation_service_spec. wait_for_cluster_creation_worker_spec.
parent 8d9d0f94
require 'spec_helper' require 'spec_helper'
require 'google/apis'
describe Ci::FetchGcpOperationService do describe Ci::FetchGcpOperationService do
describe '#execute' do describe '#execute' do
context 'when correct params' do let(:cluster) { create(:gcp_cluster) }
it 'fetch the gcp operaion' do let(:operation) { double }
context 'when suceeded' do
before do
allow_any_instance_of(GoogleApi::CloudPlatform::Client)
.to receive(:projects_zones_operations).and_return(operation)
end
it 'fetch the gcp operaion' do
expect { |b| described_class.new.execute(cluster, &b) }
.to yield_with_args(operation)
end end
end end
context 'when invalid params' do context 'when raises an error' do
it 'sets an error to cluster object' do let(:error) { Google::Apis::ServerError.new('a') }
before do
allow_any_instance_of(GoogleApi::CloudPlatform::Client)
.to receive(:projects_zones_operations).and_raise(error)
end
it 'sets an error to cluster object' do
expect { |b| described_class.new.execute(cluster, &b) }
.not_to yield_with_args
expect(cluster.reload).to be_errored
end end
end end
end end
......
...@@ -2,15 +2,59 @@ require 'spec_helper' ...@@ -2,15 +2,59 @@ require 'spec_helper'
describe Ci::FinalizeClusterCreationService do describe Ci::FinalizeClusterCreationService do
describe '#execute' do describe '#execute' do
context 'when correct params' do let(:cluster) { create(:gcp_cluster) }
it 'execute IntegrateClusterService' do let(:result) { described_class.new.execute(cluster) }
context 'when suceeded to get cluster from api' do
let(:gke_cluster) { double }
before do
allow(gke_cluster).to receive(:endpoint).and_return('111.111.111.111')
allow(gke_cluster).to receive(:master_auth).and_return(spy)
allow_any_instance_of(GoogleApi::CloudPlatform::Client)
.to receive(:projects_zones_clusters_get).and_return(gke_cluster)
end
context 'when suceeded to get kubernetes token' do
let(:kubernetes_token) { 'abc' }
before do
allow_any_instance_of(Ci::FetchKubernetesTokenService)
.to receive(:execute).and_return(kubernetes_token)
end
it 'executes integration cluster' do
expect_any_instance_of(Ci::IntegrateClusterService).to receive(:execute)
described_class.new.execute(cluster)
end
end
context 'when failed to get kubernetes token' do
before do
allow_any_instance_of(Ci::FetchKubernetesTokenService)
.to receive(:execute).and_return(nil)
end
it 'sets an error to cluster object' do
described_class.new.execute(cluster)
expect(cluster.reload).to be_errored
end end
end end
end
context 'when failed to get cluster from api' do
let(:error) { Google::Apis::ServerError.new('a') }
before do
allow_any_instance_of(GoogleApi::CloudPlatform::Client)
.to receive(:projects_zones_clusters_get).and_raise(error)
end
context 'when invalid params' do it 'sets an error to cluster object' do
it 'returns a cluster object with error' do described_class.new.execute(cluster)
expect(cluster.reload).to be_errored
end end
end end
end end
......
...@@ -4,14 +4,54 @@ describe WaitForClusterCreationWorker do ...@@ -4,14 +4,54 @@ describe WaitForClusterCreationWorker do
describe '#perform' do describe '#perform' do
context 'when cluster exists' do context 'when cluster exists' do
let(:cluster) { create(:gcp_cluster) } let(:cluster) { create(:gcp_cluster) }
let(:operation) { double }
it 'fetches gcp operation status' do before do
expect_any_instance_of(Ci::FetchGcpOperationService).to receive(:execute) allow(operation).to receive(:status).and_return(status)
allow(operation).to receive(:start_time).and_return(1.minutes.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(WaitForClusterCreationWorker).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)
end
it 'sets an error message on cluster' do
described_class.new.perform(cluster.id)
expect(cluster.reload).to be_errored
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) described_class.new.perform(cluster.id)
end end
end
context 'when operation status is others' do
let(:status) { 'others' }
# TODO: context 'when operation.status is runnning' it 'sets an error message on cluster' do
described_class.new.perform(cluster.id)
expect(cluster.reload).to be_errored
end
end
end end
context 'when cluster does not exist' do context 'when cluster does not exist' do
......
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