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

138 lines
4.5 KiB
Ruby
Raw Normal View History

2019-07-31 22:56:46 +05:30
# frozen_string_literal: true
2016-06-02 11:05:42 +05:30
require 'spec_helper'
2020-06-23 00:09:42 +05:30
RSpec.describe HealthCheckController, :request_store do
2017-08-17 22:00:37 +05:30
include StubENV
2016-06-02 11:05:42 +05:30
let(:xml_response) { Hash.from_xml(response.body)['hash'] }
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' }
2016-06-02 11:05:42 +05:30
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])
2017-08-17 22:00:37 +05:30
stub_env('IN_MEMORY_APPLICATION_SETTINGS', 'false')
end
2016-06-02 11:05:42 +05:30
describe 'GET #index' do
2017-09-10 17:25:29 +05:30
context 'when services are up but accessed from outside whitelisted ips' do
before do
2020-03-13 15:44:24 +05:30
allow(Gitlab::RequestContext.instance).to receive(:client_ip).and_return(not_whitelisted_ip)
2017-09-10 17:25:29 +05:30
end
2016-06-02 11:05:42 +05:30
it 'returns a not found page' do
get :index
2017-09-10 17:25:29 +05:30
2016-06-02 11:05:42 +05:30
expect(response).to be_not_found
end
2017-09-10 17:25:29 +05:30
context 'when services are accessed with token' do
it 'supports passing the token in the header' do
request.headers['TOKEN'] = token
get :index
2019-12-04 20:38:33 +05:30
expect(response).to be_successful
2021-02-22 17:27:13 +05:30
expect(response.media_type).to eq 'text/plain'
2017-09-10 17:25:29 +05:30
end
it 'supports passing the token in query params' do
2019-02-15 15:39:39 +05:30
get :index, params: { token: token }
2017-09-10 17:25:29 +05:30
2019-12-04 20:38:33 +05:30
expect(response).to be_successful
2021-02-22 17:27:13 +05:30
expect(response.media_type).to eq 'text/plain'
2017-09-10 17:25:29 +05:30
end
end
2016-06-02 11:05:42 +05:30
end
2017-09-10 17:25:29 +05:30
context 'when services are up and accessed from whitelisted ips' do
before do
2020-03-13 15:44:24 +05:30
allow(Gitlab::RequestContext.instance).to receive(:client_ip).and_return(whitelisted_ip)
2016-06-02 11:05:42 +05:30
end
2017-09-10 17:25:29 +05:30
it 'supports successful plaintext response' do
get :index
2019-12-04 20:38:33 +05:30
expect(response).to be_successful
2021-02-22 17:27:13 +05:30
expect(response.media_type).to eq 'text/plain'
2016-06-02 11:05:42 +05:30
end
it 'supports successful json response' do
2017-09-10 17:25:29 +05:30
get :index, format: :json
2019-12-04 20:38:33 +05:30
expect(response).to be_successful
2021-02-22 17:27:13 +05:30
expect(response.media_type).to eq 'application/json'
2016-06-02 11:05:42 +05:30
expect(json_response['healthy']).to be true
end
it 'supports successful xml response' do
2017-09-10 17:25:29 +05:30
get :index, format: :xml
2019-12-04 20:38:33 +05:30
expect(response).to be_successful
2021-02-22 17:27:13 +05:30
expect(response.media_type).to eq 'application/xml'
2016-06-02 11:05:42 +05:30
expect(xml_response['healthy']).to be true
end
it 'supports successful responses for specific checks' do
2019-02-15 15:39:39 +05:30
get :index, params: { checks: 'email' }, format: :json
2017-09-10 17:25:29 +05:30
2019-12-04 20:38:33 +05:30
expect(response).to be_successful
2021-02-22 17:27:13 +05:30
expect(response.media_type).to eq 'application/json'
2016-06-02 11:05:42 +05:30
expect(json_response['healthy']).to be true
end
end
context 'when a service is down but NO access token' do
it 'returns a not found page' do
get :index
2017-09-10 17:25:29 +05:30
2016-06-02 11:05:42 +05:30
expect(response).to be_not_found
end
end
2017-09-10 17:25:29 +05:30
context 'when a service is down and an endpoint is accessed from whitelisted ip' do
2016-06-02 11:05:42 +05:30
before do
2017-08-17 22:00:37 +05:30
allow(HealthCheck::Utils).to receive(:process_checks).with(['standard']).and_return('The server is on fire')
allow(HealthCheck::Utils).to receive(:process_checks).with(['email']).and_return('Email is on fire')
2020-03-13 15:44:24 +05:30
allow(Gitlab::RequestContext.instance).to receive(:client_ip).and_return(whitelisted_ip)
2016-06-02 11:05:42 +05:30
end
2017-09-10 17:25:29 +05:30
it 'supports failure plaintext response' do
2016-06-02 11:05:42 +05:30
get :index
2020-03-13 15:44:24 +05:30
expect(response).to have_gitlab_http_status(:internal_server_error)
2021-02-22 17:27:13 +05:30
expect(response.media_type).to eq 'text/plain'
2016-06-02 11:05:42 +05:30
expect(response.body).to include('The server is on fire')
end
it 'supports failure json response' do
2017-09-10 17:25:29 +05:30
get :index, format: :json
2020-03-13 15:44:24 +05:30
expect(response).to have_gitlab_http_status(:internal_server_error)
2021-02-22 17:27:13 +05:30
expect(response.media_type).to eq 'application/json'
2016-06-02 11:05:42 +05:30
expect(json_response['healthy']).to be false
expect(json_response['message']).to include('The server is on fire')
end
it 'supports failure xml response' do
2017-09-10 17:25:29 +05:30
get :index, format: :xml
2020-03-13 15:44:24 +05:30
expect(response).to have_gitlab_http_status(:internal_server_error)
2021-02-22 17:27:13 +05:30
expect(response.media_type).to eq 'application/xml'
2016-06-02 11:05:42 +05:30
expect(xml_response['healthy']).to be false
expect(xml_response['message']).to include('The server is on fire')
end
it 'supports failure responses for specific checks' do
2019-02-15 15:39:39 +05:30
get :index, params: { checks: 'email' }, format: :json
2017-09-10 17:25:29 +05:30
2020-03-13 15:44:24 +05:30
expect(response).to have_gitlab_http_status(:internal_server_error)
2021-02-22 17:27:13 +05:30
expect(response.media_type).to eq 'application/json'
2016-06-02 11:05:42 +05:30
expect(json_response['healthy']).to be false
expect(json_response['message']).to include('Email is on fire')
end
end
end
end