Add specs for Foreman::Process#run

This commit is contained in:
brainopia
2012-01-22 22:07:53 +04:00
parent 241b91a0d5
commit ade0005a92

View File

@@ -1,6 +1,8 @@
require "spec_helper"
require "foreman/process"
require 'spec_helper'
require 'foreman/process'
require 'ostruct'
require 'timeout'
require 'tmpdir'
describe Foreman::Process do
subject { described_class.new entry, number, port }
@@ -18,37 +20,115 @@ describe Foreman::Process do
its(:pid) { nil }
describe '#run' do
let(:pipe) { :pipe }
let(:basedir) { Dir.mktmpdir }
let(:env) {{ 'foo' => 'bar' }}
let(:pipe) { :pipe }
let(:basedir) { Dir.mktmpdir }
let(:env) {{ 'foo' => 'bar' }}
let(:init_delta) { 0.1 }
let(:run) do
after { FileUtils.remove_entry_secure basedir }
def run(cmd=command)
entry.command = cmd
subject.run pipe, basedir, env
subject.detach && sleep(init_delta)
end
it 'should change to basedir' do
mock(Dir).chdir basedir
run
def run_file(executable, code)
file = File.open("#{basedir}/script.rb", 'w+') {|it| it << code }
run "#{executable} #{file.path}"
end
it 'should set PORT for environment' do
mock(subject).run_process(command, pipe) do
ENV['PORT'].should == port.to_s
context 'options' do
it 'should change to basedir' do
mock(Dir).chdir basedir
run
end
run
end
it 'should set custom variables for environment' do
mock(subject).run_process(command, pipe) do
ENV['foo'].should == 'bar'
it 'should set PORT for environment' do
mock(subject).run_process(command, pipe) do
ENV['PORT'].should == port.to_s
end
run
end
it 'should set custom variables for environment' do
mock(subject).run_process(command, pipe) do
ENV['foo'].should == 'bar'
end
run
end
it 'should restore environment afterwards' do
mock(subject).run_process command, pipe
run
ENV.should_not include('PORT', 'foo')
end
run
end
it 'should restore environment afterwards' do
mock(subject).run_process command, pipe
run
ENV.should_not include('PORT', 'foo')
context 'process' do
around do |spec|
IO.pipe do |reader, writer|
@reader, @writer = reader, writer
spec.run
end
end
let(:pipe) { @writer }
let(:output) { @reader.read_nonblock 1024 }
it 'should not block' do
expect {
Timeout.timeout(2*init_delta) { run 'sleep 2' }
}.should_not raise_exception
end
it 'should be alive' do
run 'sleep 1'
subject.should be_alive
end
it 'should be dead' do
run 'exit'
subject.should be_dead
end
it 'should be killable' do
run 'sleep 1'
subject.kill 'TERM'
subject.should be_dead
end
it 'should send different signals' do
run_file 'ruby', <<-CODE
trap 'TERM', 'IGNORE'
loop { sleep 1 }
CODE
subject.kill 'TERM'
subject.should be_alive
subject.kill 'KILL'
subject.should be_dead
end
it 'should redirect stdout' do
run 'echo hey'
output.should include('hey')
end
it 'should redirect stderr' do
run 'echo hey >2'
output.should include('hey')
end
it 'should handle variables' do
run 'echo $PORT'
output.should include('777')
end
it 'should handle arguments' do
pending
run %{ sh -c "trap '' TERM; sleep 10" }
subject.should be_alive
end
end
end
end