diff options
author | Marc Paradise <marc@opscode.com> | 2012-04-19 20:55:50 -0400 |
---|---|---|
committer | Daniel DeLeo <dan@opscode.com> | 2012-05-04 14:33:14 -0700 |
commit | 6f633ba4d8a1f308f7de17171515e044455a2bd1 (patch) | |
tree | 55be867232306624180a40ad256252abd5092ab5 | |
parent | 805616c5faf858fb38ac789a5b8265939d9b639e (diff) | |
download | chef-6f633ba4d8a1f308f7de17171515e044455a2bd1.tar.gz |
whyrun for subversion provider
-rw-r--r-- | chef/lib/chef/provider/subversion.rb | 54 | ||||
-rw-r--r-- | chef/spec/unit/provider/subversion_spec.rb | 29 |
2 files changed, 46 insertions, 37 deletions
diff --git a/chef/lib/chef/provider/subversion.rb b/chef/lib/chef/provider/subversion.rb index 08ad7167f0..021c977526 100644 --- a/chef/lib/chef/provider/subversion.rb +++ b/chef/lib/chef/provider/subversion.rb @@ -17,6 +17,8 @@ # +#TODO subversion and git should both extend from a base SCM provider. + require 'chef/log' require 'chef/provider' require 'chef/mixin/command' @@ -40,30 +42,40 @@ class Chef end end + def define_resource_requirements + requirements.assert(:all_actions) do |a| + # Make sure the parent dir exists, or else fail. + # for why run, print a message explaining the potential error. + parent_directory = ::File.dirname(@new_resource.destination) + a.assertion { ::File.directory?(parent_directory) } + a.failure_message(Chef::Exceptions::MissingParentDirectory, + "Cannot clone #{@new_resource} to #{@new_resource.destination}, the enclosing directory #{parent_directory} does not exist") + a.whyrun("Directory #{parent_directory} does not exist, assuming it would have been created") + end + end + def action_checkout - assert_target_directory_valid! - if target_dir_non_existant_or_empty? - run_command(run_options(:command => checkout_command)) - @new_resource.updated_by_last_action(true) + if target_dir_non_existent_or_empty? + converge_by("would perform checkout of #{@new_resource.repository} into #{@new_resource.destination}") do + run_command(run_options(:command => checkout_command)) + end else Chef::Log.debug "#{@new_resource} checkout destination #{@new_resource.destination} already exists or is a non-empty directory - nothing to do" end end def action_export - assert_target_directory_valid! - if target_dir_non_existant_or_empty? - run_command(run_options(:command => export_command)) - @new_resource.updated_by_last_action(true) + if target_dir_non_existent_or_empty? + action_force_export else Chef::Log.debug "#{@new_resource} export destination #{@new_resource.destination} already exists or is a non-empty directory - nothing to do" end end def action_force_export - assert_target_directory_valid! - run_command(run_options(:command => export_command)) - @new_resource.updated_by_last_action(true) + converge_by("would export #{@new_resource.repository} into #{@new_resource.destination}") do + run_command(run_options(:command => export_command)) + end end def action_sync @@ -72,16 +84,16 @@ class Chef current_rev = find_current_revision Chef::Log.debug "#{@new_resource} current revision: #{current_rev} target revision: #{revision_int}" unless current_revision_matches_target_revision? - run_command(run_options(:command => sync_command)) - Chef::Log.info "#{@new_resource} updated to revision: #{revision_int}" - @new_resource.updated_by_last_action(true) + converge_by("would sync #{@new_resource.destination} from #{@new_resource.repository}") do + run_command(run_options(:command => sync_command)) + Chef::Log.info "#{@new_resource} updated to revision: #{revision_int}" + end end else action_checkout - @new_resource.updated_by_last_action(true) end end - + def sync_command c = scm :update, @new_resource.svn_arguments, verbose, authentication, "-r#{revision_int}", @new_resource.destination Chef::Log.debug "#{@new_resource} updated working copy #{@new_resource.destination} to revision #{@new_resource.revision}" @@ -182,8 +194,10 @@ class Chef ['svn', *args].compact.join(" ") end - # TODO these methods are the same as the git provider...need to REFACTOR - # ...the subversion and git providers should extend from the same parent + + def target_dir_non_existent_or_empty? + !::File.exist?(@new_resource.destination) || Dir.entries(@new_resource.destination).sort == ['.','..'] + end def assert_target_directory_valid! target_parent_directory = ::File.dirname(@new_resource.destination) unless ::File.directory?(target_parent_directory) @@ -191,10 +205,6 @@ class Chef raise Chef::Exceptions::MissingParentDirectory, msg end end - - def target_dir_non_existant_or_empty? - !::File.exist?(@new_resource.destination) || Dir.entries(@new_resource.destination).sort == ['.','..'] - end end end end diff --git a/chef/spec/unit/provider/subversion_spec.rb b/chef/spec/unit/provider/subversion_spec.rb index d15b9c25cc..6c56634c9b 100644 --- a/chef/spec/unit/provider/subversion_spec.rb +++ b/chef/spec/unit/provider/subversion_spec.rb @@ -199,27 +199,29 @@ describe Chef::Provider::Subversion do ::File.stub!(:directory?).with("/my/deploy").and_return(true) expected_cmd = "svn export --force -q -r12345 http://svn.example.org/trunk/ /my/deploy/dir" @provider.should_receive(:run_command).with(:command => expected_cmd) - @provider.action_force_export + @provider.run_action(:force_export) + @resource.should be_updated end it "runs the checkout command for action_checkout" do ::File.stub!(:directory?).with("/my/deploy").and_return(true) expected_cmd = "svn checkout -q -r12345 http://svn.example.org/trunk/ /my/deploy/dir" @provider.should_receive(:run_command).with(:command => expected_cmd) - @provider.action_checkout + @provider.run_action(:checkout) @resource.should be_updated end it "raises an error if the svn checkout command would fail because the enclosing directory doesn't exist" do - lambda {@provider.action_sync}.should raise_error(Chef::Exceptions::MissingParentDirectory) + lambda {@provider.run_action(:sync)}.should raise_error(Chef::Exceptions::MissingParentDirectory) end it "should not checkout if the destination exists or is a non empty directory" do + ::File.stub!(:exist?).with("/my/deploy/dir/.svn").and_return(false) ::File.stub!(:exist?).with("/my/deploy/dir").and_return(true) ::File.stub!(:directory?).with("/my/deploy").and_return(true) ::Dir.stub!(:entries).with("/my/deploy/dir").and_return(['.','..','foo','bar']) @provider.should_not_receive(:checkout_command) - @provider.action_checkout + @provider.run_action(:checkout) @resource.should_not be_updated end @@ -229,24 +231,22 @@ describe Chef::Provider::Subversion do @resource.group "thisis" expected_cmd = "svn checkout -q -r12345 http://svn.example.org/trunk/ /my/deploy/dir" @provider.should_receive(:run_command).with(:command => expected_cmd, :user => "whois", :group => "thisis") - @provider.action_checkout + @provider.run_action(:checkout) @resource.should be_updated end it "does a checkout for action_sync if there's no deploy dir" do ::File.stub!(:directory?).with("/my/deploy").and_return(true) - ::File.should_receive(:exist?).with("/my/deploy/dir/.svn").and_return(false) + ::File.should_receive(:exist?).with("/my/deploy/dir/.svn").twice.and_return(false) @provider.should_receive(:action_checkout) - @provider.action_sync - @resource.should be_updated + @provider.run_action(:sync) end it "does a checkout for action_sync if the deploy dir exists but is empty" do ::File.stub!(:directory?).with("/my/deploy").and_return(true) - ::File.should_receive(:exist?).with("/my/deploy/dir/.svn").and_return(false) + ::File.should_receive(:exist?).with("/my/deploy/dir/.svn").twice.and_return(false) @provider.should_receive(:action_checkout) - @provider.action_sync - @resource.should be_updated + @provider.run_action(:sync) end it "runs the sync_command on action_sync if the deploy dir exists and isn't empty" do @@ -256,17 +256,16 @@ describe Chef::Provider::Subversion do @provider.stub!(:current_revision_matches_target_revision?).and_return(false) expected_cmd = "svn update -q -r12345 /my/deploy/dir" @provider.should_receive(:run_command).with(:command => expected_cmd) - @provider.action_sync + @provider.run_action(:sync) @resource.should be_updated end it "does not fetch any updates if the remote revision matches the current revision" do ::File.stub!(:directory?).with("/my/deploy").and_return(true) ::File.should_receive(:exist?).with("/my/deploy/dir/.svn").and_return(true) - #::File.stub!(:directory?).with("/my/deploy").and_return(true) @provider.stub!(:find_current_revision).and_return('12345') @provider.stub!(:current_revision_matches_target_revision?).and_return(true) - @provider.action_sync + @provider.run_action(:sync) @resource.should_not be_updated end @@ -274,7 +273,7 @@ describe Chef::Provider::Subversion do ::File.stub!(:directory?).with("/my/deploy").and_return(true) expected_cmd = "svn export --force -q -r12345 http://svn.example.org/trunk/ /my/deploy/dir" @provider.should_receive(:run_command).with(:command => expected_cmd) - @provider.action_export + @provider.run_action(:export) @resource.should be_updated end |