Created
September 7, 2016 06:39
-
-
Save marzzz21/3546f86e4642b1a557c4ef8d2342cfa6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'spec_helper' | |
require 'mirrorsyncdaemon' | |
describe MirrorSyncDaemon do | |
context 'MirrorSyncDaemon.run' do | |
let(:mirrorsyncdaemon) { @mirrorsyncdaemon ||= MirrorSyncDaemon.new } | |
describe 'check_status method' do | |
it 'deletes the PID_FILE if it exists but contains invalid PID' do | |
stub_const("MirrorSyncDaemon::PID_FILE", './testfile.pid') | |
puts Dir.pwd | |
File.new(MirrorSyncDaemon::PID_FILE, 'w+') | |
#@mirrorsyncdaemon = MirrorSyncDaemon.new | |
allow(mirrorsyncdaemon).to receive(:check_pid_status).with(Integer).and_raise(Errno::ESRCH) | |
mirrorsyncdaemon.check_status | |
expect(File.exist?(MirrorSyncDaemon::PID_FILE)).to be false | |
File.rm(MirrorSyncDaemon::PID_FILE) if File.exist?(MirrorSyncDaemon::PID_FILE) | |
end | |
it 'does nothing and continues with the script if PID_FILE does not exist' do | |
mirrorsyncdaemon.check_status | |
expect(mirrorsyncdaemon).not_to receive(:check_pid_status) #check_pid_status should not be invoked since execution should not enter the if block without an existing PID_FILE | |
end | |
end | |
describe 'check_pid_status method' do | |
it 'exits with exit code:1 if process is already running' do | |
allow(Process).to receive(:kill).with(0, Integer) {} | |
begin | |
mirrorsyncdaemon.check_pid_status(65987) | |
rescue SystemExit=>e | |
expect(e.status).to eq(1) | |
end | |
end | |
#Create an example to test output message | |
it 'exits with exit code:1 if process is already running and is owned by a different user' do | |
allow(Process).to receive(:kill).with(0, Integer).and_raise(Errno::EPERM) | |
begin | |
mirrorsyncdaemon.check_pid_status(65987) | |
rescue SystemExit=>e | |
expect(e.status).to eq(1) | |
end | |
end | |
end | |
describe 'daemonize' do | |
describe 'RUBY_VERSION < 1.9' do | |
it 'should fork the current process' do | |
stub_const('RUBY_VERSION', '1.8') | |
allow(mirrorsyncdaemon).to receive(:fork) {} | |
allow(Process).to receive(:setsid) {} | |
expect(mirrorsyncdaemon).to receive(:fork).twice | |
mirrorsyncdaemon.daemonize | |
end | |
it 'should set the forked process as a session leader' do | |
stub_const('RUBY_VERSION', '1.8') | |
allow(mirrorsyncdaemon).to receive(:fork) {} | |
expect(Process).to receive(:setsid) {} | |
mirrorsyncdaemon.daemonize | |
end | |
it 'should set the working directory to root(/)' do | |
stub_const('RUBY_VERSION', '1.8') | |
allow(mirrorsyncdaemon).to receive(:fork) {} | |
allow(Process).to receive(:setsid) {} | |
expect(Dir).to receive(:chdir).with('/') | |
mirrorsyncdaemon.daemonize | |
end | |
end | |
describe 'RUBY_VERSION > 1.9' do | |
it 'should invoke Process.daemon skipping the if block' do | |
stub_const('RUBY_VERSION', '2.3') | |
expect(mirrorsyncdaemon).not_to receive(:fork) | |
expect(Process).not_to receive(:setsid) | |
expect(Dir).not_to receive(:chdir) | |
expect(Process).to receive(:daemon) | |
mirrorsyncdaemon.daemonize | |
end | |
end | |
end | |
describe 'write_pid' do | |
it "should write the pid on the PID_FILE if it doesn't exist" do | |
stub_const("MirrorSyncDaemon::PID_FILE", './testfile1.pid') | |
File.new(MirrorSyncDaemon::PID_FILE, 'w+') | |
expect(File).to receive(:open).with(MirrorSyncDaemon::PID_FILE) | |
mirrorsyncdaemon.write_pid | |
File.rm(MirrorSyncDaemon::PID_FILE) if File.exist?(MirrorSyncDaemon::PID_FILE) | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment