Commit 51588269 authored by Kamil Trzcinski's avatar Kamil Trzcinski

Initial take on tests

parent af2a8cd2
...@@ -35,6 +35,6 @@ class MockDeploymentService < DeploymentService ...@@ -35,6 +35,6 @@ class MockDeploymentService < DeploymentService
end end
def rollout_status_deployments def rollout_status_deployments
[ OpenStruct.new(instances: rollout_status_instances) ] [OpenStruct.new(instances: rollout_status_instances)]
end end
end end
...@@ -9,11 +9,9 @@ class EnvironmentEntity < Grape::Entity ...@@ -9,11 +9,9 @@ class EnvironmentEntity < Grape::Entity
expose :last_deployment, using: DeploymentEntity expose :last_deployment, using: DeploymentEntity
expose :stop_action? expose :stop_action?
expose :rollout_status, expose :rollout_status, if: -> (*) { can_read_deploy_board? }, using: RolloutStatusEntity
if: -> (environment, _) { can?(request.current_user, :read_deploy_board, environment.project) },
using: RolloutStatusEntity
expose :metrics_path, if: -> (environment, _) { environment.has_metrics? } do |environment| expose :metrics_path, if: -> (*) { environment.has_metrics? } do |environment|
metrics_project_environment_path(environment.project, environment) metrics_project_environment_path(environment.project, environment)
end end
...@@ -25,7 +23,7 @@ class EnvironmentEntity < Grape::Entity ...@@ -25,7 +23,7 @@ class EnvironmentEntity < Grape::Entity
stop_project_environment_path(environment.project, environment) stop_project_environment_path(environment.project, environment)
end end
expose :terminal_path, if: ->(environment, _) { environment.deployment_service_ready? } do |environment| expose :terminal_path, if: ->(*) { environment.deployment_service_ready? } do |environment|
can?(request.current_user, :admin_environment, environment.project) && can?(request.current_user, :admin_environment, environment.project) &&
terminal_project_environment_path(environment.project, environment) terminal_project_environment_path(environment.project, environment)
end end
...@@ -35,4 +33,16 @@ class EnvironmentEntity < Grape::Entity ...@@ -35,4 +33,16 @@ class EnvironmentEntity < Grape::Entity
end end
expose :created_at, :updated_at expose :created_at, :updated_at
private
alias_method :environment, :object
def current_user
request.current_user
end
def can_read_deploy_board?
can?(current_user, :read_deploy_board, environment.project)
end
end end
class RolloutStatusSerializer < BaseSerializer
entity RolloutStatusEntity
end
require 'spec_helper' require 'spec_helper'
describe Projects::EnvironmentsController do describe Projects::EnvironmentsController do
include KubernetesHelpers
set(:user) { create(:user) } set(:user) { create(:user) }
set(:project) { create(:project) } set(:project) { create(:project) }
...@@ -26,6 +28,7 @@ describe Projects::EnvironmentsController do ...@@ -26,6 +28,7 @@ describe Projects::EnvironmentsController do
context 'when requesting JSON response for folders' do context 'when requesting JSON response for folders' do
before do before do
allow_any_instance_of(Environment).to receive(:deployment_service_ready?).and_return(true) allow_any_instance_of(Environment).to receive(:deployment_service_ready?).and_return(true)
allow_any_instance_of(Environment).to receive(:rollout_status).and_return(kube_deployment_rollout_status)
create(:environment, project: project, create(:environment, project: project,
name: 'staging/review-1', name: 'staging/review-1',
...@@ -52,11 +55,11 @@ describe Projects::EnvironmentsController do ...@@ -52,11 +55,11 @@ describe Projects::EnvironmentsController do
it 'responds with a payload describing available environments' do it 'responds with a payload describing available environments' do
expect(environments.count).to eq 2 expect(environments.count).to eq 2
expect(environments.first['name']).to eq 'production' expect(environments.first['name']).to eq 'production'
expect(environments.first['latest']['rollout_status_path']).to be_present expect(environments.first['latest']['rollout_status']).to be_present
expect(environments.second['name']).to eq 'staging' expect(environments.second['name']).to eq 'staging'
expect(environments.second['size']).to eq 2 expect(environments.second['size']).to eq 2
expect(environments.second['latest']['name']).to eq 'staging/review-2' expect(environments.second['latest']['name']).to eq 'staging/review-2'
expect(environments.second['latest']['rollout_status_path']).to be_present expect(environments.second['latest']['rollout_status']).to be_present
end end
it 'contains values describing environment scopes sizes' do it 'contains values describing environment scopes sizes' do
...@@ -96,8 +99,8 @@ describe Projects::EnvironmentsController do ...@@ -96,8 +99,8 @@ describe Projects::EnvironmentsController do
end end
it 'does not return the rollout_status_path attribute' do it 'does not return the rollout_status_path attribute' do
expect(environments.first['latest']['rollout_status_path']).to be_blank expect(environments.first['latest']['rollout_status']).not_to be_present
expect(environments.second['latest']['rollout_status_path']).to be_blank expect(environments.second['latest']['rollout_status']).not_to be_present
end end
end end
end end
...@@ -289,59 +292,6 @@ describe Projects::EnvironmentsController do ...@@ -289,59 +292,6 @@ describe Projects::EnvironmentsController do
end end
end end
describe 'GET #status' do
context 'without deployment service' do
it 'returns 404' do
get :status, environment_params
expect(response.status).to eq(404)
end
end
context 'with deployment service' do
let(:project) { create(:kubernetes_project) }
let(:environment) { create(:environment, name: 'production', project: project) }
before do
stub_licensed_features(deploy_board: true)
allow_any_instance_of(Environment).to receive(:deployment_service_ready?).and_return(true)
end
it 'returns 204 until the rollout status is present' do
expect_any_instance_of(Environment)
.to receive(:rollout_status)
.and_return(nil)
get :status, environment_params
expect(response.status).to eq(204)
expect(response.headers['Poll-Interval']).to eq("3000")
end
it 'returns the rollout status when present' do
expect_any_instance_of(Environment)
.to receive(:rollout_status)
.and_return(::Gitlab::Kubernetes::RolloutStatus.new([]))
get :status, environment_params
expect(response.status).to eq(200)
end
end
context 'when license does not has the GitLab_DeployBoard add-on' do
before do
stub_licensed_features(deploy_board: false)
end
it 'does not return any data' do
get :status, environment_params
expect(response).to have_http_status(:not_found)
end
end
end
describe 'GET #metrics' do describe 'GET #metrics' do
before do before do
allow(controller).to receive(:environment).and_return(environment) allow(controller).to receive(:environment).and_return(environment)
......
...@@ -9,6 +9,7 @@ describe KubernetesService, models: true, use_clean_rails_memory_store_caching: ...@@ -9,6 +9,7 @@ describe KubernetesService, models: true, use_clean_rails_memory_store_caching:
describe '#rollout_status' do describe '#rollout_status' do
let(:environment) { build(:environment, project: project, name: "env", slug: "env-000000") } let(:environment) { build(:environment, project: project, name: "env", slug: "env-000000") }
subject(:rollout_status) { service.rollout_status(environment) } subject(:rollout_status) { service.rollout_status(environment) }
context 'with valid deployments' do context 'with valid deployments' do
......
...@@ -94,15 +94,21 @@ describe Gitlab::Kubernetes::RolloutStatus do ...@@ -94,15 +94,21 @@ describe Gitlab::Kubernetes::RolloutStatus do
end end
end end
describe '#valid?' do describe '#found?' do
context 'when the specs are passed' do context 'when the specs are passed' do
it { is_expected.to be_valid } it { is_expected.to be_found }
end end
context 'when no specs are passed' do context 'when no specs are passed' do
let(:specs) { specs_none } let(:specs) { specs_none }
it { is_expected.not_to be_valid } it { is_expected.not_to be_found }
end end
end end
describe '.loading_rollout' do
subject { described_class.loading_rollout }
it { is_expected.to be_loading }
end
end end
...@@ -385,6 +385,7 @@ describe Environment do ...@@ -385,6 +385,7 @@ describe Environment do
describe '#rollout_status' do describe '#rollout_status' do
let(:project) { create(:kubernetes_project) } let(:project) { create(:kubernetes_project) }
subject { environment.rollout_status } subject { environment.rollout_status }
context 'when the environment has rollout status' do context 'when the environment has rollout status' do
......
require 'spec_helper' require 'spec_helper'
describe EnvironmentEntity do describe EnvironmentEntity do
include KubernetesHelpers
let(:user) { create(:user) } let(:user) { create(:user) }
let(:environment) { create(:environment) } let(:environment) { create(:environment) }
...@@ -50,12 +52,11 @@ describe EnvironmentEntity do ...@@ -50,12 +52,11 @@ describe EnvironmentEntity do
before do before do
stub_licensed_features(deploy_board: true) stub_licensed_features(deploy_board: true)
allow(environment).to receive(:deployment_service_ready?).and_return(true) allow(environment).to receive(:deployment_service_ready?).and_return(true)
allow(environment).to receive(:rollout_status).and_return(kube_deployment_rollout_status)
end end
it 'exposes rollout_status_path' do it 'exposes rollout_status' do
expected = '/' + [environment.project.full_path, 'environments', environment.id, 'status.json'].join('/') expect(subject).to include(:rollout_status)
expect(subject[:rollout_status_path]).to eq(expected)
end end
end end
...@@ -65,7 +66,7 @@ describe EnvironmentEntity do ...@@ -65,7 +66,7 @@ describe EnvironmentEntity do
allow(environment).to receive(:deployment_service_ready?).and_return(true) allow(environment).to receive(:deployment_service_ready?).and_return(true)
end end
it 'does not expose rollout_status_path' do it 'does not expose rollout_status' do
expect(subject[:rollout_status_path]).to be_blank expect(subject[:rollout_status_path]).to be_blank
end end
end end
......
...@@ -54,7 +54,7 @@ describe EnvironmentSerializer do ...@@ -54,7 +54,7 @@ describe EnvironmentSerializer do
context 'when representing environments within folders' do context 'when representing environments within folders' do
let(:serializer) do let(:serializer) do
described_class.new(project: project).within_folders described_class.new(current_user: user, project: project).within_folders
end end
let(:resource) { Environment.all } let(:resource) { Environment.all }
...@@ -123,7 +123,7 @@ describe EnvironmentSerializer do ...@@ -123,7 +123,7 @@ describe EnvironmentSerializer do
let(:pagination) { { page: 1, per_page: 2 } } let(:pagination) { { page: 1, per_page: 2 } }
let(:serializer) do let(:serializer) do
described_class.new(project: project) described_class.new(current_user: user, project: project)
.with_pagination(request, response) .with_pagination(request, response)
end end
...@@ -169,7 +169,7 @@ describe EnvironmentSerializer do ...@@ -169,7 +169,7 @@ describe EnvironmentSerializer do
context 'when grouping environments within folders' do context 'when grouping environments within folders' do
let(:serializer) do let(:serializer) do
described_class.new(project: project) described_class.new(current_user: user, project: project)
.with_pagination(request, response) .with_pagination(request, response)
.within_folders .within_folders
end end
......
...@@ -7,11 +7,29 @@ describe RolloutStatusEntity do ...@@ -7,11 +7,29 @@ describe RolloutStatusEntity do
described_class.new(rollout_status, request: double) described_class.new(rollout_status, request: double)
end end
let(:rollout_status) { ::Gitlab::Kubernetes::RolloutStatus.from_specs(kube_deployment) }
subject { entity.as_json } subject { entity.as_json }
it { is_expected.to have_key(:instances) } context 'when kube deployment is valid' do
it { is_expected.to have_key(:completion) } let(:rollout_status) { kube_deployment_rollout_status }
it { is_expected.to have_key(:is_completed) }
it { is_expected.to have_key(:valid) } it "exposes status" do
is_expected.to include(:status)
end
it "exposes deployment data" do
is_expected.to include(:instances, :completion, :is_completed)
end
end
context 'when kube deployment is empty' do
let(:rollout_status) { empty_deployment_rollout_status }
it "exposes status" do
is_expected.to include(:status)
end
it "does not expose deployment data" do
is_expected.not_to include(:instances, :completion, :is_completed)
end
end
end end
...@@ -122,4 +122,12 @@ module KubernetesHelpers ...@@ -122,4 +122,12 @@ module KubernetesHelpers
terminal terminal
end end
end end
def kube_deployment_rollout_status
::Gitlab::Kubernetes::RolloutStatus.from_specs(kube_deployment)
end
def empty_deployment_rollout_status
::Gitlab::Kubernetes::RolloutStatus.from_specs()
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