summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/health_checks/master_check_spec.rb
blob: 287ebcec2077e2307d66df78135448786190f77e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# frozen_string_literal: true

require 'spec_helper'
require_relative './simple_check_shared'

RSpec.describe Gitlab::HealthChecks::MasterCheck do
  before do
    stub_const('SUCCESS_CODE', 100)
    stub_const('FAILURE_CODE', 101)
  end

  context 'when Puma runs in Clustered mode' do
    before do
      allow(Gitlab::Runtime).to receive(:puma_in_clustered_mode?).and_return(true)

      described_class.register_master
    end

    after do
      described_class.finish_master
    end

    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
      end

      context 'when master finishes early' do
        before do
          described_class.send(:close_write)
        end

        it 'worker does return failure' do
          _, child_status = run_worker

          expect(child_status.exitstatus).to eq(FAILURE_CODE)
        end
      end

      def run_worker
        pid = fork do
          described_class.register_worker

          exit(described_class.readiness.success ? SUCCESS_CODE : FAILURE_CODE)
        end

        Process.wait2(pid)
      end
    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

    describe '.available?' do
      specify { expect(described_class.available?).to be false }
    end
  end
end