Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
1
Merge Requests
1
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
nexedi
gitlab-ce
Commits
253bf69d
Commit
253bf69d
authored
Nov 01, 2017
by
Shinya Maeda
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
specs for feature
parent
6a65e2f5
Changes
10
Show whitespace changes
Inline
Side-by-side
Showing
10 changed files
with
165 additions
and
94 deletions
+165
-94
app/controllers/projects/clusters_controller.rb
app/controllers/projects/clusters_controller.rb
+4
-8
app/models/clusters/cluster.rb
app/models/clusters/cluster.rb
+1
-1
app/models/clusters/platforms/kubernetes.rb
app/models/clusters/platforms/kubernetes.rb
+4
-2
app/services/clusters/create_service.rb
app/services/clusters/create_service.rb
+1
-2
lib/google_api/cloud_platform/client.rb
lib/google_api/cloud_platform/client.rb
+0
-1
spec/controllers/projects/clusters_controller_spec.rb
spec/controllers/projects/clusters_controller_spec.rb
+6
-1
spec/features/projects/clusters_spec.rb
spec/features/projects/clusters_spec.rb
+12
-7
spec/models/clusters/platforms/kubernetes_spec.rb
spec/models/clusters/platforms/kubernetes_spec.rb
+28
-28
spec/models/project_services/kubernetes_service_spec.rb
spec/models/project_services/kubernetes_service_spec.rb
+2
-2
spec/services/clusters/create_service_spec.rb
spec/services/clusters/create_service_spec.rb
+107
-42
No files found.
app/controllers/projects/clusters_controller.rb
View file @
253bf69d
...
...
@@ -27,14 +27,10 @@ class Projects::ClustersController < Projects::ApplicationController
end
def
new
# @cluster = Clusters::Cluster.new(
# platform_type: :kubernetes,
# provider_type: :gcp).tap do |cluster|
# cluster.build_provider_gcp
# cluster.build_platform_kubernetes
# cluster.projects << project
# end
@cluster
=
Clusters
::
Cluster
.
new
@cluster
=
Clusters
::
Cluster
.
new
.
tap
do
|
cluster
|
cluster
.
build_provider_gcp
cluster
.
build_platform_kubernetes
end
end
def
create
...
...
app/models/clusters/cluster.rb
View file @
253bf69d
...
...
@@ -10,7 +10,7 @@ module Clusters
has_many
:projects
,
through: :cluster_projects
,
class_name:
'::Project'
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
:platform_kubernetes
,
update_only:
true
...
...
app/models/clusters/platforms/kubernetes.rb
View file @
253bf69d
...
...
@@ -30,8 +30,10 @@ module Clusters
message:
Gitlab
::
Regex
.
kubernetes_namespace_regex_message
}
validates
:api_url
,
url:
true
,
presence:
true
validates
:token
,
presence:
true
# TODO: when cluster.gcp? skip validation when create a record
# TODO: when cluster.user? validates always
# validates :api_url, url: true, presence: true
# validates :token, presence: true
after_save
:clear_reactive_cache!
...
...
app/services/clusters/create_service.rb
View file @
253bf69d
...
...
@@ -29,8 +29,7 @@ module Clusters
return
@cluster_params
if
defined?
(
@cluster_params
)
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
@cluster_params
=
params
.
merge
(
user:
current_user
)
...
...
lib/google_api/cloud_platform/client.rb
View file @
253bf69d
...
...
@@ -3,7 +3,6 @@ require 'google/apis/container_v1'
module
GoogleApi
module
CloudPlatform
class
Client
<
GoogleApi
::
Auth
DEFAULT_MACHINE_TYPE
=
'n1-standard-1'
.
freeze
SCOPE
=
'https://www.googleapis.com/auth/cloud-platform'
.
freeze
LEAST_TOKEN_LIFE_TIME
=
10
.
minutes
...
...
spec/controllers/projects/clusters_controller_spec.rb
View file @
253bf69d
...
...
@@ -208,7 +208,12 @@ describe Projects::ClustersController do
cluster:
{
name:
'new-cluster'
,
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
...
...
spec/features/projects/clusters_spec.rb
View file @
253bf69d
require
'spec_helper'
feature
'Clusters'
,
:js
do
include
GoogleApi
::
CloudPlatformHelpers
let!
(
:project
)
{
create
(
:project
,
:repository
)
}
let!
(
:user
)
{
create
(
:user
)
}
...
...
@@ -11,8 +13,10 @@ feature 'Clusters', :js do
context
'when user has signed in Google'
do
before
do
allow_any_instance_of
(
GoogleApi
::
CloudPlatform
::
Client
)
.
to
receive
(
:validate_token
).
and_return
(
true
)
allow_any_instance_of
(
Projects
::
ClustersController
)
.
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
context
'when user does not have a cluster and visits cluster index page'
do
...
...
@@ -36,15 +40,15 @@ feature 'Clusters', :js do
allow
(
WaitForClusterCreationWorker
).
to
receive
(
:perform_in
).
and_return
(
nil
)
fill_in
'cluster_gcp_project_id'
,
with:
'gcp-project-123'
fill_in
'cluster_
gcp_cluster_
name'
,
with:
'dev-cluster'
fill_in
'cluster_
provider_gcp_attributes_
gcp_project_id'
,
with:
'gcp-project-123'
fill_in
'cluster_name'
,
with:
'dev-cluster'
click_button
'Create cluster'
end
it
'user sees a cluster details page and creation status'
do
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'
)
end
...
...
@@ -62,7 +66,8 @@ feature 'Clusters', :js do
end
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
visit
project_clusters_path
(
project
)
...
...
@@ -70,7 +75,7 @@ feature 'Clusters', :js do
it
'user sees an cluster details page'
do
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
context
'when user disables the cluster'
do
...
...
spec/models/clusters/platforms/kubernetes_spec.rb
View file @
253bf69d
...
...
@@ -51,45 +51,45 @@ describe Clusters::Platforms::Kubernetes, :use_clean_rails_memory_store_caching
end
end
context
'when validates api_url'
do
let
(
:kubernetes
)
{
build
(
:platform_kubernetes
,
:configured
)
}
#
context 'when validates api_url' do
#
let(:kubernetes) { build(:platform_kubernetes, :configured) }
before
do
kubernetes
.
api_url
=
api_url
end
#
before do
#
kubernetes.api_url = api_url
#
end
context
'when api_url is invalid url'
do
let
(
:api_url
)
{
'!!!!!!'
}
#
context 'when api_url is invalid url' do
#
let(:api_url) { '!!!!!!' }
it
{
expect
(
kubernetes
.
save
).
to
be_falsey
}
end
#
it { expect(kubernetes.save).to be_falsey }
#
end
context
'when api_url is nil'
do
let
(
:api_url
)
{
nil
}
#
context 'when api_url is nil' do
#
let(:api_url) { nil }
it
{
expect
(
kubernetes
.
save
).
to
be_falsey
}
end
#
it { expect(kubernetes.save).to be_falsey }
#
end
context
'when api_url is valid url'
do
let
(
:api_url
)
{
'https://111.111.111.111'
}
#
context 'when api_url is valid url' do
#
let(:api_url) { 'https://111.111.111.111' }
it
{
expect
(
kubernetes
.
save
).
to
be_truthy
}
end
end
#
it { expect(kubernetes.save).to be_truthy }
#
end
#
end
context
'when validates token'
do
let
(
:kubernetes
)
{
build
(
:platform_kubernetes
,
:configured
)
}
#
context 'when validates token' do
#
let(:kubernetes) { build(:platform_kubernetes, :configured) }
before
do
kubernetes
.
token
=
token
end
#
before do
#
kubernetes.token = token
#
end
context
'when token is nil'
do
let
(
:token
)
{
nil
}
#
context 'when token is nil' do
#
let(:token) { nil }
it
{
expect
(
kubernetes
.
save
).
to
be_falsey
}
end
end
#
it { expect(kubernetes.save).to be_falsey }
#
end
#
end
end
describe
'#actual_namespace'
do
...
...
spec/models/project_services/kubernetes_service_spec.rb
View file @
253bf69d
...
...
@@ -156,7 +156,7 @@ describe KubernetesService, :use_clean_rails_memory_store_caching do
let
(
:discovery_url
)
{
'https://kubernetes.example.com/api/v1'
}
before
do
stub_kubeclient_discover
stub_kubeclient_discover
(
service
.
api_url
)
end
context
'with path prefix in api_url'
do
...
...
@@ -164,7 +164,7 @@ describe KubernetesService, :use_clean_rails_memory_store_caching do
it
'tests with the prefix'
do
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
(
WebMock
).
to
have_requested
(
:get
,
discovery_url
).
once
...
...
spec/services/clusters/create_service_spec.rb
View file @
253bf69d
...
...
@@ -6,6 +6,7 @@ describe Clusters::CreateService do
let
(
:user
)
{
create
(
:user
)
}
let
(
:result
)
{
described_class
.
new
(
project
,
user
,
params
).
execute
(
access_token
)
}
context
'when provider is gcp'
do
context
'when correct params'
do
let
(
:params
)
do
{
...
...
@@ -26,7 +27,12 @@ describe Clusters::CreateService do
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
)
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
)
...
...
@@ -36,6 +42,7 @@ describe Clusters::CreateService do
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
...
...
@@ -44,11 +51,15 @@ describe Clusters::CreateService do
{
name:
'test-cluster'
,
platform_type: :kubernetes
,
provider_type: :user
,
provider_type: :gcp
,
platform_kubernetes_attributes:
{
namespace:
'custom-namespace'
},
provider_gcp_attributes:
{
gcp_project_id:
'gcp-project
'
,
gcp_project_id:
'!!!!!!!
'
,
zone:
'us-central1-a'
,
num_nodes:
'ABC'
num_nodes:
1
,
machine_type:
'machine_type-a'
}
}
end
...
...
@@ -56,7 +67,61 @@ describe Clusters::CreateService do
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.num_nodes"
]).
to
be_present
expect
(
result
.
errors
[
:"provider_gcp.gcp_project_id"
]).
to
be_present
end
end
end
context
'when provider is user'
do
context
'when correct params'
do
let
(
:params
)
do
{
name:
'test-cluster'
,
platform_type: :kubernetes
,
provider_type: :user
,
platform_kubernetes_attributes:
{
namespace:
'custom-namespace'
,
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
context
'when invalid params'
do
let
(
:params
)
do
{
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
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment