diff options
Diffstat (limited to 'spec/unit/util/execution_spec.rb')
-rwxr-xr-x | spec/unit/util/execution_spec.rb | 77 |
1 files changed, 35 insertions, 42 deletions
diff --git a/spec/unit/util/execution_spec.rb b/spec/unit/util/execution_spec.rb index 7c6238f9f..6a4bee490 100755 --- a/spec/unit/util/execution_spec.rb +++ b/spec/unit/util/execution_spec.rb @@ -1,15 +1,9 @@ #! /usr/bin/env ruby require 'spec_helper' +require 'puppet/file_system/uniquefile' describe Puppet::Util::Execution do include Puppet::Util::Execution - # utility method to help deal with some windows vs. unix differences - def process_status(exitstatus) - return exitstatus if Puppet.features.microsoft_windows? - - stub('child_status', :exitstatus => exitstatus) - end - # utility methods to help us test some private methods without being quite so verbose def call_exec_posix(command, arguments, stdin, stdout, stderr) Puppet::Util::Execution.send(:execute_posix, command, arguments, stdin, stdout, stderr) @@ -28,8 +22,8 @@ describe Puppet::Util::Execution do def stub_process_wait(exitstatus) if Puppet.features.microsoft_windows? Puppet::Util::Windows::Process.stubs(:wait_process).with(process_handle).returns(exitstatus) - Process.stubs(:CloseHandle).with(process_handle) - Process.stubs(:CloseHandle).with(thread_handle) + FFI::WIN32.stubs(:CloseHandle).with(process_handle) + FFI::WIN32.stubs(:CloseHandle).with(thread_handle) else Process.stubs(:waitpid2).with(pid).returns([pid, stub('child_status', :exitstatus => exitstatus)]) end @@ -52,7 +46,7 @@ describe Puppet::Util::Execution do $stderr.stubs(:reopen) @stdin = File.open(null_file, 'r') - @stdout = Tempfile.new('stdout') + @stdout = Puppet::FileSystem::Uniquefile.new('stdout') @stderr = File.open(null_file, 'w') # there is a danger here that ENV will be modified by exec_posix. Normally it would only affect the ENV @@ -132,7 +126,7 @@ describe Puppet::Util::Execution do stub_process_wait(0) @stdin = File.open(null_file, 'r') - @stdout = Tempfile.new('stdout') + @stdout = Puppet::FileSystem::Uniquefile.new('stdout') @stderr = File.open(null_file, 'w') end @@ -223,8 +217,8 @@ describe Puppet::Util::Execution do describe "when squelch is not set" do it "should set stdout to a temporary output file" do - outfile = Tempfile.new('stdout') - Tempfile.stubs(:new).returns(outfile) + outfile = Puppet::FileSystem::Uniquefile.new('stdout') + Puppet::FileSystem::Uniquefile.stubs(:new).returns(outfile) Puppet::Util::Execution.expects(executor).with do |_,_,_,stdout,_| stdout.path == outfile.path @@ -234,8 +228,8 @@ describe Puppet::Util::Execution do end it "should set stderr to the same file as stdout if combine is true" do - outfile = Tempfile.new('stdout') - Tempfile.stubs(:new).returns(outfile) + outfile = Puppet::FileSystem::Uniquefile.new('stdout') + Puppet::FileSystem::Uniquefile.stubs(:new).returns(outfile) Puppet::Util::Execution.expects(executor).with do |_,_,_,stdout,stderr| stdout.path == outfile.path and stderr.path == outfile.path @@ -245,8 +239,8 @@ describe Puppet::Util::Execution do end it "should set stderr to the null device if combine is false" do - outfile = Tempfile.new('stdout') - Tempfile.stubs(:new).returns(outfile) + outfile = Puppet::FileSystem::Uniquefile.new('stdout') + Puppet::FileSystem::Uniquefile.stubs(:new).returns(outfile) Puppet::Util::Execution.expects(executor).with do |_,_,_,stdout,stderr| stdout.path == outfile.path and stderr.path == null_file @@ -256,8 +250,8 @@ describe Puppet::Util::Execution do end it "should combine stdout and stderr if combine is true" do - outfile = Tempfile.new('stdout') - Tempfile.stubs(:new).returns(outfile) + outfile = Puppet::FileSystem::Uniquefile.new('stdout') + Puppet::FileSystem::Uniquefile.stubs(:new).returns(outfile) Puppet::Util::Execution.expects(executor).with do |_,_,_,stdout,stderr| stdout.path == outfile.path and stderr.path == outfile.path @@ -267,8 +261,8 @@ describe Puppet::Util::Execution do end it "should default combine to true when no options are specified" do - outfile = Tempfile.new('stdout') - Tempfile.stubs(:new).returns(outfile) + outfile = Puppet::FileSystem::Uniquefile.new('stdout') + Puppet::FileSystem::Uniquefile.stubs(:new).returns(outfile) Puppet::Util::Execution.expects(executor).with do |_,_,_,stdout,stderr| stdout.path == outfile.path and stderr.path == outfile.path @@ -278,8 +272,8 @@ describe Puppet::Util::Execution do end it "should default combine to false when options are specified, but combine is not" do - outfile = Tempfile.new('stdout') - Tempfile.stubs(:new).returns(outfile) + outfile = Puppet::FileSystem::Uniquefile.new('stdout') + Puppet::FileSystem::Uniquefile.stubs(:new).returns(outfile) Puppet::Util::Execution.expects(executor).with do |_,_,_,stdout,stderr| stdout.path == outfile.path and stderr.path == null_file @@ -289,8 +283,8 @@ describe Puppet::Util::Execution do end it "should default combine to false when an empty hash of options is specified" do - outfile = Tempfile.new('stdout') - Tempfile.stubs(:new).returns(outfile) + outfile = Puppet::FileSystem::Uniquefile.new('stdout') + Puppet::FileSystem::Uniquefile.stubs(:new).returns(outfile) Puppet::Util::Execution.expects(executor).with do |_,_,_,stdout,stderr| stdout.path == outfile.path and stderr.path == null_file @@ -306,8 +300,8 @@ describe Puppet::Util::Execution do Puppet::Util::Execution.stubs(:execute_windows).returns(proc_info_stub) Puppet::Util::Windows::Process.expects(:wait_process).with(process_handle).raises('whatever') - Puppet::Util::Windows::Process.expects(:CloseHandle).with(thread_handle) - Puppet::Util::Windows::Process.expects(:CloseHandle).with(process_handle) + FFI::WIN32.expects(:CloseHandle).with(thread_handle) + FFI::WIN32.expects(:CloseHandle).with(process_handle) expect { Puppet::Util::Execution.execute('test command') }.to raise_error(RuntimeError) end @@ -507,25 +501,25 @@ describe Puppet::Util::Execution do end it "should read and return the output if squelch is false" do - stdout = Tempfile.new('test') - Tempfile.stubs(:new).returns(stdout) + stdout = Puppet::FileSystem::Uniquefile.new('test') + Puppet::FileSystem::Uniquefile.stubs(:new).returns(stdout) stdout.write("My expected command output") Puppet::Util::Execution.execute('test command').should == "My expected command output" end it "should not read the output if squelch is true" do - stdout = Tempfile.new('test') - Tempfile.stubs(:new).returns(stdout) + stdout = Puppet::FileSystem::Uniquefile.new('test') + Puppet::FileSystem::Uniquefile.stubs(:new).returns(stdout) stdout.write("My expected command output") Puppet::Util::Execution.execute('test command', :squelch => true).should == '' end it "should delete the file used for output if squelch is false" do - stdout = Tempfile.new('test') + stdout = Puppet::FileSystem::Uniquefile.new('test') path = stdout.path - Tempfile.stubs(:new).returns(stdout) + Puppet::FileSystem::Uniquefile.stubs(:new).returns(stdout) Puppet::Util::Execution.execute('test command') @@ -533,8 +527,8 @@ describe Puppet::Util::Execution do end it "should not raise an error if the file is open" do - stdout = Tempfile.new('test') - Tempfile.stubs(:new).returns(stdout) + stdout = Puppet::FileSystem::Uniquefile.new('test') + Puppet::FileSystem::Uniquefile.stubs(:new).returns(stdout) file = File.new(stdout.path, 'r') Puppet::Util.execute('test command') @@ -597,40 +591,39 @@ describe Puppet::Util::Execution do describe "#execpipe" do it "should execute a string as a string" do Puppet::Util::Execution.expects(:open).with('| echo hello 2>&1').returns('hello') - $CHILD_STATUS.expects(:==).with(0).returns(true) + Puppet::Util::Execution.expects(:exitstatus).returns(0) Puppet::Util::Execution.execpipe('echo hello').should == 'hello' end it "should print meaningful debug message for string argument" do Puppet::Util::Execution.expects(:debug).with("Executing 'echo hello'") Puppet::Util::Execution.expects(:open).with('| echo hello 2>&1').returns('hello') - $CHILD_STATUS.expects(:==).with(0).returns(true) + Puppet::Util::Execution.expects(:exitstatus).returns(0) Puppet::Util::Execution.execpipe('echo hello') end it "should print meaningful debug message for array argument" do Puppet::Util::Execution.expects(:debug).with("Executing 'echo hello'") Puppet::Util::Execution.expects(:open).with('| echo hello 2>&1').returns('hello') - $CHILD_STATUS.expects(:==).with(0).returns(true) + Puppet::Util::Execution.expects(:exitstatus).returns(0) Puppet::Util::Execution.execpipe(['echo','hello']) end it "should execute an array by pasting together with spaces" do Puppet::Util::Execution.expects(:open).with('| echo hello 2>&1').returns('hello') - $CHILD_STATUS.expects(:==).with(0).returns(true) + Puppet::Util::Execution.expects(:exitstatus).returns(0) Puppet::Util::Execution.execpipe(['echo', 'hello']).should == 'hello' end it "should fail if asked to fail, and the child does" do - Puppet::Util::Execution.stubs(:open).returns('error message') - $CHILD_STATUS.expects(:==).with(0).returns(false) + Puppet::Util::Execution.stubs(:open).with('| echo hello 2>&1').returns('error message') + Puppet::Util::Execution.expects(:exitstatus).returns(1) expect { Puppet::Util::Execution.execpipe('echo hello') }. to raise_error Puppet::ExecutionFailure, /error message/ end it "should not fail if asked not to fail, and the child does" do Puppet::Util::Execution.stubs(:open).returns('error message') - $CHILD_STATUS.stubs(:==).with(0).returns(false) Puppet::Util::Execution.execpipe('echo hello', false).should == 'error message' end end |