diff options
author | danielsdeleo <dan@opscode.com> | 2013-05-23 12:41:06 -0700 |
---|---|---|
committer | danielsdeleo <dan@opscode.com> | 2013-05-24 08:48:34 -0700 |
commit | ef5424f5d0024330fbcb68afa27cc162d5d1ec50 (patch) | |
tree | 29dc41227eaa8f0ab500eb6a4c4f0e5793833607 /spec/unit/provider/remote_file | |
parent | 5ac5a858ade3b92bf41994dddb8cd84b52f7245a (diff) | |
download | chef-ef5424f5d0024330fbcb68afa27cc162d5d1ec50.tar.gz |
refactor remote file ftp specs to use let blocks
Diffstat (limited to 'spec/unit/provider/remote_file')
-rw-r--r-- | spec/unit/provider/remote_file/ftp_spec.rb | 161 |
1 files changed, 94 insertions, 67 deletions
diff --git a/spec/unit/provider/remote_file/ftp_spec.rb b/spec/unit/provider/remote_file/ftp_spec.rb index f9e3331b6f..76c9968b48 100644 --- a/spec/unit/provider/remote_file/ftp_spec.rb +++ b/spec/unit/provider/remote_file/ftp_spec.rb @@ -26,129 +26,156 @@ describe Chef::Provider::RemoteFile::FTP do canonicalize_path(File.expand_path(File.join(enclosing_directory, "seattle.txt"))) } + let(:new_resource) { mock('Chef::Resource::RemoteFile (new resource)', :ftp_active_mode => false, :path => resource_path, :name => "seattle.txt", :binmode => true) } + let(:current_resource) { mock('Chef::Resource::RemoteFile (current resource)', :source => nil) } + + let(:ftp) do + ftp = mock(Net::FTP, { }) + ftp.stub!(:connect) + ftp.stub!(:login) + ftp.stub!(:voidcmd) + ftp.stub!(:mtime).and_return(Time.now) + ftp.stub!(:getbinaryfile) + ftp.stub!(:close) + ftp.stub!(:passive=) + ftp + end + + let(:tempfile_path) { "/tmp/somedir/remote-file-ftp-backend-spec-test" } + + let(:tempfile) do + t = StringIO.new + t.stub(:path).and_return(tempfile_path) + t + end + + let(:uri) { URI.parse("ftp://opscode.com/seattle.txt") } + before(:each) do - @ftp = mock(Net::FTP, { }) - Net::FTP.stub!(:new).and_return(@ftp) - @ftp.stub!(:connect) - @ftp.stub!(:login) - @ftp.stub!(:voidcmd) - @ftp.stub!(:mtime).and_return(Time.now) - @ftp.stub!(:getbinaryfile) - @ftp.stub!(:close) - @ftp.stub!(:passive=) - @tempfile = Tempfile.new("chef-rspec-ftp_spec-line#{__LINE__}--") - Tempfile.stub!(:new).and_return(@tempfile) - @uri = URI.parse("ftp://opscode.com/seattle.txt") + Net::FTP.stub!(:new).and_return(ftp) + Tempfile.stub!(:new).and_return(tempfile) + end describe "when constructing the object" do - before do - @new_resource = mock('Chef::Resource::RemoteFile (new resource)', :ftp_active_mode => false, :path => resource_path, :name => "seattle.txt", :binmode => true) - @current_resource = mock('Chef::Resource::RemoteFile (current resource)', :source => nil) - end it "throws an argument exception when no path is given" do - @uri.path = "" - lambda { Chef::Provider::RemoteFile::FTP.new(@uri, @new_resource, @current_resource) }.should raise_error(ArgumentError) + uri.path = "" + lambda { Chef::Provider::RemoteFile::FTP.new(uri, new_resource, current_resource) }.should raise_error(ArgumentError) end it "throws an argument exception when only a / is given" do - @uri.path = "/" - lambda { Chef::Provider::RemoteFile::FTP.new(@uri, @new_resource, @current_resource) }.should raise_error(ArgumentError) + uri.path = "/" + lambda { Chef::Provider::RemoteFile::FTP.new(uri, new_resource, current_resource) }.should raise_error(ArgumentError) end it "throws an argument exception when no filename is given" do - @uri.path = "/the/whole/path/" - lambda { Chef::Provider::RemoteFile::FTP.new(@uri, @new_resource, @current_resource) }.should raise_error(ArgumentError) + uri.path = "/the/whole/path/" + lambda { Chef::Provider::RemoteFile::FTP.new(uri, new_resource, current_resource) }.should raise_error(ArgumentError) end it "throws an argument exception when the typecode is invalid" do - @uri.typecode = "d" - lambda { Chef::Provider::RemoteFile::FTP.new(@uri, @new_resource, @current_resource) }.should raise_error(ArgumentError) + uri.typecode = "d" + lambda { Chef::Provider::RemoteFile::FTP.new(uri, new_resource, current_resource) }.should raise_error(ArgumentError) end it "sets ftp_active_mode to true when new_resource sets ftp_active_mode" do - @new_resource.stub!(:ftp_active_mode).and_return(true) - fetcher = Chef::Provider::RemoteFile::FTP.new(@uri, @new_resource, @current_resource) + new_resource.stub!(:ftp_active_mode).and_return(true) + fetcher = Chef::Provider::RemoteFile::FTP.new(uri, new_resource, current_resource) fetcher.ftp_active_mode.should == true end it "sets ftp_active_mode to false when new_resource does not set ftp_active_mode" do - @new_resource.stub!(:ftp_active_mode).and_return(false) - fetcher = Chef::Provider::RemoteFile::FTP.new(@uri, @new_resource, @current_resource) + new_resource.stub!(:ftp_active_mode).and_return(false) + fetcher = Chef::Provider::RemoteFile::FTP.new(uri, new_resource, current_resource) fetcher.ftp_active_mode.should == false end end describe "when fetching the object" do - before do - @new_resource = mock('Chef::Resource::RemoteFile (new resource)', :ftp_active_mode => false, :path => resource_path, :name => "seattle.txt", :binmode => true) - @current_resource = mock('Chef::Resource::RemoteFile (current resource)', :source => nil) - end - let(:fetcher) { Chef::Provider::RemoteFile::FTP.new(@uri, @new_resource, @current_resource) } + let(:fetcher) { Chef::Provider::RemoteFile::FTP.new(uri, new_resource, current_resource) } it "should connect to the host from the uri on the default port 21" do - @ftp.should_receive(:connect).with("opscode.com", 21) - fetcher.fetch - end - - it "should connect on an alternate port when one is provided" do - @uri = URI.parse("ftp://opscode.com:8021/seattle.txt") - @ftp.should_receive(:connect).with("opscode.com", 8021) + ftp.should_receive(:connect).with("opscode.com", 21) fetcher.fetch end it "should set passive true when ftp_active_mode is false" do - @new_resource.should_receive(:ftp_active_mode).and_return(false) - @ftp.should_receive(:passive=).with(true) + new_resource.should_receive(:ftp_active_mode).and_return(false) + ftp.should_receive(:passive=).with(true) fetcher.fetch end it "should set passive false when ftp_active_mode is false" do - @new_resource.should_receive(:ftp_active_mode).and_return(true) - @ftp.should_receive(:passive=).with(false) + new_resource.should_receive(:ftp_active_mode).and_return(true) + ftp.should_receive(:passive=).with(false) fetcher.fetch end it "should use anonymous ftp when no userinfo is provided" do - @ftp.should_receive(:login).with("anonymous", nil) + ftp.should_receive(:login).with("anonymous", nil) fetcher.fetch end - it "should use authenticated ftp when userinfo is provided" do - @uri = URI.parse("ftp://the_user:the_password@opscode.com/seattle.txt") - @ftp.should_receive(:login).with("the_user", "the_password") - fetcher.fetch + context "and the URI specifies an alternate port" do + let(:uri) { URI.parse("ftp://opscode.com:8021/seattle.txt") } + + it "should connect on an alternate port when one is provided" do + uri = URI.parse("ftp://opscode.com:8021/seattle.txt") + ftp.should_receive(:connect).with("opscode.com", 8021) + fetcher.fetch + end + end - it "should accept ascii for the typecode" do - @uri.typecode = "a" - @ftp.should_receive(:voidcmd).with("TYPE A").once - fetcher.fetch + context "and the URI contains a username and password" do + let(:uri) { URI.parse("ftp://the_user:the_password@opscode.com/seattle.txt") } + + it "should use authenticated ftp when userinfo is provided" do + ftp.should_receive(:login).with("the_user", "the_password") + fetcher.fetch + end end - it "should accept image for the typecode" do - @uri.typecode = "i" - @ftp.should_receive(:voidcmd).with("TYPE I").once - fetcher.fetch + context "and the uri sets the typecode to ascii" do + let(:uri) { URI.parse("ftp://the_user:the_password@opscode.com/seattle.txt;type=a") } + + it "fetches the file with ascii typecode set" do + ftp.should_receive(:voidcmd).with("TYPE A").once + fetcher.fetch + end + end - it "should fetch the file from the correct path" do - @uri = URI.parse("ftp://opscode.com/the/whole/path/seattle.txt") - @ftp.should_receive(:voidcmd).with("CWD the").once - @ftp.should_receive(:voidcmd).with("CWD whole").once - @ftp.should_receive(:voidcmd).with("CWD path").once - @ftp.should_receive(:getbinaryfile).with("seattle.txt", @tempfile.path) - fetcher.fetch + context "and the uri sets the typecode to image" do + let(:uri) { URI.parse("ftp://the_user:the_password@opscode.com/seattle.txt;type=i") } + + it "should accept image for the typecode" do + ftp.should_receive(:voidcmd).with("TYPE I").once + fetcher.fetch + end + end + context "and the uri specifies a nested path" do + let(:uri) { URI.parse("ftp://opscode.com/the/whole/path/seattle.txt") } + + it "should fetch the file from the correct path" do + ftp.should_receive(:voidcmd).with("CWD the").once + ftp.should_receive(:voidcmd).with("CWD whole").once + ftp.should_receive(:voidcmd).with("CWD path").once + ftp.should_receive(:getbinaryfile).with("seattle.txt", tempfile.path) + fetcher.fetch + end + + end + + it "should return a tempfile in the result" do result = fetcher.fetch - result.raw_file.should equal(@tempfile) + result.raw_file.should equal(tempfile) end - it "should return the mtime in the result" - end end |