Skip to content

Instantly share code, notes, and snippets.

@marzdgzmn
Last active September 27, 2018 07:26
Show Gist options
  • Save marzdgzmn/10061be792b770f40379e07813d0d96b to your computer and use it in GitHub Desktop.
Save marzdgzmn/10061be792b770f40379e07813d0d96b to your computer and use it in GitHub Desktop.
1) Rise::MirrorManager::Sync#execute_custom_script when the custom script succesfully runs should not raise any errors
lib/rise/mirror_manager/sync.rb|44 error| expect { mirror_sync.execute_custom_script('qwerty') }.to_not raise_error expected no Exception, got #<Rise::MirrorManager::Error::CmdError: Rise::MirrorManager::Error::CmdError> with backtrace:
# ./lib/rise/mirror_manager/sync.rb:38:in `execute_custom_script'
# ./spec/rise/mirror_manager/sync_spec.rb:29:in `block (5 levels) in <top (required)>'
# ./spec/rise/mirror_manager/sync_spec.rb:29:in `block (4 levels) in <top (required)>'
# ./spec/rise/mirror_manager/sync_spec.rb:29:in `block (4 levels) in <top (required)>'
require 'rise/mirror_manager/error/cmd_error'
module Rise
module MirrorManager
class Sync
include Rise::MirrorManager::Error
DEFAULT_RSYNC_OPTION = '-avzuH --no-motd --safe-links --numeric-ids --delay-updates --timeout=600 --contimeout=300'.freeze
DEFAULT_RSYNC_DELETE_OPTIONS = '--delete --delete-delay'.freeze
def self.run(mirror)
Sync.new(mirror).run
end
def run
begin
run_pre_cmd(pre_cmd)
execute_custom_script(custom_script)
#run sync
#run_post_cmd
rescue CmdError
#handle exception
end
end
def execute_pre_cmd(cmd)
begin
output = %x(#{cmd})
unless $?.exitstatus == 0
raise CmdError.new(output)
end
rescue Errno::ENOENT => e
raise CmdError.new(e.message)
end
end
def execute_custom_script(cmd)
begin
output = %x(#{cmd})
unless $?.exitstatus == 0
raise CmdError.new(output)
end
rescue Errno::ENOENT => e
raise CmdError.new(e.message)
end
end
private
attr_reader :log, :pre_cmd
def initialize(mirror)
@pre_cmd = mirror.pre_cmd if mirror.pre_cmd
# assign logger @log
end
end
end
end
require 'ostruct'
RSpec.describe Rise::MirrorManager::Sync do
dummy_mirror_hash = { name: 'mirror-test'}
#'pre_cmd': 'precmd' }
let(:mirror) { OpenStruct.new(dummy_mirror_hash) }
let(:mirror_sync) { Rise::MirrorManager::Sync.new(mirror) }
describe '#execute_pre_cmd' do
context 'with a succeeding command' do
it 'should run without any errors' do
expect { mirror_sync.execute_pre_cmd('ls') }.to_not raise_error
end
end
context 'with a failing command' do
it 'should raise a CmdError Exception ' do
expect { mirror_sync.execute_pre_cmd('nonExistentCmd')}.to raise_error(Rise::MirrorManager::Error::CmdError)
end
end
end
describe '#execute_custom_script' do
context "when the custom script succesfully runs" do
it 'should not raise any errors' do
#allow_any_instance_of(Rise::MirrorManager::Sync).to receive(:`).with('qwerty').and_return(0)
expect(Kernel).to receive(:`).with('qwerty').and_return(0)
expect { mirror_sync.execute_custom_script('qwerty') }.to_not raise_error
end
end
end
end
# it should run pre_rsync cmds
# pre_rsync cmd should raise an error if cmd fails
# it should run post_rsync cmds
# it should run rsync
# it should run the custom script, if provided, instead of rsync
# it should invoke the corresponding database function for the job's result
class MirrorSync
def execute_custom_script(cmd)
begin
output = %x(#{cmd})
unless $?.exitstatus == 0
raise CmdError.new(e.message)
end
end
end
end
describe '#execute_custom_script' do
context 'when the custom script succesfully runs' do
it 'should not raise any errors' do
allow_any_instance_of(Kernel).to receive(:`).with('qwerty').and_return(0)
expect { mirror_sync.execute_custom_script('qwerty').to_not raise_error
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment