add support for testing scenarios where commands fail
This commit is contained in:
@@ -10,7 +10,7 @@ module Chef::RSpec
|
|||||||
module CIBObject
|
module CIBObject
|
||||||
# Return a Mixlib::ShellOut double which mimics successful
|
# Return a Mixlib::ShellOut double which mimics successful
|
||||||
# execution of a command, returning the given string on STDOUT.
|
# execution of a command, returning the given string on STDOUT.
|
||||||
def shellout_double(string)
|
def succeeding_shellout_double(string)
|
||||||
shellout = double(Mixlib::ShellOut)
|
shellout = double(Mixlib::ShellOut)
|
||||||
shellout.stub(:environment).and_return({})
|
shellout.stub(:environment).and_return({})
|
||||||
shellout.stub(:run_command)
|
shellout.stub(:run_command)
|
||||||
@@ -19,10 +19,38 @@ module Chef::RSpec
|
|||||||
shellout
|
shellout
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Return a Mixlib::ShellOut double which mimics failed
|
||||||
|
# execution of a command, raising an exception when #error! is
|
||||||
|
# called. We expect #error! to be called, because if it isn't,
|
||||||
|
# that probably indicates the code isn't robust enough. This
|
||||||
|
# may need to be relaxed in the future.
|
||||||
|
def failing_shellout_double(stdout='', stderr='', exitstatus=1)
|
||||||
|
shellout = double(Mixlib::ShellOut)
|
||||||
|
shellout.stub(:environment).and_return({})
|
||||||
|
shellout.stub(:run_command)
|
||||||
|
shellout.stub(:stdout).and_return(stdout)
|
||||||
|
shellout.stub(:stderr).and_return(stderr)
|
||||||
|
shellout.stub(:exitstatus).and_return(exitstatus)
|
||||||
|
exception = Mixlib::ShellOut::ShellCommandFailed.new(
|
||||||
|
"Expected process to exit with 0, " +
|
||||||
|
"but received '#{exitstatus}'"
|
||||||
|
)
|
||||||
|
expect(shellout).to receive(:error!).and_raise(exception)
|
||||||
|
shellout
|
||||||
|
end
|
||||||
|
|
||||||
# This stubs Mixlib::ShellOut.new with a sequence of doubles
|
# This stubs Mixlib::ShellOut.new with a sequence of doubles
|
||||||
# whose #stdout methods return a corresponding sequence of strings.
|
# with a corresponding sequence of behaviours. This allows us
|
||||||
# This allows us to simulate the output of a series of shell
|
# to simulate the output of a series of shell commands being run
|
||||||
# commands being run via Mixlib::ShellOut.
|
# via Mixlib::ShellOut. Each double either mimics a successful
|
||||||
|
# command execution whose #stdout method returns the given
|
||||||
|
# string, or a failed execution with the given exit code and
|
||||||
|
# STDOUT/STDERR.
|
||||||
|
#
|
||||||
|
# results is an Array describing the sequence of behaviours;
|
||||||
|
# each element is either a string mimicking STDOUT from
|
||||||
|
# successful command execution, or a [stdout, stderr, exitcode]
|
||||||
|
# status mimicking command execution failure.
|
||||||
#
|
#
|
||||||
# For example, "crm configure show" is executed by
|
# For example, "crm configure show" is executed by
|
||||||
# #load_current_resource, and again later on for the :create
|
# #load_current_resource, and again later on for the :create
|
||||||
@@ -32,8 +60,12 @@ module Chef::RSpec
|
|||||||
# definition if we wanted to test modification of an existing
|
# definition if we wanted to test modification of an existing
|
||||||
# one. If the test needs subsequent doubles to return different
|
# one. If the test needs subsequent doubles to return different
|
||||||
# values then stdout_strings can have more than one element.
|
# values then stdout_strings can have more than one element.
|
||||||
def stub_shellout(*stdout_strings)
|
def stub_shellout(*results)
|
||||||
doubles = stdout_strings.map { |string| shellout_double(string) }
|
doubles = results.map { |result|
|
||||||
|
result.is_a?(String) ?
|
||||||
|
succeeding_shellout_double(result)
|
||||||
|
: failing_shellout_double(*result)
|
||||||
|
}
|
||||||
Mixlib::ShellOut.stub(:new).and_return(*doubles)
|
Mixlib::ShellOut.stub(:new).and_return(*doubles)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
Reference in New Issue
Block a user