diff options
| author | Luke Kanies <luke@reductivelabs.com> | 2010-01-18 17:26:06 -0800 |
|---|---|---|
| committer | Luke Kanies <luke@reductivelabs.com> | 2010-01-18 17:26:06 -0800 |
| commit | cdcbdc78bc399a60afaf36b6267688e72081fb6e (patch) | |
| tree | 1a2e3bf8bbe06631acdcf7d0f6acc0021dfaa048 /spec/unit | |
| parent | 67216aa5637a0e134750103abb74b5c2e3db3eb6 (diff) | |
Fixing #2914 - pre/post hooks now work for transactions
This was built to be used with etckeeper to version control
files in /etc, but can be used for essentially anything.
This patch was built to be added to 0.25.4, so it's a least-modify
approach. A better approach would be to refactor application/puppet.rb
just a bit so it uses Configurer more.
This is a simple patch - it just defines 'prerun_command' and 'postrun_command'
settings, and runs the appropriate command around each transaction
if they're set.
Signed-off-by: Luke Kanies <luke@reductivelabs.com>
Diffstat (limited to 'spec/unit')
| -rwxr-xr-x | spec/unit/application/puppet.rb | 13 | ||||
| -rwxr-xr-x | spec/unit/configurer.rb | 67 |
2 files changed, 78 insertions, 2 deletions
diff --git a/spec/unit/application/puppet.rb b/spec/unit/application/puppet.rb index 7703bac08..61dcf9061 100755 --- a/spec/unit/application/puppet.rb +++ b/spec/unit/application/puppet.rb @@ -173,6 +173,9 @@ describe "Puppet" do describe "the main command" do before :each do Puppet.stubs(:[]) + Puppet.settings.stubs(:use) + Puppet.stubs(:[]).with(:prerun_command).returns "" + Puppet.stubs(:[]).with(:postrun_command).returns "" Puppet.stubs(:[]).with(:trace).returns(true) @puppet.options.stubs(:[]) @@ -277,6 +280,16 @@ describe "Puppet" do @puppet.main end + it "should call the prerun and postrun commands on a Configurer instance" do + configurer = stub 'configurer' + + Puppet::Configurer.expects(:new).returns configurer + configurer.expects(:execute_prerun_command) + configurer.expects(:execute_postrun_command) + + @puppet.main + end + it "should apply the catalog" do @catalog.expects(:apply) diff --git a/spec/unit/configurer.rb b/spec/unit/configurer.rb index cd5102546..32c0772ec 100755 --- a/spec/unit/configurer.rb +++ b/spec/unit/configurer.rb @@ -7,6 +7,11 @@ require File.dirname(__FILE__) + '/../spec_helper' require 'puppet/configurer' describe Puppet::Configurer do + before do + Puppet.settings.stubs(:use).returns(true) + @agent = Puppet::Configurer.new + end + it "should include the Plugin Handler module" do Puppet::Configurer.ancestors.should be_include(Puppet::Configurer::PluginHandler) end @@ -19,6 +24,52 @@ describe Puppet::Configurer do Puppet.settings.expects(:value).with(:puppetdlockfile).returns("/my/lock") Puppet::Configurer.lockfile_path.should == "/my/lock" end + + describe "when executing a pre-run hook" do + it "should do nothing if the hook is set to an empty string" do + Puppet.settings[:prerun_command] = "" + Puppet::Util.expects(:exec).never + + @agent.execute_prerun_command + end + + it "should execute any pre-run command provided via the 'prerun_command' setting" do + Puppet.settings[:prerun_command] = "/my/command" + Puppet::Util.expects(:execute).with { |args| args[0] == "/my/command" } + + @agent.execute_prerun_command + end + + it "should fail if the command fails" do + Puppet.settings[:prerun_command] = "/my/command" + Puppet::Util.expects(:execute).raises Puppet::ExecutionFailure + + lambda { @agent.execute_prerun_command }.should raise_error(Puppet::Configurer::CommandHookError) + end + end + + describe "when executing a post-run hook" do + it "should do nothing if the hook is set to an empty string" do + Puppet.settings[:postrun_command] = "" + Puppet::Util.expects(:exec).never + + @agent.execute_postrun_command + end + + it "should execute any post-run command provided via the 'postrun_command' setting" do + Puppet.settings[:postrun_command] = "/my/command" + Puppet::Util.expects(:execute).with { |args| args[0] == "/my/command" } + + @agent.execute_postrun_command + end + + it "should fail if the command fails" do + Puppet.settings[:postrun_command] = "/my/command" + Puppet::Util.expects(:execute).raises Puppet::ExecutionFailure + + lambda { @agent.execute_postrun_command }.should raise_error(Puppet::Configurer::CommandHookError) + end + end end describe Puppet::Configurer, "when executing a catalog run" do @@ -74,6 +125,12 @@ describe Puppet::Configurer, "when executing a catalog run" do catalog.expects(:apply).never # because we're not yielding @agent.run end + + it "should execute post-run hooks after the run" do + @agent.expects(:execute_postrun_command) + + @agent.run + end end describe Puppet::Configurer, "when retrieving a catalog" do @@ -213,6 +270,9 @@ describe Puppet::Configurer, "when preparing for a run" do Puppet.settings.stubs(:use).returns(true) @agent = Puppet::Configurer.new @agent.stubs(:dostorage) + @agent.stubs(:download_fact_plugins) + @agent.stubs(:download_plugins) + @agent.stubs(:execute_prerun_command) @facts = {"one" => "two", "three" => "four"} end @@ -223,16 +283,19 @@ describe Puppet::Configurer, "when preparing for a run" do end it "should download fact plugins" do - @agent.stubs(:dostorage) @agent.expects(:download_fact_plugins) @agent.prepare end it "should download plugins" do - @agent.stubs(:dostorage) @agent.expects(:download_plugins) @agent.prepare end + + it "should perform the pre-run commands" do + @agent.expects(:execute_prerun_command) + @agent.prepare + end end |
