2020-01-01 13:55:28 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-10-11 01:57:18 +05:30
|
|
|
require 'fast_spec_helper'
|
2019-12-26 22:10:19 +05:30
|
|
|
require_relative './simple_check_shared'
|
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
RSpec.describe Gitlab::HealthChecks::MasterCheck do
|
2019-12-26 22:10:19 +05:30
|
|
|
before do
|
2020-05-24 23:13:21 +05:30
|
|
|
stub_const('SUCCESS_CODE', 100)
|
|
|
|
stub_const('FAILURE_CODE', 101)
|
2019-12-26 22:10:19 +05:30
|
|
|
end
|
|
|
|
|
2021-03-11 19:13:27 +05:30
|
|
|
context 'when Puma runs in Clustered mode' do
|
|
|
|
before do
|
|
|
|
allow(Gitlab::Runtime).to receive(:puma_in_clustered_mode?).and_return(true)
|
2019-12-26 22:10:19 +05:30
|
|
|
|
2021-03-11 19:13:27 +05:30
|
|
|
described_class.register_master
|
|
|
|
end
|
2019-12-26 22:10:19 +05:30
|
|
|
|
2021-03-11 19:13:27 +05:30
|
|
|
after do
|
|
|
|
described_class.finish_master
|
2019-12-26 22:10:19 +05:30
|
|
|
end
|
|
|
|
|
2021-03-11 19:13:27 +05:30
|
|
|
describe '.available?' do
|
|
|
|
specify { expect(described_class.available?).to be true }
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '.readiness' do
|
|
|
|
context 'when master is running' do
|
|
|
|
it 'worker does return success' do
|
|
|
|
_, child_status = run_worker
|
|
|
|
|
|
|
|
expect(child_status.exitstatus).to eq(SUCCESS_CODE)
|
|
|
|
end
|
2019-12-26 22:10:19 +05:30
|
|
|
end
|
|
|
|
|
2021-03-11 19:13:27 +05:30
|
|
|
context 'when master finishes early' do
|
|
|
|
before do
|
|
|
|
described_class.send(:close_write)
|
|
|
|
end
|
2019-12-26 22:10:19 +05:30
|
|
|
|
2021-03-11 19:13:27 +05:30
|
|
|
it 'worker does return failure' do
|
|
|
|
_, child_status = run_worker
|
|
|
|
|
|
|
|
expect(child_status.exitstatus).to eq(FAILURE_CODE)
|
|
|
|
end
|
2019-12-26 22:10:19 +05:30
|
|
|
end
|
|
|
|
|
2021-03-11 19:13:27 +05:30
|
|
|
def run_worker
|
|
|
|
pid = fork do
|
|
|
|
described_class.register_worker
|
2019-12-26 22:10:19 +05:30
|
|
|
|
2021-03-11 19:13:27 +05:30
|
|
|
exit(described_class.readiness.success ? SUCCESS_CODE : FAILURE_CODE)
|
|
|
|
end
|
|
|
|
|
|
|
|
Process.wait2(pid)
|
2019-12-26 22:10:19 +05:30
|
|
|
end
|
2021-03-11 19:13:27 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# '.readiness' check is not invoked if '.available?' returns false
|
|
|
|
context 'when Puma runs in Single mode' do
|
|
|
|
before do
|
|
|
|
allow(Gitlab::Runtime).to receive(:puma_in_clustered_mode?).and_return(false)
|
|
|
|
end
|
2019-12-26 22:10:19 +05:30
|
|
|
|
2021-03-11 19:13:27 +05:30
|
|
|
describe '.available?' do
|
|
|
|
specify { expect(described_class.available?).to be false }
|
2019-12-26 22:10:19 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|