2007-03-01 01:23:26 -05:00
|
|
|
require "#{File.dirname(__FILE__)}/utils"
|
2006-03-05 22:48:35 -05:00
|
|
|
require 'capistrano/command'
|
2005-08-06 15:35:25 -04:00
|
|
|
|
|
|
|
class CommandTest < Test::Unit::TestCase
|
2007-03-01 01:23:26 -05:00
|
|
|
def test_command_should_open_channels_on_all_sessions
|
|
|
|
s1 = mock(:open_channel => nil)
|
|
|
|
s2 = mock(:open_channel => nil)
|
|
|
|
s3 = mock(:open_channel => nil)
|
|
|
|
assert_equal "ls", Capistrano::Command.new("ls", [s1, s2, s3]).command
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_command_with_newlines_should_be_properly_escaped
|
|
|
|
cmd = Capistrano::Command.new("ls\necho", [mock(:open_channel => nil)])
|
|
|
|
assert_equal "ls\\\necho", cmd.command
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_command_with_windows_newlines_should_be_properly_escaped
|
|
|
|
cmd = Capistrano::Command.new("ls\r\necho", [mock(:open_channel => nil)])
|
|
|
|
assert_equal "ls\\\necho", cmd.command
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_command_with_env_key_should_have_environment_constructed_and_prepended
|
|
|
|
cmd = Capistrano::Command.new("ls", [mock(:open_channel => nil)], :env => { "FOO" => "bar" })
|
|
|
|
assert_equal "FOO=bar ls", cmd.command
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_env_with_symbolic_key_should_be_accepted_as_a_string
|
|
|
|
cmd = Capistrano::Command.new("ls", [mock(:open_channel => nil)], :env => { :FOO => "bar" })
|
|
|
|
assert_equal "FOO=bar ls", cmd.command
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_env_as_string_should_be_substituted_in_directly
|
|
|
|
cmd = Capistrano::Command.new("ls", [mock(:open_channel => nil)], :env => "HOWDY" )
|
|
|
|
assert_equal "HOWDY ls", cmd.command
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_env_with_symbolic_value_should_be_accepted_as_string
|
|
|
|
cmd = Capistrano::Command.new("ls", [mock(:open_channel => nil)], :env => { "FOO" => :bar })
|
|
|
|
assert_equal "FOO=bar ls", cmd.command
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_env_value_should_be_escaped
|
|
|
|
cmd = Capistrano::Command.new("ls", [mock(:open_channel => nil)], :env => { "FOO" => '( "bar" )' })
|
|
|
|
assert_equal "FOO=(\\ \\\"bar\\\"\\ ) ls", cmd.command
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_env_with_multiple_keys_should_chain_the_entries_together
|
|
|
|
cmd = Capistrano::Command.new("ls", [mock(:open_channel => nil)], :env => { :a => :b, :c => :d, :e => :f })
|
|
|
|
env = cmd.command[/^(.*) ls$/, 1]
|
|
|
|
assert_match(/\ba=b\b/, env)
|
|
|
|
assert_match(/\bc=d\b/, env)
|
|
|
|
assert_match(/\be=f\b/, env)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_open_channel_should_set_host_key_on_channel
|
2007-05-09 00:45:54 -04:00
|
|
|
session = mock(:xserver => server("capistrano"))
|
2007-03-01 01:23:26 -05:00
|
|
|
channel = stub_everything
|
|
|
|
|
|
|
|
session.expects(:open_channel).yields(channel)
|
|
|
|
channel.expects(:[]=).with(:host, "capistrano")
|
|
|
|
|
|
|
|
Capistrano::Command.new("ls", [session])
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_open_channel_should_set_options_key_on_channel
|
2007-05-09 00:45:54 -04:00
|
|
|
session = mock(:xserver => server("capistrano"))
|
2007-03-01 01:23:26 -05:00
|
|
|
channel = stub_everything
|
|
|
|
|
|
|
|
session.expects(:open_channel).yields(channel)
|
|
|
|
channel.expects(:[]=).with(:options, {:data => "here we go"})
|
|
|
|
|
|
|
|
Capistrano::Command.new("ls", [session], :data => "here we go")
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_open_channel_should_request_pty
|
2007-05-09 00:45:54 -04:00
|
|
|
session = mock(:xserver => server("capistrano"))
|
2007-03-01 01:23:26 -05:00
|
|
|
channel = stub_everything
|
|
|
|
|
|
|
|
session.expects(:open_channel).yields(channel)
|
|
|
|
channel.expects(:request_pty).with(:want_reply => true)
|
|
|
|
|
|
|
|
Capistrano::Command.new("ls", [session])
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_successful_channel_should_send_command
|
|
|
|
session = setup_for_extracting_channel_action(:on_success) do |ch|
|
2007-06-05 12:10:01 -04:00
|
|
|
ch.expects(:exec).with(%(sh -c "ls"))
|
2005-08-06 15:35:25 -04:00
|
|
|
end
|
2007-03-01 01:23:26 -05:00
|
|
|
Capistrano::Command.new("ls", [session])
|
2005-08-06 15:35:25 -04:00
|
|
|
end
|
|
|
|
|
2007-06-05 12:10:01 -04:00
|
|
|
def test_successful_channel_with_shell_option_should_send_command_via_specified_shell
|
|
|
|
session = setup_for_extracting_channel_action(:on_success) do |ch|
|
|
|
|
ch.expects(:exec).with(%(/bin/bash -c "ls"))
|
|
|
|
end
|
|
|
|
Capistrano::Command.new("ls", [session], :shell => "/bin/bash")
|
|
|
|
end
|
|
|
|
|
2007-03-01 01:23:26 -05:00
|
|
|
def test_successful_channel_should_send_data_if_data_key_is_present
|
|
|
|
session = setup_for_extracting_channel_action(:on_success) do |ch|
|
2007-06-05 12:10:01 -04:00
|
|
|
ch.expects(:exec).with(%(sh -c "ls"))
|
2007-03-01 01:23:26 -05:00
|
|
|
ch.expects(:send_data).with("here we go")
|
|
|
|
end
|
|
|
|
Capistrano::Command.new("ls", [session], :data => "here we go")
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_unsuccessful_channel_should_close_channel
|
|
|
|
session = setup_for_extracting_channel_action(:on_failure) do |ch|
|
|
|
|
ch.expects(:close)
|
|
|
|
end
|
|
|
|
Capistrano::Command.new("ls", [session])
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_on_data_should_invoke_callback_as_stdout
|
|
|
|
session = setup_for_extracting_channel_action(:on_data, "hello")
|
|
|
|
called = false
|
|
|
|
Capistrano::Command.new("ls", [session]) do |ch, stream, data|
|
|
|
|
called = true
|
|
|
|
assert_equal :out, stream
|
|
|
|
assert_equal "hello", data
|
|
|
|
end
|
|
|
|
assert called
|
|
|
|
end
|
2005-08-06 15:35:25 -04:00
|
|
|
|
2007-03-01 01:23:26 -05:00
|
|
|
def test_on_extended_data_should_invoke_callback_as_stderr
|
|
|
|
session = setup_for_extracting_channel_action(:on_extended_data, 2, "hello")
|
|
|
|
called = false
|
|
|
|
Capistrano::Command.new("ls", [session]) do |ch, stream, data|
|
|
|
|
called = true
|
|
|
|
assert_equal :err, stream
|
|
|
|
assert_equal "hello", data
|
2005-08-06 15:35:25 -04:00
|
|
|
end
|
2007-03-01 01:23:26 -05:00
|
|
|
assert called
|
2005-08-06 15:35:25 -04:00
|
|
|
end
|
|
|
|
|
2007-03-01 01:23:26 -05:00
|
|
|
def test_on_request_should_record_exit_status
|
|
|
|
data = mock(:read_long => 5)
|
|
|
|
session = setup_for_extracting_channel_action(:on_request, "exit-status", nil, data) do |ch|
|
|
|
|
ch.expects(:[]=).with(:status, 5)
|
|
|
|
end
|
|
|
|
Capistrano::Command.new("ls", [session])
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_on_close_should_set_channel_closed
|
|
|
|
session = setup_for_extracting_channel_action(:on_close) do |ch|
|
|
|
|
ch.expects(:[]=).with(:closed, true)
|
|
|
|
end
|
|
|
|
Capistrano::Command.new("ls", [session])
|
2005-08-06 15:35:25 -04:00
|
|
|
end
|
|
|
|
|
2007-03-01 01:23:26 -05:00
|
|
|
def test_stop_should_close_all_open_channels
|
|
|
|
sessions = [mock("session", :open_channel => new_channel(false)),
|
|
|
|
mock("session", :open_channel => new_channel(true)),
|
|
|
|
mock("session", :open_channel => new_channel(false))]
|
|
|
|
|
|
|
|
cmd = Capistrano::Command.new("ls", sessions)
|
|
|
|
cmd.stop!
|
2005-08-06 15:35:25 -04:00
|
|
|
end
|
|
|
|
|
2007-03-01 01:23:26 -05:00
|
|
|
def test_process_should_return_cleanly_if_all_channels_have_zero_exit_status
|
|
|
|
sessions = [mock("session", :open_channel => new_channel(true, 0)),
|
|
|
|
mock("session", :open_channel => new_channel(true, 0)),
|
|
|
|
mock("session", :open_channel => new_channel(true, 0))]
|
|
|
|
cmd = Capistrano::Command.new("ls", sessions)
|
|
|
|
assert_nothing_raised { cmd.process! }
|
2005-08-06 15:35:25 -04:00
|
|
|
end
|
|
|
|
|
2007-03-01 01:23:26 -05:00
|
|
|
def test_process_should_raise_error_if_any_channel_has_non_zero_exit_status
|
|
|
|
sessions = [mock("session", :open_channel => new_channel(true, 0)),
|
|
|
|
mock("session", :open_channel => new_channel(true, 0)),
|
|
|
|
mock("session", :open_channel => new_channel(true, 1))]
|
|
|
|
cmd = Capistrano::Command.new("ls", sessions)
|
2007-03-02 16:33:27 -05:00
|
|
|
assert_raises(Capistrano::CommandError) { cmd.process! }
|
2005-08-06 15:35:25 -04:00
|
|
|
end
|
2007-03-01 01:23:26 -05:00
|
|
|
|
2007-03-27 17:41:55 -04:00
|
|
|
def test_command_error_should_include_accessor_with_host_array
|
|
|
|
sessions = [mock("session", :open_channel => new_channel(true, 0)),
|
|
|
|
mock("session", :open_channel => new_channel(true, 0)),
|
|
|
|
mock("session", :open_channel => new_channel(true, 1))]
|
|
|
|
cmd = Capistrano::Command.new("ls", sessions)
|
|
|
|
|
|
|
|
begin
|
|
|
|
cmd.process!
|
|
|
|
flunk "expected an exception to be raised"
|
|
|
|
rescue Capistrano::CommandError => e
|
|
|
|
assert e.respond_to?(:hosts)
|
2007-05-09 00:45:54 -04:00
|
|
|
assert_equal %w(capistrano), e.hosts.map { |h| h.to_s }
|
2007-03-27 17:41:55 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2007-03-01 01:23:26 -05:00
|
|
|
def test_process_should_loop_until_all_channels_are_closed
|
|
|
|
new_channel = Proc.new do |times|
|
|
|
|
ch = mock("channel")
|
|
|
|
returns = [false] * (times-1)
|
|
|
|
ch.stubs(:[]).with(:closed).returns(lambda { returns.empty? ? true : returns.pop })
|
|
|
|
con = mock("connection")
|
|
|
|
con.expects(:process).with(true).times(times-1)
|
|
|
|
ch.expects(:connection).times(times-1).returns(con)
|
|
|
|
ch.expects(:[]).with(:status).returns(0)
|
|
|
|
ch
|
|
|
|
end
|
|
|
|
|
|
|
|
sessions = [mock("session", :open_channel => new_channel[5]),
|
|
|
|
mock("session", :open_channel => new_channel[10]),
|
|
|
|
mock("session", :open_channel => new_channel[7])]
|
|
|
|
cmd = Capistrano::Command.new("ls", sessions)
|
|
|
|
assert_nothing_raised { cmd.process! }
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_process_should_ping_all_connections_each_second
|
|
|
|
now = Time.now
|
|
|
|
|
|
|
|
new_channel = Proc.new do
|
|
|
|
ch = mock("channel")
|
|
|
|
ch.stubs(:[]).with(:closed).returns(lambda { Time.now - now < 1.1 ? false : true })
|
|
|
|
ch.stubs(:[]).with(:status).returns(0)
|
|
|
|
con = mock("connection")
|
|
|
|
con.stubs(:process)
|
|
|
|
con.expects(:ping!)
|
|
|
|
ch.stubs(:connection).returns(con)
|
|
|
|
ch
|
|
|
|
end
|
|
|
|
|
|
|
|
sessions = [mock("session", :open_channel => new_channel[]),
|
|
|
|
mock("session", :open_channel => new_channel[]),
|
|
|
|
mock("session", :open_channel => new_channel[])]
|
|
|
|
cmd = Capistrano::Command.new("ls", sessions)
|
|
|
|
assert_nothing_raised { cmd.process! }
|
|
|
|
end
|
|
|
|
|
2007-03-04 16:09:37 -05:00
|
|
|
def test_process_should_instantiate_command_and_process!
|
|
|
|
cmd = mock("command", :process! => nil)
|
|
|
|
Capistrano::Command.expects(:new).with("ls -l", %w(a b c), {:foo => "bar"}).yields(:command).returns(cmd)
|
|
|
|
parameter = nil
|
|
|
|
Capistrano::Command.process("ls -l", %w(a b c), :foo => "bar") { |cmd| parameter = cmd }
|
|
|
|
assert_equal :command, parameter
|
|
|
|
end
|
|
|
|
|
2007-03-14 10:22:35 -04:00
|
|
|
def test_process_with_host_placeholder_should_substitute_placeholder_with_each_host
|
|
|
|
session = setup_for_extracting_channel_action(:on_success) do |ch|
|
2007-06-05 12:10:01 -04:00
|
|
|
ch.expects(:exec).with(%(sh -c "echo capistrano"))
|
2007-03-14 10:22:35 -04:00
|
|
|
end
|
|
|
|
Capistrano::Command.new("echo $CAPISTRANO:HOST$", [session])
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_process_with_unknown_placeholder_should_not_replace_placeholder
|
|
|
|
session = setup_for_extracting_channel_action(:on_success) do |ch|
|
2007-06-05 12:10:01 -04:00
|
|
|
ch.expects(:exec).with(%(sh -c "echo $CAPISTRANO:OTHER$"))
|
2007-03-14 10:22:35 -04:00
|
|
|
end
|
|
|
|
Capistrano::Command.new("echo $CAPISTRANO:OTHER$", [session])
|
|
|
|
end
|
|
|
|
|
2007-03-01 01:23:26 -05:00
|
|
|
private
|
|
|
|
|
|
|
|
def new_channel(closed, status=nil)
|
|
|
|
ch = mock("channel")
|
|
|
|
ch.expects(:[]).with(:closed).returns(closed)
|
|
|
|
ch.expects(:[]).with(:status).returns(status) if status
|
|
|
|
ch.expects(:close) unless closed
|
|
|
|
ch.stubs(:[]).with(:host).returns("capistrano")
|
2007-05-09 00:45:54 -04:00
|
|
|
ch.stubs(:[]).with(:server).returns(server("capistrano"))
|
2007-03-01 01:23:26 -05:00
|
|
|
ch
|
|
|
|
end
|
|
|
|
|
|
|
|
def setup_for_extracting_channel_action(action, *args)
|
2007-05-09 00:45:54 -04:00
|
|
|
s = server("capistrano")
|
|
|
|
session = mock("session", :xserver => s)
|
2007-03-01 01:23:26 -05:00
|
|
|
|
|
|
|
channel = stub_everything
|
|
|
|
session.expects(:open_channel).yields(channel)
|
|
|
|
|
|
|
|
ch = mock
|
2007-05-09 00:45:54 -04:00
|
|
|
ch.stubs(:[]).with(:server).returns(s)
|
|
|
|
ch.stubs(:[]).with(:host).returns(s.host)
|
2007-03-01 01:23:26 -05:00
|
|
|
channel.expects(action).yields(ch, *args)
|
|
|
|
|
|
|
|
yield ch if block_given?
|
|
|
|
|
|
|
|
session
|
|
|
|
end
|
2005-08-06 15:35:25 -04:00
|
|
|
end
|