debian-mirror-gitlab/spec/controllers/health_controller_spec.rb

135 lines
4 KiB
Ruby
Raw Normal View History

2019-07-31 22:56:46 +05:30
# frozen_string_literal: true
2017-08-17 22:00:37 +05:30
require 'spec_helper'
describe HealthController do
include StubENV
2018-03-17 18:26:18 +05:30
let(:token) { Gitlab::CurrentSettings.health_check_access_token }
2017-09-10 17:25:29 +05:30
let(:whitelisted_ip) { '127.0.0.1' }
let(:not_whitelisted_ip) { '127.0.0.2' }
2017-08-17 22:00:37 +05:30
before do
2017-09-10 17:25:29 +05:30
allow(Settings.monitoring).to receive(:ip_whitelist).and_return([whitelisted_ip])
2018-03-17 18:26:18 +05:30
stub_storage_settings({}) # Hide the broken storage
2017-08-17 22:00:37 +05:30
stub_env('IN_MEMORY_APPLICATION_SETTINGS', 'false')
end
describe '#readiness' do
2017-09-10 17:25:29 +05:30
shared_context 'endpoint responding with readiness data' do
let(:request_params) { {} }
2019-02-15 15:39:39 +05:30
subject { get :readiness, params: request_params }
2017-09-10 17:25:29 +05:30
it 'responds with readiness checks data' do
subject
2017-08-17 22:00:37 +05:30
2019-12-21 20:55:43 +05:30
expect(json_response['db_check']).to contain_exactly({ 'status' => 'ok' })
expect(json_response['cache_check']).to contain_exactly({ 'status' => 'ok' })
expect(json_response['queues_check']).to contain_exactly({ 'status' => 'ok' })
expect(json_response['shared_state_check']).to contain_exactly({ 'status' => 'ok' })
expect(json_response['gitaly_check']).to contain_exactly(
{ 'status' => 'ok', 'labels' => { 'shard' => 'default' } })
end
it 'responds with readiness checks data when a failure happens' do
allow(Gitlab::HealthChecks::Redis::RedisCheck).to receive(:readiness).and_return(
Gitlab::HealthChecks::Result.new('redis_check', false, "check error"))
subject
expect(json_response['cache_check']).to contain_exactly({ 'status' => 'ok' })
expect(json_response['redis_check']).to contain_exactly(
{ 'status' => 'failed', 'message' => 'check error' })
expect(response.status).to eq(503)
expect(response.headers['X-GitLab-Custom-Error']).to eq(1)
2017-08-17 22:00:37 +05:30
end
end
2017-09-10 17:25:29 +05:30
context 'accessed from whitelisted ip' do
before do
allow(Gitlab::RequestContext).to receive(:client_ip).and_return(whitelisted_ip)
end
it_behaves_like 'endpoint responding with readiness data'
end
context 'accessed from not whitelisted ip' do
before do
allow(Gitlab::RequestContext).to receive(:client_ip).and_return(not_whitelisted_ip)
end
it 'responds with resource not found' do
2017-08-17 22:00:37 +05:30
get :readiness
2017-09-10 17:25:29 +05:30
2017-08-17 22:00:37 +05:30
expect(response.status).to eq(404)
end
2017-09-10 17:25:29 +05:30
context 'accessed with valid token' do
context 'token passed in request header' do
before do
request.headers['TOKEN'] = token
end
it_behaves_like 'endpoint responding with readiness data'
end
end
context 'token passed as URL param' do
it_behaves_like 'endpoint responding with readiness data' do
let(:request_params) { { token: token } }
end
end
2017-08-17 22:00:37 +05:30
end
end
describe '#liveness' do
2017-09-10 17:25:29 +05:30
shared_context 'endpoint responding with liveness data' do
subject { get :liveness }
it 'responds with liveness checks data' do
subject
2017-08-17 22:00:37 +05:30
2019-12-21 20:55:43 +05:30
expect(json_response).to eq('status' => 'ok')
2017-08-17 22:00:37 +05:30
end
end
2017-09-10 17:25:29 +05:30
context 'accessed from whitelisted ip' do
before do
allow(Gitlab::RequestContext).to receive(:client_ip).and_return(whitelisted_ip)
2017-08-17 22:00:37 +05:30
end
2017-09-10 17:25:29 +05:30
it_behaves_like 'endpoint responding with liveness data'
2017-08-17 22:00:37 +05:30
end
2017-09-10 17:25:29 +05:30
context 'accessed from not whitelisted ip' do
2017-08-17 22:00:37 +05:30
before do
2017-09-10 17:25:29 +05:30
allow(Gitlab::RequestContext).to receive(:client_ip).and_return(not_whitelisted_ip)
2017-08-17 22:00:37 +05:30
end
2017-09-10 17:25:29 +05:30
it 'responds with resource not found' do
get :liveness
2017-08-17 22:00:37 +05:30
2017-09-10 17:25:29 +05:30
expect(response.status).to eq(404)
2017-08-17 22:00:37 +05:30
end
2017-09-10 17:25:29 +05:30
context 'accessed with valid token' do
context 'token passed in request header' do
before do
request.headers['TOKEN'] = token
end
2017-08-17 22:00:37 +05:30
2017-09-10 17:25:29 +05:30
it_behaves_like 'endpoint responding with liveness data'
end
context 'token passed as URL param' do
it_behaves_like 'endpoint responding with liveness data' do
2019-02-15 15:39:39 +05:30
subject { get :liveness, params: { token: token } }
2017-09-10 17:25:29 +05:30
end
end
2017-08-17 22:00:37 +05:30
end
end
end
end