summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/puppet/util.rb5
-rw-r--r--lib/puppet/util/execution_stub.rb26
2 files changed, 31 insertions, 0 deletions
diff --git a/lib/puppet/util.rb b/lib/puppet/util.rb
index 850d147e2..d06f44808 100644
--- a/lib/puppet/util.rb
+++ b/lib/puppet/util.rb
@@ -4,6 +4,7 @@ require 'puppet/util/monkey_patches'
require 'sync'
require 'puppet/external/lock'
require 'monitor'
+require 'puppet/util/execution_stub'
module Puppet
# A command failed to execute.
@@ -264,6 +265,10 @@ module Util
arguments[:uid] = Puppet::Util::SUIDManager.convert_xid(:uid, arguments[:uid]) if arguments[:uid]
arguments[:gid] = Puppet::Util::SUIDManager.convert_xid(:gid, arguments[:gid]) if arguments[:gid]
+ if execution_stub = Puppet::Util::ExecutionStub.current_value
+ return execution_stub.call(command, arguments)
+ end
+
@@os ||= Facter.value(:operatingsystem)
output = nil
child_pid, child_status = nil
diff --git a/lib/puppet/util/execution_stub.rb b/lib/puppet/util/execution_stub.rb
new file mode 100644
index 000000000..af74e0f72
--- /dev/null
+++ b/lib/puppet/util/execution_stub.rb
@@ -0,0 +1,26 @@
+module Puppet::Util
+ class ExecutionStub
+ class << self
+ # Set a stub block that Puppet::Util.execute() should invoke instead
+ # of actually executing commands on the target machine. Intended
+ # for spec testing.
+ #
+ # The arguments passed to the block are |command, options|, where
+ # command is an array of strings and options is an options hash.
+ def set(&block)
+ @value = block
+ end
+
+ # Uninstall any execution stub, so that calls to
+ # Puppet::Util.execute() behave normally again.
+ def reset
+ @value = nil
+ end
+
+ # Retrieve the current execution stub, or nil if there is no stub.
+ def current_value
+ @value
+ end
+ end
+ end
+end