[Pkg-puppet-devel] [SCM] Puppet packaging for Debian branch, upstream, updated. 2.6.5-303-gfcfa26a

Paul Berry paul at puppetlabs.com
Thu Mar 17 10:48:51 UTC 2011


The following commit has been merged in the upstream branch:
commit 23d1c0346a609369b457da876714c6671fcf3d44
Author: Paul Berry <paul at puppetlabs.com>
Date:   Thu Feb 24 12:43:45 2011 -0800

    Maint: Added the ability to replace the behavior of
    Puppet::Util.execute with an arbitrary code block for ease in spec
    testing.
    
    Reviewed-by: Max Martin <max at puppetlabs.com>

diff --git a/lib/puppet/util.rb b/lib/puppet/util.rb
index 850d147..d06f448 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 0000000..af74e0f
--- /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
diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb
index a374fb0..ae4edb2 100644
--- a/spec/spec_helper.rb
+++ b/spec/spec_helper.rb
@@ -34,6 +34,7 @@ RSpec.configure do |config|
     Puppet.settings.clear
     Puppet::Node::Environment.clear
     Puppet::Util::Storage.clear
+    Puppet::Util::ExecutionStub.reset
 
     if defined?($tmpfiles)
       $tmpfiles.each do |file|
diff --git a/spec/unit/util/execution_stub_spec.rb b/spec/unit/util/execution_stub_spec.rb
new file mode 100644
index 0000000..14cf9c6
--- /dev/null
+++ b/spec/unit/util/execution_stub_spec.rb
@@ -0,0 +1,35 @@
+#!/usr/bin/env ruby
+
+require File.dirname(__FILE__) + '/../../spec_helper'
+
+describe Puppet::Util::ExecutionStub do
+  it "should use the provided stub code when 'set' is called" do
+    Puppet::Util::ExecutionStub.set do |command, options|
+      command.should == ['/bin/foo', 'bar']
+      "stub output"
+    end
+    Puppet::Util::ExecutionStub.current_value.should_not == nil
+    Puppet::Util.execute(['/bin/foo', 'bar']).should == "stub output"
+  end
+
+  it "should automatically restore normal execution at the conclusion of each spec test" do
+    # Note: this test relies on the previous test creating a stub.
+    Puppet::Util::ExecutionStub.current_value.should == nil
+  end
+
+  it "should restore normal execution after 'reset' is called" do
+    true_command = Puppet::Util.which('true') # Note: "true" exists at different paths in different OSes
+    stub_call_count = 0
+    Puppet::Util::ExecutionStub.set do |command, options|
+      command.should == [true_command]
+      stub_call_count += 1
+      'stub called'
+    end
+    Puppet::Util.execute([true_command]).should == 'stub called'
+    stub_call_count.should == 1
+    Puppet::Util::ExecutionStub.reset
+    Puppet::Util::ExecutionStub.current_value.should == nil
+    Puppet::Util.execute([true_command]).should == ''
+    stub_call_count.should == 1
+  end
+end

-- 
Puppet packaging for Debian



More information about the Pkg-puppet-devel mailing list