BigW Consortium Gitlab

simple_check_shared.rb 2.43 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
shared_context 'simple_check' do |metrics_prefix, check_name, success_result|
  describe '#metrics' do
    subject { described_class.metrics }
    context 'Check is passing' do
      before do
        allow(described_class).to receive(:check).and_return success_result
      end

      it { is_expected.to include(have_attributes(name: "#{metrics_prefix}_success", value: 1)) }
      it { is_expected.to include(have_attributes(name: "#{metrics_prefix}_timeout", value: 0)) }
11
      it { is_expected.to include(have_attributes(name: "#{metrics_prefix}_latency_seconds", value: be >= 0)) }
12 13 14 15 16 17 18 19 20
    end

    context 'Check is misbehaving' do
      before do
        allow(described_class).to receive(:check).and_return 'error!'
      end

      it { is_expected.to include(have_attributes(name: "#{metrics_prefix}_success", value: 0)) }
      it { is_expected.to include(have_attributes(name: "#{metrics_prefix}_timeout", value: 0)) }
21
      it { is_expected.to include(have_attributes(name: "#{metrics_prefix}_latency_seconds", value: be >= 0)) }
22 23 24 25 26 27 28 29 30
    end

    context 'Check is timeouting' do
      before do
        allow(described_class).to receive(:check).and_return Timeout::Error.new
      end

      it { is_expected.to include(have_attributes(name: "#{metrics_prefix}_success", value: 0)) }
      it { is_expected.to include(have_attributes(name: "#{metrics_prefix}_timeout", value: 1)) }
31
      it { is_expected.to include(have_attributes(name: "#{metrics_prefix}_latency_seconds", value: be >= 0)) }
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
    end
  end

  describe '#readiness' do
    subject { described_class.readiness }
    context 'Check returns ok' do
      before do
        allow(described_class).to receive(:check).and_return success_result
      end

      it { is_expected.to have_attributes(success: true) }
    end

    context 'Check is misbehaving' do
      before do
        allow(described_class).to receive(:check).and_return 'error!'
      end

50
      it { is_expected.to have_attributes(success: false, message: "unexpected #{described_class.human_name} check result: error!") }
51 52 53 54 55 56 57
    end

    context 'Check is timeouting' do
      before do
        allow(described_class).to receive(:check ).and_return Timeout::Error.new
      end

58
      it { is_expected.to have_attributes(success: false, message: "#{described_class.human_name} check timed out") }
59 60 61 62 63 64 65 66
    end
  end

  describe '#liveness' do
    subject { described_class.readiness }
    it { is_expected.to eq(Gitlab::HealthChecks::Result.new(true)) }
  end
end