summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/database/background_migration/health_status_spec.rb
blob: 6d0430dcbbb82fe76b5b3b50f82041ed14432275 (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
71
72
73
74
75
76
77
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Gitlab::Database::BackgroundMigration::HealthStatus do
  let(:connection) { Gitlab::Database.database_base_models[:main].connection }

  around do |example|
    Gitlab::Database::SharedModel.using_connection(connection) do
      example.run
    end
  end

  describe '.evaluate' do
    subject(:evaluate) { described_class.evaluate(migration, indicator_class) }

    let(:migration) { build(:batched_background_migration, :active) }

    let(:health_status) { 'Gitlab::Database::BackgroundMigration::HealthStatus' }
    let(:indicator_class) { class_double("#{health_status}::Indicators::AutovacuumActiveOnTable") }
    let(:indicator) { instance_double("#{health_status}::Indicators::AutovacuumActiveOnTable") }

    before do
      allow(indicator_class).to receive(:new).with(migration.health_context).and_return(indicator)
    end

    it 'returns a signal' do
      signal = instance_double("#{health_status}::Signals::Normal", log_info?: false)

      expect(indicator).to receive(:evaluate).and_return(signal)

      expect(evaluate).to eq(signal)
    end

    it 'logs interesting signals' do
      signal = instance_double("#{health_status}::Signals::Stop", log_info?: true)

      expect(indicator).to receive(:evaluate).and_return(signal)
      expect(described_class).to receive(:log_signal).with(signal, migration)

      evaluate
    end

    it 'does not log signals of no interest' do
      signal = instance_double("#{health_status}::Signals::Normal", log_info?: false)

      expect(indicator).to receive(:evaluate).and_return(signal)
      expect(described_class).not_to receive(:log_signal)

      evaluate
    end

    context 'on indicator error' do
      let(:error) { RuntimeError.new('everything broken') }

      before do
        expect(indicator).to receive(:evaluate).and_raise(error)
      end

      it 'does not fail' do
        expect { evaluate }.not_to raise_error
      end

      it 'returns Unknown signal' do
        expect(evaluate).to be_an_instance_of(Gitlab::Database::BackgroundMigration::HealthStatus::Signals::Unknown)
        expect(evaluate.reason).to eq("unexpected error: everything broken (RuntimeError)")
      end

      it 'reports the exception to error tracking' do
        expect(Gitlab::ErrorTracking).to receive(:track_exception)
          .with(error, migration_id: migration.id, job_class_name: migration.job_class_name)

        evaluate
      end
    end
  end
end