Files
metasploit-gs/spec/support/shared/examples/msf/db_manager/migration.rb
T

162 lines
4.0 KiB
Ruby
Raw Normal View History

2015-12-31 16:56:13 -06:00
RSpec.shared_examples_for 'Msf::DBManager::Migration' do
2018-04-02 08:08:23 -05:00
if ENV['REMOTE_DB']
before {skip("Migration is not tested for a remoted DB")}
end
2015-10-21 09:23:22 -05:00
it { is_expected.to be_a Msf::DBManager::Migration }
2013-05-20 12:45:17 -05:00
context '#add_rails_engine_migration_paths' do
def add_rails_engine_migration_paths
db_manager.add_rails_engine_migration_paths
end
it 'should not add duplicate paths to ActiveRecord::Migrator.migrations_paths' do
add_rails_engine_migration_paths
expect {
add_rails_engine_migration_paths
}.to_not change {
ActiveRecord::Migrator.migrations_paths.length
}
2015-10-20 11:30:38 -05:00
expect(ActiveRecord::Migrator.migrations_paths.uniq).to eq ActiveRecord::Migrator.migrations_paths
end
end
2013-09-30 13:47:53 -05:00
context '#migrate' do
def migrate
db_manager.migrate
end
2013-05-20 12:45:17 -05:00
2013-09-30 13:47:53 -05:00
it 'should call ActiveRecord::Migrator.migrate' do
2015-10-20 12:40:33 -05:00
expect(ActiveRecord::Migrator).to receive(:migrate).with(
2013-09-30 13:47:53 -05:00
ActiveRecord::Migrator.migrations_paths
)
2013-05-20 12:45:17 -05:00
2013-09-30 13:47:53 -05:00
migrate
end
2013-05-20 12:45:17 -05:00
2013-09-30 13:47:53 -05:00
it 'should return migrations that were ran from ActiveRecord::Migrator.migrate' do
migrations = [double('Migration 1')]
2015-10-20 13:45:31 -05:00
expect(ActiveRecord::Migrator).to receive(:migrate).and_return(migrations)
2013-05-20 12:45:17 -05:00
2015-10-20 11:30:38 -05:00
expect(migrate).to eq migrations
2013-09-30 13:47:53 -05:00
end
2013-05-20 12:45:17 -05:00
2013-09-30 13:47:53 -05:00
it 'should reset the column information' do
2015-10-20 12:40:33 -05:00
expect(db_manager).to receive(:reset_column_information)
2013-09-30 13:47:53 -05:00
migrate
end
2013-09-30 13:47:53 -05:00
context 'with StandardError from ActiveRecord::Migration.migrate' do
let(:error) do
StandardError.new(message)
end
2013-05-20 12:45:17 -05:00
2013-09-30 13:47:53 -05:00
let(:message) do
"Error during migration"
end
2013-05-20 12:45:17 -05:00
2015-12-31 16:56:13 -06:00
before(:example) do
2015-10-20 13:45:31 -05:00
expect(ActiveRecord::Migrator).to receive(:migrate).and_raise(error)
2013-09-30 13:47:53 -05:00
end
2013-05-20 12:45:17 -05:00
2013-09-30 13:47:53 -05:00
it 'should set Msf::DBManager#error' do
migrate
2013-05-20 12:45:17 -05:00
2015-10-20 11:30:38 -05:00
expect(db_manager.error).to eq error
2013-09-30 13:47:53 -05:00
end
2013-05-20 12:45:17 -05:00
2013-09-30 13:47:53 -05:00
it 'should log error message at error level' do
2015-10-20 12:40:33 -05:00
expect(db_manager).to receive(:elog) do |error_message|
2015-10-20 14:37:18 -05:00
expect(error_message).to include(error.to_s)
2013-09-30 13:47:53 -05:00
end
2013-05-20 12:45:17 -05:00
2013-09-30 13:47:53 -05:00
migrate
end
2013-05-20 12:45:17 -05:00
2013-09-30 13:47:53 -05:00
it 'should log error backtrace at debug level' do
2015-10-20 12:40:33 -05:00
expect(db_manager).to receive(:dlog) do |debug_message|
2015-10-20 14:37:18 -05:00
expect(debug_message).to include('Call stack')
2013-09-30 13:47:53 -05:00
end
2013-05-20 12:45:17 -05:00
2013-09-30 13:47:53 -05:00
migrate
end
end
2013-05-20 12:45:17 -05:00
2013-09-30 13:47:53 -05:00
context 'with verbose' do
def migrate
db_manager.migrate(verbose)
end
2013-05-20 12:45:17 -05:00
2013-09-30 13:47:53 -05:00
context 'false' do
let(:verbose) do
false
end
2013-05-20 12:45:17 -05:00
2013-09-30 13:47:53 -05:00
it 'should set ActiveRecord::Migration.verbose to false' do
2015-10-20 12:40:33 -05:00
expect(ActiveRecord::Migration).to receive(:verbose=).with(verbose)
2013-05-20 12:45:17 -05:00
2013-09-30 13:47:53 -05:00
migrate
end
end
2013-05-20 12:45:17 -05:00
2013-09-30 13:47:53 -05:00
context 'true' do
let(:verbose) do
true
end
2013-05-20 12:45:17 -05:00
2013-09-30 13:47:53 -05:00
it 'should set ActiveRecord::Migration.verbose to true' do
2015-10-20 12:40:33 -05:00
expect(ActiveRecord::Migration).to receive(:verbose=).with(verbose)
2013-05-20 12:45:17 -05:00
2013-09-30 13:47:53 -05:00
migrate
end
end
end
2013-05-20 12:45:17 -05:00
2013-09-30 13:47:53 -05:00
context 'without verbose' do
it 'should set ActiveRecord::Migration.verbose to false' do
2015-10-20 12:40:33 -05:00
expect(ActiveRecord::Migration).to receive(:verbose=).with(false)
2013-05-20 12:45:17 -05:00
2013-09-30 13:47:53 -05:00
db_manager.migrate
end
end
end
2013-05-20 12:45:17 -05:00
2013-09-30 13:47:53 -05:00
context '#migrated' do
2015-10-21 09:23:22 -05:00
it { is_expected.to respond_to :migrated }
it { is_expected.to respond_to :migrated= }
2013-09-30 13:47:53 -05:00
end
2013-09-30 13:47:53 -05:00
context '#reset_column_information' do
def reset_column_information
db_manager.send(:reset_column_information)
end
2013-09-30 13:47:53 -05:00
it 'should use ActiveRecord::Base.descendants to find both direct and indirect subclasses' do
2015-10-20 12:40:33 -05:00
expect(ActiveRecord::Base).to receive(:descendants).and_return([])
2013-09-30 13:47:53 -05:00
reset_column_information
end
2013-09-30 13:47:53 -05:00
it 'should reset column information on each descendant of ActiveRecord::Base' do
descendants = []
2013-09-30 13:47:53 -05:00
1.upto(2) do |i|
descendants << double("Descendant #{i}")
end
2015-10-20 13:45:31 -05:00
expect(ActiveRecord::Base).to receive(:descendants).and_return(descendants)
2013-09-30 13:47:53 -05:00
descendants.each do |descendant|
2015-10-20 12:40:33 -05:00
expect(descendant).to receive(:reset_column_information)
2013-09-30 13:47:53 -05:00
end
2013-09-30 13:47:53 -05:00
reset_column_information
end
end
2014-08-26 15:24:08 -05:00
end