Skip to content

Commit

Permalink
Update existing tests to function with RestClient::Request
Browse files Browse the repository at this point in the history
  • Loading branch information
jcam authored and btm committed Feb 26, 2013
1 parent 3dff33d commit 0a0c8c4
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 22 deletions.
36 changes: 17 additions & 19 deletions spec/unit/provider/remote_file_spec.rb
Expand Up @@ -46,11 +46,9 @@
describe "when fetching the file from the remote" do
before(:each) do
@tempfile = Tempfile.new("chef-rspec-remote_file_spec-line#{__LINE__}--")
@rawresp = RestClient::RawResponse.new(@tempfile, nil, nil)

@rest = mock(Chef::REST, { })
Chef::REST.stub!(:new).and_return(@rest)
@rest.stub!(:streaming_request).and_return(@tempfile)
@rest.stub!(:create_url) { |url| url }
RestClient::Request.stub!(:execute).and_return(@rawresp)
@resource.cookbook_name = "monkey"

@provider.stub!(:checksum).and_return("0fd012fdc96e96f8f7cf2046522a54aed0ce470224513e45da6bc1a17a4924aa")
Expand All @@ -66,7 +64,7 @@
end

before do
@resource.source("http://opscode.com/seattle.txt")
@resource.source("http://localhost:9000/seattle.txt")
end

describe "and the target location's enclosing directory does not exist" do
Expand All @@ -81,19 +79,19 @@

shared_examples_for "source specified with multiple URIs" do
it "should try to download the next URI when the first one fails" do
@rest.should_receive(:streaming_request).with("http://foo", {}).once.and_raise(SocketError)
@rest.should_receive(:streaming_request).with("http://bar", {}).once.and_return(@tempfile)
RestClient::Request.should_receive(:execute).with(:method => :get, :url => "http://foo", :raw_response => true).once.and_raise(SocketError)
RestClient::Request.should_receive(:execute).with(:method => :get, :url => "http://bar", :raw_response => true).once.and_return(@rawresp)
@provider.run_action(:create)
end

it "should raise an exception when all the URIs fail" do
@rest.should_receive(:streaming_request).with("http://foo", {}).once.and_raise(SocketError)
@rest.should_receive(:streaming_request).with("http://bar", {}).once.and_raise(SocketError)
RestClient::Request.should_receive(:execute).with(:method => :get, :url => "http://foo", :raw_response => true).once.and_raise(SocketError)
RestClient::Request.should_receive(:execute).with(:method => :get, :url => "http://bar", :raw_response => true).once.and_raise(SocketError)
lambda { @provider.run_action(:create) }.should raise_error(SocketError)
end

it "should download from only one URI when the first one works" do
@rest.should_receive(:streaming_request).once.and_return(@tempfile)
RestClient::Request.should_receive(:execute).once.and_return(@rawresp)
@provider.run_action(:create)
end

Expand Down Expand Up @@ -123,7 +121,7 @@
end

it "does not download the file" do
@rest.should_not_receive(:fetch).with("http://opscode.com/seattle.txt").and_return(@tempfile)
RestClient::Request.should_not_receive(:execute).with("http://localhost:9000/seattle.txt").and_return(@tempfile)
@provider.run_action(:create)
end

Expand All @@ -140,7 +138,7 @@
end

it "should not download the file if the checksum is a partial match from the beginning" do
@rest.should_not_receive(:fetch).with("http://opscode.com/seattle.txt").and_return(@tempfile)
@rawresp.should_not_receive(:fetch).with("http://localhost:9000/seattle.txt").and_return(@tempfile)
@provider.run_action(:create)
end

Expand All @@ -154,15 +152,15 @@
describe "and the existing file doesn't match the given checksum" do
it "downloads the file" do
@resource.checksum("this hash doesn't match")
@rest.should_receive(:streaming_request).with("http://opscode.com/seattle.txt", {}).and_return(@tempfile)
RestClient::Request.should_receive(:execute).with(:method => :get, :url => "http://localhost:9000/seattle.txt", :raw_response => true).and_return(@rawresp)
@provider.stub!(:update_new_file_state)
@provider.run_action(:create)
end

