2007-03-01 12:52:28 -05:00
|
|
|
require "#{File.dirname(__FILE__)}/utils"
|
|
|
|
require 'capistrano/upload'
|
|
|
|
|
|
|
|
class UploadTest < Test::Unit::TestCase
|
|
|
|
def setup
|
|
|
|
@mode = IO::WRONLY | IO::CREAT | IO::TRUNC
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_initialize_should_raise_error_if_data_is_missing
|
|
|
|
assert_raises(ArgumentError) do
|
|
|
|
Capistrano::Upload.new([], "test.txt", :foo => "bar")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_initialize_should_get_sftp_for_each_session
|
|
|
|
new_sftp = Proc.new do |state|
|
|
|
|
sftp = mock("sftp", :state => state, :open => nil)
|
|
|
|
sftp.expects(:connect) unless state == :open
|
2007-06-07 23:30:40 -04:00
|
|
|
sftp.stubs(:channel).returns({})
|
2007-03-01 12:52:28 -05:00
|
|
|
sftp
|
|
|
|
end
|
|
|
|
|
2007-05-09 00:45:54 -04:00
|
|
|
sessions = [mock("session", :xserver => server("a"), :sftp => new_sftp[:closed]),
|
|
|
|
mock("session", :xserver => server("b"), :sftp => new_sftp[:closed]),
|
|
|
|
mock("session", :xserver => server("c"), :sftp => new_sftp[:open])]
|
2007-03-01 12:52:28 -05:00
|
|
|
Capistrano::Upload.new(sessions, "test.txt", :data => "data")
|
|
|
|
end
|
|
|
|
|
2007-03-02 12:15:11 -05:00
|
|
|
def test_self_process_should_instantiate_uploader_and_start_process
|
|
|
|
Capistrano::Upload.expects(:new).with([:s1, :s2], "test.txt", :data => "data").returns(mock(:process! => nil))
|
|
|
|
Capistrano::Upload.process([:s1, :s2], "test.txt", :data => "data")
|
|
|
|
end
|
|
|
|
|
2007-03-01 12:52:28 -05:00
|
|
|
def test_process_when_sftp_open_fails_should_raise_error
|
2007-06-07 23:30:40 -04:00
|
|
|
sftp = mock_sftp
|
2007-03-01 12:52:28 -05:00
|
|
|
sftp.expects(:open).with("test.txt", @mode, 0660).yields(mock("status", :code => "bad status", :message => "bad status"), :file_handle)
|
2007-05-09 00:45:54 -04:00
|
|
|
session = mock("session", :sftp => sftp, :xserver => server("capistrano"))
|
2007-03-01 12:52:28 -05:00
|
|
|
upload = Capistrano::Upload.new([session], "test.txt", :data => "data", :logger => stub_everything)
|
2007-03-02 16:33:27 -05:00
|
|
|
assert_raises(Capistrano::UploadError) { upload.process! }
|
2007-03-01 12:52:28 -05:00
|
|
|
assert_equal 1, upload.failed
|
|
|
|
assert_equal 1, upload.completed
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_process_when_sftp_write_fails_should_raise_error
|
2007-06-07 23:30:40 -04:00
|
|
|
sftp = mock_sftp
|
2007-03-01 12:52:28 -05:00
|
|
|
sftp.expects(:open).with("test.txt", @mode, 0660).yields(mock("status1", :code => Net::SFTP::Session::FX_OK), :file_handle)
|
|
|
|
sftp.expects(:write).with(:file_handle, "data").yields(mock("status2", :code => "bad status", :message => "bad status"))
|
2007-05-09 00:45:54 -04:00
|
|
|
session = mock("session", :sftp => sftp, :xserver => server("capistrano"))
|
2007-03-01 12:52:28 -05:00
|
|
|
upload = Capistrano::Upload.new([session], "test.txt", :data => "data", :logger => stub_everything)
|
2007-03-02 16:33:27 -05:00
|
|
|
assert_raises(Capistrano::UploadError) { upload.process! }
|
2007-03-01 12:52:28 -05:00
|
|
|
assert_equal 1, upload.failed
|
|
|
|
assert_equal 1, upload.completed
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_process_when_sftp_succeeds_should_raise_nothing
|
2007-06-07 23:30:40 -04:00
|
|
|
sftp = mock_sftp
|
2007-03-01 12:52:28 -05:00
|
|
|
sftp.expects(:open).with("test.txt", @mode, 0660).yields(mock("status1", :code => Net::SFTP::Session::FX_OK), :file_handle)
|
|
|
|
sftp.expects(:write).with(:file_handle, "data").yields(mock("status2", :code => Net::SFTP::Session::FX_OK))
|
|
|
|
sftp.expects(:close_handle).with(:file_handle).yields
|
2007-05-09 00:45:54 -04:00
|
|
|
session = mock("session", :sftp => sftp, :xserver => server("capistrano"))
|
2007-03-01 12:52:28 -05:00
|
|
|
upload = Capistrano::Upload.new([session], "test.txt", :data => "data", :logger => stub_everything)
|
|
|
|
assert_nothing_raised { upload.process! }
|
|
|
|
assert_equal 0, upload.failed
|
|
|
|
assert_equal 1, upload.completed
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_process_should_loop_while_running
|
|
|
|
con = mock("connection")
|
|
|
|
con.expects(:process).with(true).times(10)
|
2007-06-07 23:30:40 -04:00
|
|
|
channel = {}
|
2007-03-01 12:52:28 -05:00
|
|
|
channel.expects(:connection).returns(con).times(10)
|
|
|
|
sftp = mock("sftp", :state => :open, :open => nil)
|
2007-06-07 23:30:40 -04:00
|
|
|
sftp.stubs(:channel).returns(channel)
|
2007-05-09 00:45:54 -04:00
|
|
|
session = mock("session", :sftp => sftp, :xserver => server("capistrano"))
|
2007-03-01 12:52:28 -05:00
|
|
|
upload = Capistrano::Upload.new([session], "test.txt", :data => "data")
|
|
|
|
upload.expects(:running?).times(11).returns(*([true]*10 + [false]))
|
|
|
|
upload.process!
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_process_should_loop_but_not_process_done_channels
|
|
|
|
new_sftp = Proc.new do |done|
|
2007-06-07 23:30:40 -04:00
|
|
|
channel = {}
|
|
|
|
channel[:needs_done] = done
|
|
|
|
|
2007-03-01 12:52:28 -05:00
|
|
|
if !done
|
|
|
|
con = mock("connection")
|
|
|
|
con.expects(:process).with(true).times(10)
|
2007-06-07 23:30:40 -04:00
|
|
|
channel.expects(:connection).returns(con).times(10)
|
2007-03-01 12:52:28 -05:00
|
|
|
end
|
2007-06-07 23:30:40 -04:00
|
|
|
|
2007-03-01 12:52:28 -05:00
|
|
|
sftp = mock("sftp", :state => :open, :open => nil)
|
2007-06-07 23:30:40 -04:00
|
|
|
sftp.stubs(:channel).returns(channel)
|
2007-03-01 12:52:28 -05:00
|
|
|
sftp
|
|
|
|
end
|
|
|
|
|
2007-06-07 23:30:40 -04:00
|
|
|
sessions = [stub("session", :sftp => new_sftp[true], :xserver => server("capistrano")),
|
|
|
|
stub("session", :sftp => new_sftp[false], :xserver => server("cap2"))]
|
2007-03-01 12:52:28 -05:00
|
|
|
upload = Capistrano::Upload.new(sessions, "test.txt", :data => "data")
|
2007-06-07 23:30:40 -04:00
|
|
|
|
|
|
|
# make sure the sftp channels we wanted to be done, start as done
|
|
|
|
# (Upload.new marks each channel as not-done, so we have to do it here)
|
|
|
|
sessions.each { |s| s.sftp.channel[:done] = true if s.sftp.channel[:needs_done] }
|
2007-03-01 12:52:28 -05:00
|
|
|
upload.expects(:running?).times(11).returns(*([true]*10 + [false]))
|
|
|
|
upload.process!
|
|
|
|
end
|
2007-06-07 23:30:40 -04:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def mock_sftp
|
|
|
|
sftp = mock("sftp", :state => :open)
|
|
|
|
sftp.stubs(:channel).returns(Hash.new)
|
|
|
|
yield sftp if block_given?
|
|
|
|
sftp
|
|
|
|
end
|
2007-03-01 12:52:28 -05:00
|
|
|
end
|