Commit 253bf69d authored by Shinya Maeda's avatar Shinya Maeda

specs for feature

parent 6a65e2f5
...@@ -27,14 +27,10 @@ class Projects::ClustersController < Projects::ApplicationController ...@@ -27,14 +27,10 @@ class Projects::ClustersController < Projects::ApplicationController
end end
def new def new
# @cluster = Clusters::Cluster.new( @cluster = Clusters::Cluster.new.tap do |cluster|
# platform_type: :kubernetes, cluster.build_provider_gcp
# provider_type: :gcp).tap do |cluster| cluster.build_platform_kubernetes
# cluster.build_provider_gcp end
# cluster.build_platform_kubernetes
# cluster.projects << project
# end
@cluster = Clusters::Cluster.new
end end
def create def create
......
...@@ -10,7 +10,7 @@ module Clusters ...@@ -10,7 +10,7 @@ module Clusters
has_many :projects, through: :cluster_projects, class_name: '::Project' has_many :projects, through: :cluster_projects, class_name: '::Project'
has_one :provider_gcp, class_name: 'Clusters::Providers::Gcp' has_one :provider_gcp, class_name: 'Clusters::Providers::Gcp'
has_one :platform_kubernetes, class_name: 'Clusters::Platforms::Kubernetes', validate: { if: :update } has_one :platform_kubernetes, class_name: 'Clusters::Platforms::Kubernetes'
accepts_nested_attributes_for :provider_gcp, update_only: true accepts_nested_attributes_for :provider_gcp, update_only: true
accepts_nested_attributes_for :platform_kubernetes, update_only: true accepts_nested_attributes_for :platform_kubernetes, update_only: true
......
...@@ -30,8 +30,10 @@ module Clusters ...@@ -30,8 +30,10 @@ module Clusters
message: Gitlab::Regex.kubernetes_namespace_regex_message message: Gitlab::Regex.kubernetes_namespace_regex_message
} }
validates :api_url, url: true, presence: true # TODO: when cluster.gcp? skip validation when create a record
validates :token, presence: true # TODO: when cluster.user? validates always
# validates :api_url, url: true, presence: true
# validates :token, presence: true
after_save :clear_reactive_cache! after_save :clear_reactive_cache!
......
...@@ -29,8 +29,7 @@ module Clusters ...@@ -29,8 +29,7 @@ module Clusters
return @cluster_params if defined?(@cluster_params) return @cluster_params if defined?(@cluster_params)
params[:provider_gcp_attributes].try do |h| params[:provider_gcp_attributes].try do |h|
h[:machine_type] ||= GoogleApi::CloudPlatform::Client::DEFAULT_MACHINE_TYPE h[:access_token] = access_token
h[:access_token] ||= access_token
end end
@cluster_params = params.merge(user: current_user) @cluster_params = params.merge(user: current_user)
......
...@@ -3,7 +3,6 @@ require 'google/apis/container_v1' ...@@ -3,7 +3,6 @@ require 'google/apis/container_v1'
module GoogleApi module GoogleApi
module CloudPlatform module CloudPlatform
class Client < GoogleApi::Auth class Client < GoogleApi::Auth
DEFAULT_MACHINE_TYPE = 'n1-standard-1'.freeze
SCOPE = 'https://www.googleapis.com/auth/cloud-platform'.freeze SCOPE = 'https://www.googleapis.com/auth/cloud-platform'.freeze
LEAST_TOKEN_LIFE_TIME = 10.minutes LEAST_TOKEN_LIFE_TIME = 10.minutes
......
...@@ -208,7 +208,12 @@ describe Projects::ClustersController do ...@@ -208,7 +208,12 @@ describe Projects::ClustersController do
cluster: { cluster: {
name: 'new-cluster', name: 'new-cluster',
platform_type: :kubernetes, platform_type: :kubernetes,
provider_type: :user provider_type: :user,
platform_kubernetes_attributes: {
namespace: 'custom-namespace',
api_url: 'https://111.111.111.111',
token: 'token'
}
} }
} }
end end
......
require 'spec_helper' require 'spec_helper'
feature 'Clusters', :js do feature 'Clusters', :js do
include GoogleApi::CloudPlatformHelpers
let!(:project) { create(:project, :repository) } let!(:project) { create(:project, :repository) }
let!(:user) { create(:user) } let!(:user) { create(:user) }
...@@ -11,8 +13,10 @@ feature 'Clusters', :js do ...@@ -11,8 +13,10 @@ feature 'Clusters', :js do
context 'when user has signed in Google' do context 'when user has signed in Google' do
before do before do
allow_any_instance_of(GoogleApi::CloudPlatform::Client) allow_any_instance_of(Projects::ClustersController)
.to receive(:validate_token).and_return(true) .to receive(:token_in_session).and_return('token')
allow_any_instance_of(Projects::ClustersController)
.to receive(:expires_at_in_session).and_return(1.hour.since.to_i.to_s)
end end
context 'when user does not have a cluster and visits cluster index page' do context 'when user does not have a cluster and visits cluster index page' do
...@@ -36,15 +40,15 @@ feature 'Clusters', :js do ...@@ -36,15 +40,15 @@ feature 'Clusters', :js do
allow(WaitForClusterCreationWorker).to receive(:perform_in).and_return(nil) allow(WaitForClusterCreationWorker).to receive(:perform_in).and_return(nil)
fill_in 'cluster_gcp_project_id', with: 'gcp-project-123' fill_in 'cluster_provider_gcp_attributes_gcp_project_id', with: 'gcp-project-123'
fill_in 'cluster_gcp_cluster_name', with: 'dev-cluster' fill_in 'cluster_name', with: 'dev-cluster'
click_button 'Create cluster' click_button 'Create cluster'
end end
it 'user sees a cluster details page and creation status' do it 'user sees a cluster details page and creation status' do
expect(page).to have_content('Cluster is being created on Google Container Engine...') expect(page).to have_content('Cluster is being created on Google Container Engine...')
Gcp::Cluster.last.make_created! Clusters::Cluster.last.provider.make_created!
expect(page).to have_content('Cluster was successfully created on Google Container Engine') expect(page).to have_content('Cluster was successfully created on Google Container Engine')
end end
...@@ -62,7 +66,8 @@ feature 'Clusters', :js do ...@@ -62,7 +66,8 @@ feature 'Clusters', :js do
end end
context 'when user has a cluster and visits cluster index page' do context 'when user has a cluster and visits cluster index page' do
let!(:cluster) { create(:gcp_cluster, :created_on_gke, :with_kubernetes_service, project: project) } let!(:cluster) { create(:cluster, :project, :provided_by_gcp) }
let(:project) { cluster.project }
before do before do
visit project_clusters_path(project) visit project_clusters_path(project)
...@@ -70,7 +75,7 @@ feature 'Clusters', :js do ...@@ -70,7 +75,7 @@ feature 'Clusters', :js do
it 'user sees an cluster details page' do it 'user sees an cluster details page' do
expect(page).to have_button('Save') expect(page).to have_button('Save')
expect(page.find(:css, '.cluster-name').value).to eq(cluster.gcp_cluster_name) expect(page.find(:css, '.cluster-name').value).to eq(cluster.name)
end end
context 'when user disables the cluster' do context 'when user disables the cluster' do
......
...@@ -51,45 +51,45 @@ describe Clusters::Platforms::Kubernetes, :use_clean_rails_memory_store_caching ...@@ -51,45 +51,45 @@ describe Clusters::Platforms::Kubernetes, :use_clean_rails_memory_store_caching
end end
end end
context 'when validates api_url' do # context 'when validates api_url' do
let(:kubernetes) { build(:platform_kubernetes, :configured) } # let(:kubernetes) { build(:platform_kubernetes, :configured) }
before do # before do
kubernetes.api_url = api_url # kubernetes.api_url = api_url
end # end
context 'when api_url is invalid url' do # context 'when api_url is invalid url' do
let(:api_url) { '!!!!!!' } # let(:api_url) { '!!!!!!' }
it { expect(kubernetes.save).to be_falsey } # it { expect(kubernetes.save).to be_falsey }
end # end
context 'when api_url is nil' do # context 'when api_url is nil' do
let(:api_url) { nil } # let(:api_url) { nil }
it { expect(kubernetes.save).to be_falsey } # it { expect(kubernetes.save).to be_falsey }
end # end
context 'when api_url is valid url' do # context 'when api_url is valid url' do
let(:api_url) { 'https://111.111.111.111' } # let(:api_url) { 'https://111.111.111.111' }
it { expect(kubernetes.save).to be_truthy } # it { expect(kubernetes.save).to be_truthy }
end # end
end # end
context 'when validates token' do # context 'when validates token' do
let(:kubernetes) { build(:platform_kubernetes, :configured) } # let(:kubernetes) { build(:platform_kubernetes, :configured) }
before do # before do
kubernetes.token = token # kubernetes.token = token
end # end
context 'when token is nil' do # context 'when token is nil' do
let(:token) { nil } # let(:token) { nil }
it { expect(kubernetes.save).to be_falsey } # it { expect(kubernetes.save).to be_falsey }
end # end
end # end
end end
describe '#actual_namespace' do describe '#actual_namespace' do
......
...@@ -156,7 +156,7 @@ describe KubernetesService, :use_clean_rails_memory_store_caching do ...@@ -156,7 +156,7 @@ describe KubernetesService, :use_clean_rails_memory_store_caching do
let(:discovery_url) { 'https://kubernetes.example.com/api/v1' } let(:discovery_url) { 'https://kubernetes.example.com/api/v1' }
before do before do
stub_kubeclient_discover stub_kubeclient_discover(service.api_url)
end end
context 'with path prefix in api_url' do context 'with path prefix in api_url' do
...@@ -164,7 +164,7 @@ describe KubernetesService, :use_clean_rails_memory_store_caching do ...@@ -164,7 +164,7 @@ describe KubernetesService, :use_clean_rails_memory_store_caching do
it 'tests with the prefix' do it 'tests with the prefix' do
service.api_url = 'https://kubernetes.example.com/prefix' service.api_url = 'https://kubernetes.example.com/prefix'
stub_kubeclient_discover stub_kubeclient_discover(service.api_url)
expect(service.test[:success]).to be_truthy expect(service.test[:success]).to be_truthy
expect(WebMock).to have_requested(:get, discovery_url).once expect(WebMock).to have_requested(:get, discovery_url).once
......
...@@ -6,57 +6,122 @@ describe Clusters::CreateService do ...@@ -6,57 +6,122 @@ describe Clusters::CreateService do
let(:user) { create(:user) } let(:user) { create(:user) }
let(:result) { described_class.new(project, user, params).execute(access_token) } let(:result) { described_class.new(project, user, params).execute(access_token) }
context 'when correct params' do context 'when provider is gcp' do
let(:params) do context 'when correct params' do
{ let(:params) do
name: 'test-cluster', {
platform_type: :kubernetes, name: 'test-cluster',
provider_type: :gcp, platform_type: :kubernetes,
platform_kubernetes_attributes: { provider_type: :gcp,
namespace: 'custom-namespace' platform_kubernetes_attributes: {
}, namespace: 'custom-namespace'
provider_gcp_attributes: { },
gcp_project_id: 'gcp-project', provider_gcp_attributes: {
zone: 'us-central1-a', gcp_project_id: 'gcp-project',
num_nodes: 1, zone: 'us-central1-a',
machine_type: 'machine_type-a' num_nodes: 1,
machine_type: 'machine_type-a'
}
} }
} end
it 'creates a cluster object and performs a worker' do
expect(ClusterProvisionWorker).to receive(:perform_async)
expect { result }
.to change { Clusters::Cluster.count }.by(1)
.and change { Clusters::Platforms::Kubernetes.count }.by(1)
.and change { Clusters::Providers::Gcp.count }.by(1)
expect(result.name).to eq('test-cluster')
expect(result.user).to eq(user)
expect(result.project).to eq(project)
expect(result.provider.gcp_project_id).to eq('gcp-project')
expect(result.provider.zone).to eq('us-central1-a')
expect(result.provider.num_nodes).to eq(1)
expect(result.provider.machine_type).to eq('machine_type-a')
expect(result.provider.access_token).to eq(access_token)
expect(result.platform.namespace).to eq('custom-namespace')
expect(result.platform.valid?).to be_falsey
end
end end
it 'creates a cluster object and performs a worker' do context 'when invalid params' do
expect(ClusterProvisionWorker).to receive(:perform_async) let(:params) do
expect { result }.to change { Clusters::Cluster.count }.by(1) {
expect(result.name).to eq('test-cluster') name: 'test-cluster',
expect(result.user).to eq(user) platform_type: :kubernetes,
expect(result.project).to eq(project) provider_type: :gcp,
expect(result.provider.gcp_project_id).to eq('gcp-project') platform_kubernetes_attributes: {
expect(result.provider.zone).to eq('us-central1-a') namespace: 'custom-namespace'
expect(result.provider.num_nodes).to eq(1) },
expect(result.provider.machine_type).to eq('machine_type-a') provider_gcp_attributes: {
expect(result.provider.access_token).to eq(access_token) gcp_project_id: '!!!!!!!',
expect(result.platform.namespace).to eq('custom-namespace') zone: 'us-central1-a',
num_nodes: 1,
machine_type: 'machine_type-a'
}
}
end
it 'returns an error' do
expect(ClusterProvisionWorker).not_to receive(:perform_async)
expect { result }.to change { Clusters::Cluster.count }.by(0)
expect(result.errors[:"provider_gcp.gcp_project_id"]).to be_present
end
end end
end end
context 'when invalid params' do context 'when provider is user' do
let(:params) do context 'when correct params' do
{ let(:params) do
name: 'test-cluster', {
platform_type: :kubernetes, name: 'test-cluster',
provider_type: :user, platform_type: :kubernetes,
provider_gcp_attributes: { provider_type: :user,
gcp_project_id: 'gcp-project', platform_kubernetes_attributes: {
zone: 'us-central1-a', namespace: 'custom-namespace',
num_nodes: 'ABC' api_url: 'https://111.111.111.111',
token: 'token'
}
} }
} end
it 'creates a cluster object and performs a worker' do
expect(ClusterProvisionWorker).to receive(:perform_async)
expect { result }
.to change { Clusters::Cluster.count }.by(1)
.and change { Clusters::Platforms::Kubernetes.count }.by(1)
expect(result.name).to eq('test-cluster')
expect(result.user).to eq(user)
expect(result.project).to eq(project)
expect(result.provider).to be_nil
expect(result.platform.namespace).to eq('custom-namespace')
expect(result.platform.valid?).to be_truthy
end
end end
it 'returns an error' do context 'when invalid params' do
expect(ClusterProvisionWorker).not_to receive(:perform_async) let(:params) do
expect { result }.to change { Clusters::Cluster.count }.by(0) {
expect(result.errors[:"provider_gcp.num_nodes"]).to be_present name: 'test-cluster',
platform_type: :kubernetes,
provider_type: :user,
platform_kubernetes_attributes: {
namespace: 'custom-namespace',
api_url: '!!!!!',
token: 'token'
}
}
end
it 'returns an error' do
# expect(ClusterProvisionWorker).not_to receive(:perform_async)
expect { result }.to change { Clusters::Cluster.count }.by(0)
expect(result.errors[:"platform_kubernetes.api_url"]).to be_present
end
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