Commit 1c1cc8b3 authored by Shinya Maeda's avatar Shinya Maeda

Merge branch 'pl-spec-requests-api-project-clusters' into 'master'

Speed up request api specs for project clusters

See merge request gitlab-org/gitlab!28269
parents 70b1b6cc cf0d2fa4
...@@ -5,9 +5,9 @@ require 'spec_helper' ...@@ -5,9 +5,9 @@ require 'spec_helper'
describe API::ProjectClusters do describe API::ProjectClusters do
include KubernetesHelpers include KubernetesHelpers
let(:current_user) { create(:user) } let_it_be(:current_user) { create(:user) }
let(:developer_user) { create(:user) } let_it_be(:developer_user) { create(:user) }
let(:project) { create(:project) } let_it_be(:project) { create(:project) }
before do before do
project.add_maintainer(current_user) project.add_maintainer(current_user)
...@@ -15,10 +15,10 @@ describe API::ProjectClusters do ...@@ -15,10 +15,10 @@ describe API::ProjectClusters do
end end
describe 'GET /projects/:id/clusters' do describe 'GET /projects/:id/clusters' do
let!(:extra_cluster) { create(:cluster, :provided_by_gcp, :project) } let_it_be(:extra_cluster) { create(:cluster, :provided_by_gcp, :project) }
let!(:clusters) do let_it_be(:clusters) do
create_list(:cluster, 5, :provided_by_gcp, :project, :production_environment, create_list(:cluster, 2, :provided_by_gcp, :project, :production_environment,
projects: [project]) projects: [project])
end end
...@@ -35,17 +35,15 @@ describe API::ProjectClusters do ...@@ -35,17 +35,15 @@ describe API::ProjectClusters do
get api("/projects/#{project.id}/clusters", current_user) get api("/projects/#{project.id}/clusters", current_user)
end end
it 'responds with 200' do
expect(response).to have_gitlab_http_status(:ok)
end
it 'includes pagination headers' do it 'includes pagination headers' do
expect(response).to have_gitlab_http_status(:ok)
expect(response).to include_pagination_headers expect(response).to include_pagination_headers
end end
it 'onlies include authorized clusters' do it 'onlies include authorized clusters' do
cluster_ids = json_response.map { |cluster| cluster['id'] } cluster_ids = json_response.map { |cluster| cluster['id'] }
expect(response).to have_gitlab_http_status(:ok)
expect(cluster_ids).to match_array(clusters.pluck(:id)) expect(cluster_ids).to match_array(clusters.pluck(:id))
expect(cluster_ids).not_to include(extra_cluster.id) expect(cluster_ids).not_to include(extra_cluster.id)
end end
...@@ -139,7 +137,7 @@ describe API::ProjectClusters do ...@@ -139,7 +137,7 @@ describe API::ProjectClusters do
end end
context 'with non-existing cluster' do context 'with non-existing cluster' do
let(:cluster_id) { 123 } let(:cluster_id) { 0 }
it 'returns 404' do it 'returns 404' do
expect(response).to have_gitlab_http_status(:not_found) expect(response).to have_gitlab_http_status(:not_found)
...@@ -185,14 +183,11 @@ describe API::ProjectClusters do ...@@ -185,14 +183,11 @@ describe API::ProjectClusters do
end end
context 'with valid params' do context 'with valid params' do
it 'responds with 201' do
expect(response).to have_gitlab_http_status(:created)
end
it 'creates a new Cluster::Cluster' do it 'creates a new Cluster::Cluster' do
cluster_result = Clusters::Cluster.find(json_response["id"]) cluster_result = Clusters::Cluster.find(json_response["id"])
platform_kubernetes = cluster_result.platform platform_kubernetes = cluster_result.platform
expect(response).to have_gitlab_http_status(:created)
expect(cluster_result).to be_user expect(cluster_result).to be_user
expect(cluster_result).to be_kubernetes expect(cluster_result).to be_kubernetes
expect(cluster_result.project).to eq(project) expect(cluster_result.project).to eq(project)
...@@ -235,15 +230,9 @@ describe API::ProjectClusters do ...@@ -235,15 +230,9 @@ describe API::ProjectClusters do
context 'with invalid params' do context 'with invalid params' do
let(:namespace) { 'invalid_namespace' } let(:namespace) { 'invalid_namespace' }
it 'responds with 400' do
expect(response).to have_gitlab_http_status(:bad_request)
end
it 'does not create a new Clusters::Cluster' do it 'does not create a new Clusters::Cluster' do
expect(response).to have_gitlab_http_status(:bad_request)
expect(project.reload.clusters).to be_empty expect(project.reload.clusters).to be_empty
end
it 'returns validation errors' do
expect(json_response['message']['platform_kubernetes.namespace'].first).to be_present expect(json_response['message']['platform_kubernetes.namespace'].first).to be_present
end end
end end
...@@ -259,8 +248,8 @@ describe API::ProjectClusters do ...@@ -259,8 +248,8 @@ describe API::ProjectClusters do
it 'responds with 400' do it 'responds with 400' do
expect(response).to have_gitlab_http_status(:bad_request) expect(response).to have_gitlab_http_status(:bad_request)
expect(json_response['message']['base'].first)
expect(json_response['message']['base'].first).to eq(_('Instance does not support multiple Kubernetes clusters')) .to eq(_('Instance does not support multiple Kubernetes clusters'))
end end
end end
...@@ -271,7 +260,6 @@ describe API::ProjectClusters do ...@@ -271,7 +260,6 @@ describe API::ProjectClusters do
it 'responds with 403' do it 'responds with 403' do
expect(response).to have_gitlab_http_status(:forbidden) expect(response).to have_gitlab_http_status(:forbidden)
expect(json_response['message']).to eq('403 Forbidden') expect(json_response['message']).to eq('403 Forbidden')
end end
end end
...@@ -281,7 +269,7 @@ describe API::ProjectClusters do ...@@ -281,7 +269,7 @@ describe API::ProjectClusters do
let(:api_url) { 'https://kubernetes.example.com' } let(:api_url) { 'https://kubernetes.example.com' }
let(:namespace) { 'new-namespace' } let(:namespace) { 'new-namespace' }
let(:platform_kubernetes_attributes) { { namespace: namespace } } let(:platform_kubernetes_attributes) { { namespace: namespace } }
let(:management_project) { create(:project, namespace: project.namespace) } let_it_be(:management_project) { create(:project, namespace: project.namespace) }
let(:management_project_id) { management_project.id } let(:management_project_id) { management_project.id }
let(:update_params) do let(:update_params) do
...@@ -321,11 +309,8 @@ describe API::ProjectClusters do ...@@ -321,11 +309,8 @@ describe API::ProjectClusters do
end end
context 'with valid params' do context 'with valid params' do
it 'responds with 200' do
expect(response).to have_gitlab_http_status(:ok)
end
it 'updates cluster attributes' do it 'updates cluster attributes' do
expect(response).to have_gitlab_http_status(:ok)
expect(cluster.domain).to eq('new-domain.com') expect(cluster.domain).to eq('new-domain.com')
expect(cluster.platform_kubernetes.namespace).to eq('new-namespace') expect(cluster.platform_kubernetes.namespace).to eq('new-namespace')
expect(cluster.management_project).to eq(management_project) expect(cluster.management_project).to eq(management_project)
...@@ -335,29 +320,24 @@ describe API::ProjectClusters do ...@@ -335,29 +320,24 @@ describe API::ProjectClusters do
context 'with invalid params' do context 'with invalid params' do
let(:namespace) { 'invalid_namespace' } let(:namespace) { 'invalid_namespace' }
it 'responds with 400' do
expect(response).to have_gitlab_http_status(:bad_request)
end
it 'does not update cluster attributes' do it 'does not update cluster attributes' do
expect(response).to have_gitlab_http_status(:bad_request)
expect(cluster.domain).not_to eq('new_domain.com') expect(cluster.domain).not_to eq('new_domain.com')
expect(cluster.platform_kubernetes.namespace).not_to eq('invalid_namespace') expect(cluster.platform_kubernetes.namespace).not_to eq('invalid_namespace')
expect(cluster.management_project).not_to eq(management_project) expect(cluster.management_project).not_to eq(management_project)
end end
it 'returns validation errors' do it 'returns validation errors' do
expect(json_response['message']['platform_kubernetes.namespace'].first).to match('can contain only lowercase letters') expect(json_response['message']['platform_kubernetes.namespace'].first)
.to match('can contain only lowercase letters')
end end
end end
context 'current user does not have access to management_project_id' do context 'current user does not have access to management_project_id' do
let(:management_project_id) { create(:project).id } let_it_be(:management_project_id) { create(:project).id }
it 'responds with 400' do
expect(response).to have_gitlab_http_status(:bad_request)
end
it 'returns validation errors' do it 'returns validation errors' do
expect(response).to have_gitlab_http_status(:bad_request)
expect(json_response['message']['management_project_id'].first).to match('don\'t have permission') expect(json_response['message']['management_project_id'].first).to match('don\'t have permission')
end end
end end
...@@ -371,12 +351,10 @@ describe API::ProjectClusters do ...@@ -371,12 +351,10 @@ describe API::ProjectClusters do
} }
end end
it 'responds with 400' do
expect(response).to have_gitlab_http_status(:bad_request)
end
it 'returns validation error' do it 'returns validation error' do
expect(json_response['message']['platform_kubernetes.base'].first).to eq(_('Cannot modify managed Kubernetes cluster')) expect(response).to have_gitlab_http_status(:bad_request)
expect(json_response['message']['platform_kubernetes.base'].first)
.to eq(_('Cannot modify managed Kubernetes cluster'))
end end
end end
...@@ -412,13 +390,10 @@ describe API::ProjectClusters do ...@@ -412,13 +390,10 @@ describe API::ProjectClusters do
} }
end end
it 'responds with 200' do
expect(response).to have_gitlab_http_status(:ok)
end
it 'updates platform kubernetes attributes' do it 'updates platform kubernetes attributes' do
platform_kubernetes = cluster.platform_kubernetes platform_kubernetes = cluster.platform_kubernetes
expect(response).to have_gitlab_http_status(:ok)
expect(cluster.name).to eq('new-name') expect(cluster.name).to eq('new-name')
expect(platform_kubernetes.namespace).to eq('new-namespace') expect(platform_kubernetes.namespace).to eq('new-namespace')
expect(platform_kubernetes.api_url).to eq('https://new-api-url.com') expect(platform_kubernetes.api_url).to eq('https://new-api-url.com')
...@@ -439,7 +414,7 @@ describe API::ProjectClusters do ...@@ -439,7 +414,7 @@ describe API::ProjectClusters do
describe 'DELETE /projects/:id/clusters/:cluster_id' do describe 'DELETE /projects/:id/clusters/:cluster_id' do
let(:cluster_params) { { cluster_id: cluster.id } } let(:cluster_params) { { cluster_id: cluster.id } }
let(:cluster) do let_it_be(:cluster) do
create(:cluster, :project, :provided_by_gcp, create(:cluster, :project, :provided_by_gcp,
projects: [project]) projects: [project])
end end
...@@ -457,11 +432,8 @@ describe API::ProjectClusters do ...@@ -457,11 +432,8 @@ describe API::ProjectClusters do
delete api("/projects/#{project.id}/clusters/#{cluster.id}", current_user), params: cluster_params delete api("/projects/#{project.id}/clusters/#{cluster.id}", current_user), params: cluster_params
end end
it 'responds with 204' do
expect(response).to have_gitlab_http_status(:no_content)
end
it 'deletes the cluster' do it 'deletes the cluster' do
expect(response).to have_gitlab_http_status(:no_content)
expect(Clusters::Cluster.exists?(id: cluster.id)).to be_falsy expect(Clusters::Cluster.exists?(id: cluster.id)).to be_falsy
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