diff --git a/app/models/gcp/cluster.rb b/app/models/gcp/cluster.rb index fceda191880..0e8b920741d 100644 --- a/app/models/gcp/cluster.rb +++ b/app/models/gcp/cluster.rb @@ -97,7 +97,7 @@ module Gcp end def api_url - 'https://' + endpoint + 'https://' + endpoint if endpoint end def restrict_modification diff --git a/app/services/ci/integrate_cluster_service.rb b/app/services/ci/integrate_cluster_service.rb index 857bca3f953..d123ce8d26b 100644 --- a/app/services/ci/integrate_cluster_service.rb +++ b/app/services/ci/integrate_cluster_service.rb @@ -20,7 +20,7 @@ module Ci token: token) end rescue ActiveRecord::RecordInvalid => e - cluster.error!("Failed to integrate cluster into kubernetes_service: #{e.message}") + cluster.make_errored!("Failed to integrate cluster into kubernetes_service: #{e.message}") end end end diff --git a/spec/factories/gcp/cluster.rb b/spec/factories/gcp/cluster.rb index 2f98d024084..630e40da888 100644 --- a/spec/factories/gcp/cluster.rb +++ b/spec/factories/gcp/cluster.rb @@ -7,9 +7,18 @@ FactoryGirl.define do gcp_cluster_name 'test-cluster' gcp_cluster_zone 'us-central1-a' gcp_cluster_size 1 + gcp_machine_type 'n1-standard-4' trait :with_kubernetes_service do - service :kubernetes_service + after(:create) do |cluster, evaluator| + create(:kubernetes_service, project: cluster.project).tap do |service| + cluster.update(service: service) + end + end + end + + trait :custom_project_namespace do + project_namespace 'sample-app' end trait :created_on_gke do diff --git a/spec/services/ci/integrate_cluster_service_spec.rb b/spec/services/ci/integrate_cluster_service_spec.rb index 7a8c80ca4e7..3a79c205bd1 100644 --- a/spec/services/ci/integrate_cluster_service_spec.rb +++ b/spec/services/ci/integrate_cluster_service_spec.rb @@ -2,15 +2,40 @@ require 'spec_helper' describe Ci::IntegrateClusterService do describe '#execute' do + let(:cluster) { create(:gcp_cluster, :custom_project_namespace) } + let(:endpoint) { '123.123.123.123' } + let(:ca_cert) { 'ca_cert_xxx' } + let(:token) { 'token_xxx' } + let(:username) { 'username_xxx' } + let(:password) { 'password_xxx' } + + before do + described_class + .new.execute(cluster, endpoint, ca_cert, token, username, password) + + cluster.reload + end + context 'when correct params' do it 'creates a cluster object' do - + expect(cluster.endpoint).to eq(endpoint) + expect(cluster.ca_cert).to eq(ca_cert) + expect(cluster.kubernetes_token).to eq(token) + expect(cluster.username).to eq(username) + expect(cluster.password).to eq(password) + expect(cluster.service.active).to be_truthy + expect(cluster.service.api_url).to eq(cluster.api_url) + expect(cluster.service.ca_pem).to eq(ca_cert) + expect(cluster.service.namespace).to eq(cluster.project_namespace) + expect(cluster.service.token).to eq(token) end end context 'when invalid params' do - it 'returns a cluster object with error' do + let(:endpoint) { nil } + it 'sets an error to cluster object' do + expect(cluster).to be_errored end end end diff --git a/spec/services/ci/provision_cluster_service_spec.rb b/spec/services/ci/provision_cluster_service_spec.rb index d1595c8917d..5ce5c788314 100644 --- a/spec/services/ci/provision_cluster_service_spec.rb +++ b/spec/services/ci/provision_cluster_service_spec.rb @@ -2,16 +2,84 @@ require 'spec_helper' describe Ci::ProvisionClusterService do describe '#execute' do - context 'when correct params' do - it 'creates a cluster on gke' do - + let(:cluster) { create(:gcp_cluster) } + let(:operation) { spy } + + shared_examples 'error' do + it 'sets an error to cluster object' do + described_class.new.execute(cluster) + + expect(cluster.reload).to be_errored end end - context 'when invalid params' do - it 'returns a cluster object with error' do - + context 'when suceeded to request provision' do + before do + allow_any_instance_of(GoogleApi::CloudPlatform::Client) + .to receive(:projects_zones_clusters_create).and_return(operation) end + + context 'when operation status is RUNNING' do + before do + allow(operation).to receive(:status).and_return('RUNNING') + end + + context 'when suceeded to parse gcp operation id' do + before do + allow_any_instance_of(GoogleApi::CloudPlatform::Client) + .to receive(:parse_operation_id).and_return('operation-123') + end + + context 'when cluster status is scheduled' do + before do + allow_any_instance_of(GoogleApi::CloudPlatform::Client) + .to receive(:parse_operation_id).and_return('operation-123') + end + + it 'schedules a worker for status minitoring' do + expect(WaitForClusterCreationWorker).to receive(:perform_in) + + described_class.new.execute(cluster) + end + end + + context 'when cluster status is creating' do + before do + cluster.make_creating! + end + + it_behaves_like 'error' + end + end + + context 'when failed to parse gcp operation id' do + before do + allow_any_instance_of(GoogleApi::CloudPlatform::Client) + .to receive(:parse_operation_id).and_return(nil) + end + + it_behaves_like 'error' + end + end + + context 'when operation status is others' do + before do + allow(operation).to receive(:status).and_return('others') + end + + it_behaves_like 'error' + end + end + + context 'when failed to request provision' do + let(:error) { Google::Apis::ServerError.new('a') } + + before do + allow_any_instance_of(GoogleApi::CloudPlatform::Client) + .to receive(:projects_zones_clusters_create).and_raise(error) + end + + it_behaves_like 'error' end end end diff --git a/spec/services/ci/update_cluster_service_spec.rb b/spec/services/ci/update_cluster_service_spec.rb index afe3d418a8e..a289385b88f 100644 --- a/spec/services/ci/update_cluster_service_spec.rb +++ b/spec/services/ci/update_cluster_service_spec.rb @@ -2,15 +2,35 @@ require 'spec_helper' describe Ci::UpdateClusterService do describe '#execute' do - context 'when correct params' do - it 'updates the cluster and the service' do - - end + let(:cluster) { create(:gcp_cluster, :created_on_gke, :with_kubernetes_service) } + + before do + described_class.new(cluster.project, cluster.user, params).execute(cluster) + + cluster.reload end - context 'when invalid params' do - it 'returns a cluster object with error' do + context 'when correct params' do + context 'when enabled is true' do + let(:params) { { 'enabled' => 'true' } } + it 'enables cluster and overwrite kubernetes service' do + expect(cluster.enabled).to be_truthy + expect(cluster.service.active).to be_truthy + expect(cluster.service.api_url).to eq(cluster.api_url) + expect(cluster.service.ca_pem).to eq(cluster.ca_cert) + expect(cluster.service.namespace).to eq(cluster.project_namespace) + expect(cluster.service.token).to eq(cluster.kubernetes_token) + end + end + + context 'when enabled is false' do + let(:params) { { 'enabled' => 'false' } } + + it 'disables cluster and kubernetes service' do + expect(cluster.enabled).to be_falsy + expect(cluster.service.active).to be_falsy + end end end end