summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLamont Granquist <lamont@opscode.com>2013-05-07 14:53:43 -0700
committerLamont Granquist <lamont@opscode.com>2013-05-07 14:53:43 -0700
commit564ac7a3761fd870eb867b985b01273510115cf2 (patch)
tree30fb77079730446e6097ed95c916136d456b4d4a
parentc9b0a2147beb8d0cd2814d5709e44e43488b7dde (diff)
downloadchef-564ac7a3761fd870eb867b985b01273510115cf2.tar.gz
working on tests (half-done, busted)
-rw-r--r--spec/unit/provider/remote_file/http_spec.rb84
1 files changed, 72 insertions, 12 deletions
diff --git a/spec/unit/provider/remote_file/http_spec.rb b/spec/unit/provider/remote_file/http_spec.rb
index 360a781cb1..7bcbd3ce12 100644
--- a/spec/unit/provider/remote_file/http_spec.rb
+++ b/spec/unit/provider/remote_file/http_spec.rb
@@ -20,9 +20,7 @@ require 'spec_helper'
describe Chef::Provider::RemoteFile::HTTP do
- before(:each) do
- @uri = URI.parse("http://opscode.com/seattle.txt")
- end
+ let(:uri) { URI.parse("http://opscode.com/seattle.txt") }
describe "when contructing the object" do
before do
@@ -37,14 +35,14 @@ describe Chef::Provider::RemoteFile::HTTP do
end
it "stores the uri it is passed" do
- fetcher = Chef::Provider::RemoteFile::HTTP.new(@uri, @new_resource, @current_resource)
- fetcher.uri.should == @uri
+ fetcher = Chef::Provider::RemoteFile::HTTP.new(uri, @new_resource, @current_resource)
+ fetcher.uri.should == uri
end
it "stores any headers it is passed" do
headers = { "foo" => "foo", "bar" => "bar", "baz" => "baz" }
@new_resource.stub!(:headers).and_return(headers)
- fetcher = Chef::Provider::RemoteFile::HTTP.new(@uri, @new_resource, @current_resource)
+ fetcher = Chef::Provider::RemoteFile::HTTP.new(uri, @new_resource, @current_resource)
fetcher.headers.should == headers
end
@@ -57,8 +55,8 @@ describe Chef::Provider::RemoteFile::HTTP do
@new_resource.should_receive(:use_last_modified).and_return(true)
@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)
+ 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['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')
end
@@ -69,8 +67,8 @@ describe Chef::Provider::RemoteFile::HTTP do
@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("a_unique_identifier")
- 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)
+ 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['if-none-match'].should == "\"#{@current_resource.etag}\""
fetcher.headers.should_not have_key('if-modified-since')
end
@@ -84,8 +82,8 @@ describe Chef::Provider::RemoteFile::HTTP do
@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)
+ 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
@@ -94,6 +92,68 @@ describe Chef::Provider::RemoteFile::HTTP do
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
+
+ before do
+ @new_resource = mock('Chef::Resource::RemoteFile (new_resource)')
+ @current_resource = mock('Chef::Resource::RemoteFile (current_resource)')
+ @new_resource.should_receive(:headers).and_return({})
+ @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)
+ @rest = mock(Chef::REST)
+ Chef::REST.should_receive(:new).and_return(@rest)
+ @tempfile = mock(Tempfile)
+ @rest.stub!(:streaming_request).and_return(@tempfile)
+ @rest.stub!(:last_response).and_return({})
+ @result = mock(Chef::Provider::RemoteFile::Result)
+ Chef::Provider::RemoteFile::Result.stub!(:new).and_return(@result)
+ end
+
+ it "should return a result" do
+ Chef::REST.should_receive(:new).and_return(@rest)
+ Chef::Provider::RemoteFile::Result.stub!(:new).and_return(@result)
+ fetcher.fetch.should == @result
+ end
+
+ it "should propagate non-304 exceptions to the caller" do
+ Chef::REST.should_receive(:new).and_return(@rest)
+ r = Net::HTTPBadRequest.new("one", "two", "three")
+ e = Net::HTTPServerException.new("fake exception", r)
+ Chef::Provider::RemoteFile::Result.stub!(:new).and_return(@result)
+ @rest.stub!(:streaming_request).and_raise(e)
+ lambda { fetcher.fetch }.should raise_error(Net::HTTPServerException)
+ end
+
+ it "should return HTTPRetriableError when Chef::REST returns a 301" do
+ Chef::REST.should_receive(:new).and_return(@rest)
+ r = Net::HTTPMovedPermanently.new("one", "two", "three")
+ e = Net::HTTPRetriableError.new("301", r)
+ Chef::Provider::RemoteFile::Result.stub!(:new).and_return(@result)
+ @rest.stub!(:streaming_request).and_raise(e)
+ lambda { fetcher.fetch }.should raise_error(Net::HTTPRetriableError)
+ end
+
+ it "should return a nil tempfile for a 304 HTTPNotModifed" do
+ Chef::REST.should_receive(:new).and_return(@rest)
+ r = Net::HTTPNotModified.new("one", "two", "three")
+ e = Net::HTTPRetriableError.new("304", r)
+ @rest.stub!(:streaming_request).and_raise(e)
+ Chef::Provider::RemoteFile::Result.should_receive(:new).with(nil, nil, nil).and_return(@result)
+ fetcher.fetch.should == @result
+ end
+
+ it "should disable gzip compression in the client for *gz files" do
+ uri = URI.parse("http://opscode.com/tarball.tgz")
+ Chef::REST.should_not_receive(:new)
+ #Chef::REST.should_receive(:new).with(uri, nil, nil, {:disable_gzip => true }).and_return(@rest)
+ Chef::Provider::RemoteFile::Result.stub!(:new).and_return(@result)
+ fetcher.fetch.should == @result
+ end
end
end