summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordanielsdeleo <dan@opscode.com>2013-05-21 14:33:24 -0700
committerdanielsdeleo <dan@opscode.com>2013-05-23 12:59:13 -0700
commit48a420aa73fbee3c04e963d599b7f6fe3f8f6910 (patch)
tree39eff755a7d9707206fd7cb783b7b956c9683d59
parent168ca518c0d83fd27bfc3d9d232f91031777a81c (diff)
downloadchef-48a420aa73fbee3c04e963d599b7f6fe3f8f6910.tar.gz
remove provider loading of cache control data
-rw-r--r--lib/chef/provider/remote_file.rb14
-rw-r--r--lib/chef/provider/remote_file/http.rb29
-rw-r--r--spec/unit/provider/remote_file/http_spec.rb136
3 files changed, 51 insertions, 128 deletions
diff --git a/lib/chef/provider/remote_file.rb b/lib/chef/provider/remote_file.rb
index 3d4d2ee599..229b44e4af 100644
--- a/lib/chef/provider/remote_file.rb
+++ b/lib/chef/provider/remote_file.rb
@@ -37,20 +37,6 @@ class Chef
def load_current_resource
@current_resource = Chef::Resource::RemoteFile.new(@new_resource.name)
super
- unless fileinfo.nil? || fileinfo["checksum"].nil? || fileinfo["checksum"] == ""
- Chef::Log.debug("found cached file information at #{Chef::Config[:file_cache_path]}/remote_file/#{new_resource.name}")
- if fileinfo["checksum"] == @current_resource.checksum
- @current_resource.etag fileinfo["etag"]
- @current_resource.last_modified fileinfo["last_modified"]
- @current_resource.source fileinfo["src"]
- Chef::Log.debug("loaded etag %s, last_modified %s, source %s from file metadata cache" % [fileinfo["etag"], fileinfo["last_modified"], fileinfo["src"]])
- else
- Chef::Log.debug("current resource checksum '#{@current_resource.checksum}' does not match cached checksum '#{fileinfo['checksum']}', removing metadata...")
- FileUtils.rm_f("#{Chef::Config[:file_cache_path]}/remote_file/#{new_resource.name}")
- end
- else
- Chef::Log.debug("no cached file information found")
- end
end
private
diff --git a/lib/chef/provider/remote_file/http.rb b/lib/chef/provider/remote_file/http.rb
index 2a28ae81b7..e216ba0632 100644
--- a/lib/chef/provider/remote_file/http.rb
+++ b/lib/chef/provider/remote_file/http.rb
@@ -37,7 +37,6 @@ class Chef
class HTTP
attr_reader :uri
- attr_reader :headers
attr_reader :new_resource
attr_reader :current_resource
@@ -46,31 +45,10 @@ class Chef
@uri = uri
@new_resource = new_resource
@current_resource = current_resource
+ end
- @headers = Hash[new_resource.headers]
- if current_resource.source && Chef::Provider::RemoteFile::Util.uri_matches_string?(uri, current_resource.source[0])
- if current_resource.etag && ( current_resource.etag != "" )
- if new_resource.use_etag
- @headers['if-none-match'] = "\"#{current_resource.etag}\""
- Chef::Log.debug("set if-none-match header to '#{current_resource.etag}'")
- else
- Chef::Log.debug("stash has etags but resource has use_etag set to false, not sending if-none-match header")
- end
- else
- Chef::Log.debug("no etag headers in file information stash, not sending if-none-match header")
- end
- if current_resource.last_modified
- if new_resource.use_last_modified
- @headers['if-modified-since'] = current_resource.last_modified.strftime("%a, %d %b %Y %H:%M:%S %Z")
- Chef::Log.debug("set if-modified-since header to '#{@headers['if-modified-since']}'")
- else
- Chef::Log.debug("stash has last-modified but resource has use_last_modified set to false, not sending if-modified-since header")
- end
- else
- Chef::Log.debug("no last-modified headers in file information stash, not sending if-modified-since header")
- end
- end
- @uri = uri
+ def headers
+ conditional_get_headers.merge(new_resource.headers)
end
def conditional_get_headers
@@ -82,6 +60,7 @@ class Chef
if etag = cache_control_data.etag and want_etag_cache_control?
cache_control_headers["if-none-match"] = etag
end
+ Chef::Log.debug("Cache control headers: #{cache_control_headers.inspect}")
cache_control_headers
end
diff --git a/spec/unit/provider/remote_file/http_spec.rb b/spec/unit/provider/remote_file/http_spec.rb
index fd5d4b4a16..9bc02838cf 100644
--- a/spec/unit/provider/remote_file/http_spec.rb
+++ b/spec/unit/provider/remote_file/http_spec.rb
@@ -77,11 +77,15 @@ describe Chef::Provider::RemoteFile::HTTP do
end
let(:new_resource) do
- new_resource = Chef::Resource::RemoteFile.new("/tmp/foo.txt")
- new_resource.headers({})
- new_resource
+ Chef::Resource::RemoteFile.new("/tmp/foo.txt")
end
+ subject(:fetcher) do
+ Chef::Provider::RemoteFile::HTTP.new(uri, new_resource, current_resource)
+ end
+
+ let(:cache_control_data) { Chef::Provider::RemoteFile::CacheControlData.new }
+
def use_last_modified!
new_resource.use_last_modified(true)
current_resource.last_modified(Time.new)
@@ -95,12 +99,7 @@ describe Chef::Provider::RemoteFile::HTTP do
current_resource.etag("a_unique_identifier")
end
- describe "when loading cache control data" do
- let(:cache_control_data) { Chef::Provider::RemoteFile::CacheControlData.new }
-
- subject(:fetcher) do
- Chef::Provider::RemoteFile::HTTP.new(uri, new_resource, current_resource)
- end
+ describe "generating cache control headers" do
context "and there is no valid cache control data for this URI on disk" do
@@ -111,13 +110,27 @@ describe Chef::Provider::RemoteFile::HTTP do
it "does not add conditional GET headers" do
fetcher.conditional_get_headers.should == {}
end
+
+ context "and the resource specifies custom headers" do
+ before do
+ new_resource.headers("x-myapp-header" => "custom-header-value")
+ end
+
+ it "has the user-specified custom headers" do
+ fetcher.headers.should == {"x-myapp-header" => "custom-header-value"}
+ end
+ end
+
end
context "and the cache control data matches the existing file" do
# http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.26
let(:etag) { "\"a-strong-unique-identifier\"" }
+
+ # http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.3
let(:mtime) { "Tue, 21 May 2013 19:19:23 GMT" }
+
before do
cache_control_data.etag = etag
cache_control_data.mtime = mtime
@@ -127,7 +140,7 @@ describe Chef::Provider::RemoteFile::HTTP do
context "and no conditional get features are enabled" do
it "does not add headers to the request" do
- fetcher.conditional_get_headers.should == {}
+ fetcher.headers.should == {}
end
end
@@ -137,11 +150,29 @@ describe Chef::Provider::RemoteFile::HTTP do
end
it "adds If-None-Match and If-Modified-Since headers to the request" do
- headers = fetcher.conditional_get_headers
+ headers = fetcher.headers
headers["if-none-match"].should == etag
headers["if-modified-since"].should == mtime
end
+ context "and custom headers are provided" do
+ before do
+ new_resource.headers("x-myapp-header" => "app-specific-header",
+ "if-none-match" => "custom-etag",
+ "if-modified-since" => "custom-last-modified")
+ end
+
+ it "preserves non-conflicting headers" do
+ fetcher.headers["x-myapp-header"].should == "app-specific-header"
+ end
+
+ it "prefers user-supplied cache control headers" do
+ headers = fetcher.headers
+ headers["if-none-match"].should == "custom-etag"
+ headers["if-modified-since"].should == "custom-last-modified"
+ end
+ end
+
end
context "and etag support is enabled" do
@@ -150,7 +181,7 @@ describe Chef::Provider::RemoteFile::HTTP do
end
it "only adds If-None-Match headers to the request" do
- headers = fetcher.conditional_get_headers
+ headers = fetcher.headers
headers["if-none-match"].should == etag
headers.should_not have_key("if-modified-since")
end
@@ -162,7 +193,7 @@ describe Chef::Provider::RemoteFile::HTTP do
end
it "only adds If-Modified-Since headers to the request" do
- headers = fetcher.conditional_get_headers
+ headers = fetcher.headers
headers["if-modified-since"].should == mtime
headers.should_not have_key("if-none-match")
end
@@ -179,85 +210,11 @@ describe Chef::Provider::RemoteFile::HTTP do
fetcher.uri.should == uri
end
- it "stores any headers it is passed" do
- headers = { "foo" => "foo", "bar" => "bar", "baz" => "baz" }
- new_resource.headers(headers)
- fetcher = Chef::Provider::RemoteFile::HTTP.new(uri, new_resource, current_resource)
- fetcher.headers.should == headers
- end
-
- end
-
- context "when the existing file was fetched from a different URI" do
- let(:existing_file_source) { ["http://opscode.com/tukwila.txt"] }
-
- it "does not set a last modified header" do
- use_last_modified!
- fetcher = Chef::Provider::RemoteFile::HTTP.new(uri, new_resource, current_resource)
- fetcher.headers.should_not have_key('if-none-match')
- fetcher.headers.should_not have_key('if-modified-since')
- end
-
- it "does not set an etag header" do
- use_etags!
- fetcher = Chef::Provider::RemoteFile::HTTP.new(uri, new_resource, current_resource)
- fetcher.headers.should_not have_key('if-none-match')
- fetcher.headers.should_not have_key('if-modified-since')
- end
- end
-
- context "when the current file was fetched from the current URI" do
- let(:existing_file_source) { ["http://opscode.com/seattle.txt"] }
-
- context "and using If-Modified-Since" do
- before do
- use_last_modified!
- end
-
- it "stores the last_modified string in the headers" do
- fetcher = Chef::Provider::RemoteFile::HTTP.new(uri, new_resource, current_resource)
- fetcher.headers['if-modified-since'].should == current_resource.last_modified.strftime("%a, %d %b %Y %H:%M:%S %Z")
- fetcher.headers.should_not have_key('if-none-match')
- pending("http://www.w3.org/Protocols/rfc2616/rfc2616-sec13.html#sec13.3.4")
- end
- end
-
- context "and using etags" do
- before do
- use_etags!
- end
-
- it "stores the etag string in the headers" do
- fetcher = Chef::Provider::RemoteFile::HTTP.new(uri, new_resource, current_resource)
- fetcher.headers['if-none-match'].should == "\"#{current_resource.etag}\""
- fetcher.headers.should_not have_key('if-modified-since')
- pending("http://www.w3.org/Protocols/rfc2616/rfc2616-sec13.html#sec13.3.4")
- end
- end
-
- end
-
- describe "when use_last_modified is disabled in the new_resource" do
-
- it "stores nil for the last_modified date" do
- current_resource.stub!(:source).and_return(["http://opscode.com/seattle.txt"])
- new_resource.should_receive(:use_last_modified).and_return(false)
- current_resource.stub!(:last_modified).and_return(Time.new)
- current_resource.stub!(:etag).and_return(nil)
- Chef::Provider::RemoteFile::Util.should_receive(:uri_matches_string?).with(uri, current_resource.source[0]).and_return(true)
- fetcher = Chef::Provider::RemoteFile::HTTP.new(uri, new_resource, current_resource)
- fetcher.headers.should_not have_key('if-modified-since')
- fetcher.headers.should_not have_key('if-none-match')
- end
end
end
describe "when fetching the uri" do
- let(:fetcher) do
- Chef::Provider::RemoteFile::Util.should_receive(:uri_matches_string?).with(uri, current_resource.source[0]).and_return(true)
- Chef::Provider::RemoteFile::HTTP.new(uri, new_resource, current_resource)
- end
let(:expected_http_opts) { {} }
let(:expected_http_args) { [uri, nil, nil, expected_http_opts] }
@@ -274,8 +231,9 @@ describe Chef::Provider::RemoteFile::HTTP do
end
before do
- new_resource.should_receive(:headers).and_return({})
- new_resource.should_receive(:use_last_modified).and_return(false)
+ new_resource.headers({})
+ new_resource.use_last_modified(false)
+ Chef::Provider::RemoteFile::CacheControlData.should_receive(:load_and_validate).with(uri, current_resource_checksum).and_return(cache_control_data)
Chef::REST.should_receive(:new).with(*expected_http_args).and_return(rest)
end