diff options
author | Claire McQuin <claire@getchef.com> | 2014-05-19 13:55:18 -0700 |
---|---|---|
committer | Claire McQuin <claire@getchef.com> | 2014-05-22 12:16:36 -0700 |
commit | 7d2891419ef04288ddd7bb2322df41cc9bd203dd (patch) | |
tree | 354eb64b83b99b4803fb8f7850483e9aca945f20 | |
parent | 92519eb2cf7ae1b362f22d5710a229c5809a47cc (diff) | |
download | chef-7d2891419ef04288ddd7bb2322df41cc9bd203dd.tar.gz |
Put cache at HOME/.chef.d if /var/chef can't be accessed.
-rw-r--r-- | lib/chef/config.rb | 22 | ||||
-rw-r--r-- | spec/integration/knife/chef_repo_path_spec.rb | 1 | ||||
-rw-r--r-- | spec/integration/knife/deps_spec.rb | 4 | ||||
-rw-r--r-- | spec/integration/knife/list_spec.rb | 4 | ||||
-rw-r--r-- | spec/integration/knife/raw_spec.rb | 4 | ||||
-rw-r--r-- | spec/integration/knife/redirection_spec.rb | 4 | ||||
-rw-r--r-- | spec/integration/knife/show_spec.rb | 4 | ||||
-rw-r--r-- | spec/unit/client_spec.rb | 1 | ||||
-rw-r--r-- | spec/unit/config_spec.rb | 75 | ||||
-rw-r--r-- | spec/unit/role_spec.rb | 6 | ||||
-rw-r--r-- | spec/unit/run_lock_spec.rb | 2 |
11 files changed, 114 insertions, 13 deletions
diff --git a/lib/chef/config.rb b/lib/chef/config.rb index 66dd4cc77e..fcab13ca5e 100644 --- a/lib/chef/config.rb +++ b/lib/chef/config.rb @@ -142,7 +142,7 @@ class Chef end end else - platform_specific_path("/var/chef") + cache_path end end @@ -240,10 +240,28 @@ class Chef if local_mode "#{config_dir}local-mode-cache" else - platform_specific_path("/var/chef") + primary_cache_root = platform_specific_path("/var") + primary_cache_path = platform_specific_path("/var/chef") + # Use /var/chef as the cache path only if that folder exists and we can read and write + # into it, or /var exists and we can read and write into it (we'll create /var/chef later). + # Otherwise, we'll create .chef.d under the user's home directory and use that as + # the cache path. + unless path_accessible?(primary_cache_path) || path_accessible?(primary_cache_root) + secondary_cache_path = File.join(user_home, '.chef.d') + secondary_cache_path.gsub!(File::SEPARATOR, platform_path_separator) # Safety, mainly for Windows... + Chef::Log.warn("Unable to access cache at #{primary_cache_path}. Switching cache to #{secondary_cache_path}") + secondary_cache_path + else + primary_cache_path + end end end + # Returns true only if the path exists and is readable and writeable for the user. + def self.path_accessible?(path) + File.exists?(path) && File.readable?(path) && File.writable?(path) + end + # Where cookbook files are stored on the server (by content checksum) default(:checksum_path) { path_join(cache_path, "checksums") } diff --git a/spec/integration/knife/chef_repo_path_spec.rb b/spec/integration/knife/chef_repo_path_spec.rb index 87619d8a58..154a8d9718 100644 --- a/spec/integration/knife/chef_repo_path_spec.rb +++ b/spec/integration/knife/chef_repo_path_spec.rb @@ -806,6 +806,7 @@ EOM Chef::Config.delete("#{object_name}_path".to_sym) end Chef::Config.delete(:chef_repo_path) + Chef::Config[:cache_path] = windows? ? 'C:\chef' : '/var/chef' Chef::Config.data_bag_path = File.join(@repository_dir, 'data_bags') end diff --git a/spec/integration/knife/deps_spec.rb b/spec/integration/knife/deps_spec.rb index 5ede0caef3..56820bc2b3 100644 --- a/spec/integration/knife/deps_spec.rb +++ b/spec/integration/knife/deps_spec.rb @@ -340,6 +340,10 @@ EOM end context 'remote' do + before do + Chef::Config[:cache_path] = windows? ? 'C:\chef' : '/var/chef' + end + when_the_chef_server 'has a role with no run_list' do role 'starring', {} it 'knife deps reports no dependencies' do diff --git a/spec/integration/knife/list_spec.rb b/spec/integration/knife/list_spec.rb index b9d75ce1f1..37e6f7fdb2 100644 --- a/spec/integration/knife/list_spec.rb +++ b/spec/integration/knife/list_spec.rb @@ -22,6 +22,10 @@ describe 'knife list' do extend IntegrationSupport include KnifeSupport + before do + Chef::Config[:cache_path] = windows? ? 'C:\chef' : '/var/chef' + end + when_the_chef_server "is empty" do it "knife list / returns all top level directories" do knife('list /').should_succeed <<EOM diff --git a/spec/integration/knife/raw_spec.rb b/spec/integration/knife/raw_spec.rb index fafd0a47ee..5cada50cc5 100644 --- a/spec/integration/knife/raw_spec.rb +++ b/spec/integration/knife/raw_spec.rb @@ -24,6 +24,10 @@ describe 'knife raw' do include KnifeSupport include AppServerSupport + before do + Chef::Config[:cache_path] = windows? ? 'C:\chef' : '/var/chef' + end + when_the_chef_server "has one of each thing" do client 'x', '{}' cookbook 'x', '1.0.0', { 'metadata.rb' => 'version "1.0.0"' } diff --git a/spec/integration/knife/redirection_spec.rb b/spec/integration/knife/redirection_spec.rb index 2ed49a7b24..481c57eebd 100644 --- a/spec/integration/knife/redirection_spec.rb +++ b/spec/integration/knife/redirection_spec.rb @@ -23,6 +23,10 @@ describe 'redirection' do include KnifeSupport include AppServerSupport + before do + Chef::Config[:cache_path] = windows? ? 'C:\chef' : '/var/chef' + end + when_the_chef_server 'has a role' do role 'x', {} diff --git a/spec/integration/knife/show_spec.rb b/spec/integration/knife/show_spec.rb index a061fab040..3755c84b9f 100644 --- a/spec/integration/knife/show_spec.rb +++ b/spec/integration/knife/show_spec.rb @@ -22,6 +22,10 @@ describe 'knife show' do extend IntegrationSupport include KnifeSupport + before do + Chef::Config[:cache_path] = windows? ? 'C:\chef' : '/var/chef' + end + when_the_chef_server "has one of each thing" do client 'x', '{}' cookbook 'x', '1.0.0', { 'metadata.rb' => 'version "1.0.0"' } diff --git a/spec/unit/client_spec.rb b/spec/unit/client_spec.rb index 36a0e9fc1b..37a2dc09ca 100644 --- a/spec/unit/client_spec.rb +++ b/spec/unit/client_spec.rb @@ -272,6 +272,7 @@ describe Chef::Client do before do Chef::Config[:client_fork] = enable_fork + Chef::Config[:cache_path] = windows? ? 'C:\chef' : '/var/chef' stub_const("Chef::Client::STDOUT_FD", stdout) stub_const("Chef::Client::STDERR_FD", stderr) diff --git a/spec/unit/config_spec.rb b/spec/unit/config_spec.rb index 82506f556c..d1449e221e 100644 --- a/spec/unit/config_spec.rb +++ b/spec/unit/config_spec.rb @@ -138,13 +138,70 @@ describe Chef::Config do end describe "default values" do + def primary_cache_path + if windows? + "#{Chef::Config.env['SYSTEMDRIVE']}\\chef" + else + "/var/chef" + end + end - it "Chef::Config[:file_backup_path] defaults to /var/chef/backup" do - backup_path = if windows? - "#{ENV['SYSTEMDRIVE']}\\chef\\backup" + def secondary_cache_path + if windows? + "#{Chef::Config.env['SYSTEMDRIVE']}\\#{Chef::Config.env['HOMEPATH']}\\.chef.d" else - "/var/chef/backup" + "#{Chef::Config.env['HOME']}/.chef.d" end + end + + before do + if windows? + Chef::Config.stub(:env).and_return({'SYSTEMDRIVE' => "C:", 'HOMEPATH' => "MyHome"}) + else + Chef::Config.stub(:env).and_return({'HOME' => "/Users/me"}) + end + + Chef::Config.stub(:path_accessible?).and_return(false) + end + + describe "Chef::Config[:cache_path]" do + context "when /var/chef exists and is accessible" do + it "defaults to /var/chef" do + Chef::Config.stub(:path_accessible?).and_return(true) + Chef::Config[:cache_path].should == primary_cache_path + end + end + + context "when /var/chef does not exist and /var is accessible" do + it "defaults to /var/chef" do + File.stub(:exists?).with(Chef::Config.platform_specific_path("/var/chef")).and_return(false) + Chef::Config.stub(:path_accessible?).with(Chef::Config.platform_specific_path("/var")).and_return(true) + Chef::Config[:cache_path].should == primary_cache_path + end + end + + context "when /var/chef does not exist and /var is not accessible" do + it "defaults to $HOME/.chef.d" do + File.stub(:exists?).with(Chef::Config.platform_specific_path("/var/chef")).and_return(false) + Chef::Config.stub(:path_accessible?).with(Chef::Config.platform_specific_path("/var")).and_return(false) + Chef::Config[:cache_path].should == secondary_cache_path + end + end + + context "when /var/chef exists and is not accessible" do + it "defaults to $HOME/.chef.d" do + File.stub(:exists?).with(Chef::Config.platform_specific_path("/var/chef")).and_return(true) + File.stub(:readable?).with(Chef::Config.platform_specific_path("/var/chef")).and_return(true) + File.stub(:writable?).with(Chef::Config.platform_specific_path("/var/chef")).and_return(false) + + Chef::Config[:cache_path].should == secondary_cache_path + end + end + end + + it "Chef::Config[:file_backup_path] defaults to /var/chef/backup" do + Chef::Config.stub(:cache_path).and_return(Chef::Config.platform_specific_path("/var/chef")) + backup_path = Chef::Config.platform_specific_path("#{Chef::Config.cache_path}/backup") Chef::Config[:file_backup_path].should == backup_path end @@ -167,17 +224,15 @@ describe Chef::Config do end it "Chef::Config[:data_bag_path] defaults to /var/chef/data_bags" do + Chef::Config.stub(:cache_path).and_return(Chef::Config.platform_specific_path("/var/chef")) data_bag_path = - Chef::Config.platform_specific_path("/var/chef/data_bags") + Chef::Config.platform_specific_path("#{Chef::Config.cache_path}/data_bags") Chef::Config[:data_bag_path].should == data_bag_path end it "Chef::Config[:environment_path] defaults to /var/chef/environments" do - environment_path = if windows? - "C:\\chef\\environments" - else - "/var/chef/environments" - end + Chef::Config.stub(:cache_path).and_return(Chef::Config.platform_specific_path("/var/chef")) + environment_path = Chef::Config.platform_specific_path("#{Chef::Config.cache_path}/environments") Chef::Config[:environment_path].should == environment_path end diff --git a/spec/unit/role_spec.rb b/spec/unit/role_spec.rb index f36b7f13bd..7410b3a0c6 100644 --- a/spec/unit/role_spec.rb +++ b/spec/unit/role_spec.rb @@ -250,6 +250,11 @@ description "like Aliens, but furry" EOR describe "when loading from disk" do + before do + default_cache_path = windows? ? 'C:\chef' : '/var/chef' + Chef::Config.stub(:cache_path).and_return(default_cache_path) + end + it "should return a Chef::Role object from JSON" do File.should_receive(:exists?).with(File.join(Chef::Config[:role_path], 'lolcat.json')).exactly(1).times.and_return(true) IO.should_receive(:read).with(File.join(Chef::Config[:role_path], 'lolcat.json')).and_return('{"name": "ceiling_cat", "json_class": "Chef::Role" }') @@ -325,4 +330,3 @@ EOR end end - diff --git a/spec/unit/run_lock_spec.rb b/spec/unit/run_lock_spec.rb index 9738f76650..80140dfcce 100644 --- a/spec/unit/run_lock_spec.rb +++ b/spec/unit/run_lock_spec.rb @@ -20,10 +20,12 @@ require 'chef/client' describe Chef::RunLock do + default_cache_path = windows? ? 'C:\chef' : '/var/chef' default_pid_location = windows? ? 'C:\chef\cache\chef-client-running.pid' : '/var/chef/cache/chef-client-running.pid' describe "when first created" do it "locates the lockfile in the file cache path by default" do + Chef::Config.stub(:cache_path).and_return(default_cache_path) run_lock = Chef::RunLock.new(Chef::Config.lockfile) run_lock.runlock_file.should == default_pid_location end |