2021-03-11 19:13:27 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
RSpec.describe Gitlab::Database::Migrations::Instrumentation do
|
|
|
|
describe '#observe' do
|
|
|
|
subject { described_class.new }
|
|
|
|
|
2021-10-27 15:23:28 +05:30
|
|
|
let(:migration_name) { 'test' }
|
|
|
|
let(:migration_version) { '12345' }
|
2021-03-11 19:13:27 +05:30
|
|
|
|
|
|
|
it 'executes the given block' do
|
2021-10-27 15:23:28 +05:30
|
|
|
expect { |b| subject.observe(version: migration_version, name: migration_name, &b) }.to yield_control
|
2021-03-11 19:13:27 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
context 'behavior with observers' do
|
2021-10-27 15:23:28 +05:30
|
|
|
subject { described_class.new([Gitlab::Database::Migrations::Observers::MigrationObserver]).observe(version: migration_version, name: migration_name) {} }
|
2021-03-11 19:13:27 +05:30
|
|
|
|
|
|
|
let(:observer) { instance_double('Gitlab::Database::Migrations::Observers::MigrationObserver', before: nil, after: nil, record: nil) }
|
|
|
|
|
2021-10-27 15:23:28 +05:30
|
|
|
before do
|
|
|
|
allow(Gitlab::Database::Migrations::Observers::MigrationObserver).to receive(:new).and_return(observer)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'instantiates observer with observation' do
|
|
|
|
expect(Gitlab::Database::Migrations::Observers::MigrationObserver)
|
|
|
|
.to receive(:new)
|
|
|
|
.with(instance_of(Gitlab::Database::Migrations::Observation)) { |observation| expect(observation.version).to eq(migration_version) }
|
|
|
|
.and_return(observer)
|
|
|
|
|
|
|
|
subject
|
|
|
|
end
|
|
|
|
|
2021-03-11 19:13:27 +05:30
|
|
|
it 'calls #before, #after, #record on given observers' do
|
|
|
|
expect(observer).to receive(:before).ordered
|
|
|
|
expect(observer).to receive(:after).ordered
|
2021-10-27 15:23:28 +05:30
|
|
|
expect(observer).to receive(:record).ordered
|
2021-03-11 19:13:27 +05:30
|
|
|
|
|
|
|
subject
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'ignores errors coming from observers #before' do
|
|
|
|
expect(observer).to receive(:before).and_raise('some error')
|
|
|
|
|
|
|
|
subject
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'ignores errors coming from observers #after' do
|
|
|
|
expect(observer).to receive(:after).and_raise('some error')
|
|
|
|
|
|
|
|
subject
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'ignores errors coming from observers #record' do
|
|
|
|
expect(observer).to receive(:record).and_raise('some error')
|
|
|
|
|
|
|
|
subject
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'on successful execution' do
|
2021-10-27 15:23:28 +05:30
|
|
|
subject { described_class.new.observe(version: migration_version, name: migration_name) {} }
|
2021-03-11 19:13:27 +05:30
|
|
|
|
|
|
|
it 'records walltime' do
|
|
|
|
expect(subject.walltime).not_to be_nil
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'records success' do
|
|
|
|
expect(subject.success).to be_truthy
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'records the migration version' do
|
2021-10-27 15:23:28 +05:30
|
|
|
expect(subject.version).to eq(migration_version)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'records the migration name' do
|
|
|
|
expect(subject.name).to eq(migration_name)
|
2021-03-11 19:13:27 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'upon failure' do
|
2021-10-27 15:23:28 +05:30
|
|
|
subject { described_class.new.observe(version: migration_version, name: migration_name) { raise 'something went wrong' } }
|
2021-03-11 19:13:27 +05:30
|
|
|
|
|
|
|
it 'raises the exception' do
|
|
|
|
expect { subject }.to raise_error(/something went wrong/)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'retrieving observations' do
|
|
|
|
subject { instance.observations.first }
|
|
|
|
|
|
|
|
before do
|
2021-10-27 15:23:28 +05:30
|
|
|
instance.observe(version: migration_version, name: migration_name) { raise 'something went wrong' }
|
2021-06-08 01:23:25 +05:30
|
|
|
rescue StandardError
|
2021-03-11 19:13:27 +05:30
|
|
|
# ignore
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:instance) { described_class.new }
|
|
|
|
|
|
|
|
it 'records walltime' do
|
|
|
|
expect(subject.walltime).not_to be_nil
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'records failure' do
|
|
|
|
expect(subject.success).to be_falsey
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'records the migration version' do
|
2021-10-27 15:23:28 +05:30
|
|
|
expect(subject.version).to eq(migration_version)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'records the migration name' do
|
|
|
|
expect(subject.name).to eq(migration_name)
|
2021-03-11 19:13:27 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'sequence of migrations with failures' do
|
|
|
|
subject { described_class.new }
|
|
|
|
|
|
|
|
let(:migration1) { double('migration1', call: nil) }
|
|
|
|
let(:migration2) { double('migration2', call: nil) }
|
|
|
|
|
|
|
|
it 'records observations for all migrations' do
|
2021-10-27 15:23:28 +05:30
|
|
|
subject.observe(version: migration_version, name: migration_name) {}
|
|
|
|
subject.observe(version: migration_version, name: migration_name) { raise 'something went wrong' } rescue nil
|
2021-03-11 19:13:27 +05:30
|
|
|
|
|
|
|
expect(subject.observations.size).to eq(2)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|