debian-mirror-gitlab/spec/lib/gitlab/downtime_check_spec.rb

116 lines
3.2 KiB
Ruby
Raw Normal View History

2019-10-12 21:52:04 +05:30
# frozen_string_literal: true
2016-08-24 12:49:21 +05:30
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
2017-09-10 17:25:29 +05:30
expect(subject).to receive(:class_for_migration_file)
.with(path)
.and_return(Class.new)
2016-08-24 12:49:21 +05:30
2017-09-10 17:25:29 +05:30
expect { subject.check([path]) }
.to raise_error(RuntimeError, /it requires downtime/)
2016-08-24 12:49:21 +05:30
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-09-10 17:25:29 +05:30
expect(subject).to receive(:class_for_migration_file)
.with(path)
.and_return(TestMigration)
2016-08-24 12:49:21 +05:30
2017-09-10 17:25:29 +05:30
expect { subject.check([path]) }
.to raise_error(RuntimeError, /no reason was given/)
2016-08-24 12:49:21 +05:30
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-09-10 17:25:29 +05:30
expect(subject).to receive(:class_for_migration_file)
.with(path)
.and_return(TestMigration)
2016-08-24 12:49:21 +05:30
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-09-10 17:25:29 +05:30
expect(subject).to receive(:class_for_migration_file)
.with(path)
.and_return(TestMigration)
2016-08-24 12:49:21 +05:30
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