it "does not consider the checksum a match if the matching string is offset" do
# i.e., the existing file is "0fd012fdc96e96f8f7cf2046522a54aed0ce470224513e45da6bc1a17a4924aa"
@resource.checksum("fd012fd")
@rest.should_receive(:streaming_request).with("http://opscode.com/seattle.txt", {}).and_return(@tempfile)
RestClient::Request.should_receive(:execute).with(:method => :get, :url => "http://localhost:9000/seattle.txt", :raw_response => true).and_return(@rawresp)
@provider.stub!(:update_new_file_state)
@provider.run_action(:create)
end
Expand All @@ -173,7 +171,7 @@
describe "and the resource doesn't specify a checksum" do
it "should download the file from the remote URL" do
@resource.checksum(nil)
@rest.should_receive(:streaming_request).with("http://opscode.com/seattle.txt", {}).and_return(@tempfile)
RestClient::Request.should_receive(:execute).with(:method => :get, :url => "http://localhost:9000/seattle.txt", :raw_response => true).and_return(@rawresp)
@provider.run_action(:create)
end
end
Expand All @@ -190,7 +188,7 @@
context "and the target file is a tarball" do
before do
@resource.path(File.expand_path(File.join(CHEF_SPEC_DATA, "seattle.tar.gz")))
Chef::REST.should_receive(:new).with("http://opscode.com/seattle.txt", nil, nil, :disable_gzip => true).and_return(@rest)
RestClient::Request.should_receive(:execute).with(:method => :get, :url => "http://localhost:9000/seattle.txt", :raw_response => true).and_return(@rawresp)
end

it "disables gzip in the http client" do
Expand All @@ -202,7 +200,7 @@
context "and the source appears to be a tarball" do
before do
@resource.source("http://example.com/tarball.tgz")
Chef::REST.should_receive(:new).with("http://example.com/tarball.tgz", nil, nil, :disable_gzip => true).and_return(@rest)
RestClient::Request.should_receive(:execute).with(:method => :get, :url => "http://example.com/tarball.tgz", :raw_response => true).and_return(@rawresp)
end

it "disables gzip in the http client" do
Expand All @@ -213,14 +211,14 @@
it "should raise an exception if it's any other kind of retriable response than 304" do
r = Net::HTTPMovedPermanently.new("one", "two", "three")
e = Net::HTTPRetriableError.new("301", r)
@rest.stub!(:streaming_request).and_raise(e)
RestClient::Request.stub!(:execute).and_raise(e)
lambda { @provider.run_action(:create) }.should raise_error(Net::HTTPRetriableError)
end

it "should raise an exception if anything else happens" do
r = Net::HTTPBadRequest.new("one", "two", "three")
e = Net::HTTPServerException.new("fake exception", r)
@rest.stub!(:streaming_request).and_raise(e)
RestClient::Request.stub!(:execute).and_raise(e)
lambda { @provider.run_action(:create) }.should raise_error(Net::HTTPServerException)
end

Expand Down
16 changes: 13 additions & 3 deletions spec/unit/resource/remote_file_spec.rb
Expand Up @@ -64,7 +64,7 @@
lambda { @resource.source("not-a-uri") }.should raise_error(Chef::Exceptions::InvalidRemoteFileURI)
end

it "should raise and exception when source is an empty array" do
it "should raise an exception when source is an empty array" do
lambda { @resource.source([]) }.should raise_error(ArgumentError)
end

Expand All @@ -80,7 +80,18 @@
@resource.checksum.should == nil
end
end


describe "ftp_active_mode" do
it "should accept a boolean for the ftp_active_mode object" do
@resource.ftp_active_mode true
@resource.ftp_active_mode.should be_true
end

it "should default to false" do
@resource.ftp_active_mode.should be_false
end
end

describe "when it has group, mode, owner, source, and checksum" do
before do
if Chef::Platform.windows?
Expand Down Expand Up @@ -119,5 +130,4 @@
end
end
end

end

0 comments on commit 0a0c8c4

Please sign in to comment.