diff options
Diffstat (limited to 'test')
-rw-r--r-- | test/session_actions_test.rb | 124 | ||||
-rw-r--r-- | test/session_test.rb | 192 |
2 files changed, 153 insertions, 163 deletions
diff --git a/test/session_actions_test.rb b/test/session_actions_test.rb new file mode 100644 index 0000000..c4f3e58 --- /dev/null +++ b/test/session_actions_test.rb @@ -0,0 +1,124 @@ +require 'common' +require 'net/ssh/multi/server' +require 'net/ssh/multi/session_actions' + +class SessionActionsTest < Test::Unit::TestCase + class SessionActionsContainer + include Net::SSH::Multi::SessionActions + + attr_reader :servers + + def initialize + @servers = [] + end + + def use(h, u, o={}) + server = Net::SSH::Multi::Server.new(self, h, u, o) + servers << server + server + end + end + + def setup + @session = SessionActionsContainer.new + end + + def test_busy_should_be_true_if_any_server_is_busy + srv1, srv2, srv3 = @session.use('h1', 'u1'), @session.use('h2', 'u2'), @session.use('h3', 'u3') + srv1.stubs(:busy?).returns(false) + srv2.stubs(:busy?).returns(false) + srv3.stubs(:busy?).returns(true) + assert @session.busy? + end + + def test_busy_should_be_false_if_all_servers_are_not_busy + srv1, srv2, srv3 = @session.use('h1', 'u1', :properties => {:a => 1}), @session.use('h2', 'u2', :properties => {:a => 1, :b => 2}), @session.use('h3', 'u3') + srv1.stubs(:busy?).returns(false) + srv2.stubs(:busy?).returns(false) + srv3.stubs(:busy?).returns(false) + assert !@session.busy? + end + + def test_send_global_request_should_delegate_to_sessions + s1 = mock('ssh') + s2 = mock('ssh') + s1.expects(:send_global_request).with("a", "b", "c").yields + s2.expects(:send_global_request).with("a", "b", "c").yields + @session.expects(:sessions).returns([s1, s2]) + calls = 0 + @session.send_global_request("a", "b", "c") { calls += 1 } + assert_equal 2, calls + end + + def test_open_channel_should_delegate_to_sessions_and_set_accessors_on_each_channel_and_return_multi_channel + srv1 = @session.use('h1', 'u1') + srv2 = @session.use('h2', 'u2') + s1 = { :server => srv1 } + s2 = { :server => srv2 } + c1 = { :stub => :value } + c2 = {} + c1.stubs(:connection).returns(s1) + c2.stubs(:connection).returns(s2) + @session.expects(:sessions).returns([s1, s2]) + s1.expects(:open_channel).with("session").yields(c1).returns(c1) + s2.expects(:open_channel).with("session").yields(c2).returns(c2) + results = [] + channel = @session.open_channel do |c| + results << c + end + assert_equal [c1, c2], results + assert_equal "h1", c1[:host] + assert_equal "h2", c2[:host] + assert_equal srv1, c1[:server] + assert_equal srv2, c2[:server] + assert_instance_of Net::SSH::Multi::Channel, channel + assert_equal [c1, c2], channel.channels + end + + def test_exec_should_raise_exception_if_channel_cannot_exec_command + c = { :host => "host" } + @session.expects(:open_channel).yields(c).returns(c) + c.expects(:exec).with('something').yields(c, false) + assert_raises(RuntimeError) { @session.exec("something") } + end + + def test_exec_with_block_should_pass_data_and_extended_data_to_block + c = { :host => "host" } + @session.expects(:open_channel).yields(c).returns(c) + c.expects(:exec).with('something').yields(c, true) + c.expects(:on_data).yields(c, "stdout") + c.expects(:on_extended_data).yields(c, 1, "stderr") + c.expects(:on_request) + results = {} + @session.exec("something") do |c, stream, data| + results[stream] = data + end + assert_equal({:stdout => "stdout", :stderr => "stderr"}, results) + end + + def test_exec_without_block_should_write_data_and_extended_data_lines_to_stdout_and_stderr + c = { :host => "host" } + @session.expects(:open_channel).yields(c).returns(c) + c.expects(:exec).with('something').yields(c, true) + c.expects(:on_data).yields(c, "stdout 1\nstdout 2\n") + c.expects(:on_extended_data).yields(c, 1, "stderr 1\nstderr 2\n") + c.expects(:on_request) + $stdout.expects(:puts).with("[host] stdout 1\n") + $stdout.expects(:puts).with("[host] stdout 2") + $stderr.expects(:puts).with("[host] stderr 1\n") + $stderr.expects(:puts).with("[host] stderr 2") + @session.exec("something") + end + + def test_exec_should_capture_exit_status_of_process + c = { :host => "host" } + @session.expects(:open_channel).yields(c).returns(c) + c.expects(:exec).with('something').yields(c, true) + c.expects(:on_data) + c.expects(:on_extended_data) + c.expects(:on_request).with("exit-status").yields(c, Net::SSH::Buffer.from(:long, 127)) + @session.exec("something") + assert_equal 127, c[:exit_status] + end + +end
\ No newline at end of file diff --git a/test/session_test.rb b/test/session_test.rb index 8aebccc..288275a 100644 --- a/test/session_test.rb +++ b/test/session_test.rb @@ -73,85 +73,66 @@ class SessionTest < Test::Unit::TestCase assert_equal s1.object_id, s2.object_id end - def test_with_should_set_active_groups_and_yield_and_restore_active_groups + def test_with_should_yield_new_subsession_with_servers_for_criteria yielded = nil - @session.with(:app, :web) do |s| + @session.expects(:servers_for).with(:app, :web).returns([:servers]) + result = @session.with(:app, :web) do |s| yielded = s - assert_equal({:app => {}, :web => {}}, @session.active_groups) end - assert_equal @session, yielded - assert_equal({}, @session.active_groups) + assert_equal result, yielded + assert_equal [:servers], yielded.servers end - def test_with_with_unknown_constraint_should_raise_error + def test_servers_for_with_unknown_constraint_should_raise_error assert_raises(ArgumentError) do - @session.with(:app => { :all => :foo }) {} + @session.servers_for(:app => { :all => :foo }) end end - def test_with_with_constraints_should_add_constraints_to_active_groups - @session.with(:app => { :only => { :primary => true }, :except => { :backup => true } }) do |s| - assert_equal({:app => {:only => {:primary => true}, :except => {:backup => true}}}, @session.active_groups) - end + def test_with_with_constraints_should_build_subsession_with_matching_servers + conditions = { :app => { :only => { :primary => true }, :except => { :backup => true } } } + @session.expects(:servers_for).with(conditions).returns([:servers]) + assert_equal [:servers], @session.with(conditions).servers end - def test_on_should_create_ad_hoc_group_and_make_that_group_the_only_active_group + def test_on_should_return_subsession_containing_only_the_given_servers s1 = @session.use('h1', 'u1') s2 = @session.use('h2', 'u2') + subsession = @session.on(s1, s2) + assert_equal [s1, s2], subsession.servers + end + + def test_on_should_yield_subsession_if_block_is_given + s1 = @session.use('h1', 'u1') yielded = nil - @session.active_groups[:g1] = [] - @session.on(s1, s2) do |s| + result = @session.on(s1) do |s| yielded = s - assert_equal 1, @session.active_groups.size - assert_not_equal :g1, @session.active_groups.keys.first - assert_equal [s1, s2], @session.groups[@session.active_groups.keys.first] + assert_equal [s1], s.servers end - assert_equal [:g1], @session.active_groups.keys - assert_equal @session, yielded + assert_equal result, yielded end - def test_active_sessions_should_return_sessions_for_all_servers_if_active_groups_is_empty - s1, s2, s3 = MockSession.new, MockSession.new, MockSession.new + def test_servers_for_should_return_all_servers_if_no_arguments srv1, srv2, srv3 = @session.use('h1', 'u1'), @session.use('h2', 'u2'), @session.use('h3', 'u3') - Net::SSH.expects(:start).with('h1', 'u1', {}).returns(s1) - Net::SSH.expects(:start).with('h2', 'u2', {}).returns(s2) - Net::SSH.expects(:start).with('h3', 'u3', {}).returns(s3) - assert_equal [s1, s2, s3], @session.active_sessions.sort + assert_equal %w(h1 h2 h3), @session.servers_for.map { |s| s.host }.sort end - def test_active_sessions_should_return_sessions_only_for_active_groups_if_active_groups_exist - s1, s2, s3 = MockSession.new, MockSession.new, MockSession.new + def test_servers_for_should_return_servers_only_for_given_group srv1, srv2, srv3 = @session.use('h1', 'u1'), @session.use('h2', 'u2'), @session.use('h3', 'u3') @session.group :app => [srv1, srv2], :db => [srv3] - Net::SSH.expects(:start).with('h1', 'u1', {}).returns(s1) - Net::SSH.expects(:start).with('h2', 'u2', {}).returns(s2) - @session.active_groups.replace(:app => {}) - assert_equal [s1, s2], @session.active_sessions.sort + assert_equal %w(h1 h2), @session.servers_for(:app).map { |s| s.host }.sort end - def test_active_sessions_should_not_return_duplicate_sessions - s1, s2, s3 = MockSession.new, MockSession.new, MockSession.new + def test_servers_for_should_not_return_duplicate_servers srv1, srv2, srv3 = @session.use('h1', 'u1'), @session.use('h2', 'u2'), @session.use('h3', 'u3') @session.group :app => [srv1, srv2], :db => [srv2, srv3] - Net::SSH.expects(:start).with('h1', 'u1', {}).returns(s1) - Net::SSH.expects(:start).with('h2', 'u2', {}).returns(s2) - Net::SSH.expects(:start).with('h3', 'u3', {}).returns(s3) - @session.active_groups.replace(:app => {}, :db => {}) - assert_equal [s1, s2, s3], @session.active_sessions.sort + assert_equal ["h1", "h2", "h3"], @session.servers_for(:app, :db).map { |s| s.host }.sort end - def test_active_sessions_should_correctly_apply_only_and_except_constraints - s1, s2, s3 = MockSession.new, MockSession.new, MockSession.new + def test_servers_for_should_correctly_apply_only_and_except_constraints srv1, srv2, srv3 = @session.use('h1', 'u1', :properties => {:a => 1}), @session.use('h2', 'u2', :properties => {:a => 1, :b => 2}), @session.use('h3', 'u3') @session.group :app => [srv1, srv2, srv3] - Net::SSH.expects(:start).with('h1', 'u1', :properties => {:a => 1}).returns(s1) - @session.active_groups.replace(:app => {:only => {:a => 1}, :except => {:b => 2}}) - assert_equal [s1], @session.active_sessions.sort - end - - def test_connect_bang_should_call_active_sessions_and_return_self - @session.expects(:active_sessions) - assert_equal @session, @session.connect! + assert_equal [srv1], @session.servers_for(:app => {:only => {:a => 1}, :except => {:b => 2}}) end def test_close_should_close_server_sessions @@ -171,22 +152,6 @@ class SessionTest < Test::Unit::TestCase @session.close end - def test_busy_should_be_true_if_any_server_is_busy - srv1, srv2, srv3 = @session.use('h1', 'u1', :properties => {:a => 1}), @session.use('h2', 'u2', :properties => {:a => 1, :b => 2}), @session.use('h3', 'u3') - srv1.stubs(:busy?).returns(false) - srv2.stubs(:busy?).returns(false) - srv3.stubs(:busy?).returns(true) - assert @session.busy? - end - - def test_busy_should_be_false_if_all_servers_are_not_busy - srv1, srv2, srv3 = @session.use('h1', 'u1', :properties => {:a => 1}), @session.use('h2', 'u2', :properties => {:a => 1, :b => 2}), @session.use('h3', 'u3') - srv1.stubs(:busy?).returns(false) - srv2.stubs(:busy?).returns(false) - srv3.stubs(:busy?).returns(false) - assert !@session.busy? - end - def test_loop_should_loop_until_process_is_false @session.expects(:process).with(5).times(4).returns(true,true,true,false).yields yielded = false @@ -234,103 +199,4 @@ class SessionTest < Test::Unit::TestCase IO.expects(:select).with([:a, :b, :c], [:a, :c], nil, 5).returns([[:b, :c], [:a, :c]]) @session.process(5) end - - def test_send_global_request_should_delegate_to_active_sessions - s1 = mock('ssh') - s2 = mock('ssh') - s1.expects(:send_global_request).with("a", "b", "c").yields - s2.expects(:send_global_request).with("a", "b", "c").yields - @session.expects(:active_sessions).returns([s1, s2]) - calls = 0 - @session.send_global_request("a", "b", "c") { calls += 1 } - assert_equal 2, calls - end - - def test_open_channel_should_delegate_to_active_sessions_and_set_accessors_on_each_channel_and_return_multi_channel - srv1 = @session.use('h1', 'u1') - srv2 = @session.use('h2', 'u2') - s1 = { :server => srv1 } - s2 = { :server => srv2 } - c1 = { :stub => :value } - c2 = {} - c1.stubs(:connection).returns(s1) - c2.stubs(:connection).returns(s2) - @session.expects(:active_sessions).returns([s1, s2]) - s1.expects(:open_channel).with("session").yields(c1).returns(c1) - s2.expects(:open_channel).with("session").yields(c2).returns(c2) - results = [] - channel = @session.open_channel do |c| - results << c - end - assert_equal [c1, c2], results - assert_equal "h1", c1[:host] - assert_equal "h2", c2[:host] - assert_equal srv1, c1[:server] - assert_equal srv2, c2[:server] - assert_instance_of Net::SSH::Multi::Channel, channel - assert_equal [c1, c2], channel.channels - end - - def test_exec_should_raise_exception_if_channel_cannot_exec_command - c = { :host => "host" } - @session.expects(:open_channel).yields(c).returns(c) - c.expects(:exec).with('something').yields(c, false) - assert_raises(RuntimeError) { @session.exec("something") } - end - - def test_exec_with_block_should_pass_data_and_extended_data_to_block - c = { :host => "host" } - @session.expects(:open_channel).yields(c).returns(c) - c.expects(:exec).with('something').yields(c, true) - c.expects(:on_data).yields(c, "stdout") - c.expects(:on_extended_data).yields(c, 1, "stderr") - c.expects(:on_request) - results = {} - @session.exec("something") do |c, stream, data| - results[stream] = data - end - assert_equal({:stdout => "stdout", :stderr => "stderr"}, results) - end - - def test_exec_without_block_should_write_data_and_extended_data_lines_to_stdout_and_stderr - c = { :host => "host" } - @session.expects(:open_channel).yields(c).returns(c) - c.expects(:exec).with('something').yields(c, true) - c.expects(:on_data).yields(c, "stdout 1\nstdout 2\n") - c.expects(:on_extended_data).yields(c, 1, "stderr 1\nstderr 2\n") - c.expects(:on_request) - $stdout.expects(:puts).with("[host] stdout 1\n") - $stdout.expects(:puts).with("[host] stdout 2") - $stderr.expects(:puts).with("[host] stderr 1\n") - $stderr.expects(:puts).with("[host] stderr 2") - @session.exec("something") - end - - def test_exec_should_capture_exit_status_of_process - c = { :host => "host" } - @session.expects(:open_channel).yields(c).returns(c) - c.expects(:exec).with('something').yields(c, true) - c.expects(:on_data) - c.expects(:on_extended_data) - c.expects(:on_request).with("exit-status").yields(c, Net::SSH::Buffer.from(:long, 127)) - @session.exec("something") - assert_equal 127, c[:exit_status] - end - - private - - class MockSession < Hash - include Comparable - - @@next_id = 0 - attr_reader :id - - def initialize - @id = (@@next_id += 1) - end - - def <=>(s) - id <=> s.id - end - end end
\ No newline at end of file |