2019-07-25 01:21:37 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-06-24 12:29:23 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
describe Gitlab::DowntimeCheck do
|
|
|
|
subject { described_class.new }
|
2019-10-18 07:11:44 -04:00
|
|
|
|
2016-06-24 12:29:23 -04:00
|
|
|
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
|
2017-06-21 09:48:12 -04:00
|
|
|
expect(subject).to receive(:class_for_migration_file)
|
|
|
|
.with(path)
|
|
|
|
.and_return(Class.new)
|
2016-06-24 12:29:23 -04:00
|
|
|
|
2017-06-21 09:48:12 -04:00
|
|
|
expect { subject.check([path]) }
|
|
|
|
.to raise_error(RuntimeError, /it requires downtime/)
|
2016-06-24 12:29:23 -04:00
|
|
|
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)
|
|
|
|
|
2017-06-21 09:48:12 -04:00
|
|
|
expect(subject).to receive(:class_for_migration_file)
|
|
|
|
.with(path)
|
|
|
|
.and_return(TestMigration)
|
2016-06-24 12:29:23 -04:00
|
|
|
|
2017-06-21 09:48:12 -04:00
|
|
|
expect { subject.check([path]) }
|
|
|
|
.to raise_error(RuntimeError, /no reason was given/)
|
2016-06-24 12:29:23 -04:00
|
|
|
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')
|
|
|
|
|
2017-06-21 09:48:12 -04:00
|
|
|
expect(subject).to receive(:class_for_migration_file)
|
|
|
|
.with(path)
|
|
|
|
.and_return(TestMigration)
|
2016-06-24 12:29:23 -04:00
|
|
|
|
|
|
|
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)
|
|
|
|
|
2017-06-21 09:48:12 -04:00
|
|
|
expect(subject).to receive(:class_for_migration_file)
|
|
|
|
.with(path)
|
|
|
|
.and_return(TestMigration)
|
2016-06-24 12:29:23 -04:00
|
|
|
|
|
|
|
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
|