summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/downtime_check_spec.rb
blob: 1f1e4e0216c4267dc9ae34aca4929102e62ff3aa (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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
require 'spec_helper'

describe Gitlab::DowntimeCheck do
  subject { described_class.new }
  let(:path) { 'foo.rb' }

  describe '#check' do
    before do
      expect(subject).to receive(:require).with(path)
    end

    context 'when a migration does not specify if downtime is required' do
      it 'raises RuntimeError' do
        expect(subject).to receive(:class_for_migration_file)
          .with(path)
          .and_return(Class.new)

        expect { subject.check([path]) }
          .to raise_error(RuntimeError, /it requires downtime/)
      end
    end

    context 'when a migration requires downtime' do
      context 'when no reason is specified' do
        it 'raises RuntimeError' do
          stub_const('TestMigration::DOWNTIME', true)

          expect(subject).to receive(:class_for_migration_file)
            .with(path)
            .and_return(TestMigration)

          expect { subject.check([path]) }
            .to raise_error(RuntimeError, /no reason was given/)
        end
      end

      context 'when a reason is specified' do
        it 'returns an Array of messages' do
          stub_const('TestMigration::DOWNTIME', true)
          stub_const('TestMigration::DOWNTIME_REASON', 'foo')

          expect(subject).to receive(:class_for_migration_file)
            .with(path)
            .and_return(TestMigration)

          messages = subject.check([path])

          expect(messages).to be_an_instance_of(Array)
          expect(messages[0]).to be_an_instance_of(Gitlab::DowntimeCheck::Message)

          message = messages[0]

          expect(message.path).to eq(path)
          expect(message.offline).to eq(true)
          expect(message.reason).to eq('foo')
        end
      end
    end
  end

  describe '#check_and_print' do
    it 'checks the migrations and prints the results to STDOUT' do
      stub_const('TestMigration::DOWNTIME', true)
      stub_const('TestMigration::DOWNTIME_REASON', 'foo')

      expect(subject).to receive(:require).with(path)

      expect(subject).to receive(:class_for_migration_file)
        .with(path)
        .and_return(TestMigration)

      expect(subject).to receive(:puts).with(an_instance_of(String))

      subject.check_and_print([path])
    end
  end

  describe '#class_for_migration_file' do
    it 'returns the class for a migration file path' do
      expect(subject.class_for_migration_file('123_string.rb')).to eq(String)
    end
  end

  describe '#online?' do
    it 'returns true when a migration can be performed online' do
      stub_const('TestMigration::DOWNTIME', false)

      expect(subject.online?(TestMigration)).to eq(true)
    end

    it 'returns false when a migration can not be performed online' do
      stub_const('TestMigration::DOWNTIME', true)

      expect(subject.online?(TestMigration)).to eq(false)
    end
  end

  describe '#downtime_reason' do
    context 'when a reason is defined' do
      it 'returns the downtime reason' do
        stub_const('TestMigration::DOWNTIME_REASON', 'hello')

        expect(subject.downtime_reason(TestMigration)).to eq('hello')
      end
    end

    context 'when a reason is not defined' do
      it 'returns nil' do
        expect(subject.downtime_reason(Class.new)).to be_nil
      end
    end
  end
end