Commit 51588269 authored by Kamil Trzcinski's avatar Kamil Trzcinski

Initial take on tests

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