summaryrefslogtreecommitdiff
path: root/spec
diff options
context:
space:
mode:
authorJohn Dyer <jdyer@tropo.com>2014-04-21 10:21:20 -0400
committerJohn Dyer <jdyer@tropo.com>2014-04-21 10:21:20 -0400
commit4bdd9dbf9eff89742ba2c203ad228014b1cda41f (patch)
treefae69e61967e19153c5e4298cf1c95dfb7d1c468 /spec
parentc90fb30fead166e23f45677e9efad5f6dfd7939f (diff)
downloadchef-4bdd9dbf9eff89742ba2c203ad228014b1cda41f.tar.gz
CHEF-5116 - Fix two tests for remote_file net/http exceptions
Diffstat (limited to 'spec')
-rw-r--r--spec/unit/provider/remote_file/content_spec.rb94
1 files changed, 58 insertions, 36 deletions
diff --git a/spec/unit/provider/remote_file/content_spec.rb b/spec/unit/provider/remote_file/content_spec.rb
index 0f311dc0ec..7adf8f187a 100644
--- a/spec/unit/provider/remote_file/content_spec.rb
+++ b/spec/unit/provider/remote_file/content_spec.rb
@@ -159,45 +159,68 @@ describe Chef::Provider::RemoteFile::Content do
end
describe "when there is an array of sources and the first fails" do
-
- let(:source) { [ "http://opscode.com/seattle.txt", "http://opscode.com/nyc.txt" ] }
- before do
- new_resource.stub(:checksum).and_return(nil)
- current_resource.stub(:checksum).and_return(nil)
- @uri0 = double("URI0")
- @uri1 = double("URI1")
- URI.should_receive(:parse).with(new_resource.source[0]).and_return(@uri0)
- URI.should_receive(:parse).with(new_resource.source[1]).and_return(@uri1)
- @http_fetcher_throws_exception = double("Chef::Provider::RemoteFile::HTTP")
- @http_fetcher_throws_exception.should_receive(:fetch).at_least(:once).and_raise(Errno::ECONNREFUSED)
- Chef::Provider::RemoteFile::Fetcher.should_receive(:for_resource).with(@uri0, new_resource, current_resource).and_return(@http_fetcher_throws_exception)
- end
-
- describe "when the second url succeeds" do
- before do
- @tempfile = double("Tempfile")
- mtime = Time.now
- http_fetcher_works = double("Chef::Provider::RemoteFile::HTTP", :fetch => @tempfile)
- Chef::Provider::RemoteFile::Fetcher.should_receive(:for_resource).with(@uri1, new_resource, current_resource).and_return(http_fetcher_works)
- end
-
- it "should return a valid tempfile" do
- content.tempfile.should == @tempfile
- end
-
- it "should not mutate the new_resource" do
- content.tempfile
- new_resource.source.length.should == 2
+ # https://github.com/opscode/chef/pull/1358#issuecomment-40853299
+ def create_exception(exception_class)
+ if [ Net::HTTPServerException, Net::HTTPFatalError ].include? exception_class
+ exception_class.new("message", {"something" => 1})
+ else
+ exception_class.new
end
end
- describe "when both urls fail" do
- before do
- Chef::Provider::RemoteFile::Fetcher.should_receive(:for_resource).with(@uri1, new_resource, current_resource).and_return(@http_fetcher_throws_exception)
- end
+ let(:source) { [ "http://opscode.com/seattle.txt", "http://opscode.com/nyc.txt" ] }
- it "should propagate the error back to the caller" do
- lambda { content.tempfile }.should raise_error(Errno::ECONNREFUSED)
+ ### Test each exception we care about and make sure they all behave properly
+ [
+ SocketError,
+ Errno::ECONNREFUSED,
+ Errno::ENOENT,
+ Errno::EACCES,
+ Timeout::Error,
+ Net::HTTPServerException,
+ Net::HTTPFatalError,
+ Net::FTPError
+ ].each do |exception|
+ describe "with an exception of #{exception}" do
+ before do
+ new_resource.stub(:checksum).and_return(nil)
+ current_resource.stub(:checksum).and_return(nil)
+ @uri0 = double("URI0")
+ @uri1 = double("URI1")
+ URI.should_receive(:parse).with(new_resource.source[0]).and_return(@uri0)
+ URI.should_receive(:parse).with(new_resource.source[1]).and_return(@uri1)
+ @http_fetcher_throws_exception = double("Chef::Provider::RemoteFile::HTTP")
+ @http_fetcher_throws_exception.should_receive(:fetch).at_least(:once).and_raise(create_exception(exception))
+ Chef::Provider::RemoteFile::Fetcher.should_receive(:for_resource).with(@uri0, new_resource, current_resource).and_return(@http_fetcher_throws_exception)
+ end
+
+ describe "the second url should succeed" do
+ before do
+ @tempfile = double("Tempfile")
+ mtime = Time.now
+ http_fetcher_works = double("Chef::Provider::RemoteFile::HTTP", :fetch => @tempfile)
+ Chef::Provider::RemoteFile::Fetcher.should_receive(:for_resource).with(@uri1, new_resource, current_resource).and_return(http_fetcher_works)
+ end
+
+ it "should return a valid tempfile" do
+ content.tempfile.should == @tempfile
+ end
+
+ it "should not mutate the new_resource" do
+ content.tempfile
+ new_resource.source.length.should == 2
+ end
+ end
+
+ describe "when both urls fail" do
+ before do
+ Chef::Provider::RemoteFile::Fetcher.should_receive(:for_resource).with(@uri1, new_resource, current_resource).and_return(@http_fetcher_throws_exception)
+ end
+
+ it "should propagate the error back to the caller" do
+ lambda { content.tempfile }.should raise_error(exception)
+ end
+ end
end
end
end
@@ -227,4 +250,3 @@ describe Chef::Provider::RemoteFile::Content do
end
end
-